private void HandleHarvesting() { if (_resourceTarget == null || _resourceTarget.currentResources <= 0) { // no more resources _currentState = HarvesterState.Idle; _resourceTarget = null; return; } var distance = (_entity.transform.position - _resourceTarget.transform.position).sqrMagnitude; if (distance > (_entity.attackRadius * _entity.attackRadius)) { // resource out of range if (!_entity.isMoving) { _entity.MoveTo(_resourceTarget.GetHarvestingPosition(_entity)); } return; } // resource within range _entity.Harvest(_resourceTarget); if (_entity.currentCarriedResources >= _entity.maxCarriableResources) { // harvester cannot carry more resources _currentState = HarvesterState.Returning; } }
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(); } }
/// <summary> /// Harvests the specified resource. /// </summary> /// <param name="resource">The resource.</param> /// <exception cref="ArgumentNullException">resource</exception> public void Harvest(ResourceComponent resource) { if (resource == null) { throw new ArgumentNullException("resource"); } var time = Time.time; if (time - _lastAttack < 1f / _attacksPerSecond) { return; } _lastAttack = time; resource.Harvest(this); }