From 92b22f60b16b1100de03fc730865cf7e94d37150 Mon Sep 17 00:00:00 2001 From: Nuno Silva Date: Sun, 9 Mar 2014 22:29:01 +0000 Subject: [PATCH] Coordinates and Element Object Corrected the movements, the coordinate object and its tests. Added more functionality to the element object added the codes to the unitsLoader --- obb.js | 3 +- src/engine/coordinate.js | 38 ++++---- src/engine/element.js | 94 ++++++++++++------- src/moves/movement/allMovement.js | 12 +-- src/moves/movement/diagonalMovement.js | 12 +-- src/moves/movement/frontMovement.js | 37 +++----- src/moves/movement/normalMovement.js | 14 +-- src/moves/movement/positioning.js | 22 ++--- src/unitsLoader.js | 6 +- test/engine/coordinate.js | 72 +++++++------- .../{frontMovement => frontMovement.js} | 2 +- test/unitsLoader.js | 8 ++ 12 files changed, 175 insertions(+), 145 deletions(-) rename test/moves/movement/{frontMovement => frontMovement.js} (97%) diff --git a/obb.js b/obb.js index 11be8a9..9285ebb 100644 --- a/obb.js +++ b/obb.js @@ -3,6 +3,7 @@ var obb = exports; obb.package = require('./package.json'); - obb.units = require('./src/unitsLoader'); + obb.units = require('./src/unitsLoader').units; + obb.codes = require('./src/unitsLoader').codes; })(exports); diff --git a/src/engine/coordinate.js b/src/engine/coordinate.js index 02f0aee..26783a2 100644 --- a/src/engine/coordinate.js +++ b/src/engine/coordinate.js @@ -34,25 +34,25 @@ function sectorTestY(x, y, numberOfPlayers ) { } var coordinateMover = { - "N_next" : { x : -1, y : 0 }, - "S_next" : { x : 1, y : 0 }, - "W_next" : { x : 0, y : -1 }, - "E_next" : { x : 0, y : 1 }, - - "N_previous" : { x : 1, y : 0 }, - "S_previous" : { x : -1, y : 0 }, - "W_previous" : { x : 0, y : 1 }, - "E_previous" : { x : 0, y : -1 }, - - "N_left" : { x : 0, y : -1 }, - "S_left" : { x : 0, y : 1 }, - "W_left" : { x : 1, y : 0 }, - "E_left" : { x : -1, y : 0 }, - - "N_right" : { x : 0, y : 1 }, - "S_right" : { x : 0, y : -1 }, - "W_right" : { x : -1, y : 0 }, - "E_right" : { x : 1, y : 0 } + "N_next" : { x : 0, y : -1 }, + "S_next" : { x : 0, y : 1 }, + "W_next" : { x : -1, y : 0 }, + "E_next" : { x : 1, y : 0 }, + + "N_previous" : { x : 0, y : 1 }, + "S_previous" : { x : 0, y : -1 }, + "W_previous" : { x : 1, y : 0 }, + "E_previous" : { x : -1, y : 0 }, + + "N_left" : { x : -1, y : 0 }, + "S_left" : { x : 1, y : 0 }, + "W_left" : { x : 0, y : 1 }, + "E_left" : { x : 0, y : -1 }, + + "N_right" : { x : 1, y : 0 }, + "S_right" : { x : -1, y : 0 }, + "W_right" : { x : 0, y : -1 }, + "E_right" : { x : 0, y : 1 } }; function resolveValidator(mover) { diff --git a/src/engine/element.js b/src/engine/element.js index a630210..0e5d851 100644 --- a/src/engine/element.js +++ b/src/engine/element.js @@ -3,6 +3,26 @@ var bonus = require("bonus.js"); var unitsLoader = require("../unitsLoader.js"); +//---------------------- +// Logic +//---------------------- + +function renewEffect(element,effect) { + if( effect.cumulative ) { + element.effects[effect.name].push(effect); + }else{ + element.effects[effect.name] = [effect]; + } +} + +function addNewEffect(element,effect) { + element.effects[effect.name] = [effect]; +} + +//---------------------- +// Utilities +//---------------------- + Element.prototype.clone : function () { } @@ -15,12 +35,23 @@ Element.prototype.toString : function () { // Effects //---------------------- -Element.prototype.addEffects : function(target) { - +Element.prototype.addEffects : function(effect) { + var currentEffect = effects[effect.name]; + if( currentEffect !== null ) { + renewEffect(this, effect); + }else{ + addNewEffect(this, effect); + } } Element.prototype.resolveEffects : function() { - + _.each(this.effects, function(element) { + if( !element.resolved ) { + if( element.resolve() ) { + delete this.effects[element.name]; + } + } + }); } //---------------------- @@ -28,16 +59,15 @@ Element.prototype.resolveEffects : function() { //---------------------- Element.prototype.getAttack : function(terrain,target) { - bonus.getAttack(this,terrain,target); - + bonus.getAttack(this,terrain,target); } Element.prototype.getDefense : function() { - - + bonus.getDefense(this,terrain,target); } Element.prototype.getRange : function() { + bonus.getRange(this,terrain,target); } @@ -50,31 +80,31 @@ Element.prototype.getRange : function() { function Element(content){ - this.canBeMoved = true, - this.canUseSpecialAbilities = true, - this.coolDown = 0; - - var splittedData = content.split("-"); - - this.unit = unitsLoader[splittedData[0]]; - this.coordinate = Coordinate.parse(splittedData[1]); - this.quantity = splittedData[2]; - this.originalQuantity = this.quantity; - - this.position = splittedData[3]; - - if( splittedData.length > 4) { - var data = splittedData[4]; - var rd = parseInt(data); - if( rd != NaN ) { - this.remainingDefense = rd; - if( splittedData.length > 5) { - this.effects = parseEffects(splittedData[5]); - } - }else{ - this.effects = parseEffects(splittedData[4]); - } - } + this.canBeMoved = true, + this.canUseSpecialAbilities = true, + this.coolDown = 0; + + var splittedData = content.split("-"); + + this.unit = unitsLoader[splittedData[0]]; + this.coordinate = Coordinate.parse(splittedData[1]); + this.quantity = splittedData[2]; + this.originalQuantity = this.quantity; + + this.position = splittedData[3]; + + if( splittedData.length > 4) { + var data = splittedData[4]; + var rd = parseInt(data); + if( rd != NaN ) { + this.remainingDefense = rd; + if( splittedData.length > 5) { + this.effects = parseEffects(splittedData[5]); + } + }else{ + this.effects = parseEffects(splittedData[4]); + } + } } module.exports = Element; \ No newline at end of file diff --git a/src/moves/movement/allMovement.js b/src/moves/movement/allMovement.js index d2908c2..c580bb3 100644 --- a/src/moves/movement/allMovement.js +++ b/src/moves/movement/allMovement.js @@ -1,12 +1,12 @@ (function allMovement(module) { module.isValid = function(src,dst,position) { - if( dst.x <= src.x + 1 && dst.x >= src.x - 1 ) { - if( dst.y <= src.y + 1 && dst.y >= src.y - 1 ) { - return true; - } - } - return false; + if( dst.x <= src.x + 1 && dst.x >= src.x - 1 ) { + if( dst.y <= src.y + 1 && dst.y >= src.y - 1 ) { + return true; + } + } + return false; }; })(exports); diff --git a/src/moves/movement/diagonalMovement.js b/src/moves/movement/diagonalMovement.js index 559b990..d2b9449 100644 --- a/src/moves/movement/diagonalMovement.js +++ b/src/moves/movement/diagonalMovement.js @@ -2,13 +2,13 @@ module.isValid = function(src,dst,position) { if( dst.x == src.x + 1 && dst.y == src.y + 1 || - dst.x == src.x - 1 && dst.y == src.y - 1 || - dst.x == src.x + 1 && dst.y == src.y - 1 || - dst.x == src.x - 1 && dst.y == src.y + 1 ) { - return true; - } + dst.x == src.x - 1 && dst.y == src.y - 1 || + dst.x == src.x + 1 && dst.y == src.y - 1 || + dst.x == src.x - 1 && dst.y == src.y + 1 ) { + return true; + } - return false; + return false; }; })(exports); diff --git a/src/moves/movement/frontMovement.js b/src/moves/movement/frontMovement.js index 897fd1f..599d915 100644 --- a/src/moves/movement/frontMovement.js +++ b/src/moves/movement/frontMovement.js @@ -1,28 +1,17 @@ (function frontMovement(module) { - module.isValid = function(src,dst,position) { - switch(position) { - case "N": - if( dst.x == src.x - 1 && src.y == dst.y ) { - return true; - } - case "S": - if( dst.x == src.x + 1 && src.y == dst.y ) { - return true; - } - case "W": - if( dst.y == src.y - 1 && src.x == dst.x ) { - return true; - } - case "E": - if( dst.y == src.y + 1 && src.x == dst.x ) { - return true; - } - default: - throw new Error("Invalid position.") - } - return false; - - }; + var frontValues = { + N : { x : 0, y: -1 }, + S : { x : 0, y: 1 }, + W : { x : -1, y: 0 }, + E : { x : 1, y: 0 } + }; + module.isValid = function(src,dst,position) { + var value = frontValues[position]; + if( value != null ) { + return dst.x == src.x + value.x && dst.y == src.y + value.y; + } + throw new Error("Invalid position.") + }; })(exports); diff --git a/src/moves/movement/normalMovement.js b/src/moves/movement/normalMovement.js index 6d19640..a6b872b 100644 --- a/src/moves/movement/normalMovement.js +++ b/src/moves/movement/normalMovement.js @@ -1,15 +1,15 @@ (function normalMovement(module) { module.isValid = function(src,dst,position) { - if( dst.x <= src.x + 1 && dst.x >= src.x - 1 && src.y == dst.y ) { - return true; - } + if( dst.x <= src.x + 1 && dst.x >= src.x - 1 && src.y == dst.y ) { + return true; + } - if( dst.y <= src.y + 1 && dst.y >= src.y - 1 && src.x == dst.x ) { - return true; - } + if( dst.y <= src.y + 1 && dst.y >= src.y - 1 && src.x == dst.x ) { + return true; + } - return false; + return false; }; })(exports); diff --git a/src/moves/movement/positioning.js b/src/moves/movement/positioning.js index d0e93c2..9a9b318 100644 --- a/src/moves/movement/positioning.js +++ b/src/moves/movement/positioning.js @@ -1,20 +1,20 @@ (function allMovement(module) { module.isValid = function(numberOfPlayers,dst) { - if( numberOfPlayers == 2 ) { - if( dst.x != 8 && dst.x != 7 ) { - return false; - } + if( numberOfPlayers == 2 ) { + if( dst.y != 8 && dst.y != 7 ) { + return false; + } - return true; - } + return true; + } - //4 Players - if( ( dst.x == 12 || dst.x == 11 ) && ( dst.y > 2 && dst.y < 11 ) ) { - return true; - } + //4 Players + if( ( dst.y == 12 || dst.y == 11 ) && ( dst.x > 2 && dst.x < 11 ) ) { + return true; + } - return false; + return false; }; })(exports); diff --git a/src/unitsLoader.js b/src/unitsLoader.js index 294990a..f59518a 100644 --- a/src/unitsLoader.js +++ b/src/unitsLoader.js @@ -2,12 +2,14 @@ var fs = require('fs'); var _ = require('underscore'); var files = fs.readdirSync('./src/units/'); var hash = {}; +var codes = {}; _.each(files, function selectedFile(file) { var unit = require('./../src/units/' + file); hash[unit.name] = unit; - hash[unit.code] = unit; + codes[unit.code] = unit; }); -module.exports = hash; +module.exports.units = hash; +module.exports.codes = codes; diff --git a/test/engine/coordinate.js b/test/engine/coordinate.js index 3750c9b..fd86589 100644 --- a/test/engine/coordinate.js +++ b/test/engine/coordinate.js @@ -5,7 +5,7 @@ describe('coordinate', function(){ describe('#coordinate', function() { - it('1,2 should be valid', function() { + it('1_2 should be valid', function() { var coordinate = new Coordinate(1,2); expect(coordinate.x).to.be.equal(1); expect(coordinate.y).to.be.equal(2); @@ -41,8 +41,8 @@ describe('coordinate', function(){ var newCoordinate = coordinate.leftCoordinate("N",2); - expect(newCoordinate.x).to.be.equal(2); - expect(newCoordinate.y).to.be.equal(1); + expect(newCoordinate.x).to.be.equal(1); + expect(newCoordinate.y).to.be.equal(2); }), @@ -51,8 +51,8 @@ describe('coordinate', function(){ var newCoordinate = coordinate.leftCoordinate("S",2); - expect(newCoordinate.x).to.be.equal(2); - expect(newCoordinate.y).to.be.equal(3); + expect(newCoordinate.x).to.be.equal(3); + expect(newCoordinate.y).to.be.equal(2); }), @@ -61,8 +61,8 @@ describe('coordinate', function(){ var newCoordinate = coordinate.leftCoordinate("W",2); - expect(newCoordinate.x).to.be.equal(3); - expect(newCoordinate.y).to.be.equal(2); + expect(newCoordinate.x).to.be.equal(2); + expect(newCoordinate.y).to.be.equal(3); }), @@ -71,8 +71,8 @@ describe('coordinate', function(){ var newCoordinate = coordinate.leftCoordinate("E",2); - expect(newCoordinate.x).to.be.equal(1); - expect(newCoordinate.y).to.be.equal(2); + expect(newCoordinate.x).to.be.equal(2); + expect(newCoordinate.y).to.be.equal(1); }), it('should throw an error', function() { @@ -87,8 +87,8 @@ describe('coordinate', function(){ var newCoordinate = coordinate.rightCoordinate("N",2); - expect(newCoordinate.x).to.be.equal(2); - expect(newCoordinate.y).to.be.equal(3); + expect(newCoordinate.x).to.be.equal(3); + expect(newCoordinate.y).to.be.equal(2); }), it('right coordinate S should be valid', function() { @@ -96,8 +96,8 @@ describe('coordinate', function(){ var newCoordinate = coordinate.rightCoordinate("S",2); - expect(newCoordinate.x).to.be.equal(2); - expect(newCoordinate.y).to.be.equal(1); + expect(newCoordinate.x).to.be.equal(1); + expect(newCoordinate.y).to.be.equal(2); }), it('right coordinate W should be valid', function() { @@ -105,8 +105,8 @@ describe('coordinate', function(){ var newCoordinate = coordinate.rightCoordinate("W",2); - expect(newCoordinate.x).to.be.equal(1); - expect(newCoordinate.y).to.be.equal(2); + expect(newCoordinate.x).to.be.equal(2); + expect(newCoordinate.y).to.be.equal(1); }), it('right coordinate E should be valid', function() { @@ -114,8 +114,8 @@ describe('coordinate', function(){ var newCoordinate = coordinate.rightCoordinate("E",2); - expect(newCoordinate.x).to.be.equal(3); - expect(newCoordinate.y).to.be.equal(2); + expect(newCoordinate.x).to.be.equal(2); + expect(newCoordinate.y).to.be.equal(3); }), it('should throw an error', function() { @@ -132,8 +132,8 @@ describe('coordinate', function(){ var newCoordinate = coordinate.nextCoordinate("N",2); - expect(newCoordinate.x).to.be.equal(1); - expect(newCoordinate.y).to.be.equal(2); + expect(newCoordinate.x).to.be.equal(2); + expect(newCoordinate.y).to.be.equal(1); }), it('next coordinate S should be valid', function() { @@ -141,8 +141,8 @@ describe('coordinate', function(){ var newCoordinate = coordinate.nextCoordinate("S",2); - expect(newCoordinate.x).to.be.equal(3); - expect(newCoordinate.y).to.be.equal(2); + expect(newCoordinate.x).to.be.equal(2); + expect(newCoordinate.y).to.be.equal(3); }), it('next coordinate W should be valid', function() { @@ -150,8 +150,8 @@ describe('coordinate', function(){ var newCoordinate = coordinate.nextCoordinate("W",2); - expect(newCoordinate.x).to.be.equal(2); - expect(newCoordinate.y).to.be.equal(1); + expect(newCoordinate.x).to.be.equal(1); + expect(newCoordinate.y).to.be.equal(2); }), it('next coordinate E should be valid', function() { @@ -159,8 +159,8 @@ describe('coordinate', function(){ var newCoordinate = coordinate.nextCoordinate("E",2); - expect(newCoordinate.x).to.be.equal(2); - expect(newCoordinate.y).to.be.equal(3); + expect(newCoordinate.x).to.be.equal(3); + expect(newCoordinate.y).to.be.equal(2); }), it('should throw an error', function() { @@ -180,7 +180,7 @@ describe('coordinate', function(){ it('next coordinate should be 9', function() { var coordinate = new Coordinate(7,8); - var newCoordinate = coordinate.nextCoordinate("E",2); + var newCoordinate = coordinate.nextCoordinate("S",2); expect(newCoordinate.x).to.be.equal(9); expect(newCoordinate.y).to.be.equal(9); @@ -189,7 +189,7 @@ describe('coordinate', function(){ it('next coordinate should be 13', function() { var coordinate = new Coordinate(1,12); - var newCoordinate = coordinate.nextCoordinate("E",4); + var newCoordinate = coordinate.nextCoordinate("S",4); expect(newCoordinate.x).to.be.equal(13); expect(newCoordinate.y).to.be.equal(13); @@ -204,8 +204,8 @@ describe('coordinate', function(){ var newCoordinate = coordinate.previousCoordinate("N",2); - expect(newCoordinate.x).to.be.equal(3); - expect(newCoordinate.y).to.be.equal(2); + expect(newCoordinate.x).to.be.equal(2); + expect(newCoordinate.y).to.be.equal(3); }), it('previous coordinate S should be valid', function() { @@ -213,8 +213,8 @@ describe('coordinate', function(){ var newCoordinate = coordinate.previousCoordinate("S",2); - expect(newCoordinate.x).to.be.equal(1); - expect(newCoordinate.y).to.be.equal(2); + expect(newCoordinate.x).to.be.equal(2); + expect(newCoordinate.y).to.be.equal(1); }), it('previous coordinate W should be valid', function() { @@ -222,8 +222,8 @@ describe('coordinate', function(){ var newCoordinate = coordinate.previousCoordinate("W",2); - expect(newCoordinate.x).to.be.equal(2); - expect(newCoordinate.y).to.be.equal(3); + expect(newCoordinate.x).to.be.equal(3); + expect(newCoordinate.y).to.be.equal(2); }), it('previous coordinate E should be valid', function() { @@ -231,8 +231,8 @@ describe('coordinate', function(){ var newCoordinate = coordinate.previousCoordinate("E",2); - expect(newCoordinate.x).to.be.equal(2); - expect(newCoordinate.y).to.be.equal(1); + expect(newCoordinate.x).to.be.equal(1); + expect(newCoordinate.y).to.be.equal(2); }), it('should throw an error', function() { @@ -250,7 +250,7 @@ describe('coordinate', function(){ }), it('previous coordinate should be 13', function() { - var coordinate = new Coordinate(12,1); + var coordinate = new Coordinate(1,12); var newCoordinate = coordinate.previousCoordinate("N",4); diff --git a/test/moves/movement/frontMovement b/test/moves/movement/frontMovement.js similarity index 97% rename from test/moves/movement/frontMovement rename to test/moves/movement/frontMovement.js index b353f1a..b169c37 100644 --- a/test/moves/movement/frontMovement +++ b/test/moves/movement/frontMovement.js @@ -7,7 +7,7 @@ describe('frontMovement', function(){ describe('#isValid', function() { it('should be available', function() { - expect(!frontMovement.isValid).to.be.ok(); + expect(frontMovement.isValid).to.be.ok(); }); it('should be valid', function() { diff --git a/test/unitsLoader.js b/test/unitsLoader.js index 525f3fe..aa1ce16 100644 --- a/test/unitsLoader.js +++ b/test/unitsLoader.js @@ -9,10 +9,18 @@ describe('unitLoader', function() { expect(obb.units).not.to.be.empty(); }) + it('codes are available on the main module', function() { + expect(obb.codes).not.to.be.empty(); + }) + it('has all available units registered', function() { expect(Object.keys(obb.units).length).to.equal(4); }) + it('has all available codes registered', function() { + expect(Object.keys(obb.codes).length).to.equal(4); + }) + describe('verifies that all units have correct properties', function() { _.each(obb.units, function process(unit) { it(unit.name + " is ok ", function(){