Ejemplo n.º 1
0
        /// <summary>
        ///     Executes this command.
        /// </summary>
        public override int Execute(LogicLevel level)
        {
            if (this._trapData != null)
            {
                if (this._trapData.GetVillageType() == level.GetVillageType())
                {
                    if (level.IsValidPlaceForBuilding(this._x, this._y, this._trapData.GetWidth(), this._trapData.GetHeight(), null))
                    {
                        LogicClientAvatar playerAvatar      = level.GetPlayerAvatar();
                        LogicResourceData buildResourceData = this._trapData.GetBuildResource();

                        int buildCost = this._trapData.GetBuildCost(0);

                        if (playerAvatar.HasEnoughResources(buildResourceData, buildCost, true, this, false) && !level.IsTrapCapReached(this._trapData, true))
                        {
                            if (buildResourceData.PremiumCurrency)
                            {
                                playerAvatar.UseDiamonds(buildCost);
                            }
                            else
                            {
                                playerAvatar.CommodityCountChangeHelper(0, buildResourceData, -buildCost);
                            }

                            LogicTrap trap = (LogicTrap)LogicGameObjectFactory.CreateGameObject(this._trapData, level, level.GetVillageType());

                            if (this._trapData.GetBuildTime(0) == 0)
                            {
                                trap.FinishConstruction(false);
                            }

                            trap.SetInitialPosition(this._x << 9, this._y << 9);
                            level.GetGameObjectManager().AddGameObject(trap, -1);

                            if (level.IsTrapCapReached(this._trapData, false))
                            {
                                level.GetGameListener().TrapCapReached(this._trapData);
                            }

                            if (trap.GetVillageType() != 0)
                            {
                                int x      = trap.GetTileX();
                                int y      = trap.GetTileY();
                                int width  = trap.GetWidthInTiles();
                                int height = trap.GetHeightInTiles();

                                for (int i = 0; i < width; i++)
                                {
                                    for (int j = 0; j < height; j++)
                                    {
                                        LogicObstacle tallGrass = level.GetTileMap().GetTile(x + i, y + j).GetTallGrass();

                                        if (tallGrass != null)
                                        {
                                            level.GetGameObjectManager().RemoveGameObject(tallGrass);
                                        }
                                    }
                                }
                            }

                            return(0);
                        }
                    }

                    return(-1);
                }

                return(-32);
            }

            return(-1);
        }