diff options
author | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2024-12-24 17:58:20 +0100 |
---|---|---|
committer | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2024-12-24 17:58:20 +0100 |
commit | 25adfc618e77db9a5ee3b98ce0dab0be832efed0 (patch) | |
tree | 36ca56758ddd4674ab5a43df4b76bbfcc5a47af1 /src/engine/collision.js | |
parent | a840990bdcabf45fb0d377478ba0ab27222434ae (diff) |
hello kitty base done
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 + ) + } +} |