示例#1
0
        private void RecoveryResource(double current, ref ResourceComponent.Component resource, ref ResourceSupplyer.Component supplyer)
        {
            //Debug.LogFormat("CurrentResource:{0}", resource.Resource);

            if (resource.Resource >= resource.ResourceMax)
            {
                resource.Resource = resource.ResourceMax;
                return;
            }

            if (supplyer.CheckedTime != 0.0f)
            {
                supplyer.ResourceFraction += (float)(supplyer.CheckedTime - current) * supplyer.RecoveryRate;
                var add = Mathf.FloorToInt(supplyer.ResourceFraction);
                if (add > 0)
                {
                    var res = resource.Resource + add;
                    resource.Resource          = Mathf.Min(res, resource.ResourceMax);
                    supplyer.ResourceFraction -= add;
                }
            }

            supplyer.CheckedTime = current;
        }
        private void FactoryQuery(Unity.Entities.Entity entity,
                                  ref UnitFactory.Component factory,
                                  ref ResourceComponent.Component resource,
                                  ref BaseUnitStatus.Component status,
                                  ref Position.Component position,
                                  ref StrongholdSight.Component sight,
                                  ref SpatialEntityId entityId)
        {
            if (status.State != UnitState.Alive)
            {
                return;
            }

            if (UnitUtils.IsBuilding(status.Type) == false)
            {
                return;
            }

            if (status.Order == OrderType.Idle)
            {
                return;
            }

            FollowerOrder?f_order      = null;
            SuperiorOrder?s_order      = null;
            TeamOrder?    team_order   = null;
            TurretOrder?  turret_order = null;

            FactoryOrderType orderType = FactoryOrderType.None;

            if (factory.SuperiorOrders.Count > 0)
            {
                s_order   = factory.SuperiorOrders[0];
                orderType = FactoryOrderType.Superior;
            }
            else if (factory.FollowerOrders.Count > 0)
            {
                f_order   = factory.FollowerOrders[0];
                orderType = FactoryOrderType.Follower;
            }
            else if (factory.TeamOrders.Count > 0)
            {
                team_order = factory.TeamOrders[0];
                orderType  = FactoryOrderType.Team;
            }
            else if (factory.TurretOrders.Count > 0)
            {
                turret_order = factory.TurretOrders[0];
                orderType    = FactoryOrderType.Turret;
            }

            if (orderType == FactoryOrderType.None)
            {
                return;
            }

            // calc time cost
            int   resourceCost;
            float timeCost;

            if (CalcOrderCost(out resourceCost, out timeCost, f_order, s_order, team_order) == false)
            {
                return;
            }

            //Debug.LogFormat("ResourceCost:{0} TimeCost:{1}", resourceCost, timeCost);

            if (factory.CurrentType == FactoryOrderType.None)
            {
                if (resource.Resource < resourceCost)
                {
                    //Debug.LogFormat("ResourcePoor:{0}", resource.Resource);
                    return;
                }

                factory.ProductInterval = IntervalCheckerInitializer.InitializedChecker(timeCost);
                factory.CurrentType     = orderType;
                resource.Resource      -= resourceCost;
            }

            factoryInter = factory.ProductInterval;
            if (CheckTime(ref factoryInter) == false)
            {
                return;
            }

            Coordinates?random = null;

            if (sight.StrategyVector.Side != UnitSide.None)
            {
                random = GetEmptyCoordinates(entityId.EntityId, position.Coords, sight.StrategyVector.Vector, height_buffer, factory.Containers);
            }

            if (random == null)
            {
                //Debug.LogFormat("There is no Empty");
                return;
            }

            //Debug.LogFormat("CreateUnit!");

            factory.ProductInterval = factoryInter;

            var            coords   = random.Value;//GetEmptyCoordinates(entityId.EntityId, position.Coords, height_buffer, factory.Containers);
            EntityTemplate template = null;

            bool     finished = false;
            UnitType type     = UnitType.None;

            if (s_order != null)
            {
                template = CreateSuperior(factory.SuperiorOrders, coords, out finished);
                type     = UnitType.Commander;
            }
            else if (f_order != null)
            {
                template = CreateFollower(factory.FollowerOrders, coords, f_order.Value.Customer, out finished);
                type     = UnitType.Soldier;
            }

            if (template != null)
            {
                var request = new WorldCommands.CreateEntity.Request
                              (
                    template,
                    context: new ProductOrderContext()
                {
                    f_order      = f_order,
                    s_order      = s_order,
                    type         = type,
                    strongholdId = entityId.EntityId,
                    container    = new UnitContainer(coords.ToFixedPointVector3(), ContainerState.Created)
                }
                              );
                this.CommandSystem.SendCommand(request);
            }
            else if (team_order != null)
            {
                CreateTeam(factory.TeamOrders, status.Side, entityId.EntityId, coords, out finished);
            }
            else if (turret_order != null)
            {
                // todo turret
            }

            if (finished)
            {
                factory.CurrentType = FactoryOrderType.None;
            }
        }