void RandomChangeRoom(bool isFuite = false) { aRoom theRoom = null; if (actualRoom != null && (Random.Range(0, 4) > 0) && isFuite == false) { theRoom = actualRoom; } else { aRoom theNewRoom = RM.Rooms[Random.Range(0, RM.Rooms.Count)]; if (Random.Range(0, 2) <= (int)theNewRoom.Densité && theNewRoom != actualRoom) { theRoom = theNewRoom; } } if (theRoom == null || (theRoom == actualRoom && myType == Type.Policeman)) { RandomChangeRoom(isFuite); } else { if (!isFuite && myType == Type.Civilian && actualRoom == theRoom && theRoom.Pieges.Count > 0 && Random.Range(0, 101) < 50) { for (int i = 0; i < theRoom.Pieges.Count; i++) { if (theRoom.Pieges[i].theUser == null) { PiegeReservé = theRoom.Pieges[i]; PiegeReservé.theUser = this; //Utilisation du piège } } } else if (isFuite && PiegeReservé != null) { PiegeReservé.theUser = null; PiegeReservé = null; } if (PiegeReservé != null) { myNavMesh.SetDestination(new Vector3(PiegeReservé.transform.GetChild(0).transform.position.x, 0, PiegeReservé.transform.GetChild(0).transform.position.z)); } else { myNavMesh.SetDestination(new Vector3(Random.Range(theRoom.downLeft.x, theRoom.downRight.x), 0, Random.Range(theRoom.downLeft.z, theRoom.upLeft.z))); } if (actualRoom != theRoom) { if (actualRoom != null) { actualRoom.Population.Remove(this); } actualRoom = theRoom; if (!theRoom.Population.ContainsKey(this)) { theRoom.Population.Add(this, gameObject.name); } } if (isFuite == false) { myAction = Action.Move; myAnimator.SetTrigger("Walk"); myNavMesh.speed = 1.5f; } else { myNavMesh.speed = 5f; myAnimator.SetTrigger("Run"); myAction = Action.Flee; } if (BasePerso) { BasePerso = false; } } }
// Update is called once per frame void Update() { if (Actif) { if (myAction != Action.Dead) { if (myAction == Action.None || myAction == Action.Dance) { if (PiegeReservé == null) { MoveTime += Time.deltaTime; if (MoveTime >= MoveLatence) { MoveTime = 0; myNavMesh.speed = 1.5f; RandomChangeRoom(); } } else { if (Vector3.Distance(transform.position, PiegeReservé.transform.GetChild(0).transform.position) < 0.7f) { Utilisation(); } } } else if (myAction == Action.Interaction && PiegeReservé != null) { transform.LookAt(new Vector3(PiegeReservé.transform.position.x, transform.position.y, PiegeReservé.transform.position.z)); MoveTime += Time.deltaTime; if (MoveTime >= 3) { MoveTime = 0; myNavMesh.enabled = true; myNavMesh.speed = 1.5f; PiegeReservé.theUser = null; PiegeReservé = null; RandomChangeRoom(); } } else if (myAction == Action.Paralysed) { MoveTime += Time.deltaTime; if (MoveTime >= 1) { MoveTime = 0; myNavMesh.enabled = true; Fuite(); } } else if (myAction == Action.Lost) { MoveTime += Time.deltaTime; if (MoveTime >= 2) { MoveTime = 0; myNavMesh.enabled = true; myNavMesh.speed = 1.5f; if (myType == Type.Policeman && TempsRestant <= 0) { PoliceStartLeave(); } else { RandomChangeRoom(false); } } } else if (myAction == Action.Found) { MoveTime += Time.deltaTime; transform.LookAt(new Vector3(PM.gameObject.transform.position.x, transform.position.y, PM.gameObject.transform.position.z)); if (MoveTime >= 1) { MoveTime = 0; myNavMesh.enabled = true; StartPursuit(); } } else if (myAction == Action.Attack) { myNavMesh.SetDestination(PM.gameObject.transform.position); Vector3 Direction = (PM.transform.position - transform.position).normalized; RaycastHit[] ToPlayer = Physics.RaycastAll(transform.position, Direction, 100, PoursuiteLayer).OrderBy(h => h.distance).ToArray();; for (int i = 0; i < ToPlayer.Length; i++) { if (ToPlayer[i].collider.tag.Equals("Player")) { // a toujours le joueur en vue break; } else if (ToPlayer[i].collider.tag.Equals("Wall")) { // a perdu la trace du joueur LostInPursuit(); break; } } } else if (myAction == Action.Move || myAction == Action.Flee) { if (myNavMesh.desiredVelocity.magnitude == 0 && Vector3.Distance(transform.position, myNavMesh.destination) < 2) { if (actualRoom.Actions_Spéciales.Count == 0 || myType != Type.Civilian) { myAction = Action.None; myAnimator.SetTrigger("StopMovement"); } else { myAction = actualRoom.Actions_Spéciales[0]; myAnimator.SetTrigger("Dance"); } } } if (myType != Type.Civilian) { UpdateRadar(); if (myAction != Action.Attack && myAction != Action.Found) { Watchout(); if (myType == Type.Policeman && myAction != Action.Leave && myAction != Action.Lost && myAction != Action.Found) { TempsRestant -= Time.deltaTime; if (TempsRestant <= 0) { PoliceStartLeave(); } } } } else { if (Brulé) { TempsBrulure += Time.deltaTime; if (TempsBrulure >= 3) { Mort(true); } } } } } }