-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
190 lines (162 loc) · 4.64 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const LEFT = 0;
const RIGHT = 1;
const UP = 2;
const DOWN = 3;
const POSITIONS = [{ x: 13, y: 13}, { x: 13, y: 11}, { x: 12, y: 11}, { x: 14, y: 11}];
const ROLES = ['pacman', 'ghost'];
const COLORS = ['pink', 'red', 'yellow', 'green'];
const HIGH_VULNERABILITY = 2;
const LOW_VULNERABILITY = 1;
const NO_VULNERABILITY = 0;
const RECOVERY_T = 1000;
/*
* Classe che si occupa di gestire le movenze e l'aspetto dei giocatori
*/
class Player {
constructor(x, pos_y, direction = LEFT, role, color) {
this.next_direction = null;
this.points = 0;
if(pos_y)
this.normalConstruction(x, pos_y, direction, role, color);
else
this.buildWithDefaultSets(x);
}
buildWithDefaultSets(x) {
this.setDefaultPosition(x);
this.setDefaultDirection(x);
if(x === 0)
this.role = ROLES[0];
else {
this.role = ROLES[1];
this.color = COLORS[x];
}
if(this.role === 'pacman')
this.life = 3;
else if(this.role === 'ghost')
this.vulnerable = NO_VULNERABILITY;
}
setDefaultPosition(x) {
this.pos = { ...POSITIONS[x]};
}
setDefaultDirection(x) {
this.direction = x % 2;
}
normalConstruction(pos_x, pos_y, direction, role, color) {
this.pos = {
'x' : pos_x,
'y' : pos_y
}
this.direction = direction;
this.role = (role in ROLES ? role : ROLES[Math.round(Math.random())]);
if(this.role === 'ghost') {
this.color = (color in COLORS ? color : COLORS[~~(Math.random() * 3)]);
} else
this.life = 3;
}
increasePoints(n) {
this.points += n;
}
updateDirection(map) {
let update = false;
switch(this.next_direction) {
case LEFT:
if(!this.isAnObstacle(map.matrix[this.pos['y']][this.pos['x'] - 1]))
update = true;
break;
case RIGHT:
if(!this.isAnObstacle(map.matrix[this.pos['y']][this.pos['x'] + 1]))
update = true;
break;
case UP:
if(!this.isAnObstacle(map.matrix[this.pos['y'] - 1][this.pos['x']]))
update = true;
break;
case DOWN:
if(!this.isAnObstacle(map.matrix[this.pos['y'] + 1][this.pos['x']]) )
update = true;
break;
default:
break;
}
if(update) {
this.direction = this.next_direction;
this.next_direction = null;
}
}
isAnObstacle(x) {
if(x >= 1 && x <= 14)
return true;
if(this.role === 'pacman' && x === 15)
return true;
return false
}
decreaseRecoveryTime(t) {
if(this.recovery_time) {
this.recovery_time -= t;
if(this.recovery_time < 0)
this.recovery_time = 0;
return Boolean(this.recovery_time);
}
return false;
}
initRecoveryTime() {
this.recovery_time = RECOVERY_T;
}
move(map) {
switch(this.direction) {
case LEFT:
this.moveLeft(map);
break;
case RIGHT:
this.moveRight(map);
break;
case UP:
this.moveUp(map);
break;
case DOWN:
this.moveDown(map);
break;
}
}
moveRight(map) {
if(map.matrix[this.pos['y']][this.pos['x'] + 1] === undefined ||
!this.isAnObstacle(map.matrix[this.pos['y']][this.pos['x'] + 1])) {
this.pos['x']++;
}
if(this.pos['x'] === 27)
this.pos['x'] = 0
}
moveLeft(map) {
if(map.matrix[this.pos['y']][this.pos['x'] - 1] === undefined ||
!this.isAnObstacle(map.matrix[this.pos['y']][this.pos['x'] - 1])) {
this.pos['x']--;
}
if(this.pos['x'] === -1)
this.pos['x'] = 26
}
moveUp(map) {
if(!this.isAnObstacle(map.matrix[~~(this.pos['y'] - 1)][this.pos['x']])) {
this.pos['y']--;
}
}
moveDown(map) {
if(!this.isAnObstacle(map.matrix[this.pos['y'] + 1][this.pos['x']])) {
this.pos['y']++;
}
}
nextLeft() {
this.next_direction = LEFT;
}
nextRight() {
this.next_direction = RIGHT;
}
nextUp() {
this.next_direction = UP;
}
nextDown() {
this.next_direction = DOWN;
}
}
module.exports = {
Player: Player
}