예제 #1
0
        private void RunUnitLifecycle(GameTime time)
        {
            var unitsToChangePlayer = new Dictionary <Players, List <IUnit> >();
            var unitsToRemove       = new List <IUnit>();

            foreach (var player in PlayerConstants.sPlayers)
            {
                if (!UnitsByPlayer.TryGetValue(player, out var nonBuildings))
                {
                    nonBuildings = new List <IUnit>(0);
                }
                foreach (var unit in nonBuildings.Concat(BuildingsByPlayer[player]))
                {
                    if (QueueForRemovalIfRequired(unit, unitsToRemove))
                    {
                        continue;
                    }

                    unit.Update(time);

                    QueueOwnershipChange(unit, player, unitsToChangePlayer);
                }

                UpdateResources(player);
            }

            ApplyOwnershipChange(unitsToChangePlayer);

            mCollision.Update();

            RemoveQueuedUnits(unitsToRemove);
        }
예제 #2
0
        public void AddUnit(Players playerId, Vector2 position, UnitTypes name, float rot = 0f, int health = 0)
        {
            IUnit unit;

            switch (name)
            {
            case UnitTypes.Arrow:
                unit = new Arrow(playerId, position, rot, mGridSize, position);
                break;

            case UnitTypes.Swordsman:
                unit = new Swordsman(playerId, position, rot, mGridSize);
                break;

            case UnitTypes.Cavalry:
                unit = new Cavalry(playerId, position, rot, mGridSize);
                break;

            case UnitTypes.Hero:
                var hero = new Hero(playerId, position, rot, mGridSize, mContent);
                unit = hero;
                HeroesByPlayer[playerId] = hero;
                break;

            case UnitTypes.Bowman:
                unit = new Bowman(playerId, position, rot, mGridSize);
                break;

            case UnitTypes.BatteringRam:
                unit = new BatteringRam(playerId, position, rot, mGridSize);
                break;

            default:
                return;
            }

            if (unit is IDamageableUnit damageableUnit && health != 0)
            {
                damageableUnit.Health = health;
            }

            if (!UnitsByPlayer.ContainsKey(playerId))
            {
                UnitsByPlayer.Add(playerId, new List <IUnit>());
            }

            UnitsByPlayer[playerId].Add(unit);
            if (unit.UnitType == UnitTypes.Hero || unit.UnitType == UnitTypes.Swordsman || unit.UnitType == UnitTypes.Cavalry ||
                unit.UnitType == UnitTypes.BatteringRam || unit.UnitType == UnitTypes.Bowman)
            {
                StatisticsByPlayer[playerId]["CreateTroops"] += 1;
            }
            if (!UnitsByModel.ContainsKey(unit.ModelType))
            {
                UnitsByModel.Add(unit.ModelType, new List <IUnit>());
            }

            UnitsByModel[unit.ModelType].Add(unit);
        }
예제 #3
0
        public void AddHero(Players playerId, Vector2 pos, float rot, int mana, int health, LevelManager levelManager, SkillManager skillManager)
        {
            var hero = new Hero(playerId, pos, rot, mGridSize, mana, health, levelManager, skillManager, mContent);

            HeroesByPlayer[playerId] = hero;
            if (!UnitsByPlayer.ContainsKey(playerId))
            {
                UnitsByPlayer.Add(playerId, new List <IUnit>());
            }

            UnitsByPlayer[playerId].Add(hero);
            StatisticsByPlayer[playerId]["CreateTroops"] += 1;
            if (!UnitsByModel.ContainsKey(hero.ModelType))
            {
                UnitsByModel.Add(hero.ModelType, new List <IUnit>());
            }

            UnitsByModel[hero.ModelType].Add(hero);
        }