/// <summary> /// This Factory Method produces the next state for every enemy calling it. /// </summary> /// <param name="e"> Used to determine what states are available to the enemy </param> /// <returns> A new EnemyState Child </returns> public static void GetNextState(Enemy e) { // Use Random number to choose next state Random rand = new Random(DateTime.Now.Millisecond); for (int i = 0; i < 999990; i++) { } int chance = rand.Next(100); if (e is Buzzard) { /*** Buzzard States ***/ Buzzard b = e as Buzzard; if (b.stateMachine.currentState is EnemyStandingState) { if (chance % 2 == 0) { if (chance % 10 < 5) { b.stateMachine.Change("run_right"); } else { b.stateMachine.Change("run_right"); } } else { b.stateMachine.Change("flap"); } } else if (b.stateMachine.currentState is EnemyRunningState) { if (chance < 2) { b.stateMachine.Change("stand"); } else if (chance == 99) { EnemyState enemySt = b.stateMachine.currentState as EnemyState; if (enemySt.Angle == 0) { b.stateMachine.Change("run_left"); } else { b.stateMachine.Change("run_right"); } } } else if (b.stateMachine.currentState is EnemyFlappingState) { EnemyState enemySt = b.stateMachine.currentState as EnemyState; switch (enemySt.Angle) { case 90: if (chance / 10 < 2 && chance % 10 < 4 || b.coords.y < 10) { b.stateMachine.Change("fall"); } else if (chance % 2 == 0) { b.stateMachine.Change("flap_right"); } else { b.stateMachine.Change("flap_left"); } break; default: if (chance < 2 || b.coords.y < 10) { b.stateMachine.Change("flap"); } break; } } else if (b.stateMachine.currentState is EnemyFallingState) { EnemyState enemySt = b.stateMachine.currentState as EnemyState; switch (enemySt.Angle) { case 270: if (chance % 10 < 3 || b.coords.y > 730) { b.stateMachine.Change("flap"); } else if (chance % 2 == 0) { b.stateMachine.Change("fall_right"); } else { b.stateMachine.Change("fall_left"); } break; default: if (chance < 3 || b.coords.y > 730) { b.stateMachine.Change("fall"); } break; } } else if (b.stateMachine.currentState is BuzzardPickupState) { BuzzardPickupState set_state = b.stateMachine.currentState as BuzzardPickupState; // Determine if the Buzzard is close enough to the Mik (Hatched Egg) being picked up if ((set_state.TargetEgg.coords.x - b.coords.x) > -5 && (set_state.TargetEgg.coords.x - b.coords.x) < 5 && ((set_state.TargetEgg.coords.y - 50) - b.coords.y) < 5 && ((set_state.TargetEgg.coords.y - 50) - b.coords.y) > -5) { // Picked up Mik (Hatched Egg) set_state.TargetEgg.mounted = true; b.droppedEgg = false; b.stateMachine.Change("stand"); } } else if (b.stateMachine.currentState is BuzzardFleeingState) { // If none of the previous states, set state to Fleeing b.stateMachine.Change("flee"); } else { b.stateMachine.Change("flap"); } } else if (e is Egg) { /*** Egg States ***/ Egg egg = e as Egg; if (egg.stateMachine.currentState is EnemyFallingState) { if (egg.coords.y > 750) { egg.stateMachine.Change("stand"); } } else { if (egg.seconds > 800) { egg.stateMachine.Change("hatched"); } else if (egg.seconds > 600) { egg.seconds++; egg.stateMachine.Change("hatching"); } else { egg.seconds++; egg.stateMachine.Change("stand"); } } } else if (e is Pterodactyl) { /*** Pterodactyl States ***/ Pterodactyl p = e as Pterodactyl; // *** Add charging state to attack nearby player *** if (p.coords.y > 300 && p.coords.y < 450 && p.coords.x > 600 && p.coords.x < 700) { p.stateMachine.Change("charge"); } if (p.stateMachine.currentState is EnemyFallingState) { EnemyState enemySt = p.stateMachine.currentState as EnemyState; switch (enemySt.Angle) { case 270: if (chance % 10 < 3 || p.coords.y > 700) { p.stateMachine.Change("flap"); } else if (chance % 2 == 0) { p.stateMachine.Change("fall_right"); } else { p.stateMachine.Change("fall_left"); } break; default: if (chance < 3 || p.coords.y > 700) { p.stateMachine.Change("fall"); } break; } } else if (p.stateMachine.currentState is EnemyFlappingState) { EnemyState enemySt = p.stateMachine.currentState as EnemyState; switch (enemySt.Angle) { case 90: if (chance / 10 < 2 && chance % 10 < 4 || p.coords.y < 10) { p.stateMachine.Change("fall"); } else if (chance % 2 == 0) { p.stateMachine.Change("flap_right"); } else { p.stateMachine.Change("flap_left"); } break; default: if (chance < 2 || p.coords.y < 10) { p.stateMachine.Change("flap"); } break; } } else if (p.stateMachine.currentState is PterodactylChargeState) { if (p.coords.y <= 300 || p.coords.y >= 450 || p.coords.x <= 600 || p.coords.x >= 700) { p.stateMachine.Change("flap"); } } } }
/// <summary> /// Determines the objects next state and fires the appropriate event /// handler to notify the view of the state change. /// </summary> public override void Update() { if (!charging) { // Determine the next state state = EnemyState.GetNextState(this); state.Setup(); } if (state is EnemyFlappingState) { // "Gravity" purposes if (speed > TERMINAL_VELOCITY) { if (prevAngle == 225 || prevAngle == 270 || prevAngle == 315) { speed = 0.05; } else { speed = TERMINAL_VELOCITY; } } } else if (state is EnemyFallingState) { // "Gravity" purposes if (speed > TERMINAL_VELOCITY) { if (prevAngle == 45 || prevAngle == 90 || prevAngle == 135) { speed = 0.05; } else { speed = TERMINAL_VELOCITY; } } } else if (state is PterodactylDestroyedState) { // Slow the last 2 frames of the destroyed pterodactyl dieAnimateTime++; updateGraphicRate = 10; if (dieAnimateTime > 18) { if (PterodactylDestroyed != null) { PterodactylDestroyed(this, null); } Die(); } } else if (state is PterodactylChargeState) { // Don't allow the state to change when charging charging = true; chargeTime++; speed = 10; if (chargeTime > 40) { charging = false; chargeTime = 0; } } // "Gravity" purposes prevAngle = angle; // Slow the rate of updating the graphic if (updateGraphic > updateGraphicRate) { updateGraphic = 0; } else { updateGraphic++; } // Always fire the move event if (PterodactylMoveEvent != null) { PterodactylMoveEvent(this, null); } // Slow the rate of updating the graphic if (updateGraphic == 0) { if (PterodactylStateChange != null) { PterodactylStateChange(this, null); } } }
/// <summary> /// Determines the objects next state and fires the appropriate event /// handler to notify the view of the state change. /// </summary> public override void Update() { // Check Collision CheckEnemyCollision(); // Determine the next state EnemyState.GetNextState(this); stateMachine.currentState.Update(); if (stateMachine.currentState is EggHatchedState) { // Allow the hatched animation to run before notifying if (milliseconds > 30 && !alreadyHatched) { if (EggHatched != null) { EggHatched(this, null); alreadyHatched = true; } } } // Check if the hatched Mik has mounted a Buzzard if (mounted || collected) { if (collected) { Task.Run(() => { PlaySounds.Instance.Play_Collect(); }); } if (EggDestroyed != null) { EggDestroyed(this, null); } Die(); Trace.WriteLine("EGG NEW COORDS" + coords); } // Slow the rate of updating the graphic if (updateGraphic > 3) { updateGraphic = 0; } else { updateGraphic++; } // Always fire the move event if (EggMoveEvent != null) { EggMoveEvent(this, null); } // Slow the rate of updating the graphic if (updateGraphic == 0) { if (EggStateChange != null) { EggStateChange(this, null); } } }
/// <summary> /// Determines the objects next state and fires the appropriate event /// handler to notify the view of the state change. /// </summary> public override void Update() { // Determine the next state state = EnemyState.GetNextState(this); state.Setup(); if (state is EnemyFlappingState) { // "Gravity" purposes if (speed > TERMINAL_VELOCITY) { if (prevAngle == 225 || prevAngle == 270 || prevAngle == 315) { speed = 0.05; } else { speed = TERMINAL_VELOCITY; } } } else if (state is EnemyFallingState) { // "Gravity" purposes if (speed > TERMINAL_VELOCITY) { if (prevAngle == 45 || prevAngle == 90 || prevAngle == 135) { speed = 0.05; } else { speed = TERMINAL_VELOCITY; } } } else if (state is EnemyRunningState) { speed = SPEED; } else if (state is BuzzardFleeingState) { // When the Buzzard has been hit, it drops an egg and // flies to the left side. Destroy the Buzzard when // close enough to the edge of the screen. if (coords.x < 3) { if (BuzzardDestroyed != null) { BuzzardDestroyed(this, null); } Die(); } } // "Gravity" purposes prevAngle = angle; // Slow the rate of updating the graphic if (updateGraphic > 3) { updateGraphic = 0; } else { updateGraphic++; } // Always fire the move event if (BuzzardMoveEvent != null) // Is anyone subscribed? { BuzzardMoveEvent(this, null); // Raise event } // Slow the rate of updating the graphic if (updateGraphic == 0) { if (BuzzardStateChange != null) { BuzzardStateChange(this, null); } } // *** Check if lost in a joust against the player *** if (coords.y > 450 && coords.y < 525 && coords.x > 650 && coords.x < 800 && !droppedEgg) { if (BuzzardDropEgg != null) { BuzzardDropEgg(this, null); } droppedEgg = true; } }
/// <summary> /// Determines the objects next state and fires the appropriate event /// handler to notify the view of the state change. /// </summary> public override void Update() { //Console.WriteLine("Buzzard state = " + stateMachine.currentState.ToString()); if (!isSpawning) { if (!droppedEgg) { CheckEnemyCollision(); } // Determine the next state EnemyState.GetNextState(this); stateMachine.currentState.Update(); } if (stateMachine.currentState is EnemyFlappingState) { // "Gravity" purposes if (speed > TERMINAL_VELOCITY) { if (prevAngle == 225 || prevAngle == 270 || prevAngle == 315) { speed = 0.05; } else { speed = TERMINAL_VELOCITY; } } } else if (stateMachine.currentState is EnemyFallingState) { // "Gravity" purposes if (speed > TERMINAL_VELOCITY) { if (prevAngle == 45 || prevAngle == 90 || prevAngle == 135) { speed = 0.05; } else { speed = TERMINAL_VELOCITY; } } } else if (stateMachine.currentState is EnemyRunningState) { speed = SPEED; } else if (stateMachine.currentState is BuzzardFleeingState) { // When the Buzzard has been hit, it drops an egg and // flies to the left side. Destroy the Buzzard when // close enough to the edge of the screen. if (!droppedEgg) { if (BuzzardDropEgg != null) { BuzzardDropEgg(this, null); } droppedEgg = true; } if (coords.x < 3) { if (BuzzardDestroyed != null) { BuzzardDestroyed(this, null); } Die(); } } else if (stateMachine.currentState is EnemySpawningState) { respawning++; isSpawning = true; prevAngle = 90; angle = 90; speed = 0.5; imagePath = "Images/Enemy/mik_respawn.png"; if (respawning > 100) { if (droppedEgg) { stateMachine.Change("pickup"); BuzzardPickupState pState = stateMachine.currentState as BuzzardPickupState; pState.TargetEgg = pickupEgg; stateMachine.currentState.Update(); } else { stateMachine.Change("spawn"); } respawning = 0; isSpawning = false; } } // "Gravity" purposes prevAngle = angle; // Slow the rate of updating the graphic if (updateGraphic > 3) { updateGraphic = 0; } else { updateGraphic++; } // Always fire the move event if (BuzzardMoveEvent != null) // Is anyone subscribed? { BuzzardMoveEvent(this, null); // Raise event } // Slow the rate of updating the graphic if (updateGraphic == 0) { if (BuzzardStateChange != null) { BuzzardStateChange(this, null); } } }