Skip to content

Commit

Permalink
Coordinates and Element Object
Browse files Browse the repository at this point in the history
Corrected the movements, the coordinate object and its tests.
Added more functionality to the element object
added the codes to the unitsLoader
  • Loading branch information
Nuno Silva committed Mar 9, 2014
1 parent 5509df2 commit 92b22f6
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 145 deletions.
3 changes: 2 additions & 1 deletion obb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
38 changes: 19 additions & 19 deletions src/engine/coordinate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
94 changes: 62 additions & 32 deletions src/engine/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {

}
Expand All @@ -15,29 +35,39 @@ 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];
}
}
});
}

//----------------------
// Unit Properties
//----------------------

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);
}


Expand All @@ -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;
12 changes: 6 additions & 6 deletions src/moves/movement/allMovement.js
Original file line number Diff line number Diff line change
@@ -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);
12 changes: 6 additions & 6 deletions src/moves/movement/diagonalMovement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
37 changes: 13 additions & 24 deletions src/moves/movement/frontMovement.js
Original file line number Diff line number Diff line change
@@ -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);
14 changes: 7 additions & 7 deletions src/moves/movement/normalMovement.js
Original file line number Diff line number Diff line change
@@ -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);
22 changes: 11 additions & 11 deletions src/moves/movement/positioning.js
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 4 additions & 2 deletions src/unitsLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Loading

0 comments on commit 92b22f6

Please sign in to comment.