Path: blob/master/SonicMania/Objects/MMZ/Piston.c
338 views
// ---------------------------------------------------------------------1// RSDK Project: Sonic Mania2// Object Description: Piston Object3// Object Author: Christian Whitehead/Simon Thomley/Hunter Bridges4// Decompiled by: Rubberduckycooly & RMGRich5// ---------------------------------------------------------------------67#include "Game.h"89ObjectPiston *Piston;1011void Piston_Update(void)12{13RSDK_THIS(Piston);1415if (self->state == Piston_State_WaitForInterval && !((Zone->timer + self->intervalOffset) % self->interval)) {16self->timer = self->distance >> 3;17self->active = ACTIVE_NORMAL;1819switch (self->pistonType) {20default:21case PISTON_MOVE_VERTICAL: self->state = Piston_StateMove_Vertical; break;22case PISTON_UP: self->state = Piston_StateMove_Up; break;23case PISTON_MOVE_DOWN: self->state = Piston_StateMove_Down; break;24case PISTON_MOVE_DOWN_REVERSE: self->state = Piston_StateMove_Down_Reverse; break;25case PISTON_MOVE_RIGHT: self->state = Piston_StateMove_Right; break;26case PISTON_MOVE_LEFT: self->state = Piston_StateMove_Left; break;27case PISTON_MOVE_HORIZONTAL: self->state = Piston_StateMove_Horizontal; break;28}29}3031Platform_Update();32}3334void Piston_LateUpdate(void) {}3536void Piston_StaticUpdate(void) {}3738void Piston_Draw(void)39{40RSDK_THIS(Piston);4142RSDK.DrawSprite(&self->animator, &self->drawPos, false);43}4445void Piston_Create(void *data)46{47RSDK_THIS(Piston);4849self->pistonType = self->type;50self->type = PLATFORM_FIXED;51self->collision = PLATFORM_C_SOLID;52if (self->pistonType > PISTON_MOVE_DOWN_REVERSE)53self->size += 3;5455Platform_Create(NULL);56if (self->pistonType <= PISTON_MOVE_DOWN_REVERSE) {57self->updateRange.x = 0x800000;58self->updateRange.y = (self->distance + 0x80) << 16;59}60else {61self->updateRange.x = (self->distance + 0x80) << 16;62self->updateRange.y = 0x800000;63}6465self->type = self->pistonType;66if (self->type == PISTON_MOVE_DOWN_REVERSE) {67self->state = Piston_StateActive_WaitForStood;68self->stateCollide = Piston_Collide_Solid;69}70else {71self->state = Piston_State_WaitForInterval;72}73}7475void Piston_StageLoad(void)76{77Piston->sfxLand = RSDK.GetSfx("MMZ/PistonLand.wav");78Piston->sfxLaunch = RSDK.GetSfx("MMZ/PistonLaunch.wav");79}8081void Piston_Collide_Solid(void)82{83Platform_Collision_Solid();8485#if MANIA_USE_PLUS86RSDK_THIS(Piston);8788for (int32 i = 0; i < Player->playerCount; ++i) {89if (((1 << i) & self->stoodPlayers) != 0) {90EntityPlayer *player = RSDK_GET_ENTITY(i, Player);91if (player->state == Player_State_MightyHammerDrop)92player->state = Player_State_Air;93}94}95#endif96}9798void Piston_State_WaitForInterval(void)99{100// this obj was fun to do and is also very weird101// there's so many fucking states but they're so tiny and almost exactly the same thing as other states102}103104void Piston_StateMove_Vertical(void)105{106RSDK_THIS(Piston);107108self->velocity.y = 0x80000;109self->drawPos.y += self->reverse ? -0x80000 : 0x80000;110111if (--self->timer <= 0) {112self->timer = self->distance;113self->state = Piston_StateMove_Vertical_Reverse;114}115}116117void Piston_StateMove_Vertical_Reverse(void)118{119RSDK_THIS(Piston);120121self->velocity.y = 0x10000;122self->drawPos.y += self->reverse ? -0x10000 : 0x10000;123124if (--self->timer <= 0) {125self->active = ACTIVE_BOUNDS;126self->state = Piston_State_WaitForInterval;127}128}129130void Piston_StateMove_Up(void)131{132RSDK_THIS(Piston);133134self->drawPos.y -= 0x80000;135self->velocity.y = 0x80000;136137if (--self->timer <= 0) {138for (int32 i = 0; i < Player->playerCount; ++i) {139EntityPlayer *player = RSDK_GET_ENTITY(i, Player);140if ((1 << i) & self->stoodPlayers) {141player->velocity.y = -0x100000;142player->state = Player_State_Air;143player->onGround = false;144RSDK.SetSpriteAnimation(player->aniFrames, ANI_SPRING_TWIRL, &player->animator, true, 0);145}146}147148self->timer = self->distance;149self->state = Piston_StateMove_Up_Reverse;150}151}152153void Piston_StateMove_Up_Reverse(void)154{155RSDK_THIS(Piston);156157self->drawPos.y += 0x10000;158self->velocity.y = 0x10000;159160if (--self->timer <= 0) {161self->active = ACTIVE_BOUNDS;162self->state = Piston_State_WaitForInterval;163}164}165166void Piston_StateMove_Down(void)167{168RSDK_THIS(Piston);169170self->drawPos.y += 0x80000;171self->velocity.y = 0x80000;172173if (--self->timer <= 0) {174self->pistonType = PISTON_MOVE_DOWN_REVERSE;175self->active = ACTIVE_BOUNDS;176self->state = Piston_State_WaitForInterval;177}178}179180void Piston_StateMove_Down_Reverse(void)181{182RSDK_THIS(Piston);183184self->drawPos.y -= 0x80000;185self->velocity.y = 0x80000;186187if (--self->timer <= 0) {188self->pistonType = PISTON_MOVE_DOWN;189self->active = ACTIVE_BOUNDS;190self->state = Piston_State_WaitForInterval;191}192}193194void Piston_StateMove_Right(void)195{196RSDK_THIS(Piston);197198self->drawPos.x += 0x80000;199self->velocity.x = 0x80000;200201if (--self->timer <= 0) {202self->pistonType = PISTON_MOVE_LEFT;203self->active = ACTIVE_BOUNDS;204self->state = Piston_State_WaitForInterval;205}206}207208void Piston_StateMove_Left(void)209{210RSDK_THIS(Piston);211212self->drawPos.x -= 0x80000;213self->velocity.x = 0x80000;214215if (--self->timer <= 0) {216self->pistonType = PISTON_MOVE_RIGHT;217self->active = ACTIVE_BOUNDS;218self->state = Piston_State_WaitForInterval;219}220}221222void Piston_StateMove_Horizontal(void)223{224RSDK_THIS(Piston);225226self->velocity.x = 0x80000;227self->drawPos.x += self->reverse ? -0x80000 : 0x80000;228229if (--self->timer <= 0) {230self->timer = self->distance;231self->state = Piston_StateMove_Horizontal_Reverse;232}233}234235void Piston_StateMove_Horizontal_Reverse(void)236{237RSDK_THIS(Piston);238239self->velocity.x = 0x10000;240self->drawPos.x += self->reverse ? -0x10000 : 0x10000;241242if (--self->timer <= 0) {243self->active = ACTIVE_BOUNDS;244self->state = Piston_State_WaitForInterval;245}246}247248void Piston_StateActive_WaitForStood(void)249{250RSDK_THIS(Piston);251252if (self->stoodPlayers)253self->state = Piston_StateActive_PreparingLaunch;254}255256void Piston_StateActive_PreparingLaunch(void)257{258RSDK_THIS(Piston);259260if (self->stoodPlayers) {261if (!self->timer)262RSDK.PlaySfx(Piston->sfxLand, false, 255);263264self->drawPos.y += 0x10000;265self->velocity.y = 0x10000;266267if (++self->timer >= 16) {268RSDK.StopSfx(Piston->sfxLand);269RSDK.PlaySfx(Piston->sfxLaunch, false, 255);270self->state = Piston_StateActive_LaunchPlayers;271}272}273else {274self->state = Piston_StateActive_ReturnToStartPos;275RSDK.StopSfx(Piston->sfxLand);276}277}278279void Piston_StateActive_LaunchPlayers(void)280{281RSDK_THIS(Piston);282283self->drawPos.y -= 0x40000;284self->velocity.y = 0x40000;285286self->timer -= 4;287if (!self->timer) {288for (int32 i = 0; i < Player->playerCount; ++i) {289EntityPlayer *player = RSDK_GET_ENTITY(i, Player);290if ((1 << i) & self->stoodPlayers) {291RSDK.PlaySfx(Piston->sfxLaunch, false, 255);292player->velocity.y = -0x100000;293player->state = Player_State_Air;294player->onGround = false;295RSDK.SetSpriteAnimation(player->aniFrames, ANI_SPRING_TWIRL, &player->animator, true, 0);296}297}298299self->state = Piston_StateActive_WaitForStood;300}301}302303void Piston_StateActive_ReturnToStartPos(void)304{305RSDK_THIS(Piston);306307if (self->stoodPlayers) {308self->state = Piston_StateActive_PreparingLaunch;309}310else {311self->drawPos.y -= 0x10000;312self->velocity.y = 0x10000;313314if (--self->timer <= 0)315self->state = Piston_StateActive_WaitForStood;316}317}318319#if GAME_INCLUDE_EDITOR320void Piston_EditorDraw(void)321{322RSDK_THIS(Piston);323324Piston_Create(NULL);325Piston_Draw();326327if (showGizmos()) {328RSDK_DRAWING_OVERLAY(true);329330for (int32 s = SceneInfo->entitySlot + 1, i = 0; i < self->childCount; ++i) {331Entity *child = RSDK_GET_ENTITY_GEN(s + i);332if (!child)333continue;334335DrawHelpers_DrawArrow(self->position.x, self->position.y, child->position.x, child->position.y, 0xE0E0E0, INK_NONE, 0xFF);336}337338RSDK_DRAWING_OVERLAY(false);339}340}341342void Piston_EditorLoad(void)343{344RSDK_ACTIVE_VAR(Piston, type);345RSDK_ENUM_VAR("Use move distance (Vertical)", PISTON_MOVE_VERTICAL);346RSDK_ENUM_VAR("Move up then down", PISTON_UP);347RSDK_ENUM_VAR("Move down then up", PISTON_MOVE_DOWN);348RSDK_ENUM_VAR("Launch players when stood on", PISTON_MOVE_DOWN_REVERSE);349RSDK_ENUM_VAR("Move right then left", PISTON_MOVE_RIGHT);350RSDK_ENUM_VAR("Move left then right", PISTON_MOVE_LEFT);351RSDK_ENUM_VAR("Use move distance (Horizontal)", PISTON_MOVE_HORIZONTAL);352353RSDK_ACTIVE_VAR(Piston, size);354RSDK_ENUM_VAR("1 Barrel", PISTON_SIZE_1);355RSDK_ENUM_VAR("2 Barrels", PISTON_SIZE_2);356RSDK_ENUM_VAR("3 Barrels", PISTON_SIZE_3);357}358#endif359360void Piston_Serialize(void)361{362RSDK_EDITABLE_VAR(Piston, VAR_ENUM, type);363RSDK_EDITABLE_VAR(Piston, VAR_BOOL, reverse);364RSDK_EDITABLE_VAR(Piston, VAR_UINT8, size);365RSDK_EDITABLE_VAR(Piston, VAR_ENUM, childCount);366RSDK_EDITABLE_VAR(Piston, VAR_UINT16, interval);367RSDK_EDITABLE_VAR(Piston, VAR_UINT16, intervalOffset);368RSDK_EDITABLE_VAR(Piston, VAR_ENUM, distance);369}370371372