Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- using System;
- using System.Linq;
- public class PathSite : MonoBehaviour, Node{
- private List<PathSite> previousNeighbouringSites=new List<PathSite>();
- public List<PathSite> NeighbouringSites=new List<PathSite>();
- void OnValidate(){
- List<PathSite> wereRemoved = previousNeighbouringSites.Except<PathSite>(NeighbouringSites).ToList();
- List<PathSite> wereAdded = NeighbouringSites.Except<PathSite>(previousNeighbouringSites).ToList();
- foreach (PathSite p in wereRemoved){
- if(p != null && p.NeighbouringSites.Contains(this)){
- Debug.Log("removed site");
- p.NeighbouringSites.Remove(this);
- p.previousNeighbouringSites=new List<PathSite>(p.NeighbouringSites);
- }
- }
- foreach (PathSite p in wereAdded){
- if(p!= null && !p.NeighbouringSites.Contains(this)){
- Debug.Log("added site");
- //check if there any empty slots
- int i=p.NeighbouringSites.IndexOf(null);
- if(i!=-1){
- p.NeighbouringSites[i]=this;
- }else{
- p.NeighbouringSites.Add(this);
- }
- p.previousNeighbouringSites=new List<PathSite>(p.NeighbouringSites);
- }
- }
- previousNeighbouringSites=new List<PathSite>(NeighbouringSites);
- }
- void Start(){}
- void Update(){}
- public List<Node> neighbours(){
- List<Node> recastedNodes=new List<Node>();
- foreach (PathSite n in NeighbouringSites){
- recastedNodes.Add(n);
- }
- return recastedNodes;
- }
- public Double weight(Node n){
- return 1;
- }
- public void OnDrawGizmos() {
- Transform me=this.GetComponent<Transform>();
- foreach (PathSite n in NeighbouringSites) {
- if(n!= null && n.NeighbouringSites.Contains(this)){
- Gizmos.color = Color.blue;
- Transform neighbour=n.GetComponent<Transform>();
- Gizmos.DrawLine(neighbour.position, me.position);
- }else{
- Gizmos.color = Color.red;
- Gizmos.DrawSphere(me.position, .4f);
- }
- }
- Gizmos.color = Color.blue;
- Gizmos.DrawSphere(me.position, .25f);
- Gizmos.color = Color.black;
- Gizmos.DrawSphere(me.position-2*Vector3.up, .4f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement