-
Notifications
You must be signed in to change notification settings - Fork 0
/
fightsim.js
71 lines (61 loc) · 1.69 KB
/
fightsim.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var SimulationMode = {
min: "minimal success",
avg: "average success",
max: "maximal success"
};
var ArmyFactory = {
// config = {"B": 100}
// army = {"B": Array(100 x UnitFactory.B()))}
createArmy: function(config)
{
console.log( "create army with ... ");
var army = {};
var self = this;
$.each(config, function(key, value) {
console.log( "... " + value + " " + key);
var units = self.createUnits(value, key);
army[key] = units;
});
return army;
},
createUnits: function(count, unitLetter) {
var arr = new Array(count);
for(var i=0; i<count; i++) {
arr[i] = UnitFactory[unitLetter]();
}
return arr;
}
};
var FightSim = {
simulationMode: SimulationMode.avg,
fight: function ( units1, units2 )
{
if (units1.length === 0 || units2.length === 0)
{
console.log("no fight");
return;
}
console.log(units1.length + " " + units1[0].letter
+ " and " + units2.length + " " + units2[0].letter + " begin a fight");
// TODO: iterate!
this.attack(units1[0], units2[0]);
this.attack(units2[0], units1[0]);
},
attack: function ( unit1, unit2 )
{
console.log(unit1.letter + " attacks " + unit2.letter + " with mode " + this.simulationMode);
switch(this.simulationMode) {
case SimulationMode.min:
unit2.hitPoints -= unit1.damageMin;
break;
case SimulationMode.max:
unit2.hitPoints -= unit1.damageMax;
break;
case SimulationMode.avg:
unit2.hitPoints -= ((unit1.damageMax * unit1.accuracy) + (unit1.damageMin * (100-unit1.accuracy))) / 100;
break;
default:
console.log("[ERROR]: unknown simulationMode: " + this.simulationMode);
}
}
}