class Collider { gameObject = null; enabled = false; constructor(gameObject) { this.gameObject = gameObject; } update(deltaTime) { if (GameInput.mousePressed()) { this.checkClick(true); } if (GameInput.mouseHeld()) { this.checkClick(false); } this.checkCollisions() } checkClick(thisFrame) { var hit = false; var clickPos = GameInput.mousePosition.subtract(this.gameObject.scene.position).subtract(this.gameObject.scene.cameraPosition); var hw = this.transform.size.x / 2; var hh = this.transform.size.y / 2; if(clickPos.x > this.transform.position.x - hw && clickPos.x < this.transform.position.x + hw) { if(clickPos.y > this.transform.position.y - hh && clickPos.y < this.transform.position.y + hh) { hit = true; } } if (hit) { if (thisFrame) { this.onClick && this.onClick(); } else { this.onMouseHeld && this.onMouseHeld(); } } } checkCollisions() { if(this.enabled && !this.gameObject.destroyed) { for(var i = 0; i < this.gameObject.scene.activeObjects.length; i++) { if(this.gameObject.scene.activeObjects[i] != this && this.gameObject.scene.activeObjects[i].ignoreCollisions) { var hit = false; var o2 = this.gameObject.scene.activeObjects[i]; //hw = halfWidth, hh = halfHeight var hw = this.gameObject.transform.size.x / 2; var hh = this.gameObject.transform.size.y / 2; var ohw = o2.transform.size.x / 2; var ohh = o2.transform.size.y / 2; //CHECK FROM OBJ PERSPECTIVE var p = [ [this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y - hh], [this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y - hh], [this.gameObject.transform.position.x - hw, this.gameObject.transform.position.y + hh], [this.gameObject.transform.position.x + hw, this.gameObject.transform.position.y + hh] ]; for(var pi = 0; pi < p.length; pi++) { if(p[pi][0] > o2.transform.position.x - ohw && p[pi][0] < o2.transform.position.x + ohw) { if(p[pi][1] > o2.transform.position.y - ohh && p[pi][1] < o2.transform.position.y + ohh) { hit = true; } } } //CHECK FROM OTHER PERSPECTIVE var p = [ [o2.transform.position.x - ohw, o2.transform.position.y - ohh], [o2.transform.position.x + ohw, o2.transform.position.y - ohh], [o2.transform.position.x - ohw, o2.transform.position.y + ohh], [o2.transform.position.x + ohw, o2.transform.position.y + ohh] ]; for(var pi = 0; pi < p.length; pi++) { if(p[pi][0] > this.transform.position.x - hw && p[pi][0] < this.transform.position.x + hw) { if(p[pi][1] > this.gameObject.transform.position.y - hh && p[pi][1] < this.gameObject.transform.position.y + hh) { hit = true; } } } if(hit) if (!this.gameObject.destroyed && typeof this.gameObject.onCollision == 'function') { this.gameObject.onCollision(o2); } } } } } }