private void CallBuildObstacle(int indexOfObstacleConstructor, ActiveObstacle.ObstacleType wallsType, Animator animator) { if (EnoughBuildGold(wallsType) && m_listObstacles[indexOfObstacleConstructor].GetCurrentState() == ObstacleConstructor.EState.Buildable) { m_counter++; m_aiBuildObstacles.SetIndex(indexOfObstacleConstructor); m_aiBuildObstacles.SetTypeBuild(wallsType); animator.SetTrigger(Constant.BotTransition.s_buildObstacle); } }
public void MakeAction(Animator animator) { float ratio = Random.Range(0f, 1f); if (ratio < m_unitSpawnRatio) // SpawnUnits { int idSpawnUnit = Random.Range(0, m_botSpawnUnits.Count); Constant.SpawnTypeUnit unitType = m_listSpawnTypeUnit[Random.Range(0, m_listSpawnTypeUnit.Count)]; GameObject prefabTemp = GameManager.Instance.GetUnitForPlayer(unitType); if (GameManager.Instance.GetPlayer(PlayerEntity.Player.Bot).GetUnitGold() >= prefabTemp.GetComponent <UnitController>().GetUnitCost()) { m_unitsQueue.Clear(); m_unitsQueue.Add(unitType); m_botSpawnUnits[idSpawnUnit].SetUnitsQueue(m_unitsQueue); } m_aiSpawnUnit.SetIndexSpawUnits(idSpawnUnit); m_aiSpawnUnit.SetListeSpawnUnits(m_botSpawnUnits); animator.SetTrigger(Constant.BotTransition.s_spawnUnit); } else if (m_unitSpawnRatio <= ratio && ratio < m_unitSpawnRatio + m_obstaclesRatio) // Obstacles Build/Destroy { int index = Random.Range(0, m_listObstacles.Count); if (m_listObstacles[index].GetCurrentState() == ObstacleConstructor.EState.Buildable) { m_aiBuildObstacle.SetIndex(index); m_aiBuildObstacle.SetObstacleConstructorList(m_listObstacles); int typeBuildIndex = Random.Range(0, m_listSpawnTypeObstacle.Count); m_aiBuildObstacle.SetTypeBuild(m_listSpawnTypeObstacle[typeBuildIndex]); animator.SetTrigger(Constant.BotTransition.s_buildObstacle); } //if (m_listObstacles[index].GetCurrentState() == ObstacleConstructor.EState.Built) //{ // m_aiDestroyObstacle.SetIndex(index); // m_aiDestroyObstacle.SetObstacleConstructorList(m_listObstacles); // animator.SetTrigger(Constant.BotTransition.s_destroyObstacle); //} } else { m_indexCheckpoint = Random.Range(0, m_listCheckpoints.Count); if (m_unitSpawnRatio + m_obstaclesRatio <= ratio && ratio < m_unitSpawnRatio + m_obstaclesRatio + m_checkpointsReleaseAllRatio) // Checkpoints ReleaseAll { m_nbUnitsReleased = m_listCheckpoints[m_indexCheckpoint].GetNbUnitsStocked(); } else if (m_unitSpawnRatio + m_obstaclesRatio + m_checkpointsReleaseAllRatio <= ratio && ratio < m_unitSpawnRatio + m_obstaclesRatio + m_checkpointsReleaseAllRatio + m_checkpointsReleaseRatio) // Checkpoints Release { m_nbUnitsReleased = 1; } if (m_listCheckpoints[m_indexCheckpoint].GetNbUnitsStocked() >= m_nbUnitsReleased && m_nbUnitsReleased != 0) { m_aiReleaseUnits.SetListCheckpoints(m_listCheckpoints); m_aiReleaseUnits.SetIndexCheckpoint(m_indexCheckpoint); m_aiReleaseUnits.SetNbUnitsReleased(m_nbUnitsReleased); animator.SetTrigger(Constant.BotTransition.s_releaseUnits); } } }
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { m_pathCorners = m_playerUnit.GetAgent().path.corners; RaycastHit enter; float minDistance = Mathf.Infinity; for (int i = 0; i < m_pathCorners.Length - 1; i++) { Vector2 line = new Vector2(m_pathCorners[i + 1].x - m_pathCorners[i].x, m_pathCorners[i + 1].z - m_pathCorners[i].z); Vector2 lineDir = line.normalized; Ray lineRay = new Ray(m_pathCorners[i], lineDir); Debug.DrawLine(m_pathCorners[i], m_pathCorners[i + 1], Color.green, 1000, false); for (int j = 0; j < m_listObstacles.Count; j++) { ObstacleConstructor obstacle = m_listObstacles[j]; Vector3 obstaclePos = obstacle.transform.position; Vector3 obstacleRot = obstacle.transform.eulerAngles; Vector3 obstacleNormal; if (obstacleRot.y == 90) { obstacleNormal = Vector3.right; } else { obstacleNormal = Vector3.forward; } GameObject plan = new GameObject(); plan.AddComponent <BoxCollider>(); plan.transform.position = obstaclePos; plan.GetComponent <BoxCollider>().transform.localScale += new Vector3(5f, 5f, 5f); if (plan.GetComponent <BoxCollider>().Raycast(lineRay, out enter, Vector3.Distance(m_pathCorners[i], m_pathCorners[i + 1]))) { Vector3 hitPoint = enter.point; if (Vector3.Distance(hitPoint, obstaclePos) < 5f) { float distance = Vector3.Distance(hitPoint, m_pathCorners[i]); if ((distance < minDistance) && (distance > Vector3.Distance(m_playerUnit.transform.position, m_pathCorners[i]))) { minDistance = distance; m_indexObstacle = j; } } } Destroy(plan); } } // Prepare pour Build Obstacles ActiveObstacle activeObstacle = m_listObstacles[m_indexObstacle].transform.GetComponentInChildren <ActiveObstacle>(); if (null != activeObstacle) { PlayerEntity.Player playerNumber = activeObstacle.GetPlayerNumber(); if (playerNumber == PlayerEntity.Player.Player1) { m_aiDestroyObstacle.SetIndex(m_indexObstacle); animator.SetTrigger(Constant.BotTransition.s_destroyObstacle); } } m_aiBuildObstacles.SetTypeBuild(m_buildType); m_aiBuildObstacles.SetIndex(m_indexObstacle); animator.SetTrigger(Constant.BotTransition.s_buildObstacle); }
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { ObstacleConstructor currentObstacle = m_listObstacleConstructors[m_ind]; if (currentObstacle.GetCurrentState() == ObstacleConstructor.EState.Buildable) { NavMeshHit hit; NavMesh.SamplePosition(m_spawnunit.GetTarget().transform.position, out hit, Vector3.Distance(m_spawnunit.transform.position, m_spawnunit.GetTarget().transform.position) + 100, NavMesh.AllAreas); NavMeshAgent tempAgent = m_spawnunit.gameObject.GetComponent <NavMeshAgent>(); if (tempAgent == null) { if (tempAgent == null) { tempAgent = m_spawnunit.gameObject.AddComponent <NavMeshAgent>(); } } tempAgent.CalculatePath(hit.position, m_newPath); //get intersection navmesh vs ckcollider Vector3[] pathCorners = m_newPath.corners; RaycastHit colliderHit; //m_isCollider = (m_ind == m_listObstacleConstructors.Length - 1); for (int i = 0; i < pathCorners.Length - 1; i++) { Vector2 line = new Vector2(pathCorners[i + 1].x - pathCorners[i].x, pathCorners[i + 1].z - pathCorners[i].z); Vector2 lineDir = line.normalized; Ray lineRay = new Ray(pathCorners[i], lineDir); Debug.DrawLine(pathCorners[i], pathCorners[i + 1], Color.green, 1000, false); if (m_colliderOnAreaCollider.Raycast(lineRay, out colliderHit, Vector3.Distance(pathCorners[i], pathCorners[i + 1]))) { if (m_ind == 0) { animator.SetTrigger(Constant.BotTransition.s_return); } else { m_isCollider = true; break; } } } if (null != m_virtualWall) { Destroy(m_virtualWall); } if (m_isCollider) { Destroy(m_virtualArea); // Prepare pour Build Obstacles m_aiBuildObstacles.SetTypeBuild(ActiveObstacle.ObstacleType.SimpleWall); m_aiBuildObstacles.SetIndex(m_ind - 2); m_aiBuildObstacles.SetObstacleConstructorList(m_listObstacleConstructors); animator.SetTrigger(Constant.BotTransition.s_buildObstacle); } // construct virtual wall to calculate new path m_virtualWall = new GameObject(); m_virtualWall.AddComponent <NavMeshObstacle>(); m_virtualWall.GetComponent <NavMeshObstacle>().size = new Vector3(3, 3, 3); m_virtualWall.transform.position = currentObstacle.transform.position; m_virtualWall.GetComponent <NavMeshObstacle>().carving = true; } if (m_ind < m_listObstacleConstructors.Count - 1) { m_ind += 1; } }