} // Is the agent alive or not? #endregion #region Methods // Use this for initialization protected virtual void Start() { //Debug.Log("Called"); Alive = true; // Initialize events mouseOverEvent = new EnemyMouseOverEvent(); enemyDeathEvent = new EnemyDeathEvent(); enemyPursueEvent = new EnemyFoundEvent(); // Add this script to the list of invokers for the EnemyMouseOverEvent EventManager.AddEnemyMouseOverInvoker(this); EventManager.AddEnemyDeathInvoker(this); GetComponentInChildren <FOVScript>().AddEnemyFoundEventListener(OnEnemyAttackEvent); // Initialize object variables speed = MAX_SPEED; nextSpot = 0; patrolDirection = PatrolDirection.FORWARD; path = new List <Vector3>(); animator = GetComponent <Animator>(); // Start in the wait state TransitionToWaitState(); }
public void SwitchDirection() { if (patrolDirection == PatrolDirection.HORIZONTAL) { patrolDirection = PatrolDirection.VERTICAL; } else { patrolDirection = PatrolDirection.HORIZONTAL; } UpdateArrowDirection(); }
/// <summary> /// Choose the next patrol location to move toward. If we are /// in the reversible patrol, we go back over the same locations. /// If we are in the cyclical patrol, we wrap aroundl. If we've /// been interrupted, pick the closest patrol point to restart. /// </summary> protected virtual void ChooseNextPatrolTarget() { Debug.Log("Patrolling"); // If there are no patrol positions, return if (patrolPositions.Length == 0) { Debug.Log("ERROR: no patrol points"); return; } nextSpot += (int)patrolDirection; // If we're on a reversible path if (ReversingPath) { // If we've reached the end, reverse the path if (nextSpot >= patrolPositions.Length || nextSpot < 0) { patrolDirection = (PatrolDirection)(-1 * (int)patrolDirection); nextSpot += (int)patrolDirection; } } // Otherwise, we're on a cyclical path else { // If we've reached the end, restart the path if (nextSpot >= patrolPositions.Length) { nextSpot = 0; } } // If somehow we're at a non-existent patrolPosition, choose the closest to resume patrol if (nextSpot >= patrolPositions.Length || nextSpot < 0) { nextSpot = 0; for (int i = 0; i < patrolPositions.Length; i++) { if (Vector2.Distance(transform.position, patrolPositions[i].transform.position) < Vector2.Distance(transform.position, patrolPositions[nextSpot].transform.position)) { nextSpot = i; } } } FindNewPath(patrolPositions[nextSpot].transform.position); }
/// <summary> /// 判断是否转到指定方向 /// </summary> /// <returns>是否可以转向</returns> private bool CanGo(PatrolDirection Direction) { switch (Direction) { case PatrolDirection.None: throw new Exception("试图转向:None"); case PatrolDirection.Up: return(GameMap.MapCellsClone[this.TabelLocation.Y - 1, this.TabelLocation.X] == Map.CellType.Ground); case PatrolDirection.Down: return(GameMap.MapCellsClone[this.TabelLocation.Y + 1, this.TabelLocation.X] == Map.CellType.Ground); case PatrolDirection.Left: return(GameMap.MapCellsClone[this.TabelLocation.Y, this.TabelLocation.X - 1] == Map.CellType.Ground); case PatrolDirection.Right: return(GameMap.MapCellsClone[this.TabelLocation.Y, this.TabelLocation.X + 1] == Map.CellType.Ground); } return(false); }
private void Update() { if (patrolState == state.PATROL) { this.transform.position = Vector3.MoveTowards(this.transform.position, target, speed * Time.deltaTime); if (transform.position == target) { direction = (PatrolDirection)(((int)direction + 1) % 4); if (direction == PatrolDirection.RIGHT) { target = new Vector3(target.x + 6, target.y, target.z); } else if (direction == PatrolDirection.DOWN) { target = new Vector3(target.x, target.y, target.z - 6); } else if (direction == PatrolDirection.UP) { target = new Vector3(target.x, target.y, target.z + 6); } else if (direction == PatrolDirection.LEFT) { target = new Vector3(target.x - 6, target.y, target.z); } } } else if (patrolState == state.CATCH) { this.transform.position = Vector3.MoveTowards(this.transform.position, player.transform.position, catchSpeed * Time.deltaTime); if (this.transform.position == player.transform.position) { Singleton <GameEventManager> .Instance.GameOver(); } } }
public GameObject GetPatrol(Vector3 pos, PatrolDirection d) { GameObject patrol = null; if (free.Count == 0) { patrol = Instantiate <GameObject>(Resources.Load <GameObject>("prefabs/FreeVoxelGirlPrefab 1")); patrol.AddComponent <PatrolData>(); } else { patrol = free[0].gameObject; free.Remove(free[0]); } patrol.transform.position = pos; patrol.SetActive(true); patrol.GetComponent <PatrolData>().direction = (PatrolDirection)(((int)d + 3) % 4); patrol.GetComponent <PatrolData>().initDirection = (PatrolDirection)(((int)d + 3) % 4); patrol.GetComponent <PatrolData>().target = pos; patrol.GetComponent <PatrolData>().initTarget = pos; used.Add(patrol.GetComponent <PatrolData>()); return(patrol); }
/// <summary> /// 敌人角色自动巡逻 /// </summary> private void StartPatrol() { //记录移动之前的坐标,作为事件参数 //Debug.Print("敌人开始巡逻!"); Point LastLocation = this.Location; //记录玩家是否紧跟着敌人防止了炸弹 bool PlaceBombClose; while (true) { PlaceBombClose = false; //Debug.Print(DateTime.Now.ToString() + " 巡逻方向:" + LastDirection.ToString()); switch (NextDirection) { case PatrolDirection.None: { //四周都是墙壁,无法巡逻时等待 System.Threading.Thread.Sleep(UnityRandom.Next(500)); if (CanGo(PatrolDirection.Up)) { NextDirection = PatrolDirection.Up; break; } if (CanGo(PatrolDirection.Down)) { NextDirection = PatrolDirection.Down; break; } if (CanGo(PatrolDirection.Left)) { NextDirection = PatrolDirection.Left; break; } if (CanGo(PatrolDirection.Right)) { NextDirection = PatrolDirection.Right; break; } break; } case PatrolDirection.Up: { if (CanGo(PatrolDirection.Up)) { this.TabelLocation.Offset(0, -1); int Target = this.TabelLocation.Y * GameMap.CellSize.Height + GameMap.PaddingSize.Height; while (this.Location.Y > Target + 5) { Thread.Sleep(100); //检查玩家是不是紧跟着放置了炸弹 if (GameMap.MapCellsClone[TabelLocation.Y, TabelLocation.X] == Map.CellType.Mine) { NextDirection = PatrolDirection.Down; PlaceBombClose = true; break; } LastLocation = new Point(this.Location.X, this.Location.Y); this.Location.Offset(0, -5); if (this == null) { System.Windows.Forms.MessageBox.Show("this is null."); } if (LastLocation == null) { System.Windows.Forms.MessageBox.Show("LastLocation is null."); } Patrol(this, LastLocation); } if (PlaceBombClose) { break; } LastLocation = new Point(this.Location.X, this.Location.Y); this.Location = new Point(this.Location.X, Target); if (this == null) { System.Windows.Forms.MessageBox.Show("this is null."); } if (LastLocation == null) { System.Windows.Forms.MessageBox.Show("LastLocation is null."); } Patrol(this, LastLocation); //正常移动过程中有很小概率转向 if (UnityRandom.NextDouble() > 0.9) { //Debug.Print(DateTime.Now.ToString() + " 巡逻时随机转向!"); NextDirection = GetRandomDirection(); } } else { //移动撞墙时转向 //Debug.Print(DateTime.Now.ToString() + " 撞墙,强制转向!"); NextDirection = GetRandomDirection(); } break; } case PatrolDirection.Down: { if (CanGo(PatrolDirection.Down)) { this.TabelLocation.Offset(0, 1); int Target = this.TabelLocation.Y * GameMap.CellSize.Height + GameMap.PaddingSize.Height; while (this.Location.Y < Target - 5) { Thread.Sleep(100); //检查玩家是不是紧跟着放置了炸弹 if (GameMap.MapCellsClone[TabelLocation.Y, TabelLocation.X] == Map.CellType.Mine) { NextDirection = PatrolDirection.Up; PlaceBombClose = true; break; } LastLocation = new Point(this.Location.X, this.Location.Y); this.Location.Offset(0, +5); if (this == null) { System.Windows.Forms.MessageBox.Show("this is null."); } if (LastLocation == null) { System.Windows.Forms.MessageBox.Show("LastLocation is null."); } Patrol(this, LastLocation); } if (PlaceBombClose) { break; } LastLocation = new Point(this.Location.X, this.Location.Y); this.Location = new Point(this.Location.X, Target); if (this == null) { System.Windows.Forms.MessageBox.Show("this is null."); } if (LastLocation == null) { System.Windows.Forms.MessageBox.Show("LastLocation is null."); } Patrol(this, LastLocation); //正常移动过程中有很小概率转向 if (UnityRandom.NextDouble() > 0.9) { //Debug.Print(DateTime.Now.ToString() + " 巡逻时随机转向!"); NextDirection = GetRandomDirection(); } } else { //移动撞墙时转向 //Debug.Print(DateTime.Now.ToString() + " 撞墙,强制转向!"); NextDirection = GetRandomDirection(); } break; } case PatrolDirection.Left: { if (CanGo(PatrolDirection.Left)) { this.TabelLocation.Offset(-1, 0); int Target = this.TabelLocation.X * GameMap.CellSize.Width + GameMap.PaddingSize.Width; while (this.Location.X > Target + 5) { Thread.Sleep(100); //检查玩家是不是紧跟着放置了炸弹 if (GameMap.MapCellsClone[TabelLocation.Y, TabelLocation.X] == Map.CellType.Mine) { NextDirection = PatrolDirection.Right; PlaceBombClose = true; break; } LastLocation = new Point(this.Location.X, this.Location.Y); this.Location.Offset(-5, 0); if (this == null) { System.Windows.Forms.MessageBox.Show("this is null."); } if (LastLocation == null) { System.Windows.Forms.MessageBox.Show("LastLocation is null."); } Patrol(this, LastLocation); } if (PlaceBombClose) { break; } LastLocation = new Point(this.Location.X, this.Location.Y); this.Location = new Point(Target, this.Location.Y); if (this == null) { System.Windows.Forms.MessageBox.Show("this is null."); } if (LastLocation == null) { System.Windows.Forms.MessageBox.Show("LastLocation is null."); } Patrol(this, LastLocation); //正常移动过程中有很小概率转向 if (UnityRandom.NextDouble() > 0.9) { //Debug.Print(DateTime.Now.ToString() + " 巡逻时随机转向!"); NextDirection = GetRandomDirection(); } } else { //移动撞墙时转向 //Debug.Print(DateTime.Now.ToString() + " 撞墙,强制转向!"); NextDirection = GetRandomDirection(); } break; } case PatrolDirection.Right: { if (CanGo(PatrolDirection.Right)) { this.TabelLocation.Offset(1, 0); int Target = this.TabelLocation.X * GameMap.CellSize.Width + GameMap.PaddingSize.Width; while (this.Location.X < Target - 5) { Thread.Sleep(100); //检查玩家是不是紧跟着放置了炸弹 if (GameMap.MapCellsClone[TabelLocation.Y, TabelLocation.X] == Map.CellType.Mine) { NextDirection = PatrolDirection.Left; PlaceBombClose = true; break; } LastLocation = new Point(this.Location.X, this.Location.Y); this.Location.Offset(+5, 0); if (this == null) { System.Windows.Forms.MessageBox.Show("this is null."); } if (LastLocation == null) { System.Windows.Forms.MessageBox.Show("LastLocation is null."); } Patrol(this, LastLocation); } if (PlaceBombClose) { break; } LastLocation = new Point(this.Location.X, this.Location.Y); this.Location = new Point(Target, this.Location.Y); if (this == null) { System.Windows.Forms.MessageBox.Show("this is null."); } if (LastLocation == null) { System.Windows.Forms.MessageBox.Show("LastLocation is null."); } Patrol(this, LastLocation); //正常移动过程中有很小概率转向 if (UnityRandom.NextDouble() > 0.9) { //Debug.Print(DateTime.Now.ToString() + " 巡逻时随机转向!"); NextDirection = GetRandomDirection(); } } else { //移动撞墙时转向 //Debug.Print(DateTime.Now.ToString() + " 撞墙,强制转向!"); NextDirection = GetRandomDirection(); } break; } } } }
public override void Patrol(PatrolDirection direction, double time) { _camera.CameraMovement.Patrol(direction, time); }
public virtual void Patrol(PatrolDirection direction, double time) { }
private void FixedUpdate() { if (isDead) { animator.SetBool("walking", false); return; } Vector3Int dir = Vector3Int.zero; switch (patrolDirection) { case PatrolDirection.Up: dir = new Vector3Int(0, 1, 0); if (!CanMove((Vector2Int)dir)) { patrolDirection = PatrolDirection.Down; return; } break; case PatrolDirection.Down: dir = new Vector3Int(0, -1, 0); if (!CanMove((Vector2Int)dir)) { patrolDirection = PatrolDirection.Up; return; } break; case PatrolDirection.Left: dir = new Vector3Int(1, 0, 0); if (!CanMove((Vector2Int)dir)) { patrolDirection = PatrolDirection.Right; return; } break; case PatrolDirection.Right: dir = new Vector3Int(-1, 0, 0); if (!CanMove((Vector2Int)dir)) { patrolDirection = PatrolDirection.Left; return; } break; } if (!isEating) { rb.position = Vector2.MoveTowards(rb.position, nextPos, speed * Time.fixedDeltaTime); if (Vector2.Distance(rb.position, nextPos) <= minDistanceBeforeNextMove) { SetNextPos((Vector3)rb.position + dir); animator.SetBool("walking", true); } } else { animator.SetBool("walking", false); tmrEating += Time.deltaTime; if (tmrEating >= eatingTime) { setEating(false); transform.eulerAngles = oldEuler; tmrEating = 0; } } }