summaryrefslogtreecommitdiff
path: root/src/object/hittableLever.c
blob: 2fc017d2e549d8f4f8a610ff0d5a2129456c4761 (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
/**
 * @file hittableLever.c
 * @ingroup Objects
 *
 * @brief Hittable Lever object
 */
#include "object.h"
#include "asm.h"
#include "sound.h"
#include "flags.h"
#include "room.h"
#include "player.h"

typedef struct {
    /*0x00*/ Entity base;
    /*0x68*/ u8 unk_68[8];
    /*0x70*/ u16 wasHit; /**< Has this lever been hit before. */
    /*0x72*/ u8 unk_72[0x14];
    /*0x86*/ u16 hitFlag;
} HittableLeverEntity;

extern void (*const HittableLever_Actions[])(HittableLeverEntity*);
extern const Hitbox HittableLever_Hitbox;

void HittableLever_UpdateTile(HittableLeverEntity*);

void HittableLever(HittableLeverEntity* this) {
    if (super->iframes == 0) {
        this->wasHit = 0;
    }
    HittableLever_Actions[super->action](this);
}

void HittableLever_Init(HittableLeverEntity* this) {
    super->action = 1;
    super->flags |= ENT_COLLIDE;
    super->collisionFlags = 7;
    super->hitType = 0x8f;
    super->collisionMask = 0xa;
    super->hitbox = (Hitbox*)&HittableLever_Hitbox;
    if (super->type == 0) {
        if (CheckFlags(this->hitFlag)) {
            super->type = 1;
        } else {
            super->type = 0;
        }
    } else {
        SetFlag(this->hitFlag);
    }
    HittableLever_UpdateTile(this);
}

void HittableLever_Idle(HittableLeverEntity* this) {
    if (((super->contactFlags & CONTACT_NOW) != 0) && (this->wasHit == 0)) {
        this->wasHit = 1;
        super->type ^= 1;
        super->iframes = -0x18;
        HittableLever_UpdateTile(this);
        if (CheckFlags(this->hitFlag)) {
            ClearFlag(this->hitFlag);
        } else {
            SetFlag(this->hitFlag);
        }
        SoundReqClipped(super, SFX_117);
    }
}

void HittableLever_UpdateTile(HittableLeverEntity* this) {
    if (super->type != 0) {
        sub_0807B7D8(0x378, COORD_TO_TILE(super), super->collisionLayer);
    } else {
        sub_0807B7D8(0x377, COORD_TO_TILE(super), super->collisionLayer);
    }
}

void (*const HittableLever_Actions[])(HittableLeverEntity*) = {
    HittableLever_Init,
    HittableLever_Idle,
};
const Hitbox HittableLever_Hitbox = { 0, 1, { 0, 0, 0, 0 }, 4, 3 };