-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.js
More file actions
88 lines (71 loc) · 1.85 KB
/
player.js
File metadata and controls
88 lines (71 loc) · 1.85 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var inherits = require('gramework').inherits,
gamejs = require('gramework').gamejs;
var Player = exports.Player = function(options) {
this.units = new gamejs.sprite.Group();
this._isDone = false;
this._selected = undefined;
this.initialize(options);
};
Player.prototype.startTurn = function() {
this._isDone = false;
this.units.forEach(function(unit){
unit.wake();
}, this);
};
Player.prototype.endTurn = function() {
this._isDone = true;
}
Player.prototype.initialize = function(options) {
this._human = options.human || false;
this._name = options.name || undefined;
};
Player.prototype.isHuman = function(){
return this._human;
};
Player.prototype.isDone = function() {
return this._isDone;
};
Player.prototype.addUnit = function(unit) {
this.units.add(unit);
};
Player.prototype.action = function(tile) {
if (this.hasUnitSelected()){
this.getSelectedUnit().action(tile);
} else if (this.allyOnTile(tile) !== false){
this.select(this.allyOnTile(tile));
console.log(this.getSelectedUnit());
}
};
Player.prototype.checkAlly = function(unit) {
var _ally = false;
this.units.forEach(function(checkUnit){
if (checkUnit == unit) {
_ally = true;
}
}, this);
return _ally;
};
Player.prototype.hasUnitSelected = function() {
if (this._selected) {
return true;
}
return false;
};
Player.prototype.getSelectedUnit = function() {
return this._selected;
};
Player.prototype.allyOnTile = function(tile) {
var _ally = false;
tile.contents.forEach(function(unit){
if (this.checkAlly(unit)){
_ally = unit;
}
}, this);
return _ally;
};
Player.prototype.select = function(unit){
this._selected = unit;
};
Player.prototype.deselect = function() {
this._selected = undefined;
};