/**1* Behavior for bhvBird. These are the birds in the castle grounds2* that fly away and scatter when Mario comes near them. There are3* 2 types of birds; spawner birds and spawned birds. Spawner birds4* are loaded by the level, and are inactive until Mario comes within5* 2000 units of them, when they spawn 6 spawned birds and start flying.6* Spawned birds are only spawned by a spawner bird, and start flying7* immediately after spawning.8*/910/**11* If the object is a spawned bird, start flying; if it's a spawner bird,12* spawn spawned birds if Mario comes within 2000 units of it.13*/14static void bird_act_inactive(void) {15// Start flying if the object is a spawned bird or if it's a spawner bird16// and Mario is within 2000 units.17if (o->oBehParams2ndByte == BIRD_BP_SPAWNED || o->oDistanceToMario < 2000.0f) {18// If the object is a spawner bird, play the sound of birds flying away,19// and spawn 6 spawned birds (which will start flying on the next frame).20if (o->oBehParams2ndByte != BIRD_BP_SPAWNED) {21s32 i;2223cur_obj_play_sound_2(SOUND_GENERAL_BIRDS_FLY_AWAY);2425for (i = 0; i < 6; i++) {26spawn_object(o, MODEL_BIRDS, bhvBird);27}2829// The spawner bird's home acts as its target location.30o->oHomeX = -20.0f;31o->oHomeZ = -3990.0f;32}3334// Start flying35o->oAction = BIRD_ACT_FLY;3637// Start with a random yaw, and a random pitch from 1000 to 5000.38// Positive pitch is downwards.39o->oMoveAnglePitch = 5000 - (s32)(4000.0f * random_float());40o->oMoveAngleYaw = random_u16();4142o->oBirdSpeed = 40.0f;4344cur_obj_unhide();45}46}4748/**49* Make the bird fly.50* The bird flies laterally towards a target; (-20, -3990) if it's a spawner bird,51* and the parent spawner bird if it's a spawned bird.52*/53static void bird_act_fly(void) {54UNUSED s32 unused;55f32 distance;5657// Compute forward velocity and vertical velocity from oBirdSpeed and pitch58obj_compute_vel_from_move_pitch(o->oBirdSpeed);5960// If the bird's parent is higher than 8000 units, despawn the bird.61// A spawned bird's parent is its spawner bird. A spawner bird's parent62// is itself. In other words, when a group of birds has its spawner bird63// fly past Y=8000, they will all despawn simultaneously. Otherwise, fly.64if (o->parentObj->oPosY > 8000.0f) {65obj_mark_for_deletion(o);66} else {67// If the bird is a spawner bird, fly towards its home; otherwise,68// fly towards the bird's spawner bird.69if (o->oBehParams2ndByte != BIRD_BP_SPAWNED) {70distance = cur_obj_lateral_dist_to_home();7172// The spawner bird will start with its downwards (positive) pitch73// and will continuously decrease its pitch (i.e. make itself face more upwards)74// until it reaches its home, at which point it will face directly up.75// This is done by making its target pitch the arctangent of its distance76// to its home and its position - 10,000 (which is always negative).77o->oBirdTargetPitch = atan2s(distance, o->oPosY - 10000.0f);78o->oBirdTargetYaw = cur_obj_angle_to_home();79} else {80distance = lateral_dist_between_objects(o, o->parentObj);8182// The bird's target pitch will face directly to its spawner bird.83o->oBirdTargetPitch = atan2s(distance, o->oPosY - o->parentObj->oPosY);84o->oBirdTargetYaw = obj_angle_to_object(o, o->parentObj);8586// The bird goes faster the farther it is from its spawner bird so it can catch up.87o->oBirdSpeed = 0.04f * dist_between_objects(o, o->parentObj) + 20.0f;88}8990// Approach to match the bird's target yaw and pitch.91obj_move_pitch_approach(o->oBirdTargetPitch, 140);92cur_obj_rotate_yaw_toward(o->oBirdTargetYaw, 800);93obj_roll_to_match_yaw_turn(o->oBirdTargetYaw, 0x3000, 600);94}9596// The bird has no gravity, so this function only97// moves the bird using its forward velocity.98// Even if it did have gravity, it would only act as99// a constant added to its Y position every frame since100// its Y velocity is reset every frame by101// obj_compute_vel_from_move_pitch.102cur_obj_move_using_fvel_and_gravity();103}104105/**106* Update function for bhvBird.107*/108void bhv_bird_update(void) {109switch (o->oAction) {110case BIRD_ACT_INACTIVE:111bird_act_inactive();112break;113case BIRD_ACT_FLY:114bird_act_fly();115break;116}117}118119120