private IEnumerator Reroute(PathfinderNPC otroNPC) { //rerouting = true; if (pies.moviendo) { yield return(new WaitWhile(() => pies.moviendo)); } if (m_objetivoFinal.HasValue) { RutaTo(m_objetivoFinal.Value, false); } /*float distancia = (Mathf.Abs(otroNPC.transform.position.x - transform.position.x) + Mathf.Abs(otroNPC.transform.position.y - transform.position.y)); * int espaciosExtras = Mathf.FloorToInt(distancia / GeneradorSuelo.sueloSize); * if (espaciosExtras < 1) espaciosExtras = 1; * int indice = rutaOriginal.FindIndex(x => x == posOcupadas[0]) + espaciosExtras; * //Si el otro npc justo dejo de caminar para elegir el siguiente objetivo considera posOcupadas.count == 1;!!!! Provoca bug * if (otroNPC.posOcupadas.Count > 1 && Vector2.Distance(otroNPC.posOcupadas[0], posOcupadas[0]) < Vector2.Distance(otroNPC.posOcupadas[1], posOcupadas[0])) * indice += 10;//Temporal, deberia ser calculado en base a cuanto tiempo extra le tomara adelantarlo segun las velocidades maximas * //Debug.Log(espaciosExtras); * //Debug.Log(indice); * //Debug.Log(rutaOriginal.Count); * if (rutaOriginal.Count - indice > 0) * { * //Debug.Log(GetPosActualGrilla()); * RutaTo(rutaOriginal.GetRange(indice, rutaOriginal.Count - indice), false); * yield return new WaitWhile(() => npc.estadoActual == EstadoNPC.Quieto); * //Debug.Log("BACK ON TRACK!"); * }*/ /*RutaTo(rutaOriginal.Last(), false); * yield return new WaitWhile(() => npc.estadoActual == EstadoNPC.Quieto);*/ /*rerouting = false; * RutaTo(m_objetivoFinal.Value, false);*/ //m_movimiento.SiguienteAccion(); }
public virtual void Init(NPC npc) { m_npc = npc; m_pathfinder = npc.pathfinder; m_pies = npc.pies; m_ciudad = npc.ciudad; }
public Suelo(Vector2 posPlayerActual, List <Vector2> posPlayerFinal, Ciudad ciudad, bool considerarNPCs, PathfinderNPC npc) { m_ciudad = ciudad; PosPlayerFinal = posPlayerFinal; PosicionNPC = posPlayerActual; ConsiderarNPCs = considerarNPCs; NpcActual = npc; Init(); }
public void Init() { npc = GetComponentInParent <NPC>(); col2D = npc.col2D; m_ciudad = npc.ciudad; m_pathfinder = npc.pathfinder; pies = npc.pies; ojos = npc.ojos; }
public void Init() { npc = GetComponentInParent <NPC>(); pathfinder = npc.pathfinder; col2D = GetComponent <CircleCollider2D>(); rBody = npc.GetComponent <Rigidbody2D>(); posMovimiento = transform.position; ciudad = npc.ciudad; LlenarResistencias(); }
public bool EsPosible(float tiempoLlegada, PathfinderNPC otroNPC, Vector2 pos, bool usarMargen = true, bool ignorarUltimaPos = false) { PosRutaActual tiempoSwap, tiempoAux; //Checar si calzan los tiempos estimados if (otroNPC.rutaActual.TryGetValue(pos, out tiempoAux)) { //Checar que no vaya a haber otro npc en ese momento float medioTiempoOtro = MedioTiempo(otroNPC, pos); float tiempoEntrada = tiempoAux.m_tiempo - medioTiempoOtro; float tiempoSalida = tiempoAux.m_tiempo + medioTiempoOtro * (1 + tiempoAux.m_vecesEsperado); if (usarMargen) { tiempoEntrada -= c_margen; tiempoSalida += c_margen; } //DebugIf(pos, "DEBUG"); if (tiempoLlegada >= tiempoEntrada && tiempoLlegada <= tiempoSalida) { DebugIf(pos, "NO NORMAL"); return(false); } //Checar si es Swap! if (otroNPC.rutaActual.TryGetValue(PosicionNPC, out tiempoSwap)) { //Checar que la ruta del npc no este contenida en la ruta del otro. float tiempoInicialNPC = tiempoLlegada - 2 * MedioTiempo(NpcActual, PosicionNPC); //Se le agrega medio tiempo extra para permitir que salgan de sus tiles float tiempoInicialOtro = tiempoSwap.m_tiempo - 3 * medioTiempoOtro; float tiempoFinalOtro = tiempoSwap.m_tiempo + medioTiempoOtro; if ((tiempoLlegada <= tiempoFinalOtro && tiempoLlegada >= tiempoInicialOtro) || (tiempoInicialNPC <= tiempoFinalOtro && tiempoInicialNPC >= tiempoInicialOtro)) { DebugIf(pos, "NO SWAP"); return(false); } } //Checar si la posicion a ocupar sera la ultima del otro npc y que llegara despues que el. (Dado que el otro npc se quedara parado ahi) if (!ignorarUltimaPos && otroNPC.ultimaPosicion.Value == pos && tiempoLlegada > tiempoAux.m_tiempo + medioTiempoOtro) { DebugIf(pos, "ULTIMA POS"); return(false); } } //DebugIf(pos, "SI"); return(true); }
void Awake() { ciudad = GetComponentInParent <Ciudad>(); spriteRenderer = GetComponent <SpriteRenderer>(); movimiento = InstantiateComponent(movimientoPrefab).GetComponent <Movimiento>(); pies = InstantiateComponent(piesPrefab).GetComponent <Pies>(); ojos = InstantiateComponent(ojosPrefab).GetComponent <Ojos>(); pathfinder = GetComponentInChildren <PathfinderNPC>(); animador = GetComponentInChildren <AnimadorNPC>(); col2D = GetComponent <BoxCollider2D>(); pies.Init(); pathfinder.Init(); movimiento.Init(); ojos.Init(); animador.Init(); ciudad.NPCs.Add(pathfinder); }
public bool EsPosible(Vector2 pos, bool usarMargen = true) { //Checar que no sea edifio int x = (int)pos.x; int y = (int)pos.y; if (m_ciudad.PosicionesActuales[x, y] == 0) { return(false); } //DebugIf(pos, "Es posible: " + x + "," + y); if (ConsiderarNPCs) { for (int i = 0; i < m_ciudad.NPCs.Count; i++) { PathfinderNPC otroNPC = m_ciudad.NPCs[i]; if (otroNPC == NpcActual) { continue; } if (otroNPC.NpcCaminando()) { float tiempoLlegada = m_tiempoAcumulado + NpcActual.TiempoPorCasilla(pos); if (!EsPosible(tiempoLlegada, otroNPC, pos, usarMargen)) { return(false); } } else if (otroNPC.posOcupadas.Contains(pos)) { return(false); } } } //DebugIf(pos, "SI TOTAL"); return(true); }
private float MedioTiempo(PathfinderNPC npc, Vector2 pos) { return(npc.TiempoPorCasilla(pos) / 2f); }
public static bool EsPosible(Vector2 posInicial, Vector2 posObjetivo, Ciudad ciudad, bool considerarNPCs, PathfinderNPC npc, bool usarMargen = true) { Suelo suelo = new Suelo(posInicial, posObjetivo, ciudad, considerarNPCs, npc); return(suelo.EsPosible(posObjetivo, usarMargen)); }
public static bool RutasCruzadas(List <Vector2> rutaA, List <Vector2> rutaB, Ciudad ciudad, PathfinderNPC npcA, PathfinderNPC npcB) { Suelo suelo; //En teoria nunca deberia haber un personaje caminando sin ultima posicion que este caminando. (WIP) #TO.DO if (npcB.ultimaPosicion == null) { return(true); } if (npcA.ultimaPosicion == null) { return(false); } Vector2 posFinal = npcB.ultimaPosicion.Value; //Ignora la ultima posicion si ambos van hacia alla. TEST QUE PASA SI LA POS FINAL NO ES UN EDIFICIO! bool ignorarUltimaPos = npcA.ultimaPosicion.Value == npcB.ultimaPosicion.Value; if (ignorarUltimaPos) { rutaA.Remove(npcA.ultimaPosicion.Value); rutaB.Remove(npcB.ultimaPosicion.Value); } for (int a = 0; a < rutaA.Count; a++) { for (int b = 0; b < rutaB.Count; b++) { if (Mathf.Abs(rutaA[a].x - rutaB[b].x) + Mathf.Abs(rutaA[a].y - rutaB[b].y) == 1) { PosRutaActual tiempoLlegada; if (npcA.rutaActual.TryGetValue(rutaB[b], out tiempoLlegada)) { suelo = new Suelo(rutaA[a], posFinal, ciudad, true, npcA); if (!suelo.EsPosible(tiempoLlegada.m_tiempo, npcB, rutaB[b], ignorarUltimaPos: ignorarUltimaPos)) { //Debug.LogError(rutaA[a] + " -> " + rutaB[b]); return(true); } } } } } //Debug.LogError("NO CRUZAN"); return(false); }
public static List <Vector2> GetVectoresRuta(Vector2 posInicial, List <Vector2> posObjetivos, Ciudad ciudad, bool considerarNPCs, PathfinderNPC npc, out TreeNode nodoMasCercano) { PathFinder pathFinder = new PathFinder(); pathFinder.SetNodoInicial(new Suelo(posInicial, posObjetivos, ciudad, considerarNPCs, npc)); List <Suelo> posFinales = new List <Suelo>(); for (int i = 0; i < posObjetivos.Count; i++) { posFinales.Add(new Suelo(posObjetivos[i], posObjetivos[i], ciudad, considerarNPCs, npc)); } pathFinder.SetNodosFinales(posFinales.ToArray()); List <TreeNode> ruta = pathFinder.GetRuta(false); nodoMasCercano = pathFinder.nodoMasCercano; DebugRuta(ruta); return(TransformarRuta(ruta)); }
IEnumerator PedirPermiso(Vector2 objetivo) { //Debug.Log("PEDIR PERMISO: " + npc.nombre); npc.estadoActual = EstadoNPC.Quieto; PathfinderNPC npcEsperado = null; Vector2 objetivoSiguiente = GetObjetivoSiguiente(objetivo); while (true) { npcEsperado = m_ciudad.NPCs.Find(x => x.posOcupadas.Contains(objetivoSiguiente)); if (npcEsperado != null) { while (true) { if (npcEsperado.posOcupadas.Contains(GetPosActualGrilla()) || !npcEsperado.NpcCaminando() || EsPosible(GetPosActualGrilla(), objetivoSiguiente)) { break; } yield return(null); } yield return(new WaitWhile(() => pies.moviendo)); if (GetPosActualGrilla().x - objetivoSiguiente.x == 0 || GetPosActualGrilla().y - objetivoSiguiente.y == 0) { if (!npcEsperado.NpcCaminando()) { //Debug.Log(npcEsperado.npc.nombre); if (!EsFuturaPosicion(objetivo)) { npcEsperado.DejarPasar(objetivo); } EsperarNPC(objetivo); yield return(new WaitWhile(() => npc.estadoActual == EstadoNPC.Esperando)); } else if (EsPosibleMoverOcupar(GetPosActualGrilla(), objetivoSiguiente)) { MoverOcupar(objetivoSiguiente); } else { DejarPasar(npcEsperado.ultimaPosicion.Value); npcEsperado.EsperarNPC(npcEsperado.ultimaPosicion.Value); yield return(new WaitWhile(() => npcEsperado.npc.estadoActual == EstadoNPC.Esperando)); } yield return(new WaitWhile(() => pies.moviendo)); } if (m_objetivoFinal.HasValue && GetPosActualGrilla() == m_objetivoFinal) { //Debug.LogError(npc.nombre); enRuta = false; m_objetivoFinal = null; } else { if (GetPosActualGrilla().x - objetivo.x != 0 && GetPosActualGrilla().y - objetivo.y != 0) { if (corutinaAux == null) { RutaTo(objetivo, false); } } else { Acercarse(objetivo); } } yield break; } else if (EsPosible(GetPosActualGrilla(), objetivo)) { RutaTo(objetivo, false); yield break; } yield return(new WaitForSeconds(0.1f)); } }
public void Init() { m_col2D = GetComponent <PolygonCollider2D>(); m_npc = GetComponentInParent <NPC>(); pathfinderNpc = m_npc.pathfinder; }