private void SpawnCreepShips(int amount) { var ships = new List<Enemy>(); Random r = new Random(DateTime.Now.Millisecond); var tileList = new List<Vector2>(); for (int i = 0; i < amount; i++) { var position = new Vector2(r.Next(0, Game.CurrentMap.Width + 1), r.Next(0, Game.CurrentMap.Height + 1)); while (tileList.Contains(position)) { position = new Vector2(r.Next(0, Game.CurrentMap.Width + 1), r.Next(0, Game.CurrentMap.Height + 1)); } tileList.Add(position); var ship = new Enemy() { CharSprite = new SpriteAnimation(GameGraphics.Ship1, 300, 4), Position = MapHelper.GetPixelsFromTileCenter(position), Behavior = Enemy.BehaviorType.Fleeing, StepSize = 3, PlayerKillable = true, CollisionRadius = 20, UniqueObjectId = 1, Name = "Ship1", Points = 10, Damage = 2, Heal = 0 }; ship.NewTargetPosition(Game.CurrentMap); ships.Add(ship); } if (Game.CurrentMap != null) { Game.CurrentMap.MapEnemies.AddRange(ships); } }
private void UpdateEnemyBehavior(Enemy enemy) { //enemy.MovementEngine.PathFinding.GameObjects = gameObjects; Vector2 movement = Vector2.Zero; //var allies = new List<Character>(); //allies.AddRange(AliveNpcs); //allies.Add(Player); //if (enemy.ActionState != ActionState.Attacking) //{ // //If creature has target, check behavior // //if no target, wander til target is found // foreach (ActionBehavior action in enemy.Behavior.Actions) // { // bool isValid = false; // Character target = null; // #region Check if condition is valid // switch (action.Condition.Target) // { // case ConditionTarget.Self: // target = enemy; // break; // case ConditionTarget.Enemy: // if (allies.Count > 0) // target = allies[0]; // break; // case ConditionTarget.Friend: // List<Enemy> targets = AliveEnemies.FindAll(x => x.HealthPercentage <= action.Condition.Value); // if (targets.Count > 0) target = targets[0]; // break; // } // if (action.Condition.Target == ConditionTarget.Friend) // { // switch (action.Condition.Type) // { // case ConditionType.HealthLower: // isValid = AliveEnemies.FindAll(x => x.Stats.Hp <= action.Condition.Value).Count > 0; // break; // case ConditionType.HealthLowerPC: // isValid = // AliveEnemies.FindAll(x => x.HealthPercentage <= action.Condition.Value).Count > 0; // break; // case ConditionType.HealthHigher: // isValid = AliveEnemies.FindAll(x => x.Stats.Hp >= action.Condition.Value).Count > 0; // break; // case ConditionType.HealthHigherPC: // isValid = // AliveEnemies.FindAll(x => x.HealthPercentage >= action.Condition.Value).Count > 0; // break; // case ConditionType.None: // isValid = true; // break; // } // } // else // { // //if (target != null) // //{ // // switch (action.Condition.Type) // // { // // case ConditionType.HealthLower: // // isValid = target.Stats.Hp <= action.Condition.Value; // // break; // // case ConditionType.HealthLowerPC: // // isValid = target.HealthPercentage <= action.Condition.Value; // // break; // // case ConditionType.HealthHigher: // // isValid = target.Stats.Hp >= action.Condition.Value; // // break; // // case ConditionType.HealthHigherPC: // // isValid = target.HealthPercentage >= action.Condition.Value; // // break; // // case ConditionType.None: // // isValid = true; // // break; // // } // //} // } // #endregion // if (isValid) // { // switch (action.ActionType) // { // case ActionType.Attack: // break; // case ActionType.Skill: // if (target != null) // CastEnemySkill(enemy, enemy.Skills.Find(x => x.Id == action.SkillId), target); // break; // case ActionType.Behavior: // enemy.Behavior.BehaviorType = action.NewBehavior; // break; // } // } // } // if (enemy.HasTarget && enemy.Target.IsAlive) // { // #region Check behavior // if (enemy.Behavior.BehaviorType == BehaviorType.Scared) // { // if (!enemy.IsInAttackRange(enemy.Target)) // { // movement = Steering.KeepDistance(enemy, enemy.Target, 200); // //if (enemy.DistanceToTarget > enemy.MaxTargetDistance) // // enemy.Target = null; // } // } // else if (enemy.Behavior.BehaviorType == BehaviorType.Agressive) // { // if (enemy.HasTarget && !enemy.IsInAttackRange(enemy.Target)) // { // movement = Steering.Seek(enemy, enemy.Target, enemy.CurrentWeapon.AttackRange); // //if (enemy.DistanceToTarget > enemy.MaxTargetDistance) // // enemy.Target = null; // } // } // else if (enemy.Behavior.BehaviorType == BehaviorType.Coward) // { // movement = Steering.KeepDistance(enemy, Player, 500); // } // else if (enemy.Behavior.BehaviorType == BehaviorType.Passive) // { // enemy.Behavior.Wandering(); // } // #endregion // } // else // Wander // { // Character target = // allies.Find(x => x.IsAlive && (enemy.CanHear(x) || enemy.IsInVisionRange(x.Position))); // //gameObjects.Find(delegate(GameObject o) { return (o is Character) && ((o as Character).IsPlayer || (o is Npc && (o as Npc).IsAlly)); }) as Character; // if (target != null) // { // enemy.Target = target; // } // else // { // enemy.Behavior.Wandering(); // } // } // MOVE CREATURE TO TARGET LOCATION // WILL BE OVERRIDEN BY THE FLEEING OR CHASING BEHAVIOR if ((enemy.Position - enemy.CurrentTargetPosition).Length() < enemy.CollisionRadius * 5) { enemy.NewTargetPosition(Game.CurrentMap); } movement = Steering.Seek(enemy, enemy.CurrentTargetPosition); #region Idle switch (enemy.Behavior) { case Enemy.BehaviorType.Fleeing: var fleeMovement = Steering.Flee(enemy, Player, 200); if (fleeMovement != Vector2.Zero) { movement = fleeMovement; } break; case Enemy.BehaviorType.Wandering: var softFlee = Steering.Flee(enemy, Player, 150f); if (softFlee != Vector2.Zero) movement = softFlee; break; } if (movement != Vector2.Zero) { enemy.Move(movement); } //else if (enemy.MovementEngine.CurrentWaypoint == null && enemy.ActionState != ActionState.Attacking) //{ // Log.ScreenLog.Add(string.Format("{0}. {1} going idle", enemy.UniqueObjectId, enemy.Name)); // enemy.Idle(); // if (enemy.HasTarget) // { // enemy.Face(enemy.Target); // } //} #endregion //} //if (enemy.HasTarget && enemy.MeleeReady && enemy.CanAttackCharacter(enemy.Target)) //{ // if (enemy.IsInAttackRange(enemy.Target)) // { // enemy.StartAttack(); // attackQueue.Add(new MeleeAttack(enemy, enemy.Target, enemy.AttackDelay, null)); // } //} }
private void SpawnHealthKits(int amount, Vector2 sourcePosition) { var kits = new List<Enemy>(); Random r = new Random(DateTime.Now.Millisecond); var tileList = new List<Vector2>(); var spawnRange = 50; for (int i = 0; i < amount; i++) { var position = new Vector2(r.Next((int)sourcePosition.X - spawnRange, (int)sourcePosition.X + spawnRange), r.Next((int)sourcePosition.Y - spawnRange, (int)sourcePosition.Y + spawnRange)); while (tileList.Contains(position)) { position = new Vector2(r.Next((int)sourcePosition.X - spawnRange, (int)sourcePosition.X + spawnRange), r.Next((int)sourcePosition.Y - spawnRange, (int)sourcePosition.Y + spawnRange)); } tileList.Add(position); var kit = new Enemy() { CharSprite = new SpriteAnimation(GameGraphics.HealthKit, 300, 4), Position = position, Behavior = Enemy.BehaviorType.Wandering, StepSize = 2, PlayerKillable = true, CollisionRadius = 20, Priority = 0, UniqueObjectId = 3, Name = "HealthKit", Damage = 0, Points = 5, Heal = 10 }; kit.NewTargetPosition(Game.CurrentMap); kits.Add(kit); } Game.CurrentMap?.MapEnemies.AddRange(kits); }
private void SpawnEnemy(Enemy deadEnemy) { if (deadEnemy.UniqueObjectId == 1) { SpawnCreepShips(1); } else if (deadEnemy.UniqueObjectId == 2) { SpawnBigShips(1); } else if (deadEnemy.UniqueObjectId == 4) { SpawnCreepShips2(1); } }
public static Vector2 KeepDistance(Enemy creature, Character character, int distance) { Vector2 steeringForce = Vector2.Zero; if ((creature.Position - character.Position).Length() < distance) { steeringForce += Flee(creature, character); if (steeringForce != Vector2.Zero) steeringForce.Normalize(); } return steeringForce; }