コード例 #1
0
        /// <summary>
        /// Gets a position for harvesters to be in when harvesting.
        /// </summary>
        /// <returns>The nearest position in a circle around this resource</returns>
        public Vector3 GetHarvestingPosition(UnitBase unit)
        {
            var unitPos = unit.transform.position;
            var nearest = _harvestPositions[0];

            for (int i = 1; i < _harvestPositions.Length; i++)
            {
                var pos = _harvestPositions[i];
                if ((unitPos - pos).sqrMagnitude < (unitPos - nearest).sqrMagnitude)
                {
                    nearest = pos;
                }
            }

            return(nearest);
        }
コード例 #2
0
        private void HandleIdle()
        {
            var observations = _entity.observations;

            observations.Sort(new GameObjectDistanceSortComparer(this.transform.position));

            var count = observations.Count;

            for (int i = 0; i < count; i++)
            {
                var obs = observations[i];

                var otherUnit = obs.GetComponent <UnitBase>();
                if (otherUnit != null)
                {
                    if (_entity.IsAllied(otherUnit))
                    {
                        // don't flee from allies
                        continue;
                    }

                    if ((otherUnit.transform.position - _entity.transform.position).sqrMagnitude > (_entity.fleeRadius * _entity.fleeRadius))
                    {
                        // enemy is outside of flee radius
                        continue;
                    }

                    _fleeTarget   = otherUnit;
                    _currentState = HarvesterState.Fleeing;
                    return;
                }

                var resource = obs.GetComponent <ResourceComponent>();
                if (resource != null)
                {
                    _resourceTarget = resource;
                    _currentState   = HarvesterState.Harvesting;
                    return;
                }
            }

            // nothing interesting in memory, do some random wandering
            if (!_entity.isMoving)
            {
                _entity.RandomWander();
            }
        }
コード例 #3
0
        private void HandleFleeing()
        {
            var fleeDir = (_entity.transform.position - _fleeTarget.transform.position);

            if (_fleeTarget == null || _fleeTarget.isDead || fleeDir.sqrMagnitude > (_entity.fleeRadius * _entity.fleeRadius))
            {
                // flee target is null, dead or outside of flee range
                _currentState = HarvesterState.Idle;
                _fleeTarget   = null;
                return;
            }

            if (!_entity.isMoving)
            {
                var fleePos = _entity.transform.position + fleeDir.normalized * _entity.fleeRadius;
                _entity.MoveTo(fleePos);
            }
        }
コード例 #4
0
 public void OnEnable()
 {
     _unit     = this.transform.GetComponentInParent <UnitBase>();
     _startPos = this.transform.localPosition;
 }
コード例 #5
0
 private void OnEnable()
 {
     _unit = this.GetComponent <UnitBase>();
 }
コード例 #6
0
 private void OnEnable()
 {
     // Get a reference to the unit
     _unit = this.GetComponent <UnitBase>();
 }
コード例 #7
0
ファイル: UnitBase.cs プロジェクト: KasperHdL/ApexTeaching
 /// <summary>
 /// Determines whether the specified other unit is allied.
 /// </summary>
 /// <param name="otherUnit">The other unit.</param>
 /// <returns><c>true</c> if the other unit is allied; otherwise, <c>false</c>.</returns>
 public bool IsAllied(UnitBase otherUnit)
 {
     return(ReferenceEquals(this.nest, otherUnit.nest));
 }
コード例 #8
0
 private void OnEnable()
 {
     _unit         = this.GetComponent <UnitBase>();
     _steerForPath = _unit.GetComponent <SteerForPath>();
 }
コード例 #9
0
 /// <summary>
 /// Returns the specified unit to the pool.
 /// </summary>
 /// <param name="item">The entity.</param>
 public void Return(UnitBase item)
 {
     _count--;
     item.gameObject.SetActive(false);
     _pool.Enqueue(item);
 }
コード例 #10
0
 private void OnEnable()
 {
     // Get references to unit and the steering scanner
     _unit    = this.GetComponent <UnitBase>();
     _scanner = this.GetComponent <SteeringScanner>();
 }
コード例 #11
0
 private void OnEnable()
 {
     _unit    = this.GetComponent <UnitBase>();
     _scanner = this.GetComponent <SteeringScanner>();
 }