diff options
Diffstat (limited to 'src/engine/collision.js')
-rw-r--r-- | src/engine/collision.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/engine/collision.js b/src/engine/collision.js new file mode 100644 index 0000000..78e9a1a --- /dev/null +++ b/src/engine/collision.js @@ -0,0 +1,45 @@ +import { boshy, enemy } from "../main.js" +import { Vector } from "./vector.js" + +export class Collision { + collidingWithBoshy(position, hitbox = position) { + if (boshy == undefined) return false + return this.areColliding({ + position_a: position, + position_b: boshy.position, + hitbox_a: hitbox, + hitbox_b: boshy.hitbox, + }) + } + + collidingWithEnemy(position, hitbox = position) { + if (enemy == undefined) return false + return this.areColliding({ + position_a: position, + position_b: enemy.position, + hitbox_a: hitbox, + hitbox_b: enemy.hitbox, + }) + } + + areColliding({ + position_a, + position_b, + hitbox_a= position_a, + hitbox_b= position_b, + }) { + let hitbox_value = new Vector( + (Math.abs(hitbox_a.x) + Math.abs(hitbox_b.x)) / 2, + (Math.abs(hitbox_a.y) + Math.abs(hitbox_b.y)) / 2, + ) + let position_difference = new Vector( + Math.abs( Math.abs(position_a.x) - Math.abs(position_b.x)), + Math.abs( Math.abs(position_a.y) - Math.abs(position_b.y)), + ) + + return ( + position_difference.x <= hitbox_value.x && + position_difference.y <= hitbox_value.y + ) + } +} |