コード例 #1
0
ファイル: RLAIService.cs プロジェクト: sambeaven/Rougelike
        public Tuple<int, int> moveAway(RLMonster monster, RLHero hero)
        {
            //work out differences to find the axis with the greatest difference. We'll move along that axis.

            int x = monster.locationX, y = monster.locationY;

            int xDiff = monster.locationX - hero.locationX;
            int yDiff = monster.locationY - hero.locationY;

            if (xDiff < 0)
            {
                xDiff = xDiff * -1;
            }

            if (yDiff < 0)
            {
                yDiff = yDiff * -1;
            }

            if (xDiff >= yDiff)
            {
                if (hero.locationX > monster.locationX)
                {
                    x--;
                }
                else
                {
                    x++;
                }
            }
            else
            {
                if (hero.locationY > monster.locationY)
                {
                    y--;
                }
                else
                {
                    y++;
                }
            }

            return new Tuple<int, int>(x, y);
        }
コード例 #2
0
ファイル: RLGame.cs プロジェクト: sambeaven/Rougelike
        private void PlaceMonster(RLMap map, RLMonster monster)
        {
            Tuple<int, int> destination = null;

            //if monster can see hero, move according to monster behaviour
            if (aiService.CanSeeEachOther(monster, hero, map))
            {
                switch (monster.monsterBehaviour)
                {
                    case RLMonster.MonsterBehaviour.aggressive:
                        destination = aiService.moveTowards(monster, hero);
                        break;
                    case RLMonster.MonsterBehaviour.passive:
                        destination = aiService.moveRandom(monster);
                        break;
                    case RLMonster.MonsterBehaviour.cowardly:
                        destination = aiService.moveAway(monster, hero);
                        break;
                    default:
                        break;
                }
            }
            else //otherwise, move at random
            {
                destination = aiService.moveRandom(monster);
            }

            if (destination != null)
            {
                if (destination.Item1 == hero.locationX && destination.Item2 == hero.locationY)
                {
                    //redraw in the same location
                    renderer.DrawAgent(map, monster, monster.locationX, monster.locationY);
                    //attack hero
                    var attackResults = hero.attackedBy(monster, dice);
                    foreach (var attackMessage in attackResults)
                    {
                        messages.Push(new Tuple<ConsoleColor, string>(ConsoleColor.Red, attackMessage));
                    }
                }
                else if (map.isLocationPassable(destination.Item1, destination.Item2))
                {
                    renderer.DrawAgent(map, monster, destination.Item1, destination.Item2);
                    monster.locationX = destination.Item1;
                    monster.locationY = destination.Item2;
                }
                else
                {
                    //redraw in the same location
                    renderer.DrawAgent(map, monster, monster.locationX, monster.locationY);
                }
            }
        }