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; } }
public override void ClickRightMouseEvent(object sender, GameEventArgs e) { base.ClickRightMouseEvent(sender, e); if (!IsSelected || IsOnBuilding) { return; } MouseClickEventArgs m = e as MouseClickEventArgs; if (m.dataGameobject.layer == 11) { _navMeshAgent.enabled = false; _FSMSystemManager.ControlStateTransition(StateID.Move); Vector3 pos = CrowdBehaviorManager.Instance.GetBattle_FormationPos(); Debug.Log(pos); moveState.SetMoveTargetPos(m.ClickPosition); } else if (m.dataGameobject.tag == "Mine") { _navMeshAgent.enabled = false; miningState.MinePosition = m.dataGameobject.transform.position; _FSMSystemManager.ControlStateTransition(StateID.Mining); harvesterState = HarvesterState.mine; } }
public void Mining_AI() { _navMeshAgent.enabled = false; miningState.MinePosition = minePos; _FSMSystemManager.ControlStateTransition(StateID.Mining); harvesterState = HarvesterState.mine; }
protected override void OnActivate() { base.OnActivate(); AddToGameObjectManager(); harvesterState = HarvesterState.idle; //_FSMSystemManager.ControlStateTransition(StateID.Idle); IsOnBuilding = false; StopAllCoroutines(); }
IEnumerator ProductingBuilding(GameObject building) { building.GetComponent <IPlayerCampUnitsBase>().SetSelected(true); float z = Camera.main.WorldToScreenPoint(building.transform.position).z; //先获得世界坐标转屏幕坐标的z值 GameMapManager.Instance.IsCanBuilding = true; building.GetComponent <HighlightableObject>().ConstantOn(Color.red); UIBuildColliderDetector = PoolManager.Instance.GetInstance("UIBuildColliderDetector", building.transform.position, new Quaternion(0, 0, 0, 0)); UIBuildColliderDetector.GetComponent <UIBuildColliderDetector>().FatherNode = building; while (true) { Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, z)); pos.y = 0; building.transform.position = pos; Vector3 buildPos; if (Input.GetMouseButtonDown(0)) { yield return(new WaitForSeconds(0.1f)); if (GameMapManager.Instance.IsCanBuilding) { UIBuildColliderDetector.SetActive(false); buildPos = building.transform.position; building.GetComponent <IPlayerCampUnitsBase>().SetSelected(false); building.SetActive(false); GameDataManager.Instance.ChangeMineCount(-Cost, Object_Camp, IsPlayerCamp); if (_FSMSystemManager.CurrentState.State_ID != StateID.Move) { _FSMSystemManager.ControlStateTransition(StateID.Move); } harvesterState = HarvesterState.build; moveState.SetMoveTargetPos(buildPos, 0, () => { StartCoroutine(MoveToBuilding(building)); }); yield break; } else { GameMapManager.Instance.IsCanBuilding = true; } } else if (Input.GetMouseButtonDown(1)) { UIBuildColliderDetector.SetActive(false); GameMapManager.Instance.IsCanBuilding = true; building.GetComponent <IPlayerCampUnitsBase>().SetSelected(false); building.SetActive(false); yield break; } yield return(null); } }
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(); } }
public void ProductBuilding_AI(string name, Vector3 pos) { productint_Unit = name; GameObject building = PoolManager.Instance.GetInstance(Object_Camp.ToString() + name, pos, new Quaternion(0, 180, 0, 0)); building.SetActive(false); harvesterState = HarvesterState.build; if (_FSMSystemManager.CurrentState.State_ID != StateID.Move) { _FSMSystemManager.ControlStateTransition(StateID.Move); } moveState.SetMoveTargetPos(pos, 0, () => { StartCoroutine(MoveToBuilding(building)); }); }
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); } }
private void HandleReturning() { var nest = _entity.nest; var distance = (_entity.transform.position - nest.transform.position).sqrMagnitude; if (distance > (nest.returnHarvestRadius * nest.returnHarvestRadius)) { // nest ouside of range if (!_entity.isMoving) { _entity.MoveTo(_entity.nest.GetReturningPosition(_entity)); } return; } // nest inside range _entity.ReturnHarvest(); _currentState = HarvesterState.Idle; }
private void HandleReturning() { var distance = (_entity.transform.position - _entity.nest.transform.position).sqrMagnitude; if (distance > (_entity.returnHarvestRadius * _entity.returnHarvestRadius)) { // nest ouside of range if (!_entity.isMoving) { _entity.MoveTo(_entity.nest.transform.position); } return; } // nest inside range _entity.nest.currentResources += _entity.currentCarriedResources; _entity.currentCarriedResources = 0; _currentState = HarvesterState.Idle; }
void InitFSM() { _FSMSystemManager = new FSMSystemManager(gameObject, _navMeshAgent); if (IsPlayerCamp) { CenterPos = GameMapManager.Instance.PlayerCenter.transform.position; } else { CenterPos = GameMapManager.Instance.AICenter.transform.position; } miningState = new MiningState(CenterPos, Object_Camp, IsPlayerCamp);; idleState = new IdleState(); moveState = new MoveState(); _FSMSystemManager.AddState(miningState); _FSMSystemManager.AddState(idleState); _FSMSystemManager.AddState(moveState); _FSMSystemManager.CurrentState = idleState; harvesterState = HarvesterState.idle; }