Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rubberduckycooly
GitHub Repository: rubberduckycooly/Sonic-Mania-Decompilation
Path: blob/master/SonicMania/Objects/MMZ/FarPlane.c
338 views
1
// ---------------------------------------------------------------------
2
// RSDK Project: Sonic Mania
3
// Object Description: FarPlane Object
4
// Object Author: Christian Whitehead/Simon Thomley/Hunter Bridges
5
// Decompiled by: Rubberduckycooly & RMGRich
6
// ---------------------------------------------------------------------
7
8
#include "Game.h"
9
10
ObjectFarPlane *FarPlane;
11
12
void FarPlane_Update(void) {}
13
14
void FarPlane_LateUpdate(void)
15
{
16
RSDK_THIS(FarPlane);
17
18
if (self->active == ACTIVE_ALWAYS) {
19
FarPlane_SetupEntities();
20
}
21
else if (self->active == ACTIVE_BOUNDS) {
22
self->active = ACTIVE_NORMAL;
23
RSDK.GetTileLayer(FarPlane->layerID)->drawGroup[0] = 0;
24
25
FarPlane->originPos.x = self->origin.x;
26
FarPlane->originPos.y = self->origin.y;
27
28
FarPlane->position.x = self->position.x;
29
FarPlane->position.y = self->position.y;
30
31
RSDK.SetDrawGroupProperties(1, false, FarPlane_DrawHook_ApplyFarPlane);
32
RSDK.SetDrawGroupProperties(3, false, FarPlane_DrawHook_RemoveFarPlane);
33
34
FarPlane_SetEntityActivities(ACTIVE_NORMAL);
35
}
36
else if (!RSDK.CheckOnScreen(self, NULL)) {
37
self->active = ACTIVE_BOUNDS;
38
RSDK.GetTileLayer(FarPlane->layerID)->drawGroup[0] = DRAWGROUP_COUNT;
39
40
RSDK.SetDrawGroupProperties(1, false, StateMachine_None);
41
RSDK.SetDrawGroupProperties(3, false, StateMachine_None);
42
43
FarPlane_SetEntityActivities(ACTIVE_NEVER);
44
}
45
}
46
47
void FarPlane_StaticUpdate(void) {}
48
49
void FarPlane_Draw(void)
50
{
51
RSDK_THIS(FarPlane);
52
53
if (SceneInfo->currentDrawGroup) {
54
RSDK.SetActivePalette(0, 0, ScreenInfo->size.y);
55
}
56
else {
57
int32 x = (ScreenInfo->position.x + ScreenInfo->center.x) << 16;
58
int32 y = (ScreenInfo->position.y + ScreenInfo->center.y) << 16;
59
60
FarPlane->screenPos.x = (x + self->origin.x - self->position.x) & 0xFFFE0000;
61
FarPlane->screenPos.y = (y + self->origin.y - self->position.y) & 0xFFFE0000;
62
63
FarPlane->worldPos.x = self->position.x - ((self->position.x - x) >> 1) + 0x8000;
64
FarPlane->worldPos.y = self->position.y - ((self->position.y - y) >> 1) + 0x8000;
65
66
if (!SceneInfo->currentScreenID)
67
RSDK.AddDrawListRef(1, SceneInfo->entitySlot);
68
}
69
}
70
71
void FarPlane_Create(void *data)
72
{
73
RSDK_THIS(FarPlane);
74
75
if (!SceneInfo->inEditor) {
76
self->updateRange.x = self->size.x + (self->size.x >> 1);
77
self->updateRange.y = self->size.y + (self->size.y >> 1);
78
self->active = ACTIVE_ALWAYS;
79
self->visible = true;
80
self->drawGroup = 0;
81
}
82
}
83
84
void FarPlane_StageLoad(void)
85
{
86
FarPlane->layerID = RSDK.GetTileLayerID("Far Plane");
87
88
if (FarPlane->layerID != (uint16)-1) {
89
TileLayer *farPlane = RSDK.GetTileLayer(FarPlane->layerID);
90
farPlane->drawGroup[0] = DRAWGROUP_COUNT;
91
farPlane->scanlineCallback = FarPlane_Scanline_FarPlaneView;
92
93
RSDK.GetTileLayer(Zone->fgLayer[0])->drawGroup[0] = 2;
94
RSDK.GetTileLayer(Zone->fgLayer[1])->drawGroup[0] = 7;
95
96
RSDK.SetDrawGroupProperties(1, false, StateMachine_None);
97
RSDK.SetDrawGroupProperties(2, false, StateMachine_None);
98
99
RSDK.CopyPalette(0, 0, 3, 0, 128);
100
RSDK.CopyPalette(0, 144, 3, 144, 112);
101
RSDK.CopyPalette(3, 0, 4, 0, 255);
102
103
#if MANIA_USE_PLUS
104
if (SceneInfo->filter & FILTER_MANIA)
105
#endif
106
RSDK.SetLimitedFade(4, 0, 3, 96, 128, 143);
107
108
RSDK.CopyTileLayer(FarPlane->layerID, 0, 192, Zone->fgLayer[0], 0, 192, 1024, 208);
109
110
for (int32 s = 0; s < PLAYER_COUNT; ++s) {
111
Zone->cameraBoundsB[s] -= 2048;
112
Zone->deathBoundary[s] -= 2048 << 16;
113
}
114
115
++Zone->objectDrawGroup[0];
116
++Zone->playerDrawGroup[0];
117
++Zone->fgDrawGroup[1];
118
++Zone->objectDrawGroup[1];
119
++Zone->playerDrawGroup[1];
120
121
RSDK.ClearCameras();
122
123
EntityCamera *camera = RSDK_GET_ENTITY(SLOT_CAMERA1, Camera);
124
RSDK.AddCamera(&camera->center, ScreenInfo[camera->screenID].center.x << 16, ScreenInfo[camera->screenID].center.y << 16, 0);
125
}
126
}
127
128
void FarPlane_SetupEntities(void)
129
{
130
RSDK_THIS(FarPlane);
131
132
self->entityCount = 0;
133
for (int32 i = 0; i < SCENEENTITY_COUNT && self->entityCount < FARPLANE_ENTITY_COUNT; ++i) {
134
Entity *child = RSDK_GET_ENTITY_GEN(i);
135
if (abs(self->origin.x - child->position.x) < self->size.x && abs(self->origin.y - child->position.y) < self->size.y) {
136
self->entitySlots[self->entityCount++] = i;
137
child->active = ACTIVE_NEVER;
138
child->drawGroup = 1;
139
}
140
}
141
142
self->active = ACTIVE_BOUNDS;
143
}
144
145
void FarPlane_SetEntityActivities(uint8 active)
146
{
147
RSDK_THIS(FarPlane);
148
149
for (int32 i = 0; i < self->entityCount; ++i) {
150
RSDK_GET_ENTITY_GEN(self->entitySlots[i])->active = active;
151
}
152
}
153
154
void FarPlane_DrawHook_ApplyFarPlane(void)
155
{
156
int32 id = 0;
157
for (int32 i = 0; i < 0x200 && id < 0x200; ++i) {
158
Entity *entity = RSDK.GetDrawListRef(1, i);
159
if (!entity)
160
break;
161
162
FarPlane->positionList[id].x = entity->position.x;
163
FarPlane->positionList[id].y = entity->position.y;
164
165
entity->position.x = FarPlane->worldPos.x + ((entity->position.x - FarPlane->originPos.x) >> 1);
166
entity->position.y = FarPlane->worldPos.y + ((entity->position.y - FarPlane->originPos.y) >> 1);
167
entity->drawFX |= FX_SCALE;
168
entity->scale.x = 0x100;
169
entity->scale.y = 0x100;
170
id++;
171
}
172
173
for (int32 i = 0; i < 0x200 && id < 0x200; ++i) {
174
Entity *entity = RSDK.GetDrawListRef(2, i);
175
if (!entity)
176
break;
177
178
FarPlane->positionList[id].x = entity->position.x;
179
FarPlane->positionList[id].y = entity->position.y;
180
entity->position.x = FarPlane->worldPos.x + ((entity->position.x - FarPlane->originPos.x) >> 1);
181
entity->position.y = FarPlane->worldPos.y + ((entity->position.y - FarPlane->originPos.y) >> 1);
182
entity->drawFX |= FX_SCALE;
183
entity->scale.x = 0x100;
184
entity->scale.y = 0x100;
185
id++;
186
}
187
188
foreach_active(InvincibleStars, invincibleStars)
189
{
190
if (invincibleStars->drawGroup < 3 && id < 0x200) {
191
invincibleStars->starOffset = 10;
192
invincibleStars->drawFX = FX_SCALE;
193
invincibleStars->scale.x = 0x100;
194
invincibleStars->scale.y = 0x100;
195
196
for (int32 s = 0; s < 8; ++s) {
197
FarPlane->positionList[id].x = invincibleStars->starPos[s].x;
198
FarPlane->positionList[id].y = invincibleStars->starPos[s].y;
199
200
invincibleStars->starPos[s].x = FarPlane->worldPos.x + ((invincibleStars->starPos[s].x - FarPlane->originPos.x) >> 1);
201
invincibleStars->starPos[s].y = FarPlane->worldPos.y + ((invincibleStars->starPos[s].y - FarPlane->originPos.y) >> 1);
202
203
id++;
204
}
205
}
206
}
207
208
foreach_active(ImageTrail, imageTrail)
209
{
210
if (imageTrail->drawGroup < 3 && id < 0x200) {
211
imageTrail->scale.x = 0x100;
212
imageTrail->scale.y = 0x100;
213
214
FarPlane->positionList[id].x = imageTrail->currentPos.x;
215
FarPlane->positionList[id].y = imageTrail->currentPos.y;
216
217
imageTrail->currentPos.x = FarPlane->worldPos.x + ((imageTrail->currentPos.x - FarPlane->originPos.x) >> 1);
218
imageTrail->currentPos.y = FarPlane->worldPos.y + ((imageTrail->currentPos.y - FarPlane->originPos.y) >> 1);
219
220
id++;
221
222
for (int32 s = 0; s < 7; ++s) {
223
FarPlane->positionList[id].x = imageTrail->statePos[s].x;
224
FarPlane->positionList[id].y = imageTrail->statePos[s].y;
225
226
imageTrail->statePos[s].x = FarPlane->worldPos.x + ((imageTrail->statePos[s].x - FarPlane->originPos.x) >> 1);
227
imageTrail->statePos[s].y = FarPlane->worldPos.y + ((imageTrail->statePos[s].y - FarPlane->originPos.y) >> 1);
228
229
id++;
230
}
231
}
232
}
233
}
234
235
void FarPlane_DrawHook_RemoveFarPlane(void)
236
{
237
int32 id = 0;
238
for (int32 i = 0; i < 0x200 && id < 0x200; ++i) {
239
Entity *entity = RSDK.GetDrawListRef(1, i);
240
if (!entity)
241
break;
242
243
entity->position.x = FarPlane->positionList[id].x;
244
entity->position.y = FarPlane->positionList[id].y;
245
id++;
246
}
247
248
for (int32 i = 0; i < 0x200 && id < 0x200; ++i) {
249
Entity *entity = RSDK.GetDrawListRef(2, i);
250
if (!entity)
251
break;
252
253
entity->position.x = FarPlane->positionList[id].x;
254
entity->position.y = FarPlane->positionList[id].y;
255
id++;
256
}
257
258
foreach_active(InvincibleStars, invincibleStars)
259
{
260
if (invincibleStars->drawGroup == 1 && id < 0x200) {
261
for (int32 s = 0; s < 8; ++s) {
262
invincibleStars->starPos[s].x = FarPlane->positionList[id].x;
263
invincibleStars->starPos[s].y = FarPlane->positionList[id].y;
264
id++;
265
}
266
}
267
}
268
269
foreach_active(ImageTrail, imageTrail)
270
{
271
if (imageTrail->drawGroup == 1 && id < 0x200) {
272
imageTrail->currentPos.x = FarPlane->positionList[id].x;
273
imageTrail->currentPos.y = FarPlane->positionList[id].y;
274
id++;
275
276
for (int32 s = 0; s < 7; ++s) {
277
imageTrail->statePos[s].x = FarPlane->positionList[id].x;
278
imageTrail->statePos[s].y = FarPlane->positionList[id].y;
279
id++;
280
}
281
}
282
}
283
}
284
285
void FarPlane_Scanline_FarPlaneView(ScanlineInfo *scanline)
286
{
287
int32 x = FarPlane->screenPos.x - (ScreenInfo->center.x << 17);
288
int32 y = FarPlane->screenPos.y - (ScreenInfo->center.y << 17);
289
290
for (int32 h = 0; h < ScreenInfo->size.y; ++h) {
291
scanline->position.x = x;
292
scanline->position.y = y;
293
294
scanline->deform.x = 0x20000;
295
scanline->deform.y = 0;
296
297
y += 0x20000;
298
scanline++;
299
}
300
301
RSDK.CopyPalette(0, 0, 4, 0, 128);
302
RSDK.SetActivePalette(4, 0, ScreenInfo->size.y);
303
}
304
305
#if GAME_INCLUDE_EDITOR
306
void FarPlane_EditorDraw(void)
307
{
308
RSDK_THIS(FarPlane);
309
310
self->updateRange.x = self->size.x << 1;
311
self->updateRange.y = self->size.y << 1;
312
313
DrawHelpers_DrawRectOutline(self->position.x, self->position.y, self->size.x << 1, self->size.y << 1, 0xFF0000);
314
315
if (showGizmos()) {
316
RSDK_DRAWING_OVERLAY(true);
317
318
DrawHelpers_DrawRectOutline(self->origin.x, self->origin.y, self->size.x << 1, self->size.y << 1, 0xFFFF00);
319
320
DrawHelpers_DrawArrow(self->position.x, self->position.y, self->origin.x, self->origin.y, 0x00FF00, INK_NONE, 0xFF);
321
322
RSDK_DRAWING_OVERLAY(false);
323
}
324
}
325
326
void FarPlane_EditorLoad(void) {}
327
#endif
328
329
void FarPlane_Serialize(void)
330
{
331
RSDK_EDITABLE_VAR(FarPlane, VAR_VECTOR2, size);
332
RSDK_EDITABLE_VAR(FarPlane, VAR_VECTOR2, origin);
333
}
334
335