summaryrefslogtreecommitdiff
path: root/src/object/objectA2.c
blob: 39372890ca8480c30482690b93fb5e06b89dd1af (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
/**
 * @file objectA2.c
 * @ingroup Objects
 *
 * @brief Object A2 object
 */
#include "menu.h"
#include "object.h"
#include "asm.h"
#include "sound.h"
#include "effects.h"
#include "physics.h"
#include "color.h"
#ifndef EU
#include "save.h"
#endif

void ObjectA2_Init(Entity*);
void ObjectA2_Action1(Entity*);
void ObjectA2_Action2(Entity*);
void ObjectA2_Action3(Entity*);
void sub_0809F448(Entity*);

void (*const ObjectA2_Actions[])(Entity*) = { ObjectA2_Init, ObjectA2_Action1, ObjectA2_Action2, ObjectA2_Action3 };

const u8 gUnk_08124704[] = { 0, 1, 2, 4 };

typedef struct {
    u8 unk_0;
    u8 unk_1;
} PACKED struct_08124708;

const struct_08124708 gUnk_08124708[5] = { { 0, 0x2F }, { 1, 0x20 }, { 1, 0x16 }, { 2, 0x10 }, { 3, 0x01 } };

void ObjectA2(Entity* this) {
    ObjectA2_Actions[this->action](this);
    GetNextFrame(this);
}

#define fp(n) ((n) << 8)

void ObjectA2_Init(Entity* this) {
    InitializeAnimation(this, 0);
    if (Random() & 0x10) {
        this->spriteSettings.flipX = 1;
    }
    this->x.HALF.HI = Q_8_8(0.16);
    this->y.HALF.HI = Q_8_8(0.285);
    this->z.HALF.HI = Q_8_8(-0.315);
    this->spriteOrientation.flipY = 2;
    this->action = 1;
    ChangeObjPalette(this, gUnk_08124704[this->type]);
    SetEntityPriority(this, PRIO_HIGHEST);
}

void ObjectA2_Action1(Entity* this) {
    LinearMoveUpdate(this);
#ifndef EU
    if (gSaveHeader->language < 2) {
        if (BounceUpdate(this, Q_8_8(32.0)) < BOUNCE_AIRBORNE) {
            this->z.WORD = 0;
            this->action = 2;
            InitializeAnimation(this, 1);
            SoundReq(SFX_186);
        } else {
            this->timer++;
        }
    } else {
#endif
        switch (BounceUpdate(this, Q_8_8(32.0))) {
            default:
                this->timer++;
                break;
            case BOUNCE_DONE_ALL:
                this->action = 2;
                InitializeAnimation(this, 1);
                // fall through
            case BOUNCE_INIT_NEXT:
                this->timer = 0;
                sub_0809F448(this);
                SoundReq(SFX_186);
        }
#ifndef EU
    }
#endif
}

void ObjectA2_Action2(Entity* this) {
    if (this->frame & ANIM_DONE) {
        this->action = 3;
        InitializeAnimation(this, 2);
    }
}

void ObjectA2_Action3(Entity* this) {
    switch (this->frame) {
        case 9:
            this->frame = 0;
            CreateFx(this, FX_BIG_EXPLOSION2, 0);
            gMenu.field_0x0 = 1;
            break;
        case 0x80:
            gMenu.field_0x4 = 1;
            DeleteThisEntity();
    }
}

void sub_0809F448(Entity* this) {
    s32 tmp;
    int rand = Random();
    const struct_08124708* tmp2 = &gUnk_08124708[this->subtimer];
    this->subtimer++;
    tmp = Q_16_16(40.0) - this->x.WORD;
    switch (tmp2->unk_0) {
        case 0:
            tmp = rand % 0x180000;
            if (rand & 0x1000000) {
                tmp = -tmp;
            }
            break;
        case 1:
            tmp += -0x80000 + (rand & 0xFFFFF);
            break;
    }
    this->speed = (tmp > 0 ? tmp : -tmp) / (tmp2->unk_1 << 8);
    this->direction = CalculateDirectionFromOffsets(tmp, 0) >> 3;
}