-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameobject.js
167 lines (134 loc) · 4.95 KB
/
gameobject.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
//@ts-check
var ID = 0;
const OUT_OF_BOUNDS_THRESHOLD = 300;
/**
* Represents an entity with movement, collision, health, and an appearance. Added to World and updated in a loop.
*/
class GameObject{
constructor(type, x, y, depth){
this.slatedForDeletion = false;
this.ID = ID;
this.collider = new Collider(this, 30);//Movement requires Collider so initialize it first
this.movement = new Movement(x, y, this);
this.x = this.movement.position.x;
this.y = this.movement.position.y;
/**
* Used to indentify the type of object (i.e. for collisions, etc) for internal purposes. This is NOT its name.
*/
this.types = [];
if(depth!=null){
this.depth=depth;
} else {
this.depth=0;
}
this.position = this.movement.position;
this.maxHitpoints = 100;
this.hitpoints = 100;
if(type==undefined){
throw new Error("GameObjects require a type string parameter");
}
this.types.push(type);
/**
* Whether to automatically slate this object for deletion if it goes a certain distance
* beyond borders (controlled by a constant).
*/
this.outOfBoundsDisposal = true;
/**
* Whether to log a warning message if this object gets OOB-disposed on its first update frame
*/
this.immediateDisposalWarning = true;
this.framesExisted = 0;
ID++;
}
/**
* Calls this GameObject's MovementModule's update() function, then the GameObject's update() function.
* This function is not intended to be overriden, override update() instead
*/
mainUpdate(){
this.framesExisted++;
if(this.outOfBoundsDisposal){
this.detectOutOfBounds();
}
this.movement.update();
this.collider.update();
//shorthands for convenience purposes
this.x = this.movement.position.x;
this.y = this.movement.position.y;
this.position = this.movement.position;
this.update();
}
/**
* Called from the game loop each frame.
*/
update(){
}
/**
* Called after the update() function of each GameObject from the main game loop.
*/
display(){
}
/**
* By default, sets the GameObject's SlatedForDeletion property to true; it will be removed next frame without update() being called.
* If overriden, remember that you must set this GameObject's slatedForDeletion property to true to remove it from the game loop.
*/
destroy(){
this.slatedForDeletion = true;
}
/**
* Called by the coupled Collider when the circular hitbox collides with this one.
*/
collide(other){
}
isTouchingMouse(){
let distance = dist(this.x, this.y, mouseX, mouseY);
if(distance<=this.collider.collideRadius){
return true;
}
return false;
}
/**
* These are not unique. Returns this GameObject's name identifier (null by default unless overriden). This will be displayed alongside the ID when using debugLog.
* This is NOT the GameObject *type* property, which is used for internal purposes.
* @returns the custom name for this object; null by default
*/
getName(){
return `<${this.types}>`;
}
/**
* NOT intended to be overriden or called from outside of gameObject.mainUpdate().
* If automatic OOB disposal is set to true, this slate this object for deletion if the gameObject
* position goes a certain distance outside of the canvas. This does NOT call destroy()
*/
detectOutOfBounds(){
let offset = OUT_OF_BOUNDS_THRESHOLD;
if(this.x > width+offset || this.x < 0-offset || this.y > height+offset || this.y < 0-offset){
this.slatedForDeletion = true;
if(this.framesExisted==1 && this.immediateDisposalWarning){
this.debugLog("WARNING: I was disposed on frame 1");
}
}
}
/**
* Logs the GameObject's ID, name (its type if not defined), and the specified text to the console.
* @param {Object} stuff The text to display.
*/
debugLog(stuff){
let t = `<${this.types}>`;
console.log(`GameObject ID ${this.ID} (${this.getName()!=null?this.getName():t}): ${stuff}`);
}
/**
* Draws the GameObject's ID, name (its type if not defined), and the specified text at the location of this GameObject.
* @param {Object} str The text to display.
*/
debugDraw(str){
push();{
fill(color(255, 0, 0));
textAlign(CENTER);
let t = `<${this.types}>`;
text(`${this.ID} (${this.getName()!=null?this.getName():t}): ${str==undefined?"":str}`, this.movement.position.x, this.movement.position.y);
}pop();
}
damage(amount){
this.hitpoints-=amount;
}
}