public override void Update() { if (StateEnemy is Buzzard) { Buzzard b = StateEnemy as Buzzard; b.angle = Angle; b.speed += 0.05; if (b.imagePath.EndsWith("fly1.png")) { b.imagePath = "Images/Enemy/mik_" + b.Color + "_fly2.png"; } else { b.imagePath = "Images/Enemy/mik_" + b.Color + "_fly1.png"; } } else if (StateEnemy is Pterodactyl) { Pterodactyl p = StateEnemy as Pterodactyl; p.angle = Angle; p.speed += 0.05; switch (p.imagePath) { case "Images/Enemy/pterodactyl_fly1.png": p.imagePath = "Images/Enemy/pterodactyl_fly2.png"; break; default: p.imagePath = "Images/Enemy/pterodactyl_fly1.png"; break; } } }
public override void Update() { if (StateEnemy is Pterodactyl) { Pterodactyl p = StateEnemy as Pterodactyl; try { Point playerCoords = new Point(0, 0); foreach (WorldObject obj in World.Instance.objects) { if (obj is Ostrich) { Ostrich o = obj as Ostrich; playerCoords = o.coords; } } double x = (p.coords.x - playerCoords.x); double y = (p.coords.y - playerCoords.y); p.angle = Math.Atan(x / y); if (x < 0) { p.angle += 180; } p.imagePath = "Images/Enemy/pterodactyl_charge.png"; } catch (InvalidOperationException op) { Trace.WriteLine(op.Message); } } }
public void TestDie() { Pterodactyl p = new Pterodactyl(); p.coords = new Point(500, 500); p.Die(); Assert.AreEqual(new List <WorldObject> { }, World.Instance.objects); }
/// <summary> /// Executes on moveTimer Tick in MainWindow.xaml.cs. It loops through the /// objects in the World and executes their Update method if a Buzzard, Egg, /// or a Pterodactyl. /// </summary> public void UpdateAllEnemies_Position() { try { foreach (WorldObject obj in objects) { Buzzard buzzardObj = obj as Buzzard; if (buzzardObj != null) { buzzardObj.Update(); } Egg eggObj = obj as Egg; if (eggObj != null) { eggObj.Update(); } // Used to keep track of the stage time for spawning the Pterodactyls stageTimeFrame++; if (stageTimeFrame == 500) { stageTimeSeconds++; stageTimeFrame = 0; } if (stageTimeSeconds == 60) { stageTimeMinutes++; stageTimeSeconds = 0; } if (stageTimeMinutes == PTERODACTYL_SPAWN_MINUTES) { stageTimeMinutes = 0; if (SpawnPterodactyl != null) { SpawnPterodactyl(Instance, null); } } Pterodactyl pterodactylObj = obj as Pterodactyl; if (pterodactylObj != null) { pterodactylObj.Update(); } } } catch (InvalidOperationException op) { // This exception was being thrown when I added to the objects // List from other code while this loop is executing. No noticable // bugs. Possible that it skips a Tick, but not an extreme problem. Trace.WriteLine(op.Message); } }
public override void Setup() { if (StateEnemy is Buzzard) { Buzzard b = StateEnemy as Buzzard; b.angle = Angle; b.speed += 0.05; b.imagePath = "Images/Enemy/mik_" + b.Color + "_fly1.png"; } else if (StateEnemy is Pterodactyl) { Pterodactyl p = StateEnemy as Pterodactyl; p.angle = Angle; p.speed += 0.05; p.imagePath = "Images/Enemy/pterodactyl_fly1.png"; } }
public override void Update() { if (StateEnemy is Pterodactyl) { Pterodactyl p = StateEnemy as Pterodactyl; p.angle = 0; p.speed = 0; switch (p.imagePath) { case "Images/Enemy/pterodactyl.die1.png": p.imagePath = "Images/Enemy/pterodactyl_die2.png"; break; default: p.imagePath = "Images/Enemy/pterodactyl_die1.png"; break; } } }
public override void Update() { if (StateEnemy is Buzzard) { Buzzard b = StateEnemy as Buzzard; b.angle = Angle; b.speed += 0.05; b.imagePath = "Images/Enemy/mik_" + b.Color + "_fly1.png"; } else if (StateEnemy is Pterodactyl) { Pterodactyl p = StateEnemy as Pterodactyl; p.angle = Angle; p.speed += 0.05; p.imagePath = "Images/Enemy/pterodactyl_fly1.png"; } else if (StateEnemy is Egg) { Egg egg = StateEnemy as Egg; egg.angle = Angle; egg.speed = 5; egg.imagePath = "Images/Enemy/egg1.png"; } }
/// <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> /// 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 EnemyState GetNextState(Enemy e) { Random rand = new Random(DateTime.Now.Millisecond); for (int i = 0; i < 999990; i++) { } // Use up time so each Enemy's random updates differently int chance = rand.Next(100); if (e is Buzzard) { /*** Buzzard States ***/ Buzzard b = e as Buzzard; // *** Check if lost in a joust against the player *** if (b.coords.y > 450 && b.coords.y < 525 && b.coords.x > 650 && b.coords.x < 800) { return new BuzzardFleeingState() { StateEnemy = b } } ; // *** Check if Buzzard is near the ground of a platform and return new EnemyRunningState like below *** if (b.state is EnemyStandingState) { if (chance % 2 == 0) { if (chance % 10 < 5) { return new EnemyRunningState() { Angle = 0, StateEnemy = b } } ; // Running right state else { return new EnemyRunningState() { Angle = 180, StateEnemy = b } }; // Running left state } else { return(new EnemyFlappingState() { Angle = 90, StateEnemy = b }); // Flapping up state } } else if (b.state is EnemyRunningState) { if (chance < 2) { return new EnemyStandingState() { Angle = b.state.Angle, StateEnemy = b } } ; // Standing state else if (chance < 99) { return new EnemyRunningState() { Angle = b.state.Angle, StateEnemy = b } } ; // Running in same direction state b.state.Angle += 180; if (b.state.Angle > 360) { b.state.Angle -= 360; } return(new EnemyRunningState() { Angle = b.state.Angle, StateEnemy = b }); // Running in opposite direction state } else if (b.state is EnemyFlappingState) { switch (b.state.Angle) { case 90: if (chance / 10 < 2 && chance % 10 < 4 || b.coords.y < 10) { return new EnemyFallingState() { Angle = 270, StateEnemy = b } } ; // Falling down state else if (chance % 2 == 0) { return new EnemyFlappingState() { Angle = 45, StateEnemy = b } } ; // Flapping right state else { return new EnemyFlappingState() { Angle = 135, StateEnemy = b } }; // Flapping left state default: if (chance < 2 || b.coords.y < 10) { return new EnemyFlappingState() { Angle = 90, StateEnemy = b } } ; // Flapping up state else { return new EnemyFlappingState() { Angle = b.state.Angle, StateEnemy = b } }; } } else if (b.state is EnemyFallingState) { switch (b.state.Angle) { case 270: if (chance % 10 < 3 || b.coords.y > 800) { return new EnemyFlappingState() { Angle = 90, StateEnemy = b } } ; // Flapping up state else if (chance % 2 == 0) { return new EnemyFallingState() { Angle = 315, StateEnemy = b } } ; // Falling right state else { return new EnemyFallingState() { Angle = 225, StateEnemy = b } }; // Falling left state default: if (chance < 3) { return new EnemyFallingState() { Angle = 270, StateEnemy = b } } ; // Falling down state else { return new EnemyFallingState() { Angle = b.state.Angle, StateEnemy = b } }; } } else if (b.state is BuzzardPickupState) { BuzzardPickupState set_state = b.state as BuzzardPickupState; // Determine if the Buzzard is close enough to the Mik 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 set_state.TargetEgg.mounted = true; b.droppedEgg = false; return(new EnemyStandingState() { StateEnemy = b, Angle = 0 }); } else { return(new BuzzardPickupState() { StateEnemy = b, Angle = b.state.Angle, TargetEgg = set_state.TargetEgg }); } } else { // If non of the previous states, set state to Fleeing return(new BuzzardFleeingState() { StateEnemy = b }); } } else if (e is Egg) { /*** Egg States ***/ Egg egg = e as Egg; if (egg.state is EnemyFallingState) { // *** Implement when the egg lands on a platform *** if (egg.coords.y > 800) { return new EnemyStandingState() { Angle = egg.state.Angle, StateEnemy = egg } } ; // Standing state else { return new EnemyFallingState() { Angle = egg.state.Angle, StateEnemy = egg } }; // Falling state } else { if (egg.seconds > 800) { return(new EggHatchedState() { StateEnemy = egg }); // Egg has hatched state } else if (egg.seconds > 600) { egg.seconds++; return(new EggHatchingState() { StateEnemy = egg }); // Egg is hatching state } else { egg.seconds++; return(new EnemyStandingState() { Angle = 0, StateEnemy = egg }); // Standing state } } } 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 < 500 && p.coords.x > 600 && p.coords.x < 800) { return new PterodactylChargeState() { StateEnemy = p } } ; // *** Add hitbox check to destroy pterodactyl *** if (p.coords.y > 450 && p.coords.y < 525 && p.coords.x > 650 && p.coords.x < 800) { return new PterodactylDestroyedState() { StateEnemy = p } } ; if (p.state is EnemyFallingState) { switch (p.state.Angle) { case 270: if (chance % 10 < 3 || p.coords.y > 800) { return new EnemyFlappingState() { Angle = 90, StateEnemy = p } } ; // Flapping up state else if (chance % 2 == 0) { return new EnemyFallingState() { Angle = 315, StateEnemy = p } } ; // Falling right state else { return new EnemyFallingState() { Angle = 225, StateEnemy = p } }; // Falling left state default: if (chance < 3) { return new EnemyFallingState() { Angle = 270, StateEnemy = p } } ; // Falling down state else { return new EnemyFallingState() { Angle = p.state.Angle, StateEnemy = p } }; } } else if (p.state is EnemyFlappingState) { switch (p.state.Angle) { case 90: if (chance / 10 < 2 && chance % 10 < 4 || p.coords.y < 10) { return new EnemyFallingState() { Angle = 270, StateEnemy = p } } ; // Falling down state else if (chance % 2 == 0) { return new EnemyFlappingState() { Angle = 45, StateEnemy = p } } ; // Flapping right state else { return new EnemyFlappingState() { Angle = 135, StateEnemy = p } }; // Flapping left state default: if (chance < 2 || p.coords.y < 10) { return new EnemyFlappingState() { Angle = 90, StateEnemy = p } } ; // Flapping up state else { return new EnemyFlappingState() { Angle = p.state.Angle, StateEnemy = p } }; } } else { // Default is Falling state return(new EnemyFallingState() { Angle = p.state.Angle, StateEnemy = p }); } } else { /*** No Know Enemy Default State ***/ return(new EnemyStandingState() { Angle = 0 }); } }