Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class createTiles : MonoBehaviour
- {
- GameObject tile;
- bool black;
- GameObject currentActor;
- bool diagonalMode;
- GameObject currentTile;
- public List<Vector3> possibleSteps = new List<Vector3>();
- public Text text;
- public int step = 3;
- bool allow;
- public Transform player;
- bool correct;
- public int width, height;
- void Start()
- {
- // пока просто ровное поле шахматное 20x20
- for (int x = -width; x < width; x++)
- {
- black = !black;
- for (int z = -height; z < height; z++)
- {
- tile = Instantiate(GameObject.CreatePrimitive(PrimitiveType.Cube), new Vector3(x, 0, z), Quaternion.identity);
- tile.GetComponent<Renderer>().material.color = black ? Color.gray : Color.white;
- black = !black;
- tile.transform.parent = transform;
- }
- }
- }
- Vector3 FindNearest(float y)
- {
- // очень большое число
- Vector3 nearby = Vector3.zero;
- float d = Mathf.Infinity;
- for (int i = 0; i < possibleSteps.Count; i++)
- {
- // считаем дист до всех зверей
- float dist = Vector3.Distance(player.position, possibleSteps[i]);
- // если дист меньше очень большого числа (d), делаем d равным этой дистанции
- // поскольку это продолжающийся цикл, таким способом мы найдем самую небольшую дистанцию
- if (dist < d)
- {
- nearby = new Vector3(possibleSteps[i].x, y, possibleSteps[i].z);
- d = dist;
- }
- }
- // возвращаем ближайшего
- return nearby;
- }
- void Update()
- {
- // делаем лучик
- Vector3 pos = Input.mousePosition;
- Ray ray = Camera.main.ScreenPointToRay(pos);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit, 100f))
- {
- if (hit.collider.CompareTag("Player") && Input.GetMouseButtonDown(0))
- {
- Debug.Log(hit.collider.name);
- currentActor = hit.collider.gameObject;
- }
- if (currentActor != null)
- {
- if (hit.collider.tag != "Player")
- {
- currentTile = hit.collider.gameObject;
- }
- Vector3 heading = hit.collider.transform.position - currentActor.transform.position;
- var distance = heading.magnitude;
- // text.text = Mathf.Floor(distance).ToString();
- var direction = heading / distance;
- // Vector3.Dot это разница направлений от -1 до 1
- // мы сравниваем два напрвления актора вперед и вправо с тайлом
- // (d и d2) игнорируем отрицаиельные числа через Mathf.Abs (все равно прямая или диагональ сзади или спереди)
- // если режим Вперед и d или d2 равны 0 добро перемещаться в этот тайл
- // ecли режим Диагональ и d = d2 значит тоже добро
- // d и d2 всегда число от 0,0 до 1,0
- float d = Mathf.Abs(Vector3.Dot(direction, currentActor.transform.forward));
- float d2 = Mathf.Abs(Vector3.Dot(direction, currentActor.transform.right));
- int Mode = currentActor.GetComponent<ActorBehaviour>().walkingType;
- diagonalMode = Mode > 0; // если walkingType у актора 1 значит он должен ходить по диагонали, если 0 прямо
- bool diagonal = (d - d2) < 0.01f && (d - d2) > -0.01f; // чтобы разрешить ход, если диагональный режим
- bool straight = d < 0.01f || d2 < 0.01f;
- bool correctWay = diagonalMode ? diagonal : straight;
- if (diagonal) // ecли оба одинаковы это диагональ
- Debug.LogWarning("DIAGONAL");
- else
- Debug.LogWarning("STRAIGHT");
- if (Input.GetMouseButtonDown(0))
- {
- allow = false;
- possibleSteps = new List<Vector3>();
- for (int x = -step; x <= step; x++)
- {
- for (int z = -step; z <= step + 1; z++)
- {
- Vector3 posPos = new Vector3(currentActor.transform.position.x + x, currentActor.transform.position.y,
- currentActor.transform.position.z + z);
- Vector3 h = posPos - currentActor.transform.position;
- var dist = h.magnitude;
- var dir = h / dist;
- float pd = Mathf.Abs(Vector3.Dot(dir, currentActor.transform.forward));
- float pd2 = Mathf.Abs(Vector3.Dot(dir, currentActor.transform.right));
- bool diag = (pd - pd2) < 0.01f && (pd - pd2) > -0.01f; // чтобы разрешить ход, если диагональный режим
- bool str = pd < 0.01f || pd2 < 0.01f;
- correct = diagonalMode ? diag : str;
- if (correct)
- {
- possibleSteps.Add(posPos);
- Debug.Log("forward: " + pd + " right: " + pd2);
- //Instantiate(GameObject.CreatePrimitive(PrimitiveType.Sphere), posPos, Quaternion.identity);
- }
- }
- }
- allow = true;
- }
- if (Input.GetKeyDown(KeyCode.A) && allow)
- {
- currentActor.GetComponent<ActorBehaviour>().Target = FindNearest(currentActor.transform.position.y);
- allow = false;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement