Beispiel #1
0
        /// <summary>
        /// Performs the execution of the <see cref="BuyDecorationCommand"/> on the specified <see cref="Avatar"/>.
        /// </summary>
        /// <param name="level"><see cref="Level"/> on which to perform the <see cref="BuyDecorationCommand"/>.</param>
        /// <exception cref="ArgumentNullException"><paramref name="level"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="level.Village"/> is null.</exception>
        public override void Execute(Level level)
        {
            ThrowIfLevelNull(level);
            ThrowIfLevelVillageNull(level);

            var dataRef         = new CsvDataRowRef <DecorationData>(DecorationDataID);
            var assets          = level.Assets;
            var tableCollection = assets.DataTables;
            var row             = dataRef.Get(tableCollection);

            if (row == null)
            {
                level.Logs.Log($"Unable to find CsvDataRow<DecorationData> for data ID {DecorationDataID}.");
            }
            else
            {
                // Use the first level.
                var data = row[0];
                if (data == null)
                {
                    level.Logs.Log($"Unable to find DecorationData of level 0 for data ID {DecorationDataID}.");
                }
                else
                {
                    level.Avatar.UseResource(data.BuildResource, data.BuildCost);

                    var deco = new Decoration(level.Village, data);
                    deco.X = X;
                    deco.Y = Y;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Performs the execution of the <see cref="BuyTrapCommand"/> on the specified <see cref="Avatar"/>.
        /// </summary>
        /// <param name="level"><see cref="Level"/> on which to perform the <see cref="BuyTrapCommand"/>.</param>
        /// <exception cref="ArgumentNullException"><paramref name="level"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="level.Village"/> is null.</exception>
        public override void Execute(Level level)
        {
            ThrowIfLevelNull(level);
            ThrowIfLevelVillageNull(level);

            var dataRef         = new CsvDataRowRef <TrapData>(TrapDataID);
            var assets          = level.Assets;
            var tableCollection = assets.DataTables;
            var row             = dataRef.Get(tableCollection);

            if (row == null)
            {
                level.Logs.Log($"Unable to find CsvDataRow<TrapData> for data ID {TrapDataID}.");
            }
            else
            {
                // Use the first level.
                var data = row[0];
                if (data == null)
                {
                    level.Logs.Log($"Unable to find TrapData of level 0 for data ID {TrapDataID}.");
                }
                else
                {
                    level.Avatar.UseResource(data.BuildResource, data.BuildCost);

                    var trap = new Trap(level.Village, data);
                    trap.X = X;
                    trap.Y = Y;
                    trap.BeginConstruction(Tick);
                }
            }
        }
        /// <summary>
        /// Updates the <see cref="VillageObject{TCsvData}.Data"/> associated with this <see cref="Buildable{TCsvData}"/> using
        /// <see cref="VillageObject.Assets"/>, the specified data ID and level.
        /// </summary>
        /// <param name="dataId"></param>
        /// <param name="level"></param>
        protected virtual void UpdateData(int dataId, int level)
        {
            Debug.Assert(level >= NotConstructedLevel, "Level was less than NotConstructedLevel.");

            // If we haven't cached the CsvDataRow in which Data
            // is found, we do it.
            if (RowCache == null)
            {
                var tableCollections = Assets.DataTables;
                var dataRef          = new CsvDataRowRef <TCsvData>(dataId);
                var row = dataRef.Get(tableCollections);
                if (row == null)
                {
                    throw new InvalidOperationException("Could not find CsvDataRow with ID '" + dataId + "'.");
                }

                _rowCache = row;
            }

            if (RowCache.Name == "Town Hall")
            {
                Village._townhall = this as Building;
            }
            else if (RowCache.Name == "Worker Building")
            {
                Village.WorkerManager._totalWorkers++;
            }

            // Data is null when lvl is -1
            // However NextUpgrade should not.
            if (level == NotConstructedLevel)
            {
                _data = null;
            }
            else
            {
                _data = RowCache[level];
                if (_data == null)
                {
                    throw new InvalidOperationException("Could not find CsvData with ID '" + dataId + "' and with level '" + level + "'.");
                }
            }

            _upgradeLevel = level;
        }
Beispiel #4
0
        /// <summary>
        /// Performs the execution of the <see cref="BuyBuildingCommand"/> on the specified <see cref="Avatar"/>.
        /// </summary>
        /// <param name="level"><see cref="Level"/> on which to perform the <see cref="BuyBuildingCommand"/>.</param>
        /// <exception cref="ArgumentNullException"><paramref name="level"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="level.Village"/> is null.</exception>
        public override void Execute(Level level)
        {
            ThrowIfLevelNull(level);
            ThrowIfLevelVillageNull(level);

            var dataRef         = new CsvDataRowRef <BuildingData>(BuildingDataID);
            var assets          = level.Assets;
            var tableCollection = assets.DataTables;
            var row             = dataRef.Get(tableCollection);

            if (row == null)
            {
                level.Logs.Log($"Unable to find CsvDataRow<BuildingData> for data ID {BuildingDataID}.");
            }
            else
            {
                // Use the first level.
                var data = row[0];
                if (data == null)
                {
                    level.Logs.Log($"Unable to find BuildingData of level 0 for data ID {BuildingDataID}.");
                }
                else
                {
                    var globalsTable = assets.DataTables.GetTable <GlobalData>();
                    var resource     = data.BuildResource;
                    var cost         = data.BuildCost;

                    var workerManager = level.Village.WorkerManager;
                    if (data.Name == "Worker Building" && workerManager.TotalWorkers > 0)
                    {
                        var wgloRow = (CsvDataRow <GlobalData>)null;
                        if (workerManager.TotalWorkers == 1)
                        {
                            wgloRow = globalsTable.Rows["WORKER_COST_2ND"];
                        }
                        else if (workerManager.TotalWorkers == 2)
                        {
                            wgloRow = globalsTable.Rows["WORKER_COST_3RD"];
                        }
                        else if (workerManager.TotalWorkers == 3)
                        {
                            wgloRow = globalsTable.Rows["WORKER_COST_4TH"];
                        }
                        else if (workerManager.TotalWorkers == 4)
                        {
                            wgloRow = globalsTable.Rows["WORKER_COST_5TH"];
                        }

                        if (wgloRow == null)
                        {
                            level.Logs.Log("Unable to find correct GlobalData for worker cost.");
                        }
                        else
                        {
                            cost = wgloRow[0].NumberValue;
                        }
                    }

                    level.Avatar.UseResource(resource, cost);

                    var building = new Building(level.Village, data);
                    building.X = X;
                    building.Y = Y;
                    building.BeginConstruction(Tick);
                }
            }
        }
Beispiel #5
0
        /// <summary/>
        protected internal override void FromJsonReader(JsonReader reader)
        {
            var instance = CsvData.GetInstance <DecorationData>();

            var dataId    = -1;
            var dataIdSet = false;

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.EndObject)
                {
                    break;
                }

                if (reader.TokenType == JsonToken.PropertyName)
                {
                    var propertyName = (string)reader.Value;
                    switch (propertyName)
                    {
                    case "id":
                        // Ignore for now.
                        break;

                    case "data":
                        dataId    = reader.ReadAsInt32().Value;
                        dataIdSet = true;
                        break;

                    case "x":
                        X = reader.ReadAsInt32().Value;
                        break;

                    case "y":
                        Y = reader.ReadAsInt32().Value;
                        break;
                    }
                }
            }

            if (!dataIdSet)
            {
                throw new InvalidOperationException($"Decoration JSON at {reader.Path} does not contain a 'data' field.");
            }

            if (instance.InvalidDataID(dataId))
            {
                throw new InvalidOperationException($"Decoration JSON at {reader.Path} contained an invalid DecorationData ID. {instance.GetArgsOutOfRangeMessage("Data ID")}");
            }

            var tableCollections = Assets.Get <CsvDataTableCollection>();
            var dataRef          = new CsvDataRowRef <DecorationData>(dataId);
            var row = dataRef.Get(tableCollections);

            if (row == null)
            {
                throw new InvalidOperationException("Could not find CsvDataRow with ID '" + dataId + "'.");
            }

            var data = row[0];

            if (data == null)
            {
                throw new InvalidOperationException("Could not find DecorationData with ID '" + dataId + "'.");
            }

            _data = data;
        }
Beispiel #6
0
        /// <summary/>
        protected internal override void FromJsonReader(JsonReader reader)
        {
            var instance = CsvData.GetInstance <ObstacleData>();
            // clear_t value.
            var clearTime    = -1;
            var clearTimeSet = false;

            var dataId    = -1;
            var dataIdSet = false;

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.EndObject)
                {
                    break;
                }

                if (reader.TokenType == JsonToken.PropertyName)
                {
                    var propertyName = (string)reader.Value;
                    switch (propertyName)
                    {
                    case "id":
                        // Ignore for now.
                        break;

                    case "data":
                        dataId    = reader.ReadAsInt32().Value;
                        dataIdSet = true;
                        break;

                    case "clear_t":
                        clearTime    = reader.ReadAsInt32().Value;
                        clearTimeSet = true;
                        break;

                    case "x":
                        X = reader.ReadAsInt32().Value;
                        break;

                    case "y":
                        Y = reader.ReadAsInt32().Value;
                        break;

                    case "loot_multiply_ver":
                        LootMultiplier = reader.ReadAsInt32().Value;
                        break;
                    }
                }
            }

            if (!dataIdSet)
            {
                throw new InvalidOperationException($"Obstacle JSON at {reader.Path} does not contain a 'data' field.");
            }

            if (instance.InvalidDataID(dataId))
            {
                throw new InvalidOperationException($"Obstacle JSON at {reader.Path} contained an invalid ObstacleData ID. {instance.GetArgsOutOfRangeMessage("Data ID")}");
            }

            var tableCollection = Assets.DataTables;
            var dataRef         = new CsvDataRowRef <ObstacleData>(dataId);
            var data            = dataRef.Get(tableCollection)[0];

            if (data == null)
            {
                throw new InvalidOperationException("Could not find ObstacleData with ID '" + dataId + "'.");
            }

            _data = data;

            if (clearTimeSet)
            {
                Village.WorkerManager.AllocateWorker(this);
                _timer.Start(Village.LastTickTime, 0, clearTime);
            }
        }