forked from ManWhoSoldTheW0rld/pacman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Character.js
112 lines (86 loc) · 3.2 KB
/
Character.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
import {GRID_SIZE, CELL_SIZE, RIGHT_TUNNEL, LEFT_TUNNEL, DIRECTIONS} from './setup.js';
class Character {
constructor(speed, startPos, name) {
this.name = name;
this.pos = startPos;
this.speed = speed;
this.dir = null;
this.timer = 0;
this.top = 0;
this.left = 0;
this.isStepDone = true;
this.rotation = false;
this.div = this.getDiv();
this.setToPosition(startPos, true);
}
getDiv() {
return null;
}
shouldMove() {
if (!this.dir) {
return false;
}
if (this.timer === this.speed) {
this.timer = 0;
return true;
}
this.timer++;
}
setToPosition(position) {
let top = Math.floor(position / GRID_SIZE);
let left = position % GRID_SIZE;
this.setNextPositionForAnimation(position);
this.setTransform(left * CELL_SIZE, top * CELL_SIZE);
this.currentLeft = this.left;
this.currentTop = this.top;
}
setNextPositionForAnimation(position) {
let top = Math.floor(position / GRID_SIZE);
let left = position % GRID_SIZE;
this.top = top * CELL_SIZE;
this.left = left * CELL_SIZE;
}
moveDiv(dir) {
if (this.pos == RIGHT_TUNNEL || this.pos == LEFT_TUNNEL) {
this.setToPosition(this.pos)
}
if (dir !== null) {
if (dir.code == DIRECTIONS.ArrowLeft.code) {
let newLeft = parseInt(this.currentLeft) - CELL_SIZE / this.speed;
if (newLeft >= this.left) {
this.setTransform(newLeft, this.top);
this.currentLeft = newLeft;
this.isStepDone = (newLeft === this.left);
}
} else if (dir.code == DIRECTIONS.ArrowUp.code) {
let newTop = parseInt(this.currentTop) - CELL_SIZE / this.speed;
if (newTop >= this.top) {
this.setTransform(this.left, newTop);
this.currentTop = newTop;
this.isStepDone = newTop === this.top;
}
} else if (dir.code == DIRECTIONS.ArrowRight.code) {
let newLeft = parseInt(this.currentLeft) + CELL_SIZE / this.speed;
if (newLeft <= this.left) {
this.setTransform(newLeft, this.top);
this.currentLeft = newLeft;
this.isStepDone = (newLeft === this.left);
}
} else if (dir.code == DIRECTIONS.ArrowDown.code) {
let newTop = parseInt(this.currentTop) + CELL_SIZE / this.speed;
if (newTop <= this.top) {
this.setTransform(this.left, newTop);
this.currentTop = newTop;
this.isStepDone = (newTop === this.top);
}
}
}
}
setTransform(left, top) {
this.div.style.transform = `translate(${left}px,${top}px)`;
if (this.rotation && this.dir) {
this.div.style.transform = this.div.style.transform + `rotate(${this.dir.rotation}deg)`;
}
}
}
export default Character;