Esempio n. 1
0
 public static Direction DetermineDirectionCautious(Monster m, World w)
 {
     Direction result;
     if (IsCautious(m, w))
         {
         bool alterDirection = m.Direction != Direction.None && (MonsterRandom.Next(256) & 3) == 0;
         if (alterDirection)
             result = AlterDirection(m.Direction);
         else
             {
             var d = DetermineDirectionTowardsPlayer(m, w);
             result = (d == Direction.None) ? RandomDirection() : d.Reversed();
             }
         }
     else
         {
         if ((MonsterRandom.Next(256) & 7) == 0)
             {
             result = RandomDirection();
             }
         else
             {
             result = DetermineDirectionTowardsPlayer(m, w);
             if (result == Direction.None)
                 result = RandomDirection();
             }
         }
     return result;
 }
Esempio n. 2
0
 public static Direction DetermineDirectionSemiAggressive(Monster m, World w)
 {
     bool makeAggressiveMove = (MonsterRandom.Next(256) & 1) == 0;
     if (m.ChangeRooms != ChangeRooms.FollowsPlayer)
         makeAggressiveMove &= IsPlayerInSameRoomAsMonster(m, w);
     var shouldFollowPlayer = makeAggressiveMove && IsPlayerInSight(m, w);
     var result = shouldFollowPlayer ? DetermineDirectionTowardsPlayer(m, w) : RandomDirection();
     return result;
 }
Esempio n. 3
0
        public static Direction DetermineDirectionRolling(Monster m, World w)
        {
            if (m.Direction == Direction.None)
                return RandomDirection();

            bool changeDirection = (MonsterRandom.Next(256) & 7) == 0;
            var result = changeDirection ? AlterDirection(m.Direction) : DetermineDirectionStandardPatrolling(m, w);
            return result;
        }
Esempio n. 4
0
        public static Direction DetermineDirectionStandardPatrolling(Monster m, World w)
        {
            if (m.Direction == Direction.None)
                throw new InvalidOperationException("Direction must be set for patrolling.");

            TilePos tp = m.TilePosition;
            TilePos pp = tp.GetPositionAfterOneMove(m.Direction);
            bool isCurrentlyMovingTowardsFreeSpace = w.CanTileBeOccupied(pp, true);
            Vector2 potentiallyMovingTowards = pp.ToPosition();
            bool isInSameRoom = IsInSameRoom(m.Position, potentiallyMovingTowards);
            bool canContinueMovingInTheSameDirection = isCurrentlyMovingTowardsFreeSpace && isInSameRoom;
            var result = canContinueMovingInTheSameDirection ? m.Direction : m.Direction.Reversed();
            System.Diagnostics.Trace.WriteLine(string.Format("{0} {1} {2} {3} {4}", tp, pp, isCurrentlyMovingTowardsFreeSpace, isInSameRoom, result));
            return result;
        }
Esempio n. 5
0
        public static Direction DetermineDirectionFullPursuit(Monster m, World w)
        {
            Direction result = DetermineDirectionTowardsPlayer(m, w);
            if (result == Direction.None)
                result = RandomDirection();

            result = UpdateDirectionWhereMovementBlocked(m, w, result);

            Vector2 potentiallyMovingTowards = m.TilePosition.GetPositionAfterOneMove(result).ToPosition();
            Vector2 diff = (potentiallyMovingTowards - w.Player.Position) / Tile.Size;
            float tilesToPlayer = Math.Min(Math.Abs(diff.X), Math.Abs(diff.Y));
            if (tilesToPlayer <= 2)
                result = AlterDirection(result);

            return result;
        }
        public MovingItemAndMovingItemInteraction(World world, MovingItem movingItem1, MovingItem movingItem2)
        {
            if (world == null)
                throw new ArgumentNullException("world");
            if (movingItem1 == null)
                throw new ArgumentNullException("movingItem1");
            if (movingItem2 == null)
                throw new ArgumentNullException("movingItem2");
            if (movingItem1 is Shot)
                throw new ArgumentOutOfRangeException("movingItem1");
            if (movingItem2 is Shot)
                throw new ArgumentOutOfRangeException("movingItem2");

            this._world = world;
            var items = new[] {movingItem1, movingItem2};
            this._player = items.OfType<Player>().SingleOrDefault();
            this._boulder = items.OfType<Boulder>().SingleOrDefault();
            this._mine = items.OfType<Mine>().SingleOrDefault();
            this._monster1 = items.OfType<Monster.Monster>().FirstOrDefault();
            this._monster2 = items.OfType<Monster.Monster>().Skip(1).FirstOrDefault();

            this._moveableObject = items.FirstOrDefault(item => item.Solidity == ObjectSolidity.Moveable);
            this._insubstantialObject = items.FirstOrDefault(item => item.Solidity == ObjectSolidity.Insubstantial);
        }
        private static bool ShotHitsMonster(World world, Shot shot, Monster.Monster monster)
        {
            if (!monster.IsActive)
                monster.IsActive = true;

            if (monster.Mobility == MonsterMobility.Patrolling)
                monster.Mobility = MonsterMobility.Placid;

            var standardShot = shot as StandardShot;
            if (standardShot != null && monster.ShotsBounceOff)
                {
                if (!standardShot.HasRebounded)
                    standardShot.Reverse();
                return false;
                }

            var score = ((Math.Min(shot.Energy, monster.Energy) >> 1) + 1)*10;
            world.IncreaseScore(score);
            monster.ReduceEnergy(shot.Energy);
            if (monster.IsAlive())
                {
                var sound = monster.IsEgg ? GameSound.PlayerShootsAndInjuresEgg : GameSound.PlayerShootsAndInjuresMonster;
                world.Game.SoundPlayer.Play(sound);
                }
            world.ConvertShotToBang(shot);
            return true;
        }
Esempio n. 8
0
 public void AddMonster(Monster.Monster m)
 {
     this.GameObjects.Add(m);
 }
Esempio n. 9
0
 private static Direction DetermineDirectionTowardsPlayer(Monster m, World w)
 {
     Vector2 diff = (m.Position - w.Player.Position);
     double hMove = MonsterRandom.NextDouble() * Math.Abs(diff.X);
     double vMove = MonsterRandom.NextDouble() * Math.Abs(diff.Y);
     Direction result;
     if (hMove > vMove)
         {
         result = diff.X > 0 ? Direction.Left : Direction.Right;
         }
     else
         {
         result = diff.Y > 0 ? Direction.Up : Direction.Down;
         }
     return result;
 }
Esempio n. 10
0
        public static Direction UpdateDirectionWhereMovementBlocked(Monster m, World w, Direction d)
        {
            Direction intendedDirection = d;

            TilePos tp = m.TilePosition;
            do
                {
                TilePos potentiallyMovingTowardsTile = tp.GetPositionAfterOneMove(d);
                Vector2 potentiallyMovingTowards = potentiallyMovingTowardsTile.ToPosition();

                if (m.ChangeRooms == ChangeRooms.StaysWithinRoom && !IsInSameRoom(m.Position, potentiallyMovingTowards))
                    {
                    d = GetNextDirection(d);
                    continue;
                    }

                if (w.CanTileBeOccupied(potentiallyMovingTowardsTile, true))
                    {
                    return d;
                    }

                d = GetNextDirection(d);
                } while (d != intendedDirection);

            return Direction.None;
        }
Esempio n. 11
0
 public static Direction RandomDirection(Monster m, World w)
 {
     var result = RandomDirection();
     return result;
 }
Esempio n. 12
0
 public static bool IsPlayerInSight(Monster m, World w)
 {
     Vector2 diff = (m.Position - w.Player.Position) / Tile.Size;
     bool result;
     if (Math.Abs(diff.X) >= Math.Abs(diff.Y))
         {
         float tilesDistance = Math.Abs(diff.X) / Tile.Width;
         result = tilesDistance <= World.WindowSizeX * 2;
         }
     else
         {
         float tilesDistance = Math.Abs(diff.Y) / Tile.Height;
         result = tilesDistance <= World.WindowSizeY * 2;
         }
     return result;
 }
Esempio n. 13
0
        public static bool IsPlayerInSameRoomAsMonster(Monster m, World w)
        {
            Player p = w.Player;
            if (!p.IsExtant)
                return false;

            var result = IsInSameRoom(p.Position, m.Position);
            return result;
        }
Esempio n. 14
0
        private static Direction DetermineDirectionAggressive(Monster m, World w)
        {
            TilePos tp = m.TilePosition;
            bool isCurrentlyMovingTowardsFreeSpace;
            if (m.Direction == Direction.None)
                isCurrentlyMovingTowardsFreeSpace = false;
            else
                {
                TilePos pp = tp.GetPositionAfterOneMove(m.Direction);
                isCurrentlyMovingTowardsFreeSpace = w.CanTileBeOccupied(pp, true);

                if (isCurrentlyMovingTowardsFreeSpace)
                    {
                    Vector2 newPos = pp.ToPosition();
                    if (!MonsterMovement.IsInSameRoom(m.Position, newPos) && MonsterRandom.Next(4) != 0)
                        isCurrentlyMovingTowardsFreeSpace = false;
                    }
                }

            Direction newDirection = Direction.None;
            if (w.Player != null)
                {
                TilePos playerPosition = w.Player.TilePosition;
                int yDiff = tp.Y - playerPosition.Y;
                int xDiff = tp.X - playerPosition.X;

                if (xDiff > 16 || yDiff > 16)
                    {
                    // player is out of sight
                    }
                else if (m.Direction == Direction.Left || m.Direction == Direction.Right)
                    {
                    // Check if travelling on same row as player
                    if (yDiff == 0 && ShouldMakeMove())
                        {
                        // Move aside
                        newDirection = MonsterRandom.Next(2) == 0 ? Direction.Up : Direction.Down;
                        }
                    else if ((m.Direction == Direction.Left && xDiff <= -5) || (m.Direction == Direction.Right && xDiff >= 5))
                        {
                        newDirection = yDiff > 0 ? Direction.Up : Direction.Down;
                        }
                    }
                else if (m.Direction == Direction.Up || m.Direction == Direction.Down)
                    {
                    if (xDiff == 0 && ShouldMakeMove())
                        {
                        // Move aside
                        newDirection = MonsterRandom.Next(2) == 0 ? Direction.Left : Direction.Right;
                        }
                    else if ((m.Direction == Direction.Up && yDiff <= -5) || (m.Direction == Direction.Down && yDiff >= 5))
                        {
                        newDirection = xDiff > 0 ? Direction.Left : Direction.Right;
                        }
                    }

                if (newDirection != Direction.None)
                    {
                    TilePos pp = tp.GetPositionAfterOneMove(newDirection);
                    if (!w.CanTileBeOccupied(pp, true))
                        newDirection = Direction.None;
                    }
                }

            if (newDirection == Direction.None)
                {
                if (m.Direction == Direction.None)
                    {
                    newDirection = MonsterMovement.RandomDirection(m, w);
                    }
                else if (!isCurrentlyMovingTowardsFreeSpace)
                    {
                    // tend to bounce backwards
                    newDirection = MonsterRandom.Next(5) == 0 ? MonsterMovement.GetRandomPerpendicularDirection(m.Direction) : m.Direction.Reversed();
                    }
                else if (MonsterRandom.Next(16) == 0)
                    {
                    newDirection = MonsterMovement.GetRandomPerpendicularDirection(m.Direction);
                    }
                else
                    {
                    newDirection = m.Direction;
                    }
                }
            return newDirection;
        }
Esempio n. 15
0
 private static bool IsCautious(Monster m, World w)
 {
     int compareTo = m.Energy << 2;
     bool result = w.Player.Energy > compareTo;
     return result;
 }
Esempio n. 16
0
        private static Direction DetermineDirectionAggressive(Monster m, World w, Func<bool> shouldMakeAnAggressiveMove)
        {
            var p = w.Player;
            if (p == null || !p.IsExtant)
                {
                return MonsterMovement.DetermineDirectionRolling(m, w);
                }

            TilePos tp = m.TilePosition;
            /*            bool isCurrentlyMovingTowardsFreeSpace;
            if (this.Direction == Direction.None)
                isCurrentlyMovingTowardsFreeSpace = false;
            else
                {
                TilePos pp = TilePos.GetPositionAfterOneMove(tp, this.Direction);
                isCurrentlyMovingTowardsFreeSpace = this._world.IsTileUnoccupied(pp, true);

                if (isCurrentlyMovingTowardsFreeSpace)
                    {
                    Vector2 newPos = World.GetBounds(pp).GetCentre();
                    if (!IsInSameRoom(this.Position, newPos) && MonsterRandom.Next(4) != 0)
                        isCurrentlyMovingTowardsFreeSpace = false;
                    }
                }
            */
            Direction newDirection = Direction.None;
            TilePos playerPosition = p.TilePosition;
            int yDiff = tp.Y - playerPosition.Y;
            int xDiff = tp.X - playerPosition.X;
            if (m.Direction == Direction.Left || m.Direction == Direction.Right)
                {
                if (Math.Abs(tp.X - playerPosition.X) == 1 && Math.Abs(yDiff) > 3 && Math.Abs(yDiff) <= 16 && shouldMakeAnAggressiveMove())
                    {
                    newDirection = yDiff > 0 ? Direction.Up : Direction.Down;
                    }
                else if ((tp.X - playerPosition.X) == 0 && Math.Abs(yDiff) <= 3 && shouldMakeAnAggressiveMove())
                    {
                    newDirection = yDiff > 0 ? Direction.Up : Direction.Down;
                    }
                }
            else if (m.Direction == Direction.Up || m.Direction == Direction.Down)
                {
                if (Math.Abs(tp.Y - playerPosition.Y) == 1 && Math.Abs(xDiff) > 3 && Math.Abs(xDiff) <= 16 && shouldMakeAnAggressiveMove())
                    {
                    newDirection = xDiff > 0 ? Direction.Left : Direction.Right;
                    }
                else if ((tp.Y - playerPosition.Y) == 0 && Math.Abs(xDiff) <= 3 && shouldMakeAnAggressiveMove())
                    {
                    newDirection = xDiff > 0 ? Direction.Left : Direction.Right;
                    }
                }
            if (newDirection != Direction.None)
                {
                TilePos pp = tp.GetPositionAfterOneMove(newDirection);
                if (w.CanTileBeOccupied(pp, true))
                    {
                    return newDirection;
                    }
                }

            return MonsterMovement.DetermineDirectionRolling(m, w);
        }
Esempio n. 17
0
 public void AddDiamondDemon(Monster.Monster m)
 {
     var dd = new DiamondDemon(this, m.Position, 30)
                  {
                      Mobility = MonsterMobility.Aggressive,
                      LaysEggs = true,
                      ChangeRooms = ChangeRooms.FollowsPlayer,
                      MonsterShootBehaviour = MonsterShootBehaviour.ShootsImmediately
                  };
     this.GameObjects.Add(dd);
 }