summaryrefslogtreecommitdiff
path: root/src/object/treeHidingPortal.c
blob: 99d0456544026f4ade0f3993c14e907ff06aa19f (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
/**
 * @file treeHidingPortal.c
 * @ingroup Objects
 *
 * @brief Tree Hiding Portal object
 */
#include "effects.h"
#include "entity.h"
#include "flags.h"
#include "functions.h"
#include "object.h"
#include "player.h"
#include "room.h"
#include "sound.h"
#include "tiles.h"

typedef struct {
    /*0x00*/ Entity base;
    /*0x68*/ u8 unused1[30];
    /*0x86*/ u16 flag;
} TreeHidingPortalEntity;

extern const s16 gUnk_080B4468[];

void TreeHidingPortal_Init(TreeHidingPortalEntity* this);
void TreeHidingPortal_Action1(TreeHidingPortalEntity* this);
void TreeHidingPortal_Action2(TreeHidingPortalEntity* this);
void TreeHidingPortal_Action3(TreeHidingPortalEntity* this);

static void sub_0809E96C(TreeHidingPortalEntity* this);
static u32 sub_0809E9A0(void);
static void sub_0809E918(TreeHidingPortalEntity* this);

void TreeHidingPortal(TreeHidingPortalEntity* this) {
    static void (*const TreeHidingPortal_Actions[])(TreeHidingPortalEntity*) = {
        TreeHidingPortal_Init,
        TreeHidingPortal_Action1,
        TreeHidingPortal_Action2,
        TreeHidingPortal_Action3,
    };
    TreeHidingPortal_Actions[super->action](this);
}

void TreeHidingPortal_Init(TreeHidingPortalEntity* this) {
    if (CheckFlags(this->flag)) {
        sub_0809E96C(this);
        DeleteThisEntity();
    }
    super->action = 1;
    super->collisionLayer = 2;
    UpdateSpriteForCollisionLayer(super);
}

void TreeHidingPortal_Action1(TreeHidingPortalEntity* this) {

    if (sub_0800419C(super, &gPlayerEntity.base, 0x30, 0x30)) {
        if (CheckGlobalFlag(EZERO_1ST)) {
            if (((gRoomTransition.frameCount & 3) == 0)) {
                CreateSparkle(super);
            }
        }
    }
    if (sub_0809E9A0() == ACT_TILE_84) {
        super->action = 2;
        super->timer = 15;
        SetPlayerControl(1);
    }
}

void TreeHidingPortal_Action2(TreeHidingPortalEntity* this) {
    if (--super->timer == 0) {
        super->action = 3;
        super->timer = 60;
        super->spriteSettings.draw = 0;
        sub_0809E96C(this);
        sub_0809E918(this);
    }
}

void TreeHidingPortal_Action3(TreeHidingPortalEntity* this) {
    if (--super->timer == 0) {
        SetFlag(this->flag);
        SetPlayerControl(0);
        SoundReq(SFX_SECRET_BIG);
        DeleteThisEntity();
    }
}

static void sub_0809E918(TreeHidingPortalEntity* this) {
    static const s16 gUnk_08124364[] = {
        0, -4, 8,  -4,  16, -4, 22, -4,  -8, -4,  -16, -4,  -22, -4,  0,  -12, 0,   4,   8,     -12,
        8, 4,  -8, -12, -8, 4,  8,  -16, -8, -16, 12,  -16, -12, -16, 16, -14, -16, -14, -1000, 0,
    };
    Entity* fx;
    const s16* i = gUnk_08124364;
    while (*i != -1000) {
        fx = CreateFx(super, FX_BUSH, 0);
        if (fx != NULL) {
            fx->x.HALF.HI += i[0];
            fx->y.HALF.HI += i[1];
            fx->collisionLayer = 2;
            UpdateSpriteForCollisionLayer(fx);
        }
        i += sizeof(s16);
    }
}

static void sub_0809E96C(TreeHidingPortalEntity* this) {
    CreateMinishEntrance(COORD_TO_TILE_OFFSET(super, 0x20, 0x8));
}

static u32 sub_0809E9A0(void) {
    u32 actTile;
    const s16* ptr;

    if (gPlayerEntity.base.action != PLAYER_BOUNCE) {
        actTile = ACT_TILE_0;
    } else {
        ptr = &gUnk_080B4468[gPlayerEntity.base.animationState & 6];
        actTile = GetActTileAtTilePos(COORD_TO_TILE_OFFSET(&gPlayerEntity.base, -ptr[0], -ptr[1]), 1);
    }

    return actTile;
}