-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.js
113 lines (98 loc) · 2.68 KB
/
Player.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
class Player {
static fullHealth(){
return 20;
}
constructor(){
this.health = Player.fullHealth();
this.retreatPoint = Player.fullHealth() / 2;
this.recoveryPoint = Player.fullHealth() * 3 / 4;
}
playTurn(warrior) {
var space = warrior.feel();
if (this.isUnderAttack(warrior)){
(this.isTooInjured(warrior))?warrior.walk('backward'):this.faceEnemies(warrior);
}
else if (this.needRest(warrior)){
warrior.rest();
}
else{
if (this.anyEnemies(warrior)){
this.faceEnemies(warrior);
}
else if (this.anyCaptives(warrior)){
this.rescueCaptives(warrior);
}
else if (space.isWall()){
warrior.pivot();
}
else{
warrior.walk();
}
}
this.health = warrior.health();
}
isInjured(warrior){
return warrior.health() < Player.fullHealth();
}
isTooInjured(warrior){
return warrior.health() <= this.retreatPoint
}
needRest(warrior){
return warrior.health() <= this.recoveryPoint;
}
isUnderAttack(warrior){
return (warrior.health() < this.health);
}
lookForEnemies(warrior, dir){
var spaceWithUnit = warrior.look(dir).find(space => space.isUnit());
return spaceWithUnit && spaceWithUnit.getUnit().isEnemy();
}
// -- enmemies manager
anyEnemies(warrior){
return (this.lookForEnemies(warrior, 'backward') || this.lookForEnemies(warrior, 'forward'));
}
isEnemyCloseToMe(warrior, direction){
var isUnit = warrior.feel(direction).isUnit();
return (isUnit)?warrior.feel(this.direction).getUnit(this.direction).isEnemy():false;
}
faceEnemies(warrior){
if (this.isEnemyCloseToMe(warrior, 'backward')){
warrior.pivot();
}
else if (this.isEnemyCloseToMe(warrior, 'forward')){
warrior.attack();
}
else if (this.lookForEnemies(warrior, 'backward')){
warrior.shoot('backward');
}
else if (this.lookForEnemies(warrior, 'forward')){
warrior.shoot('forward');
}
}
// -- CAPTIVEs MANAGER
anyCaptives(warrior){
return (this.lookForCaptives(warrior, 'backward') || this.lookForCaptives(warrior, 'forward'));
}
lookForCaptives(warrior, dir){
var spaceWithUnit = warrior.look(dir).find(space => space.isUnit());
return spaceWithUnit && spaceWithUnit.getUnit().isBound();
}
isCaptiveCloseToMe(warrior, direction){
var isUnit = warrior.feel(direction).isUnit();
return (isUnit)?warrior.feel(direction).getUnit(direction).isBound():false;
}
rescueCaptives(warrior){
if (this.isCaptiveCloseToMe(warrior, 'backward')){
warrior.pivot();
}
else if (this.isCaptiveCloseToMe(warrior, 'forward')){
warrior.rescue();
}
else if (this.lookForCaptives(warrior, 'backward')){
warrior.walk('backward');
}
else if (this.lookForCaptives(warrior, 'forward')){
warrior.walk('forward');
}
}
}