/// <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); }
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(); } }
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); } }
public void OnEnable() { _unit = this.transform.GetComponentInParent <UnitBase>(); _startPos = this.transform.localPosition; }
private void OnEnable() { _unit = this.GetComponent <UnitBase>(); }
private void OnEnable() { // Get a reference to the unit _unit = this.GetComponent <UnitBase>(); }
/// <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)); }
private void OnEnable() { _unit = this.GetComponent <UnitBase>(); _steerForPath = _unit.GetComponent <SteerForPath>(); }
/// <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); }
private void OnEnable() { // Get references to unit and the steering scanner _unit = this.GetComponent <UnitBase>(); _scanner = this.GetComponent <SteeringScanner>(); }
private void OnEnable() { _unit = this.GetComponent <UnitBase>(); _scanner = this.GetComponent <SteeringScanner>(); }