-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEntity.js
270 lines (222 loc) · 9.66 KB
/
Entity.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
268
269
270
/**
*
* @licstart The following is the entire license notice for the
* JavaScript code in this page.
*
* Copyright (C) 2012 David "aniwey" L.
*
*
* The JavaScript code in this page is free software: you can
* redistribute it and/or modify it under the terms of the GNU
* General Public License (GNU GPL) as published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version. The code is distributed WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
*
* As additional permission under GNU GPL version 3 section 7, you
* may distribute non-source (e.g., minimized or compacted) forms of
* that code without the copy of the GNU GPL normally required by
* section 4, provided you include this license notice and a URL
* through which recipients can access the Corresponding Source.
*
* @licend The above is the entire license notice
* for the JavaScript code in this page.
*
*/
function Entity(x, y, width, height, color, affectedByCollisions, affectedByGravity, canBeKilled, stuffCanGoThrough){
// Set members from parameters
this.x = x;
this.y = y;
this.affectedByCollisions = affectedByCollisions;
this.affectedByGravity = affectedByGravity;
this.canBeKilled = canBeKilled;
this.stuffCanGoThrough = stuffCanGoThrough;
// Last entity loop report
this.lastEntityLoopReport = undefined;
// Set the default speed
this.xSpeed = 0;
this.ySpeed = 0;
// Set the default movement
this.xMovement = 0;
this.yMovement = 0;
// Not dead by default
this.dead = false;
// Add the Kinetic rectangle of the entity
this.kineticRectangle = new Kinetic.Rect({
x: x - Game.viewportXOffset,
y: y,
width: width,
height: height,
fill: color
});
}
// Test collision with another entity
Entity.prototype.testCollisionWithAnEntity = function(entity){
// Return false if there is no collision
if(this.x + this.kineticRectangle.width() <= entity.x)
return false;
if(this.x >= entity.x + entity.kineticRectangle.width())
return false;
if(this.y + this.kineticRectangle.height() <= entity.y)
return false;
if(this.y >= entity.y + entity.kineticRectangle.height())
return false;
// Collision, return true
return true;
};
// Get a collision report with another entity
Entity.prototype.getCollisionReportWithAnEntity = function(entity){
// Create the entity collision report
var entityCollisionReport = new EntityCollisionReport();
// If there is a collision
if(this.testCollisionWithAnEntity(entity)){
// Add the entity to the entity collision report entities list
entityCollisionReport.entitiesList.push(entity);
// Unless we can go through it
if(!entity.stuffCanGoThrough){
// Set the entity collision report collision boolean
entityCollisionReport.collisionBoolean = true;
// Set the entity collision report collision points minimum and maximum positions
if(!entityCollisionReport.minimumXCollisionPoint || entity.x < entityCollisionReport.minimumXCollisionPoint)
entityCollisionReport.minimumXCollisionPoint = entity.x;
if(!entityCollisionReport.minimumYCollisionPoint || entity.y < entityCollisionReport.minimumYCollisionPoint)
entityCollisionReport.minimumYCollisionPoint = entity.y;
if(!entityCollisionReport.maximumXCollisionPoint || entity.x + entity.kineticRectangle.width() > entityCollisionReport.maximumXCollisionPoint)
entityCollisionReport.maximumXCollisionPoint = entity.x + entity.kineticRectangle.width();
if(!entityCollisionReport.maximumYCollisionPoint || entity.y + entity.kineticRectangle.height() > entityCollisionReport.maximumYCollisionPoint)
entityCollisionReport.maximumYCollisionPoint = entity.y + entity.kineticRectangle.height();
}
}
// Return the entity collision report
return entityCollisionReport;
};
// Loop method
Entity.prototype.loop = function(){
// Create the entity loop report
var entityLoopReport = new EntityLoopReport();
// Apply gravity on our speed
this.ySpeed += this.getGravityForce();
// If the entity should move
if(this.xSpeed + this.xMovement != 0 || this.ySpeed + this.yMovement != 0){
// Move the entity depending on its speed
entityLoopReport.entityMoveReport = this.move();
// Reset speed depending on the move report
if(entityLoopReport.entityMoveReport.leftCollision || entityLoopReport.entityMoveReport.rightCollision)
this.xSpeed = 0;
if(entityLoopReport.entityMoveReport.topCollision || entityLoopReport.entityMoveReport.bottomCollision)
this.ySpeed = 0;
}
// Store the new entity loop report as the last entity loop report
this.lastEntityLoopReport = entityLoopReport;
// Return the entity loop report
return entityLoopReport;
};
// Moving method
Entity.prototype.move = function(){
// Create the entity move report
var entityMoveReport = new EntityMoveReport();
// If we're not dead
if(!this.dead){
// Move along x
if(this.xSpeed + this.xMovement != 0)
entityMoveReport.merge(this.moveAlongX());
}
// If we're not dead
if(!this.dead){
// Move along y
if(this.ySpeed + this.yMovement != 0)
entityMoveReport.merge(this.moveAlongY());
}
// Reset the movement
this.xMovement = 0;
this.yMovement = 0;
// Return the entity move report
return entityMoveReport;
};
// Move along x
Entity.prototype.moveAlongX = function(){
// Create the entity move report
var entityMoveReport = new EntityMoveReport();
// Move
this.x += (this.xSpeed + this.xMovement) * (Game.gameState == GameState.WON? Math.random() * 20 : 1);
// Get the collision report
entityMoveReport.entityCollisionReport = Game.getEntityFullCollisionReport(this);
// If we are affected by collisions and there is a collision
if(this.affectedByCollisions && entityMoveReport.entityCollisionReport.collisionBoolean){
// If we were moving to the right
if(this.xSpeed + this.xMovement > 0){
// Set the new x position and note that there was a right collision
this.x = entityMoveReport.entityCollisionReport.minimumXCollisionPoint - this.kineticRectangle.width();
entityMoveReport.rightCollision = true;
}
// Else, we were moving to the left
else{
// Set the new x position and note that there was a left collision
this.x = entityMoveReport.entityCollisionReport.maximumXCollisionPoint;
entityMoveReport.leftCollision = true;
}
}
// If we have a collision report
if(entityMoveReport.entityCollisionReport){
// Get hit by and hit all entities listed in the collision report
for(var i = 0; i < entityMoveReport.entityCollisionReport.entitiesList.length; i++){
entityMoveReport.entityCollisionReport.entitiesList[i].hit(this);
this.hit(entityMoveReport.entityCollisionReport.entitiesList[i]);
}
}
// Return the entity move report
return entityMoveReport;
};
// Move along y
Entity.prototype.moveAlongY = function(){
// Create the entity move report
var entityMoveReport = new EntityMoveReport();
// Move
this.y += (this.ySpeed + this.yMovement) * (Game.gameState == GameState.WON? Math.random() * 20 : 1);
// Get the collision report
entityMoveReport.entityCollisionReport = Game.getEntityFullCollisionReport(this);
// If we are affected by collisions and there is a collision
if(this.affectedByCollisions && entityMoveReport.entityCollisionReport.collisionBoolean){
// If we were moving to the bottom
if(this.ySpeed + this.yMovement > 0){
// Set the new y position and note that there was a bottom collision
this.y = entityMoveReport.entityCollisionReport.minimumYCollisionPoint - this.kineticRectangle.height();
entityMoveReport.bottomCollision = true;
}
// Else, we were moving to the top
else{
// Set the new y position and note that there was a top collision
this.y = entityMoveReport.entityCollisionReport.maximumYCollisionPoint;
entityMoveReport.topCollision = true;
}
}
// If we have a collision report
if(entityMoveReport.entityCollisionReport){
// Get hit by and hit all entities listed in the collision report
for(var i = 0; i < entityMoveReport.entityCollisionReport.entitiesList.length; i++){
entityMoveReport.entityCollisionReport.entitiesList[i].hit(this);
this.hit(entityMoveReport.entityCollisionReport.entitiesList[i]);
}
}
// Return the entity move report
return entityMoveReport;
};
// Hitting method
Entity.prototype.hit = function(entity){
};
// Get the gravity force applied to the entity
Entity.prototype.getGravityForce = function(){
if(this.affectedByGravity)
return Game.gravityForce;
else
return 0;
};
// Die
Entity.prototype.die = function(){
// We set ourselves dead so that the Game can remove us
this.dead = true;
// We remove our rectangle from the main layer if we're still playing
if(Game.gameState === GameState.PLAYING)
this.kineticRectangle.remove();
}