IEnumerator FindNode() { while (true) { foreach (Spider _monster in monstersClass) { if (_monster.inGameObject) { if (_monster.m_Script.findPlayer && !_monster.m_Script.isCornering && !_monster.m_Script.isShooting) { if (_monster.prePosition != _monster.GetPosition()) { _monster.prePosition = _monster.GetPosition(); } DirandRot _nextDR = GetNextNode(_monster); if (_nextDR.Equals(null)) { _nextDR = new DirandRot(new Vector2(), Quaternion.Euler(0, 0, 0)); } _monster.inGameObject.GetComponent <Monster_Spider_AI>().PassNextNode(_nextDR); } } } yield return(new WaitForSeconds(0.1f)); } }
void Cornering(DirandRot dr) { isCornering = true; m_rigidbody2D.velocity = Vector2.zero; if (Mathf.Round(Quaternion.Euler(0, 0, transform.rotation.eulerAngles.z + 90f).eulerAngles.z) == Mathf.Round(dr.rot.eulerAngles.z)) { StartCoroutine("InCornering", dr); } else if (Mathf.Round(Quaternion.Euler(0, 0, transform.rotation.eulerAngles.z - 90f).eulerAngles.z) == Mathf.Round(dr.rot.eulerAngles.z)) { StartCoroutine("OutCornering", dr); } }
void Move() { if (isCornering) { return; } if (!findPlayer || isHooking || isShooting) { return; } //is this have to Cornering? if (currentDR.Equals(null)) { currentDR = new DirandRot(new Vector2(), Quaternion.Euler(0, 0, 0)); } if (currentDR.dir != null && (Mathf.Round(transform.right.x) != currentDR.dir.x && Mathf.Round(transform.right.y) != currentDR.dir.y)) { if (!isCornering) { Cornering(currentDR); } } else { if (currentDR.dir != Vector2.zero && currentDR.dir != null) { m_Animator.SetBool("Walk", true); } else { m_Animator.SetBool("Walk", false); return; } transform.position += (Vector3)currentDR.dir * spiderInfo.moveSpeed * Time.deltaTime; transform.rotation = currentDR.rot; } }
IEnumerator InCornering(DirandRot dr) { //Debug.Log("InCornering"); float rotate = 0f; float checkRotate = 0f; float turnRadius = 0f; if (Mathf.Round(transform.up.y) == 0f) { turnRadius = 0.5f + Mathf.Abs(Mathf.FloorToInt(transform.position.x) + 0.5f - transform.position.x); rotate = spiderInfo.turnSpeed / turnRadius / Mathf.PI * 180f; } else if (Mathf.Round(transform.up.x) == 0f) { turnRadius = 0.5f + Mathf.Abs(Mathf.FloorToInt(transform.position.y) + 0.5f - transform.position.y); rotate = spiderInfo.turnSpeed / turnRadius / Mathf.PI * 180f; } while (true) { if (!isShooting) { m_Animator.SetBool("Walk", true); if (checkRotate >= 90f) { break; } //Debug.Log("InCornering: " + rotate); transform.Rotate(0, 0, rotate * Time.deltaTime); checkRotate += rotate * Time.deltaTime; transform.position += transform.right.normalized * Mathf.Sin((rotate * Mathf.PI / 180f * Time.deltaTime) / 2) * turnRadius * 2; } yield return(null); } transform.rotation = dr.rot; isCornering = false; yield break; }
public List <SpiderNode> FindNextNode(List <SpiderNode> nodes, Spider _monster) { List <SpiderNode> _connectNodeList = new List <SpiderNode>(); if (parentNode == this) { List <DirandRot> _canMoveVectors = _monster.FindCanMoveVectors(this); foreach (DirandRot _DR in _canMoveVectors) { if (_DR == null) { continue; } foreach (DirandRot _parentDR in _monster.FindCollisionVectors()) { if (_parentDR == null) { continue; } parentDR = _parentDR; Vector2 nextNodePosition = this.position; DirandRot downDR; if (parentDR.rot.eulerAngles.y == 0f) { downDR = new DirandRot(new Vector2(parentDR.dir.y, -parentDR.dir.x), Quaternion.Euler(parentDR.rot.eulerAngles.x, parentDR.rot.eulerAngles.y, parentDR.rot.eulerAngles.z - 90f)); } else { downDR = new DirandRot(new Vector2(-parentDR.dir.y, parentDR.dir.x), Quaternion.Euler(parentDR.rot.eulerAngles.x, parentDR.rot.eulerAngles.y, parentDR.rot.eulerAngles.z - 90f)); } DirandRot checkDR; checkDR = new DirandRot(new Vector2(-parentDR.dir.y, parentDR.dir.x), parentDR.rot.eulerAngles.y == 0f ? Quaternion.Euler(parentDR.rot.eulerAngles.x, parentDR.rot.eulerAngles.y, parentDR.rot.eulerAngles.z + 90f) : Quaternion.Euler(parentDR.rot.eulerAngles.x, parentDR.rot.eulerAngles.y, parentDR.rot.eulerAngles.z - 90f)); if (_DR == parentDR) // 직진할 경우 { bool isFloorExist = true; foreach (DirandRot d in _canMoveVectors) { if (d == null) { continue; } if (d == downDR) // 바닥이 없을 경우. { isFloorExist = false; break; } } if (isFloorExist) { nextNodePosition = this.position + _DR.dir; checkDR = parentDR; } } if (this.position == nextNodePosition && _DR == checkDR) //반시계 90도 회전. { bool canTurn = true; if (parentDR.rot.eulerAngles.y != 0f) { foreach (DirandRot d in _canMoveVectors) { if (d == null) { continue; } if (d == downDR) // 바닥이 없을 경우. { canTurn = false; break; } else if (d == parentDR) // 앞쪽 벽이 없을 경우. { canTurn = false; break; } } } if (canTurn) { nextNodePosition = this.position + checkDR.dir; } } else if (this.position == nextNodePosition) { // 시계 90도 회전. 아마도. checkDR = new DirandRot(new Vector2(parentDR.dir.y, -parentDR.dir.x), parentDR.rot.eulerAngles.y == 0f ? Quaternion.Euler(parentDR.rot.eulerAngles.x, parentDR.rot.eulerAngles.y, parentDR.rot.eulerAngles.z - 90f) : Quaternion.Euler(parentDR.rot.eulerAngles.x, parentDR.rot.eulerAngles.y, parentDR.rot.eulerAngles.z + 90f)); if (_DR == checkDR) //가는 경로 바닥에 타일 있는지 확인 조건 추가. { bool canTurn = true; if (parentDR.rot.eulerAngles.y == 0f) { foreach (DirandRot d in _canMoveVectors) { if (d == null) { continue; } if (d == downDR) // 바닥이 없을 경우. { canTurn = false; break; } else if (d == parentDR) // 앞쪽 벽이 없을 경우. { canTurn = false; break; } } } if (canTurn) { nextNodePosition = this.position + checkDR.dir; } } } if (nextNodePosition != this.position) { foreach (SpiderNode _node in nodes) { if (_node.position == nextNodePosition) { _node.parentDR = new DirandRot(checkDR.dir, checkDR.rot); _connectNodeList.Add(_node); break; } } } } } } else { foreach (DirandRot _DR in canMoveVectors) { if (_DR == null) { continue; } Vector2 nextNodePosition = this.position; DirandRot downDR; if (parentDR.rot.eulerAngles.y == 0f) { downDR = new DirandRot(new Vector2(parentDR.dir.y, -parentDR.dir.x), Quaternion.Euler(parentDR.rot.eulerAngles.x, parentDR.rot.eulerAngles.y, parentDR.rot.eulerAngles.z - 90f)); } else { downDR = new DirandRot(new Vector2(-parentDR.dir.y, parentDR.dir.x), Quaternion.Euler(parentDR.rot.eulerAngles.x, parentDR.rot.eulerAngles.y, parentDR.rot.eulerAngles.z - 90f)); } DirandRot checkDR; checkDR = new DirandRot(new Vector2(-parentDR.dir.y, parentDR.dir.x), parentDR.rot.eulerAngles.y == 0f ? Quaternion.Euler(parentDR.rot.eulerAngles.x, parentDR.rot.eulerAngles.y, parentDR.rot.eulerAngles.z + 90f) : Quaternion.Euler(parentDR.rot.eulerAngles.x, parentDR.rot.eulerAngles.y, parentDR.rot.eulerAngles.z - 90f)); if (_DR == parentDR) // 직진할 경우 { bool isFloorExist = true; foreach (DirandRot d in canMoveVectors) { if (d == null) { continue; } if (d == downDR) // 바닥이 없을 경우. { isFloorExist = false; break; } } if (isFloorExist) { nextNodePosition = this.position + _DR.dir; checkDR = parentDR; } } if (this.position == nextNodePosition && _DR == checkDR) //반시계 90도 회전. { bool canTurn = true; if (parentDR.rot.eulerAngles.y != 0f) { foreach (DirandRot d in canMoveVectors) { if (d == null) { continue; } if (d == downDR) // 바닥이 없을 경우. { canTurn = false; break; } else if (d == parentDR) // 앞쪽 벽이 없을 경우. { canTurn = false; break; } } } if (canTurn) { nextNodePosition = this.position + checkDR.dir; } } else if (this.position == nextNodePosition) { // 시계 90도 회전. 아마도. checkDR = new DirandRot(new Vector2(parentDR.dir.y, -parentDR.dir.x), parentDR.rot.eulerAngles.y == 0f ? Quaternion.Euler(parentDR.rot.eulerAngles.x, parentDR.rot.eulerAngles.y, parentDR.rot.eulerAngles.z - 90f) : Quaternion.Euler(parentDR.rot.eulerAngles.x, parentDR.rot.eulerAngles.y, parentDR.rot.eulerAngles.z + 90f)); if (_DR == checkDR) //가는 경로 바닥에 타일 있는지 확인 조건 추가. { bool canTurn = true; if (parentDR.rot.eulerAngles.y == 0f) { foreach (DirandRot d in canMoveVectors) { if (d == null) { continue; } if (d == downDR) // 바닥이 없을 경우. { canTurn = false; break; } else if (d == parentDR) // 앞쪽 벽이 없을 경우. { canTurn = false; break; } } } if (canTurn) { nextNodePosition = this.position + checkDR.dir; } } } if (nextNodePosition != this.position) { foreach (SpiderNode _node in nodes) { if (_node.position == nextNodePosition) { _node.parentDR = new DirandRot(checkDR.dir, checkDR.rot); _connectNodeList.Add(_node); break; } } } } } if (_connectNodeList.Count == 0) { Debug.Log(this.position + " : this SpiderNode can't find connected Node!"); } return(_connectNodeList); }
public void PassNextNode(DirandRot _currentDR) { currentDR = _currentDR; }
public bool AngleEquals(DirandRot obj1) { return(Mathf.Round(obj1.rot.eulerAngles.x) == Mathf.Round(this.rot.eulerAngles.x) && Mathf.Round(obj1.rot.eulerAngles.y) == Mathf.Round(this.rot.eulerAngles.y) && Mathf.Round(obj1.rot.eulerAngles.z) == Mathf.Round(this.rot.eulerAngles.z)); }