private void TargetQuery(Entity entity,
                                 ref CommanderStatus.Component commander,
                                 ref CommanderTeam.Component team,
                                 ref BaseUnitStatus.Component status,
                                 ref UnitActionData action,
                                 ref SpatialEntityId entityId)
        {
            if (status.State != UnitState.Alive)
            {
                return;
            }

            if (status.Type != UnitType.Commander)
            {
                return;
            }

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

            var trans = EntityManager.GetComponentObject <Transform>(entity);
            var pos   = trans.position;

            applyOrder(status, entityId, pos, action.SightRange, ref commander, ref team);
        }
        void Start()
        {
            Assert.IsNotNull(settings);

            float sightRange  = settings.SightRange;
            float attackRange = 0;

            health.SendUpdate(new BaseUnitHealth.Update
            {
                MaxHealth = settings.MaxHp,
                Health    = settings.MaxHp,
                Defense   = settings.Defense,
            });

            fuel.SendUpdate(new FuelComponent.Update
            {
                MaxFuel = settings.MaxFuel,
                Fuel    = settings.MaxFuel,
            });

            if (gunInitializer != null && container != null)
            {
                GunIdPair[] gunIds = null;
                if (container.CannonDic.Count > 0)
                {
                    gunIds = container.CannonDic.Select(kvp => new GunIdPair()
                    {
                        Id = kvp.Value.GunId, bone = kvp.Key
                    }).ToArray();
                }

                gunInitializer.SetGunIds(gunIds);
                attackRange = gunInitializer.AttackRange;
            }

            if (this.EntityManager != null &&
                this.Worker != null && this.Worker.TryGetEntity(SpatialComp.EntityId, out Entity entity))
            {
                this.EntityManager.Value.AddComponentData <UnitActionData>(entity, UnitActionData.CreateData(sightRange, attackRange));
            }
        }
        private void Query(Entity entity,
                           ref BaseUnitSight.Component sight,
                           ref UnitActionData action,
                           ref BaseUnitStatus.Component status,
                           ref BaseUnitTarget.Component target,
                           ref SpatialEntityId entityId)
        {
            if (status.State != UnitState.Alive)
            {
                return;
            }

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

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

            // initial
            target.State = TargetState.None;

            var id = entityId.EntityId.Id;

            if (enemyPositionsContainer.ContainsKey(id) == false)
            {
                enemyPositionsContainer[id] = new List <FixedPointVector3>();
            }

            var enemyPositions = enemyPositionsContainer[id];

            enemyPositions.Clear();

            var trans = EntityManager.GetComponentObject <Transform>(entity);
            var pos   = trans.position;

            UnitInfo enemy      = null;
            var      sightRange = action.SightRange;

            var backBuffer = sightRange / 2;

            if (status.Type == UnitType.Commander)
            {
                backBuffer += RangeDictionary.AllyRange / 2;
            }

            // strategy target
            SetStrategyTarget(pos, backBuffer, ref sight, ref target);

            // keep logic
            if (status.Order == OrderType.Keep)
            {
                sightRange *= target.PowerRate;
            }

            enemy = getNearestEnemy(status.Side, pos, sightRange);

            if (enemy != null)
            {
                var tgtPos = sight.TargetPosition.ToWorkerPosition(this.Origin);
                var epos   = enemy.pos.ToWorldPosition(this.Origin);

                if (Vector3.Dot(tgtPos - pos, enemy.pos - pos) > 0)
                {
                    target.State         = TargetState.ActionTarget;
                    sight.TargetPosition = epos;
                    sight.TargetSize     = enemy.size;
                }

                enemyPositions.Add(epos);
            }

            float range;

            switch (target.State)
            {
            case TargetState.ActionTarget:
                range = action.AttackRange;
                range = AttackLogicDictionary.GetOrderRange(status.Order, range) * target.PowerRate;
                break;

            default:
                range = RangeDictionary.BodySize;
                break;
            }

            // set behind
            if (status.Type == UnitType.Commander)
            {
                var addRange = RangeDictionary.AllyRange / 2;
                range += AttackLogicDictionary.RankScaled(addRange, status.Rank);
            }

            sight.TargetRange = range;
            sight.State       = target.State;
        }
示例#4
0
        private void Query(Entity entity,
                           ref UnitActionData action,
                           ref GunComponent.Component gun,
                           ref PostureAnimation.Component anim,
                           ref BaseUnitTarget.Component target,
                           ref BaseUnitStatus.Component status,
                           ref SpatialEntityId entityId)
        {
            if (status.State != UnitState.Alive)
            {
                return;
            }

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

            if (target.State != TargetState.ActionTarget)
            {
                return;
            }

            var current = Time.ElapsedTime;

            var id = entityId.EntityId.Id;

            if (this.EnemyPositionsContainer == null ||
                this.EnemyPositionsContainer.TryGetValue(id, out var enemyPositions) == false)
            {
                return;
            }

            Vector3?epos = null;

            if (enemyPositions.Count > 0)
            {
                epos = enemyPositions[0].ToWorkerPosition(this.Origin);

                var container = EntityManager.GetComponentObject <PostureBoneContainer>(entity);
                Attack(container, current, epos.Value, entityId, ref gun);
            }

            var  type   = AnimTargetType.None;
            bool isDiff = false;

            if (epos != null)
            {
                isDiff = anim.AnimTarget.Position.ToWorkerPosition(this.Origin) != epos.Value;
                type   = AnimTargetType.Position;
            }

            if (anim.AnimTarget.Type != type || isDiff)
            {
                var animTarget = anim.AnimTarget;
                animTarget.Type = type;

                if (epos != null)
                {
                    animTarget.Position = epos.Value.ToWorldPosition(this.Origin);
                }

                anim.AnimTarget = animTarget;
            }
        }