コード例 #1
0
        public bool AddBuilding(Players playerId, Vector2 position, UnitTypes name, float rotation = 0f, int health = 0)
        {
            ABuilding unit;

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

            case UnitTypes.Wall:
                unit = new Wall(playerId, position, rotation, mGridSize);
                break;

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

            case UnitTypes.Village:
                unit = new Village(playerId, position, rotation, mGridSize);
                VillagePos.Add(playerId, position);
                VillagesByPlayer.Add(playerId, (Village)unit);
                break;

            case UnitTypes.GoldMine:
                unit = new Mine(playerId, position, rotation, true, mGridSize, Resources);
                break;

            case UnitTypes.Quarry:
                unit = new Mine(playerId, position, rotation, false, mGridSize, Resources);
                break;

            case UnitTypes.Rock:
                unit = new Rock(position, mGridSize, rotation);
                break;

            default:
                return(false);
            }

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

            var blockedFields  = unit.BlockedFields();
            var passableFields = unit.PassableFields();

            if (AreFieldsOccupied(blockedFields, passableFields))
            {
                return(false);
            }

            ChangeFieldState(TileStates.Blocked, blockedFields);
            ChangeFieldState(TileStates.Passable, passableFields);

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

            if ((unit.UnitType == UnitTypes.GoldMine || unit.UnitType == UnitTypes.Quarry ||
                 unit.UnitType == UnitTypes.SacredStar) && !BuildingsByPlayer[Players.Global].Contains(unit))
            {
                BuildingsByPlayer[Players.Global].Add(unit);
            }
            BuildingsByPlayer[playerId].Add(unit);
            if (!UnitsByModel.ContainsKey(unit.ModelType))
            {
                UnitsByModel.Add(unit.ModelType, new List <IUnit>());
            }

            UnitsByModel[unit.ModelType].Add(unit);

            return(true);
        }