public static IGameObject Collide(IGameObject collider, IEnumerable <IGameObject> collisionItems) { foreach (var item in collisionItems) { Rectangle obj = GfxHandler.GetBBox(item); if (((item is ICollidable && ((ICollidable)item).CanCollide) || item is IPickable) && GfxHandler.GetBBox(collider).Intersects(obj)) { return(item); } } return(null); }
public static void AiCollide(IGameObject collider, ICharacter character) { Rectangle colliderBBox = GfxHandler.GetBBox(collider); Rectangle charBBox = GfxHandler.GetBBox(character); if (colliderBBox.Intersects(charBBox)) { character.TakeDamage((ICharacter)collider); // Console.Beep(3000, 49); if (character.CurrentHealth < 0) { // guess =) } } }
public static void FindPath(ICharacter player, ICharacter ai) { if (ai.CurrentHealth < 0) { return; } Vector2 playerCenter = new Vector2(player.Position.X - 16, player.Position.Y - 16); var enemyAi = (Enemy)ai; if ((ai as Enemy).IsAggroed == false) { Enemy vrag = ai as Enemy; Rectangle vragBbox = GfxHandler.GetBBox(vrag); Rectangle playerBbox = GfxHandler.GetBBox(player); ISpawnLocation spawnLocation = vrag.SpawnLocation; Rectangle spawnBbox = spawnLocation.InfalateBBox(); Rectangle aggroBox = vragBbox; aggroBox.Inflate(100, 100); if (vrag.Direction == NotSet) { // Random initial movement direction. vrag.Direction = (MoveDirection)GlobalVariables.Rng.Next(1, 5); } if (aggroBox.Intersects(playerBbox)) { vrag.IsAggroed = true; } if (vragBbox.Intersects(spawnBbox) && CollisionHandler.Collide(ai, ai.Level.Assets) == null) { if (vrag.Direction == Down) { MovementHandler.MoveDown(ai, 0.5f); } else if (vrag.Direction == Up) { MovementHandler.MoveUp(ai, 0.5f); } else if (vrag.Direction == Left) { MovementHandler.MoveLeft(ai, 0.5f); } else if (vrag.Direction == Right) { MovementHandler.MoveRight(ai, 0.5f); } } else { if (vrag.Direction == Down) { MovementHandler.MoveUp(ai, 2f); enemyAi.Direction = Up; } else if (vrag.Direction == Up) { MovementHandler.MoveDown(ai, 2f); enemyAi.Direction = Down; } else if (vrag.Direction == Right) { MovementHandler.MoveLeft(ai, 2f); enemyAi.Direction = Left; } else if (vrag.Direction == Left) { MovementHandler.MoveRight(ai, 2f); enemyAi.Direction = Right; } } return; } if (Math.Abs(ai.Position.X - playerCenter.X) > Tolerance && Math.Abs(ai.Position.Y - playerCenter.Y) > Tolerance) { if (GlobalVariables.Rng.Next(1, 3) == 1) { if (ai.Position.X < playerCenter.X) { MovementHandler.MoveRight(ai); enemyAi.Direction = Right; } else if (ai.Position.X > playerCenter.X) { MovementHandler.MoveLeft(ai); enemyAi.Direction = Left; } //else if(ai.Position.X < player.Position.X +10 || ai.Position.X > player.Position.X-10) //{ if (ai.Position.Y < playerCenter.Y) { MovementHandler.MoveDown(ai); enemyAi.Direction = Down; } else if (ai.Position.Y > playerCenter.Y) { MovementHandler.MoveUp(ai); enemyAi.Direction = Up; } // } } } else { if (ai.Position.Y < playerCenter.Y) { MovementHandler.MoveDown(ai); enemyAi.Direction = Down; } else if (ai.Position.Y > playerCenter.Y) { MovementHandler.MoveUp(ai); enemyAi.Direction = Up; } if (ai.Position.X < playerCenter.X) { MovementHandler.MoveRight(ai); enemyAi.Direction = Right; } else if (ai.Position.X > playerCenter.X) { MovementHandler.MoveLeft(ai); enemyAi.Direction = Left; } } }
public static void PlayerReaction(ICharacter character, Keys key) { IEnumerable <IGameObject> collisionItems = character.Level.Assets.Concat(character.Level.Enemies).ToList(); var collider = Collide(character, collisionItems); if (collider != null) { Vector2 temp = new Vector2(character.Position.X, character.Position.Y); // movement reactions if (collider is ICollidable && !(collider is IKillable)) { if (key == Keys.D) { temp.X -= character.Speed; character.Position = temp; } if (key == Keys.W) { temp.Y += character.Speed; character.Position = temp; } if (key == Keys.S) { temp.Y -= character.Speed; character.Position = temp; } if (key == Keys.A) { temp.X += character.Speed; character.Position = temp; } } if (collider is IPickable) { var item = (IPickable)collider; // legit cool gold-pickup sound if ((character as Player).TryToPick(item)) { item.PickedUpBy(character); Console.Beep(8000, 50); } var healingItem = item as IHeals; healingItem?.HealCharacter(character); var weaponItem = item as IWeapon; weaponItem?.EquipCharacter(character); } } IGameObject door = character.Level.Assets.Find(a => a.Type.Contains("Door")); if (door != null) { Rectangle doorBox = GfxHandler.GetBBox(door); Rectangle characterBox = GfxHandler.GetBBox(character); if (doorBox.Intersects(characterBox)) { character.Level = LevelFactory.MakeLevel(character); GfxHandler.ClearEffects(); } } }
public static void MoveRight(ICharacter character, float speedModifier = 1f) { if (character.Position.X + character.Speed <= GlobalVariables.WindowWidthDefault - GfxHandler.GetWidth(character)) { character.Position = new Vector2(character.Position.X + (character.Speed * speedModifier), character.Position.Y); } }
public static void MoveDown(ICharacter character, float speedModifier = 1f) { if (character.Position.Y + character.Speed <= GlobalVariables.WindowHeightDefault - GfxHandler.GetHeight(character)) { character.Position = new Vector2(character.Position.X, character.Position.Y + (character.Speed * speedModifier)); } }