public IBuilding BuildBuilding(IBuildingTemplate template, ICity city)
        {
            if (template == null)
            {
                throw new ArgumentNullException("template");
            }
            else if (city == null)
            {
                throw new ArgumentNullException("city");
            }

            var newBuilding = new Building(template);

            var slots = new List <IWorkerSlot>();

            for (int i = 0; i < template.SpecialistCount; i++)
            {
                slots.Add(WorkerSlotFactory.BuildSlot(newBuilding));
            }

            newBuilding.Slots = slots.AsReadOnly();

            if (!PossessionCanon.CanChangeOwnerOfPossession(newBuilding, city))
            {
                throw new BuildingCreationException("The building produced from this template cannot be placed into this city");
            }
            PossessionCanon.ChangeOwnerOfPossession(newBuilding, city);

            allBuildings.Add(newBuilding);

            return(newBuilding);
        }
Exemplo n.º 2
0
        public IUnit BuildUnit(IHexCell location, IUnitTemplate template, ICivilization owner, IPromotionTree promotionTree)
        {
            if (location == null)
            {
                throw new ArgumentNullException("location");
            }
            else if (template == null)
            {
                throw new ArgumentNullException("template");
            }
            else if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            var newUnitObject  = GameObject.Instantiate(UnitConfig.UnitPrefab);
            var newUnitDisplay = GameObject.Instantiate(template.DisplayPrefab);

            newUnitDisplay.transform.SetParent(newUnitObject.transform);

            Container.InjectGameObject(newUnitObject);

            var newUnit = newUnitObject.GetComponent <GameUnit>();

            newUnit.transform.SetParent(UnitContainer, false);

            newUnit.Template = template;

            newUnit.CurrentMovement  = template.MaxMovement;
            newUnit.CurrentHitpoints = newUnit.MaxHitpoints;
            newUnit.CanAttack        = true;
            newUnit.Level            = 1;
            newUnit.PromotionTree    = promotionTree;

            allUnits.Add(newUnit);

            if (UnitPossessionCanon.CanChangeOwnerOfPossession(newUnit, owner))
            {
                UnitPossessionCanon.ChangeOwnerOfPossession(newUnit, owner);
            }
            else
            {
                throw new UnitCreationException("The newly created unit cannot be assigned to its owner");
            }

            if (newUnit.CanRelocate(location))
            {
                newUnit.Relocate(location);
            }
            else
            {
                throw new UnitCreationException("The newly created unit cannot be placed at its location");
            }

            UnitSignals.NewUnitCreated.OnNext(newUnit);

            return(newUnit);
        }
        private void OnCityOwnershipChanged(Tuple <ICivilization, ICity> data)
        {
            var newOwner = data.Item1;
            var city     = data.Item2;

            if (newOwner != null && city.CombatFacade != null)
            {
                UnitPossessionCanon.ChangeOwnerOfPossession(city.CombatFacade, newOwner);
            }
        }
        public override IOngoingDiplomaticExchange ExecuteBetweenCivs(ICivilization fromCiv, ICivilization toCiv)
        {
            if (!CanExecuteBetweenCivs(fromCiv, toCiv))
            {
                throw new InvalidOperationException("CanExecuteBetweenCivs must return true on the given arguments");
            }

            CityPossessionCanon.ChangeOwnerOfPossession(CityInput, toCiv);

            return(null);
        }
Exemplo n.º 5
0
        public void ClearRuntime(bool immediateMode)
        {
            foreach (var city in new List <ICity>(CityFactory.AllCities))
            {
                CityLocationCanon.ChangeOwnerOfPossession(city, null);

                city.Destroy(immediateMode);
            }

            FreeBuildingApplier.Clear();
        }
        private void OnCellClicked(Tuple <IHexCell, PointerEventData> data)
        {
            if (DisplayType != CityDisplayType.MapEditor)
            {
                return;
            }

            var cell = data.Item1;

            if (CellPossessionCanon.GetOwnerOfPossession(cell) == ObjectToDisplay)
            {
                if (CellPossessionCanon.CanChangeOwnerOfPossession(cell, null))
                {
                    CellPossessionCanon.ChangeOwnerOfPossession(cell, null);
                }
            }
            else if (BorderExpansionLogic.IsCellAvailable(ObjectToDisplay, cell))
            {
                CellPossessionCanon.ChangeOwnerOfPossession(cell, ObjectToDisplay);
            }
        }
Exemplo n.º 7
0
        private void OnUnitBeingDestroyed(IUnit unit)
        {
            if (UnitPositionCanon.GetOwnerOfPossession(unit) != null)
            {
                UnitPositionCanon.ChangeOwnerOfPossession(unit, null);
            }

            if (UnitPossessionCanon.GetOwnerOfPossession(unit) != null)
            {
                UnitPossessionCanon.ChangeOwnerOfPossession(unit, null);
            }
        }
Exemplo n.º 8
0
        /// <inheritdoc/>
        public void PerformExpansion()
        {
            CellBeingPursued = ExpansionLogic.GetNextCellToPursue(this);

            var costOfPursuit = ExpansionLogic.GetCultureCostOfAcquiringCell(this, CellBeingPursued);

            if (CellBeingPursued != null &&
                costOfPursuit <= CultureStockpile &&
                TilePossessionCanon.CanChangeOwnerOfPossession(CellBeingPursued, this)
                )
            {
                CultureStockpile -= costOfPursuit;
                TilePossessionCanon.ChangeOwnerOfPossession(CellBeingPursued, this);

                CellBeingPursued = ExpansionLogic.GetNextCellToPursue(this);
            }
        }
Exemplo n.º 9
0
        public void RespondToCombat(IUnit attacker, IUnit defender, CombatInfo combatInfo)
        {
            if (attacker.CurrentHitpoints <= 0 && attacker.Type != UnitType.City)
            {
                DestroyUnit(attacker);
            }

            if (defender.CurrentHitpoints <= 0)
            {
                if (defender.Type == UnitType.City)
                {
                    defender.CurrentHitpoints = 1;
                }
                else
                {
                    var defenderLocation = UnitPositionCanon.GetOwnerOfPossession(defender);

                    var attackerOwner = UnitPossessionCanon.GetOwnerOfPossession(attacker);

                    if (combatInfo.CombatType == CombatType.Melee &&
                        UnitConfig.CapturableTemplates.Contains(defender.Template) &&
                        UnitPossessionCanon.CanChangeOwnerOfPossession(defender, attackerOwner)
                        )
                    {
                        UnitPossessionCanon.ChangeOwnerOfPossession(defender, attackerOwner);
                    }
                    else
                    {
                        DestroyUnit(defender);
                    }

                    if (attacker.CurrentHitpoints > 0 && combatInfo.CombatType == CombatType.Melee)
                    {
                        attacker.CurrentPath = new List <IHexCell>()
                        {
                            defenderLocation
                        };
                        attacker.PerformMovement(false);
                    }
                }
            }
        }
        public IResourceNode BuildNode(IHexCell location, IResourceDefinition resource, int copies)
        {
            if (!CanBuildNode(location, resource))
            {
                throw new InvalidOperationException("CanBuildNode must return true on the argued location and resource");
            }

            var newNode = Container.InstantiateComponentOnNewGameObject <ResourceNode>();

            newNode.Resource = resource;
            newNode.Copies   = copies;

            ResourceNodeLocationCanon.ChangeOwnerOfPossession(newNode, location);

            if (NodeContainer != null)
            {
                newNode.transform.SetParent(NodeContainer, false);
            }

            allNodes.Add(newNode);

            return(newNode);
        }
Exemplo n.º 11
0
        public void HandleCommandOnUnit(AbilityCommandRequest command, IUnit unit)
        {
            if (CanHandleCommandOnUnit(command, unit))
            {
                var unitOwner = UnitPossessionCanon.GetOwnerOfPossession(unit);

                var unitLocation = UnitPositionCanon.GetOwnerOfPossession(unit);

                var nearestCity = GetNearestDomesticCity(unitLocation, unitOwner);

                foreach (var nearbyCell in Grid.GetNeighbors(unitLocation))
                {
                    if (CellPossessionCanon.CanChangeOwnerOfPossession(nearbyCell, nearestCity))
                    {
                        CellPossessionCanon.ChangeOwnerOfPossession(nearbyCell, nearestCity);
                    }
                }
            }
            else
            {
                throw new InvalidOperationException("Command cannot be handled");
            }
        }
Exemplo n.º 12
0
        public ICity Create(IHexCell location, ICivilization owner, string name)
        {
            if (location == null)
            {
                throw new ArgumentNullException("location");
            }
            else if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            var newCityGameObject = GameObject.Instantiate(CityPrefab);

            Container.InjectGameObject(newCityGameObject);

            newCityGameObject.transform.position = location.AbsolutePosition;
            newCityGameObject.name = name;
            newCityGameObject.transform.SetParent(CityContainer, true);

            var newCity = newCityGameObject.GetComponent <City>();

            CityLocationCanon.ChangeOwnerOfPossession(newCity, location);

            location.SuppressSlot = true;

            CellModificationLogic.ChangeVegetationOfCell(location, CellVegetation.None);

            if (CityPossessionCanon.CanChangeOwnerOfPossession(newCity, owner))
            {
                CityPossessionCanon.ChangeOwnerOfPossession(newCity, owner);
            }
            else
            {
                throw new CityCreationException("Cannot assign the newly created city to its intended civilization");
            }

            var combatantTemplate = Container.Instantiate <CityCombatantTemplate>(new object[] { newCity });

            newCity.CombatFacade = UnitFactory.BuildUnit(location, combatantTemplate, owner);

            if (CellPossessionCanon.CanChangeOwnerOfPossession(location, newCity))
            {
                CellPossessionCanon.ChangeOwnerOfPossession(location, newCity);
            }
            else
            {
                throw new CityCreationException("Cannot assign the given location to the newly created city");
            }

            foreach (var neighbor in Grid.GetNeighbors(location))
            {
                if (CellPossessionCanon.CanChangeOwnerOfPossession(neighbor, newCity))
                {
                    CellPossessionCanon.ChangeOwnerOfPossession(neighbor, newCity);
                }
            }

            newCity.YieldFocus = YieldFocusType.TotalYield;

            newCity.Population = 1;

            allCities.Add(newCity);

            return(newCity);
        }