summaryrefslogtreecommitdiff
path: root/src/engine/collision.js
blob: 78e9a1a25c52be0f91717fbd5a557ee3ca59ae74 (plain)
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
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
        )
    }
}