-
Notifications
You must be signed in to change notification settings - Fork 0
/
collider.js
55 lines (46 loc) · 1.76 KB
/
collider.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
//@ts-check
/**
* Handles collisions for the GameObject.
* Every GameObject has this.
* Uses simple circle radius collision detection
*/
class Collider {
constructor(gameObject, collideRadius){
this.gameObject = gameObject;
this.collideRadius = collideRadius;
/**
* whether this detects collisions and tells the parent gameobject it's collided with something
*/
this.detectCollisions = true;
if(!(gameObject instanceof GameObject)){
throw new Error("Collider object wasn't passed a required gameObject reference");
}
}
/**
* Called from this GameObject's MainUpdate(); no need to call this from anywhere else
*/
update(){
if(this.detectCollisions){
//loop through all gameObjects and their colliders
for(let i = 0; i < World.instance.gameObjects.length; i++){
let other = World.instance.gameObjects[i];
if(other==this.gameObject)continue;//avoid colliding with self!!
let dist = this.gameObject.movement.position.dist(other.movement.position);
if(dist < this.collideRadius + other.collider.collideRadius){//if collision radius is touching
this.gameObject.collide(other);
}
}
}
}
/**
* Draws a circle at the location of the GameObject representing the precise collision radius of this collider
*/
drawCollisionRadius(){
push();{
stroke(color(239, 14, 104));
fill(color(0, 0));//transparent fill
//seriously? the third param is diameter and not circle?
circle(this.gameObject.x, this.gameObject.y, this.collideRadius*2);
}pop();
}
}