summaryrefslogtreecommitdiff
path: root/src/enemy/sensorBladeTrap.c
blob: 627a75bbe1569b3f1f44fc8139c34e6e862a7456 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
 * @file sensorBladeTrap.c
 * @ingroup Enemies
 *
 * @brief Sensor Blade Trap enemy
 */
#include "collision.h"
#include "enemy.h"
#include "map.h"
#include "physics.h"

extern u32 sub_0804A024(Entity*, u32, u32);

void sub_0802BB10(Entity*);
bool32 sub_0802BB2C(Entity*, u32);

extern void (*const gUnk_080CD3C4[])(Entity*);
extern const u16 gUnk_080CD3D4[];
extern const s8 gUnk_080CD3DC[];

void SensorBladeTrap(Entity* this) {
    gUnk_080CD3C4[this->action](this);
}

void sub_0802B9EC(Entity* this) {
    this->action = 1;
    this->field_0x78.HWORD = gUnk_080CD3D4[this->type];
    this->field_0x7c.HALF.LO = this->cutsceneBeh.HWORD;
    this->field_0x7c.HALF.HI = this->field_0x86.HWORD;
}

void sub_0802BA18(Entity* this) {
    u32 direction = sub_0804A024(this, 1, 0xe);
    if (direction == 0xff)
        return;

    if (sub_0802BB2C(this, direction))
        return;

    this->action = 2;
    this->speed = this->field_0x78.HWORD;
    this->direction = direction;

    switch (direction >> 3) {
        case 0:
            this->field_0x7a.HWORD = this->y.HALF.HI - this->field_0x7c.HALF.HI;
            break;
        case 1:
            this->field_0x7a.HWORD = this->x.HALF.HI + this->field_0x7c.HALF.LO;
            break;
        case 2:
            this->field_0x7a.HWORD = this->y.HALF.HI + this->field_0x7c.HALF.HI;
            break;
        case 3:
            this->field_0x7a.HWORD = this->x.HALF.HI - this->field_0x7c.HALF.LO;
            break;
    }
}

void sub_0802BA8C(Entity* this) {
    if (!ProcessMovement12(this)) {
        sub_0802BB10(this);
    } else {
        switch (this->direction >> 3) {
            case 0:
                if (this->field_0x7a.HWORD >= this->y.HALF.HI)
                    sub_0802BB10(this);
                break;
            case 1:
                if (this->field_0x7a.HWORD <= this->x.HALF.HI)
                    sub_0802BB10(this);
                break;
            case 2:
                if (this->field_0x7a.HWORD <= this->y.HALF.HI)
                    sub_0802BB10(this);
                break;
            case 3:
                if (this->field_0x7a.HWORD >= this->x.HALF.HI)
                    sub_0802BB10(this);
                break;
        }
    }
}

void sub_0802BAFC(Entity* this) {
    if (!ProcessMovement12(this)) {
        this->action = 1;
    }
}

void sub_0802BB10(Entity* this) {
    this->action = 3;
    this->speed = 0xc0;
    this->direction = this->direction ^ 0x10;
    EnqueueSFX(SFX_METAL_CLINK);
}

bool32 sub_0802BB2C(Entity* this, u32 param_2) {
    u8* layer = this->collisionLayer == 2 ? gMapTop.collisionData : gMapBottom.collisionData;
    const s8* ptr = &gUnk_080CD3DC[param_2 >> 2];
    return IsTileCollision(layer, this->x.HALF.HI + ptr[0], this->y.HALF.HI + ptr[1], 0);
}

// clang-format off
void (*const gUnk_080CD3C4[])(Entity*) = {
    sub_0802B9EC,
    sub_0802BA18,
    sub_0802BA8C,
    sub_0802BAFC,
};

const u16 gUnk_080CD3D4[] = {
    0x100, 0x180,
    0x200, 0x280,
};

const s8 gUnk_080CD3DC[] = {
      0, -12,
     12,   0,
      0,  12,
    -12,   0,
};
// clang-format on