summaryrefslogtreecommitdiff
path: root/src/enemy/vaatiProjectile.c
blob: 71c84c4463f8bc8dfa9403c1ebc4849baec419c8 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
 * @file vaatiProjectile.c
 * @ingroup Enemies
 *
 * @brief Vaati Projectile enemy
 */
#include "enemy.h"
#include "functions.h"
#include "screenTransitions.h"
#include "tiles.h"

typedef struct {
    /*0x00*/ Entity base;
    /*0x68*/ u8 unused1[16];
    /*0x78*/ u16 unk_78;
} VaatiProjectileEntity;

extern Entity* gUnk_020000B0;

bool32 sub_0803E4A0(VaatiProjectileEntity*);
void VaatiProjectile_OnTick(VaatiProjectileEntity*);
void VaatiProjectile_OnCollision(VaatiProjectileEntity*);
void VaatiProjectile_OnDeath(VaatiProjectileEntity*);
void VaatiProjectile_OnGrabbed(VaatiProjectileEntity*);
void VaatiProjectileFunction0Action0(VaatiProjectileEntity*);
void VaatiProjectileFunction0Action1(VaatiProjectileEntity*);
void VaatiProjectileFunction0Action2(VaatiProjectileEntity*);
void VaatiProjectileFunction0Action3(VaatiProjectileEntity*);
void VaatiProjectileFunction0Action4(VaatiProjectileEntity*);
void VaatiProjectileFunction0Action5(VaatiProjectileEntity*);
void VaatiProjectileFunction0Action6(VaatiProjectileEntity*);
void VaatiProjectileFunction0Action7(VaatiProjectileEntity*);
void VaatiProjectileFunction0Action8(VaatiProjectileEntity*);
void VaatiProjectileFunction0Action9(VaatiProjectileEntity*);
void sub_0803E444(VaatiProjectileEntity*);
void sub_0803E480(VaatiProjectileEntity*);
void sub_0803E4D8(VaatiProjectileEntity*);

void (*const VaatiProjectile_Functions[])(VaatiProjectileEntity*) = {
    VaatiProjectile_OnTick,
    VaatiProjectile_OnCollision,
    (void (*)(VaatiProjectileEntity*))GenericKnockback2,
    VaatiProjectile_OnDeath,
    (void (*)(VaatiProjectileEntity*))GenericConfused,
    VaatiProjectile_OnGrabbed,
};
void (*const vaatiProjectileFunction0Actions[])(VaatiProjectileEntity*) = {
    VaatiProjectileFunction0Action0, VaatiProjectileFunction0Action1, VaatiProjectileFunction0Action2,
    VaatiProjectileFunction0Action3, VaatiProjectileFunction0Action4, VaatiProjectileFunction0Action5,
    VaatiProjectileFunction0Action6, VaatiProjectileFunction0Action7, VaatiProjectileFunction0Action8,
    VaatiProjectileFunction0Action9,
};

void VaatiProjectile(VaatiProjectileEntity* this) {
    if (sub_0803E4A0(this)) {
        COLLISION_OFF(super);
        super->health = 0;
        super->parent = NULL;
    }
    VaatiProjectile_Functions[GetNextFunction(super)](this);
}

void VaatiProjectile_OnTick(VaatiProjectileEntity* this) {
    vaatiProjectileFunction0Actions[super->action](this);
}

void VaatiProjectile_OnCollision(VaatiProjectileEntity* this) {
    Entity* entity;

    if (super->contactFlags == CONTACT_NOW) {
#ifndef EU
        if (super->health != 0) {
#endif
            super->action = 5;
            COLLISION_OFF(super);
            super->spritePriority.b1 = 0;
            gPlayerEntity.base.flags &= ~ENT_COLLIDE;
            gPlayerEntity.base.spriteOrientation.flipY = super->spriteOrientation.flipY;
            gPlayerEntity.base.spriteRendering.b3 = super->spriteRendering.b3;
            sub_0803E444(this);
#ifndef EU
            SetPlayerControl(2);
            entity = super->parent;
            if (entity != NULL) {
                entity->flags = entity->flags & ~ENT_COLLIDE;
            }
        } else {
            gPlayerState.flags &= ~PL_DISABLE_ITEMS;
            entity = &gPlayerEntity.base;
            entity->flags = gPlayerEntity.base.flags | ENT_COLLIDE;
        }
#endif
    }
    EnemyFunctionHandlerAfterCollision(super, (EntityActionArray)VaatiProjectile_Functions);
}

void VaatiProjectile_OnDeath(VaatiProjectileEntity* this) {
    if (super->parent != NULL) {
        super->parent->subtimer--;
        super->parent = NULL;
    }
    GenericDeath(super);
}

void VaatiProjectile_OnGrabbed(VaatiProjectileEntity* this) {
}

void VaatiProjectileFunction0Action0(VaatiProjectileEntity* this) {
    Entity* entity;

    if (super->type == 0) {
        entity = CreateEnemy(VAATI_PROJECTILE, 1);
        if (entity != NULL) {
            entity->parent = super;
            super->child = entity;
            if (super->type2 == 0) {
                super->action = 1;
                super->z.HALF.HI = -0x18;
            } else {
                super->action = 9;
                super->z.HALF.HI = -0x80;
                super->collisionMask = 1;
            }
            InitializeAnimation(super, 0);
        }
    } else {
        super->action = 8;
        COLLISION_OFF(super);
        super->spriteOffsetY = 1;
        super->spriteOrientation.flipY = super->parent->spriteOrientation.flipY;
        super->spriteRendering.b3 = super->parent->spriteRendering.b3;
        super->spritePriority.b1 = 0;
        PositionRelative(super->parent, super, 0, Q_16_16(-1.0));
        InitializeAnimation(super, 1);
    }
}

void VaatiProjectileFunction0Action1(VaatiProjectileEntity* this) {
    sub_0803E480(this);
    if (PlayerInRange(super, 0, 8) != 0) {
        super->action = 2;
        super->timer = 10;
        InitializeAnimation(super->child, 2);
    } else {
        if (gUnk_020000B0 != NULL) {
            sub_08004596(super, GetFacingDirection(super, gUnk_020000B0));
            LinearMoveUpdate(super);
        }
    }
    GetNextFrame(super);
}

void VaatiProjectileFunction0Action2(VaatiProjectileEntity* this) {
    if (super->timer != 0) {
        super->timer--;
    } else {
        if (++super->z.HALF.HI == 0) {
            super->action = 3;
        }
    }
    GetNextFrame(super);
}

void VaatiProjectileFunction0Action3(VaatiProjectileEntity* this) {
    if (super->child->frame & ANIM_DONE) {
        if (--super->z.HALF.HI <= -0x18) {
            super->action = 4;
            super->timer = (Random() & 0xf) + 15;
            InitializeAnimation(super->child, 1);
        }
        GetNextFrame(super);
    }
}

void VaatiProjectileFunction0Action4(VaatiProjectileEntity* this) {
    if (--super->timer == 0) {
        super->action = 1;
        super->direction = GetFacingDirection(super, &gPlayerEntity.base);
    }
    GetNextFrame(super);
}

void VaatiProjectileFunction0Action5(VaatiProjectileEntity* this) {
    sub_0803E444(this);
    if (super->timer != 0) {
        super->timer--;
    } else {
        if (-0x18 < --super->z.HALF.HI) {
            return;
        }
        super->action = 6;
        super->timer = 20;
    }
}

void VaatiProjectileFunction0Action6(VaatiProjectileEntity* this) {
    if (--super->timer == 0) {
        super->action = 7;
        super->direction = DirectionSouth;
        super->speed = 0x300;
    }
    sub_0803E444(this);
}

void VaatiProjectileFunction0Action7(VaatiProjectileEntity* this) {
    sub_0803E444(this);
    LinearMoveUpdate(super);
    sub_0803E4D8(this);
    if ((gRoomControls.origin_y + gRoomControls.height + -0x10) <= super->y.HALF.HI) {
        SetInitializationPriority();
        // TODO super screen transition is to mazaal. Is super also the projectile for Mazaals shrink ray?
        DoExitTransition(&gUnk_0813AB94);
    }
}

void VaatiProjectileFunction0Action8(VaatiProjectileEntity* this) {
    if (super->parent->next == NULL) {
        DeleteThisEntity();
    }
    PositionRelative(super->parent, super, 0, Q_16_16(-1.0));
    GetNextFrame(super);
}

void VaatiProjectileFunction0Action9(VaatiProjectileEntity* this) {
    super->x.HALF.HI = gPlayerEntity.base.x.HALF.HI;
    super->y.HALF.HI = gPlayerEntity.base.y.HALF.HI;
    if (super->z.HALF.HI < -8) {
        if (super->animIndex != 2) {
            super->timer = 0;
            InitializeAnimation(super->child, 2);
        }
        VaatiProjectileFunction0Action2(this);
    } else {
        super->z.HALF.HI += 8;
    }
}

void sub_0803E444(VaatiProjectileEntity* this) {
    ResetActiveItems();
    gPlayerState.mobility |= 0x80;
    gPlayerState.field_0xa |= 0x80;
    sub_0806FA90(super, super->contactedEntity, 0, -2);
    gPlayerEntity.base.spriteOffsetY += 0xe;
}

void sub_0803E480(VaatiProjectileEntity* this) {
    if (this->unk_78 >= 0x4b1) {
        super->speed = 0x180;
    } else {
        this->unk_78++;
    }
}

bool32 sub_0803E4A0(VaatiProjectileEntity* this) {
#ifdef EU
    bool32 ret;
    if (gRoomTransition.field_0x39 == 0) {
        return TRUE;
    } else {
        if (super->parent == NULL) {
            return FALSE;
        }
        ret = super->parent->next == NULL;
    }
    return ret;
#else
    bool32 ret;
    if (gRoomTransition.field_0x39 != 0) {
        if (super->parent == NULL) {
            return FALSE;
        }
        if (super->parent->health == 0) {
            return TRUE;
        } else {
            ret = super->parent->next == NULL;
        }
    } else {
        return TRUE;
    }
    return ret;
#endif
}

void sub_0803E4D8(VaatiProjectileEntity* this) {
    u32 tilePos = TILE(super->x.HALF.HI, super->y.HALF.HI + 8);
    if (GetCollisionDataAtTilePos(tilePos, gPlayerEntity.base.collisionLayer) != COLLISION_DATA_255) {
        SetTile(SPECIAL_TILE_116, tilePos, gPlayerEntity.base.collisionLayer);
    }
}