-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
267 lines (260 loc) · 7.86 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"use strict";
class Vector {
constructor(position0, position1) {
this.position0 = position0;
this.position1 = position1;
}
isXaxelPositive() {
return this.position0.getCoordinates().x < this.position1.getCoordinates().x;
}
isYaxelPositive() {
return this.position0.getCoordinates().y < this.position1.getCoordinates().y;
}
isStallX() {
return this.position0.getCoordinates().x === this.position1.getCoordinates().x;
}
isStallY() {
return this.position0.getCoordinates().y === this.position1.getCoordinates().y;
}
}
class Position {
constructor(coordinates) {
this.coordinates = coordinates;
this.prevCoordinates = null;
}
getCoordinates() {
return this.coordinates;
}
setCoordinates(coordinates) {
this.prevCoordinates = this.coordinates;
this.coordinates = coordinates;
}
getPreviousCoordinates() {
return this.prevCoordinates;
}
isOutOfBoundaries(boundaries) {
return JSON.stringify(this.getOutOfBoundariesShift(boundaries)) !== "{}";
}
getOutOfBoundariesShift(boundaries) {
let currentPos = this.getCoordinates();
let outOfBoundShift = {};
for (let axel in currentPos) {
if (boundaries.hasOwnProperty(axel) && boundaries[axel] < currentPos[axel]) {
outOfBoundShift[axel] = currentPos[axel] - boundaries[axel];
} else if (currentPos[axel] < 0) {
outOfBoundShift[axel] = currentPos[axel];
}
}
return outOfBoundShift;
}
}
class GameInterface {
constructor() {
this.subscribers = [];
}
addSubscriber(subscriber) {
this.subscribers.push(subscriber);
}
removeSubscriber(subscriber) {
let i = this.subscribers.indexOf(subscriber);
if (i !== -1) {
this.subscribers.splice(i, 1);
}
}
notify(message) {
this.subscribers.forEach(function (item) {
item.onMessage(message);
});
}
}
class GameObject {
constructor() {
this.subscriptions = [];
}
subscribe(eventSource) {
eventSource.addSubscriber(this);
this.subscriptions.push(eventSource);
}
onMessage(message) {
console.log('default action:', message);
}
unsubscribe(eventSource) {
let i = this.this.subscriptions.indexOf(eventSource);
if (i !== -1) {
this.subscriptions[i].removeSubscriber(this);
this.subscriptions.splice(i, 1);
}
}
}
class Shape extends GameObject {
constructor (properties) {
super();
this.timestamp = new Date();
let shape = document.createElement('div');
this.setHtmlElement(shape);
this.addProperties(properties);
let currentPos = new Position({x: shape.offsetLeft, y: shape.offsetTop});
this.setPosition(currentPos);
let prevPos = new Position({x: shape.offsetLeft, y: shape.offsetTop});
this.setPreviousPosition(prevPos);
this.setVector(new Vector(prevPos, currentPos));
}
/**
* Реализовать самому
*/
addProperties() {
//parend.addProperties() called. Need to redefine this functuon.
}
setHtmlElement(element) {
this.htmlElement = element;
}
getHtmlElement(element) {
return this.htmlElement;
}
setPosition(position) {
let element = this.getHtmlElement();
let coords = position.getCoordinates();
if (position.getPreviousCoordinates()) {
this.getPreviousPosition().setCoordinates(position.getPreviousCoordinates());
}
element.style.left = coords.x + 'px';
element.style.top = coords.y + 'px';
this.setHtmlElement(element);
this.position = position;
this.setVector(new Vector(this.getPreviousPosition(), this.getPosition()));
}
getPosition() {
return this.position;
}
setPreviousPosition(position) {
this.previousPosition = position;
}
getPreviousPosition() {
return this.previousPosition;
}
getVector() {
return this.vector;
}
setVector(vector) {
this.vector = vector;
}
onMessage(message) {
if (message.hasOwnProperty('boundaries')) {
this.move(message.boundaries);
}
}
move(boundaries) {
let currentPosition = this.getPosition();
let vector = this.getVector();
let x = currentPosition.getCoordinates().x;
let y = currentPosition.getCoordinates().y;
let adjustX = 5;
let adjustY = 5;
let isOutOfBound = currentPosition.isOutOfBoundaries(boundaries);
let outOfBoundShift = null;
if (isOutOfBound) {
outOfBoundShift = currentPosition.getOutOfBoundariesShift(boundaries);
}
let xDirectionForward = true;
let yDirectionForward = true;
if (!(vector.isXaxelPositive() || vector.isStallX())) {
xDirectionForward = false;
}
if (!(vector.isYaxelPositive() || vector.isStallY())) {
yDirectionForward = false;
}
//adjusting "speed" as vector tell us
if (!xDirectionForward) {
adjustX = -adjustX;
}
if (!yDirectionForward) {
adjustY = -adjustY;
}
if (isOutOfBound) {
//changing vectors directions because of borders
if (outOfBoundShift.hasOwnProperty('x')) {
adjustX = -adjustX;
}
if (outOfBoundShift.hasOwnProperty('y')) {
adjustY = -adjustY;
}
}
x += adjustX;
y += adjustY;
let pos = this.getPosition();
pos.setCoordinates({x: x, y: y});
this.setPosition(pos);
}
}
class Square extends Shape {
constructor(color) {
super({'color': color});
}
addProperties(properties) {
let element = this.getHtmlElement();
element.style.background = properties.color;
element.classList.add('square');
this.setHtmlElement(element);
}
}
class Game extends GameInterface {
constructor(options) {
super();
this.boundaries = options.boundaries;
this.objects = [];
}
start() {
this.tickInterval = setInterval(this.gameTick.bind(this), 50);
}
stop() {
clearInterval(this.tickInterval);
this.tickInterval = null;
}
isRunning() {
return this.tickInterval !== null;
}
addObject(object) {
object.subscribe(this);
this.objects.push(object);
}
gameTick() {
this.objects.forEach(this.movePosition.bind(this));
}
movePosition(object) {
this.notify({boundaries: this.boundaries});
}
}
(function () {
let world = document.getElementById('world');
window.addEventListener('keydown', function (e) {
if (e.code === 'Escape') {
if (game.isRunning()) {
game.stop();
} else {
game.start();
}
}
});
const worldHeight = world.offsetHeight;
const worldWidth = world.offsetWidth;
let square = new Square('green');
square.setPosition(new Position({x: 380, y: 250}));
world.append(square.getHtmlElement());
let game = new Game({boundaries: {x: worldWidth, y: worldHeight}});
game.start();
game.addObject(square);
for (let i = 1; i < 5; i++) {
let color = 'red';
if (i % 2 > 0) {
color = 'blue';
} else if (i % 3 > 0) {
color = 'yellow';
} else if (i % 5 > 0) {
color = 'brown';
}
let square2 = new Square(color);
square2.setPosition(new Position({x: Math.round(Math.random() * 300), y: Math.round(Math.random() * 250)}));
world.append(square2.getHtmlElement());
game.addObject(square2);
}
})(window);