Path: blob/master/src/game/behaviors/butterfly.inc.c
7861 views
// butterfly.c.inc12void bhv_butterfly_init(void) {3cur_obj_init_animation(1);45o->oButterflyYPhase = random_float() * 100.0f;6o->header.gfx.animInfo.animFrame = random_float() * 7.0f;7o->oHomeX = o->oPosX;8o->oHomeY = o->oPosY;9o->oHomeZ = o->oPosZ;10}1112// sp28 = speed1314void butterfly_step(s32 speed) {15struct FloorGeometry *sp24;16s16 yaw = o->oMoveAngleYaw;17s16 pitch = o->oMoveAnglePitch;18s16 yPhase = o->oButterflyYPhase;19f32 floorY;2021o->oVelX = sins(yaw) * (f32) speed;22o->oVelY = sins(pitch) * (f32) speed;23o->oVelZ = coss(yaw) * (f32) speed;2425o->oPosX += o->oVelX;26o->oPosZ += o->oVelZ;2728if (o->oAction == BUTTERFLY_ACT_FOLLOW_MARIO)29o->oPosY -= o->oVelY + coss((s32)(yPhase * 655.36)) * 20.0f / 4;30else31o->oPosY -= o->oVelY;3233floorY = find_floor_height_and_data(o->oPosX, o->oPosY, o->oPosZ, &sp24);3435if (o->oPosY < floorY + 2.0f)36o->oPosY = floorY + 2.0f;3738o->oButterflyYPhase++;39if (o->oButterflyYPhase >= 101)40o->oButterflyYPhase = 0;41}4243void butterfly_calculate_angle(void) {44gMarioObject->oPosX += 5 * o->oButterflyYPhase / 4;45gMarioObject->oPosZ += 5 * o->oButterflyYPhase / 4;46obj_turn_toward_object(o, gMarioObject, 16, 0x300);47gMarioObject->oPosX -= 5 * o->oButterflyYPhase / 4;48gMarioObject->oPosZ -= 5 * o->oButterflyYPhase / 4;4950gMarioObject->oPosY += (5 * o->oButterflyYPhase + 0x100) / 4;51obj_turn_toward_object(o, gMarioObject, 15, 0x500);52gMarioObject->oPosY -= (5 * o->oButterflyYPhase + 0x100) / 4;53}5455void butterfly_act_rest(void) {56if (is_point_within_radius_of_mario(o->oPosX, o->oPosY, o->oPosZ, 1000)) {57cur_obj_init_animation(0);5859o->oAction = BUTTERFLY_ACT_FOLLOW_MARIO;60o->oMoveAngleYaw = gMarioObject->header.gfx.angle[1];61}62}6364void butterfly_act_follow_mario(void) {65butterfly_calculate_angle();6667butterfly_step(7);6869if (!is_point_within_radius_of_mario(o->oHomeX, o->oHomeY, o->oHomeZ, 1200))70o->oAction = BUTTERFLY_ACT_RETURN_HOME;71}7273void butterfly_act_return_home(void) {74f32 homeDistX = o->oHomeX - o->oPosX;75f32 homeDistY = o->oHomeY - o->oPosY;76f32 homeDistZ = o->oHomeZ - o->oPosZ;77s16 hAngleToHome = atan2s(homeDistZ, homeDistX);78s16 vAngleToHome = atan2s(sqrtf(homeDistX * homeDistX + homeDistZ * homeDistZ), -homeDistY);7980o->oMoveAngleYaw = approach_s16_symmetric(o->oMoveAngleYaw, hAngleToHome, 0x800);81o->oMoveAnglePitch = approach_s16_symmetric(o->oMoveAnglePitch, vAngleToHome, 0x50);8283butterfly_step(7);8485if (homeDistX * homeDistX + homeDistY * homeDistY + homeDistZ * homeDistZ < 144.0f) {86cur_obj_init_animation(1);8788o->oAction = BUTTERFLY_ACT_RESTING;89o->oPosX = o->oHomeX;90o->oPosY = o->oHomeY;91o->oPosZ = o->oHomeZ;92}93}9495void bhv_butterfly_loop(void) {96switch (o->oAction) {97case BUTTERFLY_ACT_RESTING:98butterfly_act_rest();99break;100101case BUTTERFLY_ACT_FOLLOW_MARIO:102butterfly_act_follow_mario();103break;104105case BUTTERFLY_ACT_RETURN_HOME:106butterfly_act_return_home();107break;108}109110set_object_visibility(o, 3000);111}112113114