Advertisement
DCSquid

Pathsite

Mar 25th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System;
  4. using System.Linq;
  5.  
  6.  
  7. public class PathSite : MonoBehaviour, Node{
  8.  
  9.     private List<PathSite> previousNeighbouringSites=new List<PathSite>();
  10.     public List<PathSite> NeighbouringSites=new List<PathSite>();
  11.  
  12.     void OnValidate(){
  13.         List<PathSite> wereRemoved = previousNeighbouringSites.Except<PathSite>(NeighbouringSites).ToList();
  14.         List<PathSite> wereAdded = NeighbouringSites.Except<PathSite>(previousNeighbouringSites).ToList();
  15.  
  16.         foreach (PathSite p in wereRemoved){
  17.             if(p != null && p.NeighbouringSites.Contains(this)){
  18.                 Debug.Log("removed site");
  19.                 p.NeighbouringSites.Remove(this);
  20.                 p.previousNeighbouringSites=new List<PathSite>(p.NeighbouringSites);
  21.             }
  22.         }
  23.  
  24.         foreach (PathSite p in wereAdded){
  25.             if(p!= null && !p.NeighbouringSites.Contains(this)){
  26.                 Debug.Log("added site");
  27.                 //check if there any empty slots
  28.                 int i=p.NeighbouringSites.IndexOf(null);
  29.                 if(i!=-1){
  30.                     p.NeighbouringSites[i]=this;
  31.                 }else{
  32.                     p.NeighbouringSites.Add(this);     
  33.                 }
  34.                 p.previousNeighbouringSites=new List<PathSite>(p.NeighbouringSites);
  35.             }
  36.         }
  37.  
  38.         previousNeighbouringSites=new List<PathSite>(NeighbouringSites);
  39.     }  
  40.  
  41.  
  42.     void Start(){}
  43.  
  44.     void Update(){}
  45.  
  46.     public List<Node> neighbours(){
  47.         List<Node> recastedNodes=new List<Node>();
  48.         foreach (PathSite n in NeighbouringSites){
  49.             recastedNodes.Add(n);
  50.         }
  51.         return recastedNodes;
  52.     }
  53.  
  54.     public Double weight(Node n){
  55.         return 1;
  56.     }
  57.  
  58.     public void OnDrawGizmos() {
  59.         Transform me=this.GetComponent<Transform>();
  60.         foreach (PathSite n in NeighbouringSites) {
  61.             if(n!= null && n.NeighbouringSites.Contains(this)){
  62.                 Gizmos.color = Color.blue;
  63.                 Transform neighbour=n.GetComponent<Transform>();
  64.                Gizmos.DrawLine(neighbour.position, me.position);
  65.             }else{
  66.                 Gizmos.color = Color.red;
  67.                 Gizmos.DrawSphere(me.position, .4f);   
  68.             }
  69.         }
  70.         Gizmos.color = Color.blue;
  71.         Gizmos.DrawSphere(me.position, .25f);
  72.         Gizmos.color = Color.black;
  73.         Gizmos.DrawSphere(me.position-2*Vector3.up, .4f);
  74.     }
  75.  
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement