pianotm

Tile

Dec 5th, 2021 (edited)
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Tile : MonoBehaviour
  6. {
  7.     public bool walkable = true;
  8.     public bool current = false;
  9.     public bool target = false;
  10.     public bool selectable = false;
  11.  
  12.     public List<Tile> adjacencyList = new List<Tile>();
  13.  
  14.     //Needs Breadth First Search (BFS)
  15.     public bool visited = false;
  16.     public Tile parent = null;
  17.     public int distance = 0;
  18.  
  19.     // Start is called before the first frame update
  20.     void Start()
  21.     {
  22.  
  23.     }
  24.  
  25.     // Update is called once per frame
  26.     void Update ()
  27.     {
  28.         if (current)
  29.         {
  30.             GetComponent<Renderer>().material.color = Color.magenta;
  31.         }
  32.         else if (target)
  33.         {
  34.             GetComponent<Renderer>().material.color = Color.green;
  35.         }
  36.         else if (selectable)
  37.         {
  38.             GetComponent<Renderer>().material.color = Color.red;
  39.         }
  40.         else
  41.         {
  42.             GetComponent<Renderer>().material.color = Color.white;
  43.         }
  44.  
  45.     }
  46.  
  47.     public void Reset()
  48.     {
  49.         adjacencyList.Clear();
  50.  
  51.         current = false;
  52.         target = false;
  53.         selectable = false;
  54.        
  55.         visited = false;
  56.         parent = null;
  57.         distance = 0;
  58.     }
  59.  
  60.     public void FindNeighbors(float jumpHeight)
  61.     {
  62.         Reset();
  63.  
  64.         CheckTile(Vector3.forward, jumpHeight);
  65.         CheckTile(-Vector3.forward, jumpHeight);
  66.         CheckTile(Vector3.right, jumpHeight);
  67.         CheckTile(-Vector3.right, jumpHeight);
  68.     }
  69.  
  70.     public void CheckTile(Vector3 direction, float jumpHeight)
  71.     {
  72.         Vector3 halfExtents = new Vector3(0.25f, (1 + jumpHeight) / 2.0f, 0.25f);
  73.         Collider[] colliders = Physics.OverlapBox(transform.position + direction, halfExtents);
  74.  
  75.         foreach (Collider item in colliders)
  76.         {
  77.             Tile tile = item.GetComponent<Tile>();
  78.             if (tile != null && tile.walkable)
  79.             {
  80.                 RaycastHit hit;
  81.  
  82.                 if (Physics.Raycast(tile.transform.position, Vector3.up, out hit, 1))
  83.                 {
  84.                     adjacencyList.Add(tile);
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }
Add Comment
Please, Sign In to add comment