-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.js
More file actions
256 lines (246 loc) · 7.29 KB
/
GameObject.js
File metadata and controls
256 lines (246 loc) · 7.29 KB
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
/**
* The object and its movements are static relative to the screen.
*/
const RELATIVE_MOVEMENT_STATIC = 0;
/**
* The object moves relative to the stage (towards the player at stage speed).
*/
const RELATIVE_MOVEMENT_STAGE = 1;
/**
* The object is influenced by gravity relative to the screen.
*/
const RELATIVE_MOVEMENT_GRAVITY = 2;
const STAGE_DEFAULT_SPEED = 500;
const GRAVITY_DEFAULT = 25;
const OBJ_BEHAVIOURS = {
constant_speed: {
default: function(dT) {
this.targetX = this.x;
this.targetY = 10000;
}
},
home_and_ram: {
default: function(dT) {
this.targetX = this.scene.player.x;
this.targetY = this.scene.player.y;
}
},
aim: {
default: function(dT) {
this.targetX = this.scene.player.x;
this.targetY = this.scene.player.y;
// move the object towards the target
// calculate distances in X and Y
const xDiff = this.targetX-this.x;
const yDiff = this.targetY-this.y;
// get the angle pointing towards target
const axisAngle = Math.atan2(yDiff,xDiff);
this.targetX=Math.cos(axisAngle)*10000;
this.targetY=Math.sin(axisAngle)*10000;
this.ai_state = "on_the_way";
},
on_the_way: function(dT) {
},
reached_target: function(dT) {
}
},
wander_top_shoot: {
default: function(dT) {
let mw=this.scene.shortSide;
let mh=this.scene.longSide;
let x=Math.random()*mw;
let y=Math.random()*(mh*0.3);
this.targetX = x
this.targetY = y;
this.ai_state = "on_the_way";
},
on_the_way: function(dT) {
if(this.abilities && this.abilities[0])
{
this.abilities[0].use();
}
},
reached_target: function(dT) {
this.ai_state="default";
}
}
}
/**
* Represents an object in the game that can be drawn and updated.
*/
class GameObject
{
/**
Basic type of an object.
*/
type = "object";
/**
Reference to the scene the object belongs to.
*/
scene = null;
/**
X coordinate of the object.
*/
x = 0;
/**
Y coordinate of the object.
*/
y = 0;
/**
X coordinate the object is currently moving towards.
*/
targetX = 0;
/**
Y coordinate the object is currently moving towards.
*/
targetY = 0;
/**
Speed of the movement towards target coordinates.
*/
speed = 0;
movementVector = {x:0,y:0};
/**
The movement behaviour of the object.
*/
screenMovement = RELATIVE_MOVEMENT_STATIC;
/**
If object is dead, it shall not be updated or drawn.
*/
isDead = false;
/**
The hitbox of the object without translation to object's coordinates.
*/
originalHitbox = null;
/**
The hitbox of the object relative to the screen, translated by its coordinates.
*/
hitbox = null;
/**
Contains the VectorSprite used to represent the entity on the playing field.
*/
sprite = null;
ai_state ="";
ai_behaviour = null;
layer = "main";
/**
* Creates an instance of the object given type
* @param {string} type - the type of the object.
*/
constructor(type)
{
this.type = type;
// init some default values
this.originalHitbox = new Rectangle(0,0,1,1);
this.hitbox = new Rectangle(0,0,1,1);
}
/**
* Function to run on object's death.
*/
onDeath = function(){};
/**
* Makes the object die and runs onDeath()
*/
die()
{
this.onDeath();
this.isDead=true;
}
/**
* Updates the object's hitbox
*/
recalcHitbox()
{
this.hitbox.x= this.originalHitbox.x+this.x;
this.hitbox.y=this.originalHitbox.y+this.y;
this.hitbox.width = this.originalHitbox.width;
this.hitbox.height = this.originalHitbox.height;
}
/**
* Updates the object's state given elapsed time.
* @param {number} dT - elapsed time
* @returns
*/
update(dT)
{
// don't do anything if object is dead
if(this.isDead)
return;
if(this.ai_behaviour)
{
let action = this.ai_behaviour[this.ai_state];
if(!action)
action = this.ai_behaviour.default;
if(action)
{
action.call(this,dT);
}
}
if(this.screenMovement==RELATIVE_MOVEMENT_STATIC)
{
// move the object towards the target
// calculate distances in X and Y
const xDiff = this.targetX-this.x;
const yDiff = this.targetY-this.y;
// get the angle pointing towards target
const axisAngle = Math.atan2(yDiff,xDiff);
// get offsets based on the angle and object's speed
this.movementVector.x = this.speed * Math.cos(axisAngle) * dT;
this.movementVector.y = this.speed * Math.sin(axisAngle) * dT;
// if the new offset "overshoots" the destination,
// snap the object to the destination coordinate
// this prevents jitter
const dist = Math.sqrt(xDiff*xDiff+yDiff*yDiff);
const delta = Math.sqrt(this.movementVector.x*this.movementVector.x+this.movementVector.y*this.movementVector.y);
if(delta>dist)
{
this.x=this.targetX;
this.y=this.targetY;
this.movementVector={x:0,y:0};
if(this.ai_state=="on_the_way")
{
this.ai_state="reached_target";
}
}
}
else if(this.screenMovement==RELATIVE_MOVEMENT_STAGE)
{
this.movementVector.x=0;
this.movementVector.y=(STAGE_DEFAULT_SPEED+this.speed)*this.scene.speedMultiplier*dT;
}
else if(this.screenMovement==RELATIVE_MOVEMENT_GRAVITY)
{
this.movementVector.y+=GRAVITY_DEFAULT*dT;
}
this.x+=this.movementVector.x;
this.y+=this.movementVector.y;
this.recalcHitbox();
// clean up any objects that are long past the player
if(this.x<-1000 || this.x>3000 || this.y<-1000 || this.y>3000)
{
this.isDead=true;
}
if(this.sprite)
this.sprite.update(dT);
}
/**
* Draws the object using given canvas context.
* @param {CanvasRenderingContext2D} ctx
*/
draw(ctx)
{
if(!this.sprite)
return;
// ensure object gets cleanly translated
ctx.resetTransform();
ctx.translate(this.x,this.y);
// draw the animation #TODO - make this less hardcoded
this.sprite.draw(ctx);
ctx.resetTransform();
if(window.gameManager.debug)
{
ctx.strokeStyle="#FF0000";
ctx.lineWidth=1;
ctx.strokeRect(this.hitbox.x+0.5,this.hitbox.y+0.5,this.hitbox.width,this.hitbox.height);
}
}
}