-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameobject.js
182 lines (154 loc) · 4.68 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
var gameObjectArray = [];
// GameObject
function GameObject(spawnPosX, spawnPosY, spawnPosZ, name){
// Initial Variables
this.id = gameObjectArray.length;
this.name = name;
this.tags = [];
this.width = 10;
this.height = 10;
this.position = [];
this.position.x = spawnPosX;
this.position.y = spawnPosY;
this.position.z = spawnPosZ;
this.visible = true;
this.color = "white";
this.changeDirection = false;
this.collider = [];
this.collider.height = this.height;
this.collider.width = this.width;
this.collider.enabled = false;
this.collider.borders = [];
this.collider.borders.enabled = false;
// Add GameObject to array
gameObjectArray.push(this);
// Move
this.move = function(x,y,z){
// Detect Level Border Collisions
if (this.collider.borders.enabled == true){
if (this.detectBorderCollision() == 1){
if (y < 0){y = 0;}
} else if (this.detectBorderCollision() == 2){
if (y > 0){y = 0;}
} else if (this.detectBorderCollision() == 3){
if (x < 0){x = 0;}
} else if (this.detectBorderCollision() == 4){
if (x > 0){x = 0;}
} else if (this.detectBorderCollision() == 5){
if (y > 0){y = 0;}
if (x > 0){x = 0;}
} else if (this.detectBorderCollision() == 6){
if (y < 0){y = 0;}
if (x < 0){x = 0;}
} else if (this.detectBorderCollision() == 7){
if (x > 0){x = 0;}
if (y < 0){y = 0;}
} else if (this.detectBorderCollision() == 8){
if (x < 0){x = 0;}
if (y > 0){y = 0;}
}
}
// Move GameObject
this.position.x += x;
this.position.y += y;
this.position.z += z;
}
// Level Border Collision Detection
this.detectBorderCollision = function(){
if (this.position.y < Level.border.top){
return 1; // Top
} else if (this.position.y > Level.border.bottom){
return 2; // Bottom
} else if (this.position.x < Level.border.left){
return 3; // Left
} else if (this.position.x > Level.border.right){
return 4; // Right
} else if (this.position.x > Level.border.right && this.position.y > Level.border.bottom){
return 5; // Right Bottom
} else if (this.position.x < Level.border.left && this.position.y < Level.border.top){
return 6; // Left Top
} else if (this.position.x > Level.border.right && this.position.y < Level.border.top){
return 7; // Right Top
} else if (this.position.x < Level.border.left && this.position.y > Level.border.bottom){
return 8; // Left Bottom
} else {
return 0;
}
}
// Collision Trigger
this.collision = function(id){
}
// Detect ObjectCollision
this.detectCollisions = function(){
var collisions = [];
// Search for local colliding objects
for (var i = 0; i < gameObjectArray.length; i++){
if (gameObjectArray[i] != null){
if (gameObjectArray[i].collider.enabled && gameObjectArray[i].id != this.id){
if ((gameObjectArray[i].position.x > this.position.x - this.width/2 - gameObjectArray[i].width*0.2)
&& (gameObjectArray[i].position.x < this.position.x + this.width/2 + gameObjectArray[i].width*0.2)
&& (gameObjectArray[i].position.y > this.position.y - this.width/2 - gameObjectArray[i].width*0.2)
&& (gameObjectArray[i].position.y < this.position.y + this.width/2 + gameObjectArray[i].width*0.2)){
collisions.push(gameObjectArray[i].id);
}
}
}
}
// Call the collision trigger
if (collisions.length > 0){
for (var i = 0; i < collisions.length; i++){
this.collision(collisions[i]);
}
}
}
// Set GameObject position
this.setPosition = function(x,y,z){
this.position.x = x;
this.position.y = y;
this.position.z = z;
}
// Check if GameObject contains tag
this.containsTag = function(tag){
return !!(this.tags.indexOf(tag) + 1);
}
this.destroy = function(object){
delete gameObjectArray[object.id];
}
// Update
this.update = function(){
}
}
// Spawn
GameObject.spawn = function(type, posX, posY, posZ, name){
var gameObject = new GameObject(posX, posY, posZ, name)[type]();
}
// Destroy
GameObject.destroy = function(object){
delete gameObjectArray[object.id];
}
// Tags
GameObject.tags = [];
// Check if tag exists
GameObject.tags.contains = function(tag){
for (var i = 0; i < gameObjectArray.length; i++){
return !!(gameObjectArray[i].tags.indexOf(tag) + 1);
}
}
// Return GameObject IDs of a tag
GameObject.tags.getIDs = function(tag){
var IDArray = [];
for (var i = 0; i < gameObjectArray.length; i++){
if (!!(gameObjectArray[i].tags.indexOf(tag) + 1)){
IDArray.push(gameObjectArray[i].id);
}
}
return IDArray;
}
// Return GameObject ID of a name
GameObject.getID = function(name){
for (var i = 0; i < gameObjectArray.length; i++){
if (!!(gameObjectArray[i].name.indexOf(name) + 1)){
return gameObjectArray[i].id;
}
}
}