1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class PelletScript : MonoBehaviour { private static Dictionary<Point, Node> nodes; private Point neighboursPos; public List<Node> neighbours = new List<Node>(); public List<Vector2> validDirections; public static PelletScript Instance { get; set; } public Point GridPosition { get; private set; } public Vector2 WorldPosition { get { return new Vector2(transform.position.x + (GetComponent<SpriteRenderer>().bounds.size.x / 2), transform.position.y - (GetComponent<SpriteRenderer>().bounds.size.y / 2)); } } public bool Walkable { get; set; } private void Awake() { Instance = this; } // Start is called before the first frame update void Start() { if (nodes == null) { CreateNodes(); } PelletScript currentPellet = this; for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { //Debug.Log(x + ";" + y); neighboursPos = new Point(currentPellet.GridPosition.X - x, currentPellet.GridPosition.Y - y); //Debug.Log(neighboursPos.X + " " + neighboursPos.Y); if (MapManager.instance.InBounds(neighboursPos) && MapManager.instance.AllPellets[neighboursPos].Walkable && neighboursPos != currentPellet.GridPosition) { Node neighbour = nodes[neighboursPos]; neighbours.Add(neighbour); } } } validDirections = new List<Vector2>(neighbours.Count); for (int i = 0; i < neighbours.Count; i++) { Node currentNeighbour = neighbours[i]; Vector3 currNPos = new Vector3(currentNeighbour.GridPosition.X, currentNeighbour.GridPosition.Y, 0); Vector2 tempVector = currNPos - transform.localPosition; validDirections[i] = tempVector.normalized; } } private static void CreateNodes() { //Instantiates the dictionary nodes = new Dictionary<Point, Node>(); //Run through all pellets in the game foreach (PelletScript pellet in MapManager.instance.AllPellets.Values) { nodes.Add(pellet.GridPosition, new Node(pellet)); } } public void Setup(Point gridPos, Vector3 worldPos) { Walkable = true; this.GridPosition = GridPosition; transform.position = worldPos; MapManager.instance.AllPellets.Add(gridPos, this); } } | cs |
PelletScript.CreateNodes () (at Assets/Scripts/PelletScript.cs:77)
PelletScript.Start () (at Assets/Scripts/PelletScript.cs:37)
가 뜨는데 어떻게 해야 키 중복이 안되는거냐?
A* 알고리즘 구동 어렵네
아래는 Point랑 노드 구조체임
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | using UnityEngine; public struct Point { public int X { get; set; } public int Y { get; set; } public Point (int x, int y) { this.X = x; this.Y = y; } public static bool operator == (Point first, Point second) { return first.X == second.X && first.Y == second.Y; } public static bool operator != (Point first, Point second) { return first.X != second.X || first.Y != second.Y; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Node { public Point GridPosition { get; private set; } public PelletScript PelletRef { get; private set; } public Node(PelletScript pelletRef) { this.PelletRef = pelletRef; this.GridPosition = pelletRef.GridPosition; } } | cs |
혹시 아는 애 있냐? 왜 키중복이 뜨지?
array가 Null로 들어가고 있기때문임. 저 안에 문제가 아니라 gridpos Setup하는 곳에서 포지션을 못받아오면서 생긴 문제.