-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.js
162 lines (128 loc) · 4.56 KB
/
Game.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
class Game {
constructor(context, canvas) {
this._context = context;
this._canvas = canvas;
var self = this;
this._player = new Player();
this._manager = {}
this._levels = [
new Level1(), new Level2(), new Level3(),
new Level4(), new Level5(), new Level6(),
new Level7()
];
this._currentLevel = 0;
var gridPosition = {
x: canvas.width / 2 - (10 * Slot.Size) / 2,
y: canvas.height / 2 - (10 * Slot.Size) / 2 - Slot.Size
}
var toolbarPosition = {
x: canvas.width / 2 - (10 * Slot.Size) / 2,
y: canvas.height / 2 + (10 * Slot.Size) / 2
}
this._grid = new Grid(gridPosition, this._player, this._manager, 10, 10);
this._toolbar = new Toolbar(toolbarPosition, this._player, this._manager);
this._nextBtn = new Button(
{x: toolbarPosition.x + Slot.Size * 10, y: toolbarPosition.y},
100, 40,
"Next",
() => {
self._nextBtn.disable();
// switch the level
self._currentLevel = (self._currentLevel + 1) % self._levels.length;
self.initCurrentLevel();
self.render();
if(self._currentLevel === self._levels.length - 1)
self._nextBtn.text = ""
}
)
this._nextBtn.disable();
canvas.addEventListener("mousedown", (event) => {
self.onMouseDown(event);
})
canvas.addEventListener("mousemove", (event) => {
self.onMouseMove(event);
});
}
getMousePosition(event) {
return {
x: event.clientX,
y: event.clientY
}
}
allTargetsActive() {
let targets = this._levels[this._currentLevel].targets;
if(targets.length === 0) return false;
for (const target of targets) {
console.log(target.isActivated());
console.log(target);
if(!target.isActivated()) {
return false;
}
}
return true;
}
initCurrentLevel() {
this._levels[this._currentLevel].init(this._grid, this._toolbar);
}
render() {
this._context.clearRect(0, 0, this._canvas.width, this._canvas.height);
context.font = "15px Arial";
context.fillStyle = "black";
context.textAlign = "left";
context.textBaseline = "top";
this._context.fillText("Level: " + (this._currentLevel + 1), 25, 25)
this._levels[this._currentLevel].render(this._context);
this._grid.render(this._context);
this._toolbar.render(this._context);
this._nextBtn.render(this._context);
this._levels[this._currentLevel].targets.forEach((target) => {
target.deactivate();
});
this._grid.projectLaser(this._context);
}
onMouseDown(event) {
// find the clicked game object
let point = this.getMousePosition(event);
let slot = this._toolbar.findSlotContainingPoint(point);
slot = (slot) ? slot : this._grid.findSlotContainingPoint(point);
if(slot)
if(!this._player.hand && !slot.isFixed) {
this._player.hand = slot.item;
slot.removeItem();
} else if(slot.isEmpty()) {
slot.addItem(this._player.hand);
this._player.hand = null;
}
this._levels[this._currentLevel].targets.forEach((target) => {
target.deactivate();
});
this._grid.projectLaser(this._context);
this._nextBtn.onClick(point);
if(this.allTargetsActive()) {
this._nextBtn.enable();
if(this._currentLevel === this._levels.length - 1)
this._nextBtn.text = "You Win!!";
}
}
onMouseMove(event) {
this._context.clearRect(0, 0, this._canvas.width, this._canvas.height);
this.render(this._context);
if(this._player.hand) {
this._player.hand.position = {
x: event.clientX - GameObject.Size / 2,
y: event.clientY - GameObject.Size / 2
};
this._player.hand.render(this._context);
}
this._context.beginPath();
this._context.ellipse(
event.clientX,
event.clientY,
2, 2, 0,
0, Math.PI * 2
)
this._context.closePath();
this._context.fillStyle = "red";
this._context.fill();
}
}