summaryrefslogtreecommitdiff
path: root/src/special_field_anim.c
blob: 4193f0b5a127bfc69c11cfbb3484a7b51da33748 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "global.h"
#include "event_data.h"
#include "task.h"
#include "field_player_avatar.h"
#include "fieldmap.h"
#include "field_camera.h"
#include "constants/metatile_labels.h"

static EWRAM_DATA u8 sEscalatorTaskId = 0;

static void SetEscalatorMetatile(u8 taskId, const s16 *metatileIds, u16 metatileMasks);
static void Task_DrawEscalator(u8 taskId);
static void Task_DrawTeleporterHousing(u8 taskId);
static void Task_DrawTeleporterCable(u8 taskId);

#define ESCALATOR_STAGES     3
#define LAST_ESCALATOR_STAGE (ESCALATOR_STAGES - 1)

static const u16 sEscalatorMetatiles_BottomNextRail[ESCALATOR_STAGES] = 
{
    METATILE_PokemonCenter_Escalator_BottomNextRail_Transition2, 
    METATILE_PokemonCenter_Escalator_BottomNextRail_Transition1, 
    METATILE_PokemonCenter_Escalator_BottomNextRail_Normal
};

static const u16 sEscalatorMetatiles_BottomRail[ESCALATOR_STAGES] = 
{
    METATILE_PokemonCenter_Escalator_BottomRail_Transition2, 
    METATILE_PokemonCenter_Escalator_BottomRail_Transition1, 
    METATILE_PokemonCenter_Escalator_BottomRail_Normal
};

static const u16 sEscalatorMetatiles_BottomNext[ESCALATOR_STAGES] = 
{
    METATILE_PokemonCenter_Escalator_BottomNext_Transition2, 
    METATILE_PokemonCenter_Escalator_BottomNext_Transition1, 
    METATILE_PokemonCenter_Escalator_BottomNext_Normal
};

static const u16 sEscalatorMetatiles_Bottom[ESCALATOR_STAGES] = 
{
    METATILE_PokemonCenter_Escalator_Bottom_Transition2, 
    METATILE_PokemonCenter_Escalator_Bottom_Transition1, 
    METATILE_PokemonCenter_Escalator_Bottom_Normal
};

static const u16 sEscalatorMetatiles_TopNext[ESCALATOR_STAGES] = 
{
    METATILE_PokemonCenter_Escalator_TopNext_Normal, 
    METATILE_PokemonCenter_Escalator_TopNext_Transition1, 
    METATILE_PokemonCenter_Escalator_TopNext_Transition2
};

static const u16 sEscalatorMetatiles_Top[ESCALATOR_STAGES] = 
{
    METATILE_PokemonCenter_Escalator_Top_Normal, 
    METATILE_PokemonCenter_Escalator_Top_Transition1, 
    METATILE_PokemonCenter_Escalator_Top_Transition2
};

static const u16 sEscalatorMetatiles_TopNextRail[ESCALATOR_STAGES] = 
{
    METATILE_PokemonCenter_Escalator_TopNextRail_Normal, 
    METATILE_PokemonCenter_Escalator_TopNextRail_Transition1, 
    METATILE_PokemonCenter_Escalator_TopNextRail_Transition2
};

#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, y, transitionStage;
    s16 i, j;
    
    x = gTasks[taskId].tPlayerX - 1;
    y = gTasks[taskId].tPlayerY - 1;
    transitionStage = gTasks[taskId].tTransitionStage;
    
    // 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++)
            {
                s32 id = MapGridGetMetatileIdAt(x + j, y + i);
                
                if (metatileIds[transitionStage] == (s16)id)
                {
                    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++)
            {
                s32 id = MapGridGetMetatileIdAt(x + j, y + i);

                if (metatileIds[LAST_ESCALATOR_STAGE - transitionStage] == (s16)id)
                {
                    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;
    u16 state;
    
    tDrawingEscalator = TRUE;

    // Set tile for each section of the escalator in sequence for current transition stage
    switch (tState)
    {
    case 0:
        SetEscalatorMetatile(taskId, sEscalatorMetatiles_BottomNextRail, 0);
        break;
    case 1:
        SetEscalatorMetatile(taskId, sEscalatorMetatiles_BottomRail, 0);
        break;
    case 2:
        SetEscalatorMetatile(taskId, sEscalatorMetatiles_BottomNext, METATILE_COLLISION_MASK);
        break;
    case 3:
        SetEscalatorMetatile(taskId, sEscalatorMetatiles_Bottom, 0);
        break;
    case 4:
        SetEscalatorMetatile(taskId, sEscalatorMetatiles_TopNext, METATILE_COLLISION_MASK);
        break;
    case 5:
        SetEscalatorMetatile(taskId, sEscalatorMetatiles_Top, 0);
        break;
    case 6:
        SetEscalatorMetatile(taskId, sEscalatorMetatiles_TopNextRail, 0);
    default:
        break;
    }
    
    tState = (tState + 1) & 7;
    state = tState & 7;

    // If all sections of the escalator have been set, draw map and progress to next stage
    if (state == 0)
    {
        DrawWholeMapView();
        tTransitionStage = (tTransitionStage + 1) % ESCALATOR_STAGES;
        tDrawingEscalator = FALSE;
    }
}

static u8 CreateEscalatorTask(bool16 goingUp)
{
    u8 taskId;
    s16 *data;

    taskId = CreateTask(Task_DrawEscalator, 0);
    data = gTasks[taskId].data;
    PlayerGetDestCoords(&tPlayerX, &tPlayerY);
    tState = 0;
    tTransitionStage = 0;
    tGoingUp = goingUp;
    Task_DrawEscalator(taskId);
    return taskId;
}

void StartEscalator(bool8 goingUp)
{
    u8 taskId;
    
    taskId = CreateEscalatorTask(goingUp);
    sEscalatorTaskId = taskId;
}

void StopEscalator(void)
{
    DestroyTask(sEscalatorTaskId);
}

bool8 IsEscalatorMoving(void)
{    
    if (gTasks[sEscalatorTaskId].tDrawingEscalator == FALSE)
    {
        if (gTasks[sEscalatorTaskId].tTransitionStage != LAST_ESCALATOR_STAGE)
            return TRUE;
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

#undef tState
#undef tTransitionStage
#undef tGoingUp
#undef tDrawingEscalator
#undef tPlayerX
#undef tPlayerY

#define tTimer data[0]
#define tState data[1]
#define tX     data[2]
#define tY     data[3]

void AnimateTeleporterHousing(void)
{
    u8 taskId;
    s16 *data;
    
    taskId = CreateTask(Task_DrawTeleporterHousing, 0);
    gTasks[taskId].tTimer = 0;
    gTasks[taskId].tState = 0;
    data = gTasks[taskId].data;
    PlayerGetDestCoords(&tX, &tY);

    // Set the coords of whichever teleporter is being animated
    // 0 for the right teleporter, 1 for the left teleporter
    if (gSpecialVar_0x8004 == 0)
    {
        gTasks[taskId].tX += 6;
        gTasks[taskId].tY -= 5;
    }
    else
    {
        gTasks[taskId].tX -= 1;
        gTasks[taskId].tY -= 5;
    }    
}

static void Task_DrawTeleporterHousing(u8 taskId)
{
    s16 *data = gTasks[taskId].data;
    
    if (tTimer == 0)
    {
        // Alternate the teleporter light / brightness of the teleporter door
        if ((tState & 1) == 0)
        {
            MapGridSetMetatileIdAt(tX, tY, METATILE_SeaCottage_Teleporter_Light_Yellow | METATILE_COLLISION_MASK);
            MapGridSetMetatileIdAt(tX, tY + 2, METATILE_SeaCottage_Teleporter_Door_HalfGlowing | METATILE_COLLISION_MASK);
        }
        else
        {
            MapGridSetMetatileIdAt(tX, tY, METATILE_SeaCottage_Teleporter_Light_Red | METATILE_COLLISION_MASK);
            MapGridSetMetatileIdAt(tX, tY + 2, METATILE_SeaCottage_Teleporter_Door_FullGlowing | METATILE_COLLISION_MASK);
        }
        CurrentMapDrawMetatileAt(tX, tY);
        CurrentMapDrawMetatileAt(tX, tY + 2);
    }
    
    tTimer++;
    if (tTimer != 16)
        return;
    
    tTimer = 0;
    tState++;
    if (tState != 13)
        return;
    
    MapGridSetMetatileIdAt(tX, tY, METATILE_SeaCottage_Teleporter_Light_Green | METATILE_COLLISION_MASK);
    MapGridSetMetatileIdAt(tX, tY + 2, METATILE_SeaCottage_Teleporter_Door | METATILE_COLLISION_MASK);
    CurrentMapDrawMetatileAt(tX, tY);
    CurrentMapDrawMetatileAt(tX, tY + 2);
    DestroyTask(taskId);
}

void AnimateTeleporterCable(void)
{
    u8 taskId;
    s16 *data;
    
    taskId = CreateTask(Task_DrawTeleporterCable, 0);
    gTasks[taskId].tTimer = 0;
    gTasks[taskId].tState = 0;
    data = gTasks[taskId].data;
    PlayerGetDestCoords(&tX, &tY);
    gTasks[taskId].tX += 4;
    gTasks[taskId].tY -= 5;
}

static void Task_DrawTeleporterCable(u8 taskId)
{
    s16 *data = gTasks[taskId].data;
    
    if (tTimer == 0)
    {
        if (tState != 0)
        {
            // Set default cable tiles to clear the ball
            MapGridSetMetatileIdAt(tX, tY, METATILE_SeaCottage_Teleporter_Cable_Top | METATILE_COLLISION_MASK);
            MapGridSetMetatileIdAt(tX, tY + 1, METATILE_SeaCottage_Teleporter_Cable_Bottom | METATILE_COLLISION_MASK);
            CurrentMapDrawMetatileAt(tX, tY);
            CurrentMapDrawMetatileAt(tX, tY + 1);

            // End after drawing 4 times (length of the cable)
            if (tState == 4)
            {
                DestroyTask(taskId);
                return;
            }
            
            tX--;
        }

        // Draw the cable ball
        MapGridSetMetatileIdAt(tX, tY, METATILE_SeaCottage_Teleporter_CableBall_Top | METATILE_COLLISION_MASK);
        MapGridSetMetatileIdAt(tX, tY + 1, METATILE_SeaCottage_Teleporter_CableBall_Bottom | METATILE_COLLISION_MASK);
        CurrentMapDrawMetatileAt(tX, tY);
        CurrentMapDrawMetatileAt(tX, tY + 1);
    }
    
    tTimer++;
    if (tTimer == 4)
    {
        tTimer = 0;
        tState++;
    }
}

#undef tTimer
#undef tState
#undef tX
#undef tY