pianotm

Tactics Controller

Dec 5th, 2021 (edited)
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TacticsController : MonoBehaviour
  6. {
  7.     List<Tile> selectableTiles = new List<Tile>();
  8.     GameObject[] tiles;
  9.  
  10.     Stack<Tile> path = new Stack<Tile>();
  11.     Tile currentTile;
  12.  
  13.     public int move = 5;
  14.     public float jumpHeight = 2;
  15.     public float moveSpeed = 2;
  16.  
  17.     Vector3 velocity = new Vector3();
  18.     Vector3 heading = new Vector3();
  19.  
  20.     float halfHeight = 0;
  21.  
  22.     protected void Init()
  23.     {
  24.         tiles = GameObject.FindGameObjectsWithTag("Tile");
  25.  
  26.         halfHeight = GetComponent<Collider>().bounds.extents.y;
  27.  
  28.     }
  29.  
  30.     public void GetCurrentTile()
  31.     {
  32.         currentTile = GetTargetTile(gameObject);
  33.         currentTile.current = true;
  34.     }
  35.  
  36.     public Tile GetTargetTile(GameObject target)
  37.     {
  38.         RaycastHit hit;
  39.         Tile tile = null;
  40.  
  41.         if (Physics.Raycast(target.transform.position, -Vector3.up, out hit, 1))
  42.         {
  43.             tile = hit.collider.GetComponent<Tile>();
  44.         }
  45.  
  46.         return tile;
  47.     }
  48.  
  49.     public void ComputeAdjacencyLists()
  50.     {
  51.         foreach (GameObject tile in tiles)
  52.         {
  53.             Tile t = tile.GetComponent<Tile>();
  54.             t.FindNeighbors(jumpHeight);
  55.         }
  56.     }
  57.  
  58.     public void FindSelectableTiles()
  59.     {
  60.         ComputeAdjacencyLists();
  61.         GetCurrentTile();
  62.  
  63.         Queue<Tile> process = new Queue<Tile>();
  64.  
  65.         process.Enqueue(currentTile);
  66.         currentTile.visited = true;
  67.         //currentTile.parent = ?? Leave as null
  68.  
  69.         while (process.Count > 0)
  70.         {
  71.             Tile t = process.Dequeue();
  72.  
  73.             selectableTiles.Add(t);
  74.             t.selectable = true;
  75.  
  76.             if (t.distance < move)
  77.             {
  78.                 foreach (Tile tile in t.adjacencyList)
  79.                 {
  80.                     if (!tile.visited)
  81.                     {
  82.                         tile.parent = t;
  83.                         tile.visited = true;
  84.                         tile.distance = 1 + t.distance;
  85.                         process.Enqueue(tile);
  86.                     }
  87.                 }
  88.             }
  89.         }
  90.  
  91.     }
  92. }
  93.  
Add Comment
Please, Sign In to add comment