summaryrefslogtreecommitdiff
path: root/src/enemy/flyingPot.c
blob: 0c99cc7aa0268e419ea9b02e6e0c83bf8abe08dd (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
 * @file flyingPot.c
 * @ingroup Enemies
 *
 * @brief Flying pot enemy
 */
#include "collision.h"
#include "enemy.h"
#include "physics.h"
#include "player.h"
#include "room.h"
#include "tiles.h"
#include "asm.h"

extern Hitbox gUnk_080FD34C;

typedef struct {
    /* 0x00 */ Entity base;
    /* 0x68 */ u8 filler[0xC];
    /* 0x74 */ u16 tileIndex;
} FlyingPotEntity;

enum FlyingPotActions {
    /* 0 */ FLYING_POT_ACTION_0,
    /* 1 */ FLYING_POT_ACTION_1,
    /* 2 */ FLYING_POT_ACTION_2,
    /* 3 */ FLYING_POT_ACTION_3,
    /* 4 */ FLYING_POT_ACTION_4,
    /* 5 */ FLYING_POT_ACTION_5,
    /* 6 */ FLYING_POT_ACTION_6,
};

enum FlyingPotSubActions {
    /* 0 */ FLYING_POT_SUBACTION_0,
    /* 1 */ FLYING_POT_SUBACTION_1,
    /* 2 */ FLYING_POT_SUBACTION_2,
    /* 3 */ FLYING_POT_SUBACTION_3,
    /* 4 */ FLYING_POT_SUBACTION_DO_NOTHING,
    /* 5 */ FLYING_POT_SUBACTION_5,
};

// Functions
void FlyingPot_OnTick(FlyingPotEntity*);
void FlyingPot_OnCollision(FlyingPotEntity*);
void FlyingPot_OnGrabbed(FlyingPotEntity*);

// Subactions
void FlyingPot_SubAction0(FlyingPotEntity*);
void FlyingPot_SubAction1(FlyingPotEntity*);
void FlyingPot_SubAction2(FlyingPotEntity*);
void FlyingPot_SubAction3(FlyingPotEntity*);
void FlyingPot_SubActionDoNothing(FlyingPotEntity*);
void FlyingPot_SubAction5(FlyingPotEntity*);

// Actions
void FlyingPot_Init(FlyingPotEntity*);
void FlyingPot_Action1(FlyingPotEntity*);
void FlyingPot_Action2(FlyingPotEntity*);
void FlyingPot_Action3(FlyingPotEntity*);
void FlyingPot_Action4(FlyingPotEntity*);
void FlyingPot_Action5(FlyingPotEntity*);
void FlyingPot_Action6(FlyingPotEntity*);

void sub_08037408(FlyingPotEntity*);
void sub_08037418(FlyingPotEntity*);

void (*const FlyingPot_Functions[])(Entity*) = {
    (EntityActionPtr)FlyingPot_OnTick,
    (EntityActionPtr)FlyingPot_OnCollision,
    GenericKnockback,
    GenericDeath,
    GenericConfused,
    (EntityActionPtr)FlyingPot_OnGrabbed,
};

void FlyingPot(Entity* thisx) {
    s32 index = sub_080012DC(thisx);

    if (index != 0) {
        gUnk_080012C8[index](thisx);
    } else {
        FlyingPot_Functions[GetNextFunction(thisx)](thisx);
    }
}

void FlyingPot_OnTick(FlyingPotEntity* this) {
    static void (*const FlyingPot_Actions[])(FlyingPotEntity*) = {
        FlyingPot_Init,    FlyingPot_Action1, FlyingPot_Action2, FlyingPot_Action3,
        FlyingPot_Action4, FlyingPot_Action5, FlyingPot_Action6,
    };

    FlyingPot_Actions[super->action](this);
}

void FlyingPot_OnCollision(FlyingPotEntity* this) {
    sub_08037418(this);

    if (super->contactFlags == (CONTACT_NOW | 0x1d)) {
        super->action = FLYING_POT_ACTION_6;
        COLLISION_OFF(super);
        super->zVelocity = Q_16_16(2.625);
        super->spritePriority.b1 = 1;

        SetTile(this->tileIndex, TILE(super->x.HALF.HI, super->y.HALF.HI), super->collisionLayer);
    } else if (super->z.HALF.HI != 0) {
        sub_08037408(this);
    }

    EnemyFunctionHandlerAfterCollision(super, FlyingPot_Functions);
}

void FlyingPot_OnGrabbed(FlyingPotEntity* this) {
    static void (*const FlyingPot_SubActions[])(FlyingPotEntity*) = {
        FlyingPot_SubAction0, FlyingPot_SubAction1,         FlyingPot_SubAction2,
        FlyingPot_SubAction3, FlyingPot_SubActionDoNothing, FlyingPot_SubAction5,
    };

    FlyingPot_SubActions[super->subAction](this);
}

void FlyingPot_SubAction0(FlyingPotEntity* this) {
    sub_08037418(this);

    super->subAction = FLYING_POT_SUBACTION_1;
    super->timer = 0;
    super->gustJarTolerance = 0x30;
}

void FlyingPot_SubAction1(FlyingPotEntity* this) {
    sub_08037418(this);

    if (sub_0806F520(super)) {
        sub_0806F4E8(super);
    } else {
        super->spriteOffsetX = 0;

        if (super->z.HALF.HI != 0) {
            sub_08037408(this);
        }
    }
}

void FlyingPot_SubAction2(FlyingPotEntity* this) {
    if (super->timer == 0) {
        sub_08037418(this);
        super->timer = 1;
        COLLISION_OFF(super);
        super->spriteOffsetX = 0;

        SetTile(this->tileIndex, TILE(super->x.HALF.HI, super->y.HALF.HI), super->collisionLayer);
    }

    if (sub_0806F520(super)) {
        sub_0806F3E4(super);
    } else {
        sub_08037408(this);
    }
}

void FlyingPot_SubAction3(FlyingPotEntity* this) {
    if (!(gPlayerState.gustJarState & 0xF)) {
        sub_08037408(this);
    }
}

void FlyingPot_SubActionDoNothing(FlyingPotEntity* this) {
}

void FlyingPot_SubAction5(FlyingPotEntity* this) {
    sub_08037408(this);
}

void FlyingPot_Init(FlyingPotEntity* this) {
    u32 tilePos;

    super->action = FLYING_POT_ACTION_1;
    super->gustJarFlags = 2;
    super->y.HALF.HI += 3;

    tilePos = TILE(super->x.HALF.HI, super->y.HALF.HI);
    this->tileIndex = GetTileIndex(tilePos, super->collisionLayer);
    SetTile(SPECIAL_TILE_0, tilePos, super->collisionLayer);
    InitializeAnimation(super, 5);
}

void FlyingPot_Action1(FlyingPotEntity* this) {
    sub_08037418(this);

    if (GetTileTypeAtEntity(super) != SPECIAL_TILE_0) {
        SetTile(this->tileIndex, TILE(super->x.HALF.HI, super->y.HALF.HI), super->collisionLayer);
        sub_08037408(this);
    }

    if (PlayerInRange(super, 1, 0x40)) {
        super->action = FLYING_POT_ACTION_2;
        super->timer = 30;
    }
}

void FlyingPot_Action2(FlyingPotEntity* this) {
    static const u8 offsets[] = { -1, 1, 1, -1 };

    sub_08037418(this);

    super->spriteOffsetX += offsets[super->timer & 3];

    if (--super->timer == 0) {
        super->action = FLYING_POT_ACTION_3;
        super->spritePriority.b1 = 1;
        super->spriteOffsetX = 0;
        super->hitType = 0xA0;
        super->collisionMask = 0xF;
        super->hitbox = &gUnk_080FD34C;

        SetTile(this->tileIndex, TILE(super->x.HALF.HI, super->y.HALF.HI), super->collisionLayer);
    }
}

void FlyingPot_Action3(FlyingPotEntity* this) {
    super->z.WORD -= Q_16_16(1.0);

    if (super->z.HALF.HI <= -6) {
        super->action = FLYING_POT_ACTION_4;
        super->timer = 10;
        super->direction = GetFacingDirection(super, &gPlayerEntity.base);
    }
}

void FlyingPot_Action4(FlyingPotEntity* this) {
    if (--super->timer == 0) {
        super->action = FLYING_POT_ACTION_5;
    }
}

void FlyingPot_Action5(FlyingPotEntity* this) {
    ProcessMovement2(super);

    if (super->collisions != COL_NONE) {
        sub_08037408(this);
    }
}

void FlyingPot_Action6(FlyingPotEntity* this) {
    if (super->zVelocity < 0) {
        super->spriteSettings.flipY = 1;
    }

    if (!GravityUpdate(super, Q_8_8(32.0))) {
        sub_08037408(this);
    }
}

void sub_08037408(FlyingPotEntity* this) {
    CreateFx(super, FX_POT_SHATTER, 0);
    DeleteThisEntity();
}

void sub_08037418(FlyingPotEntity* this) {
    u32 tilePos = COORD_TO_TILE(super);

    if (GetTileIndex(tilePos, super->collisionLayer) == SPECIAL_TILE_103) {
        SetTile(this->tileIndex, tilePos, super->collisionLayer);
        DeleteThisEntity();
    }
}