Ejemplo n.º 1
0
        public UnitAction GetAction(Unit unit, Game game, Debug debug)
        {
            _game  = game;
            _unit  = unit;
            _debug = debug;
            if (_pointMaster == null)
            {
                _pointMaster = new PointMaster(ref game);
            }
            if (_pathFinder == null)
            {
                _pathFinder = new PathFinder(_pointMaster);
            }
            if (_rayTracer == null)
            {
                _rayTracer = new RayTracer(ref game);
            }
            _pointMaster.DebugDraw(debug);

            var action = new UnitAction();

            var enemy = _game.Units.First(u => u.PlayerId != unit.PlayerId);

            if (!_unit.Weapon.HasValue || unit.Weapon.Value.Typ == WeaponType.RocketLauncher)
            {
                action.SwapWeapon = true;
                var lootbox = _game.LootBoxes
                              .Where(lb => lb.Item is Item.Weapon)
                              .Select(lb => (lb,
                                             Math.Abs(lb.Position.X - unit.Position.X) + Math.Abs(lb.Position.Y - unit.Position.Y)))
                              .OrderBy(x => (x.Item1.Item as Item.Weapon).WeaponType == WeaponType.RocketLauncher ? x.Item2 * 100 : x.Item2)
                              .First().Item1;
                if (GoToTarget(lootbox.Position, lootbox.Size.X / 2, ref action))
                {
                    return(action);
                }
            }
            else
            {
                action.Aim = new Vec2Double(enemy.Position.X - _unit.Position.X, enemy.Position.Y - _unit.Position.Y);
                var hitProb = _rayTracer.PercentOfHit(ref _unit, Math.Atan2(action.Aim.Y, action.Aim.X), ref enemy);
                action.Shoot = hitProb > 0.5;
            }

            if (_unit.Health < _game.Properties.UnitMaxHealth && _game.LootBoxes.Count(x => x.Item is Item.HealthPack) > 0)
            {
                var hpkit = _game.LootBoxes
                            .Where(lb => lb.Item is Item.HealthPack)
                            .Select(lb => (lb,
                                           Math.Abs(lb.Position.X - unit.Position.X) + Math.Abs(lb.Position.Y - unit.Position.Y)))
                            .OrderBy(x => x.Item2)
                            .First().Item1;
                if (GoToTarget(hpkit.Position, hpkit.Size.X / 2, ref action))
                {
                    return(action);
                }
            }

            if (GoToTarget(enemy.Position, enemy.Size.X / 2, ref action))
            {
                return(action);
            }

            return(action);
        }