forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfldeff_escalator.c
193 lines (167 loc) · 5.85 KB
/
fldeff_escalator.c
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
#include "global.h"
#include "field_camera.h"
#include "field_player_avatar.h"
#include "fieldmap.h"
#include "fldeff.h"
#include "task.h"
#include "constants/metatile_labels.h"
static EWRAM_DATA u8 sEscalatorAnim_TaskId = 0;
static void SetEscalatorMetatile(u8 taskId, const s16 *metatileIds, u16 metatileMasks);
static void Task_DrawEscalator(u8 taskId);
#define ESCALATOR_STAGES 3
#define LAST_ESCALATOR_STAGE (ESCALATOR_STAGES - 1)
static const s16 sEscalatorMetatiles_1F_0[ESCALATOR_STAGES] = {
METATILE_PokemonCenter_Escalator1F_Tile0_Frame2,
METATILE_PokemonCenter_Escalator1F_Tile0_Frame1,
METATILE_PokemonCenter_Escalator1F_Tile0_Frame0
};
static const s16 sEscalatorMetatiles_1F_1[ESCALATOR_STAGES] = {
METATILE_PokemonCenter_Escalator1F_Tile1_Frame2,
METATILE_PokemonCenter_Escalator1F_Tile1_Frame1,
METATILE_PokemonCenter_Escalator1F_Tile1_Frame0
};
static const s16 sEscalatorMetatiles_1F_2[ESCALATOR_STAGES] = {
METATILE_PokemonCenter_Escalator1F_Tile2_Frame2,
METATILE_PokemonCenter_Escalator1F_Tile2_Frame1,
METATILE_PokemonCenter_Escalator1F_Tile2_Frame0
};
static const s16 sEscalatorMetatiles_1F_3[ESCALATOR_STAGES] = {
METATILE_PokemonCenter_Escalator1F_Tile3_Frame2,
METATILE_PokemonCenter_Escalator1F_Tile3_Frame1,
METATILE_PokemonCenter_Escalator1F_Tile3_Frame0
};
static const s16 sEscalatorMetatiles_2F_0[ESCALATOR_STAGES] = {
METATILE_PokemonCenter_Escalator2F_Tile0_Frame0,
METATILE_PokemonCenter_Escalator2F_Tile0_Frame1,
METATILE_PokemonCenter_Escalator2F_Tile0_Frame2
};
static const s16 sEscalatorMetatiles_2F_1[ESCALATOR_STAGES] = {
METATILE_PokemonCenter_Escalator2F_Tile1_Frame0,
METATILE_PokemonCenter_Escalator2F_Tile1_Frame1,
METATILE_PokemonCenter_Escalator2F_Tile1_Frame2
};
static const s16 sEscalatorMetatiles_2F_2[ESCALATOR_STAGES] = {
METATILE_PokemonCenter_Escalator2F_Tile2_Frame0,
METATILE_PokemonCenter_Escalator2F_Tile2_Frame1,
METATILE_PokemonCenter_Escalator2F_Tile2_Frame2
};
#define tState data[0]
#define tTransitionStage data[1]
#define tGoingUp data[2]
#define tDrawingEscalator data[3]
#define tPlayerX data[4]
#define tPlayerY data[5]
static void SetEscalatorMetatile(u8 taskId, const s16 *metatileIds, u16 metatileMasks)
{
s16 x = gTasks[taskId].tPlayerX - 1;
s16 y = gTasks[taskId].tPlayerY - 1;
s16 transitionStage = gTasks[taskId].tTransitionStage;
s16 i;
s16 j;
// Check all the escalator sections and only progress the selected one to the next stage
if (!gTasks[taskId].tGoingUp)
{
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
s16 metatileId = MapGridGetMetatileIdAt(x + j, y + i);
if (metatileIds[transitionStage] == metatileId)
{
if (transitionStage != LAST_ESCALATOR_STAGE)
MapGridSetMetatileIdAt(x + j, y + i, metatileMasks | metatileIds[transitionStage + 1]);
else
MapGridSetMetatileIdAt(x + j, y + i, metatileMasks | metatileIds[0]);
}
}
}
}
else
{
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
s16 metatileId = MapGridGetMetatileIdAt(x + j, y + i);
if (metatileIds[LAST_ESCALATOR_STAGE - transitionStage] == metatileId)
{
if (transitionStage != LAST_ESCALATOR_STAGE)
MapGridSetMetatileIdAt(x + j, y + i, metatileMasks | metatileIds[1 - transitionStage]);
else
MapGridSetMetatileIdAt(x + j, y + i, metatileMasks | metatileIds[LAST_ESCALATOR_STAGE]);
}
}
}
}
}
static void Task_DrawEscalator(u8 taskId)
{
s16 *data = gTasks[taskId].data;
tDrawingEscalator = TRUE;
// Set tile for each section of the escalator in sequence for current transition stage
switch (tState)
{
case 0:
SetEscalatorMetatile(taskId, sEscalatorMetatiles_1F_0, 0);
break;
case 1:
SetEscalatorMetatile(taskId, sEscalatorMetatiles_1F_1, 0);
break;
case 2:
SetEscalatorMetatile(taskId, sEscalatorMetatiles_1F_2, MAPGRID_COLLISION_MASK);
break;
case 3:
SetEscalatorMetatile(taskId, sEscalatorMetatiles_1F_3, 0);
break;
case 4:
SetEscalatorMetatile(taskId, sEscalatorMetatiles_2F_0, MAPGRID_COLLISION_MASK);
break;
case 5:
SetEscalatorMetatile(taskId, sEscalatorMetatiles_2F_1, 0);
break;
case 6:
SetEscalatorMetatile(taskId, sEscalatorMetatiles_2F_2, 0);
break;
}
tState = (tState + 1) & 7;
// If all metatiles of the escalator have been set, draw map and progress to next stage
if (tState == 0)
{
DrawWholeMapView();
tTransitionStage = (tTransitionStage + 1) % ESCALATOR_STAGES;
tDrawingEscalator = FALSE;
}
}
static u8 CreateEscalatorTask(bool16 goingUp)
{
u8 taskId = CreateTask(Task_DrawEscalator, 0);
s16 *data = gTasks[taskId].data;
PlayerGetDestCoords(&tPlayerX, &tPlayerY);
tState = 0;
tTransitionStage = 0;
tGoingUp = goingUp;
Task_DrawEscalator(taskId);
return taskId;
}
void StartEscalator(bool8 goingUp)
{
sEscalatorAnim_TaskId = CreateEscalatorTask(goingUp);
}
void StopEscalator(void)
{
DestroyTask(sEscalatorAnim_TaskId);
}
bool8 IsEscalatorMoving(void)
{
if (gTasks[sEscalatorAnim_TaskId].tDrawingEscalator == FALSE
&& gTasks[sEscalatorAnim_TaskId].tTransitionStage == LAST_ESCALATOR_STAGE)
return FALSE;
else
return TRUE;
}
#undef tState
#undef tTransitionStage
#undef tGoingUp
#undef tDrawingEscalator
#undef tPlayerX
#undef tPlayerY