private void UpdateCurrentTile() { //What sort of tile are we on Cell currentTile = CurrentCell; CurrentCell = Map.GetClosestTile(transform.position); if (currentTile != null && currentTile != CurrentCell) { //We've changed tiles, make sure the old tile is no longer occupied currentTile.NoLongerOccupied(self); if (!CurrentCell.Occupied) { //Since we've changed, our next tile should equal the target tile if (CurrentCell != TCell) { //If it doesn't equal then we've drifted off the tiles slightly, set the current tile the target tile CurrentCell = TCell; } if (Path != null) { if (Path.Count == 1) { CurrentCell.SetOccupied(self, true); } else { CurrentCell.SetOccupied(self, false); } } } } }
void Update() { //Если объект создан на данном компьютере if (GetComponent <NetworkView>().isMine) { LayerMask lrr = (1 << 12); if (!Physics.Raycast(transform.position, -transform.up, 10f, lrr)) { if (VARS.HP > 0) { //Пускаем луч в землю, находим точку передвижения #if UNITY_ANDROID Touch[] touches = Input.touches; if (touches.Length == 1) { if (touches[0].phase == TouchPhase.Began) #else if (Input.GetMouseButtonDown(1)) #endif { Ray rc = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(rc, out hit, Mathf.Infinity)) { if (hit.transform.tag == "Ground") { target = null; ACell = Map.GetClosestTile(hit.point); CurrentCell = Map.GetClosestTile(transform.position); FindObjectOfType <ThreadManager>().AddPathfindingThread(new GetPathThread(self, CurrentCell, hit.point, Const.BLOCKINGLEVEL_Normal)); Instantiate(Resources.Load("PointClick", typeof(GameObject)), hit.point, Quaternion.Euler(90, 0, 0)); } if ((hit.transform.tag == "Player") || (hit.transform.tag == "Enemy")) { target = hit.transform; if (target == transform) { transform.rotation = Quaternion.LookRotation((hit.point - transform.position).normalized); transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0); target = null; TCell = null; Path = null; rig.velocity = Vector3.zero; charS = CharS.Idle; } else { ACell = Map.GetClosestTile(target.position); CurrentCell = Map.GetClosestTile(transform.position); FindObjectOfType <ThreadManager>().AddPathfindingThread(new GetPathThread(self, CurrentCell, target.position, Const.BLOCKINGLEVEL_Normal)); } } } } } //Если мы не в точке назначения if (Path != null && Path.Count > 0) { //Двигаемся согласно пути MoveForward(); //Обновить текущий тайл UpdateCurrentTile(); } if (target != null) { if ((target.position - transform.position).magnitude < VARS.Range) { TCell = null; Path = null; charS = CharS.Attack; //Поворот модели в сторону противника Quaternion rot = Quaternion.LookRotation((target.position - transform.position).normalized); transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime * 10f); transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0); if (Time.time - damage_timer >= VARS.AttackSpeed + 1f) { damage_timer = Time.time; } if (target.tag == "Player") { if (target.GetComponent <PlayerVars>().HP <= 0) { target = null; } else if (Time.time - damage_timer >= VARS.AttackSpeed) { damage_timer = Time.time; } } if (target.tag == "Enemy") { if (target.GetComponent <EnemyAI>().HP > 0) { float DMG = Time.time - damage_timer; if (DMG >= VARS.AttackSpeed) { target.GetComponent <EnemyAI>().HP -= VARS.Damage * DMG; damage_timer = Time.time; } } else { target = null; } } } else if ((ACell != null) && (ACell != Map.GetClosestTile(target.position))) { ACell = Map.GetClosestTile(target.position); FindObjectOfType <ThreadManager>().AddPathfindingThread(new GetPathThread(self, CurrentCell, target.position, Const.BLOCKINGLEVEL_OccupiedStatic)); } } else if (charS == CharS.Attack) { charS = CharS.Idle; } } //Смерть else { charS = CharS.Death; TCell = null; Path = null; rig.velocity = Vector3.zero; GameObject.FindGameObjectWithTag("GameController").GetComponent <Connection>().Respawn = GetComponent <Controller>(); } } if (Input.GetKeyUp("z")) { VARS.HP = 0; } } //Если это клон других игроков, то назначаем принятые переменные в методе OnSerializeNetworkView else { syncTime += Time.deltaTime; rig.position = Vector3.Slerp(syncStartPosition, syncEndPosition, syncTime / syncDelay); Cell curcell = Map.GetClosestTile(transform.position); if (CurrentCell != curcell) { CurrentCell.NoLongerOccupied(self); CurrentCell = curcell; CurrentCell.SetOccupied(self, false); } if (chr == CharS.Idle || chr == CharS.Attack) { if (charS == CharS.Death) { VARS.HP = VARS.HPMAX; } CurrentCell.SetOccupied(self, true); } charS = chr; VARS.HP = HPP; transform.rotation = rtOUT; if (charS == CharS.Attack) { if (Time.time - damage_timer >= VARS.AttackSpeed + 1f) { damage_timer = Time.time; } Vector3 pos = new Vector3(transform.position.x, 1.6f, transform.position.z); if (Physics.Raycast(pos, transform.forward, out hit, speed / 4)) { float DMG = Time.time - damage_timer; if (DMG >= VARS.AttackSpeed) { if (hit.transform.tag == "Player") { hit.transform.GetComponent <PlayerVars>().HP -= VARS.Damage * DMG; damage_timer = Time.time; } if (hit.transform.tag == "Enemy") { hit.transform.GetComponent <EnemyAI>().HP -= VARS.Damage * DMG; damage_timer = Time.time; } } } } } //Контроллер анимаций if (anim) { switch (charS) { case CharS.Idle: anim.CrossFade("idle"); break; case CharS.Run: anim.CrossFade("run"); break; case CharS.Attack: anim.CrossFade("attack"); if (CurrentCell.Occupied) { CurrentCell.NoLongerOccupied(self); } break; case CharS.Death: anim.CrossFade("death"); break; } } }