mirror of
https://github.com/DerTyp7/traffic-unity.git
synced 2025-10-30 05:07:10 +01:00
nodes in street
This commit is contained in:
@@ -6,23 +6,38 @@ public class NodePlacer : MonoBehaviour
|
||||
private GameObject nodeObject;
|
||||
|
||||
[SerializeField]
|
||||
private TrafficNode prevPlacedNode;
|
||||
private TrafficNode selectedPlacedNode;
|
||||
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.N))
|
||||
{
|
||||
Debug.Log("H2213123IT");
|
||||
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
|
||||
|
||||
if (hit.collider != null)
|
||||
{
|
||||
Debug.Log("HIT");
|
||||
selectedPlacedNode = hit.collider.transform.gameObject.GetComponent<TrafficNode>();
|
||||
}
|
||||
else
|
||||
{
|
||||
selectedPlacedNode = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.B))
|
||||
{
|
||||
Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
newPosition.z = 0;
|
||||
TrafficNode newNode = Instantiate(nodeObject, newPosition, Quaternion.identity).GetComponent<TrafficNode>();
|
||||
|
||||
if (prevPlacedNode != null)
|
||||
if (selectedPlacedNode != null)
|
||||
{
|
||||
prevPlacedNode.AddNextNode(newNode);
|
||||
selectedPlacedNode.AddNextNode(newNode);
|
||||
|
||||
}
|
||||
prevPlacedNode = newNode;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
13
Assets/Scripts/PrefabDictionary.cs
Normal file
13
Assets/Scripts/PrefabDictionary.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using UnityEngine;
|
||||
public class PrefabDictionary : MonoBehaviour
|
||||
{
|
||||
public static PrefabDictionary instance;
|
||||
|
||||
public GameObject nodePrefab;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (instance == null)
|
||||
instance = this;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/PrefabDictionary.cs.meta
Normal file
11
Assets/Scripts/PrefabDictionary.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9bf91a6c0d2139a4dba9954918414857
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
76
Assets/Scripts/TrafficSystem/Street.cs
Normal file
76
Assets/Scripts/TrafficSystem/Street.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Street : MonoBehaviour
|
||||
{
|
||||
private List<TrafficNode> nodes = new List<TrafficNode>();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
nodes.Add(Instantiate(PrefabDictionary.instance.nodePrefab, transform).GetComponent<TrafficNode>());
|
||||
nodes.Add(Instantiate(PrefabDictionary.instance.nodePrefab, transform).GetComponent<TrafficNode>());
|
||||
nodes[0].transform.localPosition = new Vector3(-0.5f, 0, 0);
|
||||
nodes[1].transform.localPosition = new Vector3(0.5f, 0, 0);
|
||||
|
||||
nodes[0].AddNextNode(nodes[1]);
|
||||
|
||||
CreateNode(new Vector3(0, 0, 0));
|
||||
CreateNode(new Vector3(-0.25f, 0, 0));
|
||||
RemoveNode(CreateNode(new Vector3(0.33f, 0, 0)));
|
||||
CreateNode(new Vector3(-0.12f, 0, 0));
|
||||
}
|
||||
|
||||
private TrafficNode[] getNearestNodes(float positionX)
|
||||
{
|
||||
TrafficNode[] nearestNodes = new TrafficNode[2];
|
||||
nearestNodes[0] = nodes[0];
|
||||
nearestNodes[1] = nodes[1];
|
||||
foreach (TrafficNode node in nodes)
|
||||
{
|
||||
float distance = Mathf.Abs(node.transform.localPosition.x - positionX);
|
||||
|
||||
if (distance < Mathf.Abs(nearestNodes[0].transform.localPosition.x - positionX) && node.transform.localPosition.x < positionX)
|
||||
{
|
||||
nearestNodes[0] = node;
|
||||
}
|
||||
|
||||
if (distance < Mathf.Abs(nearestNodes[1].transform.localPosition.x - positionX) && node.transform.localPosition.x > positionX)
|
||||
{
|
||||
nearestNodes[1] = node;
|
||||
}
|
||||
}
|
||||
return nearestNodes;
|
||||
}
|
||||
|
||||
public TrafficNode CreateNode(Vector3 localPosition)
|
||||
{
|
||||
TrafficNode[] nearestNodes = getNearestNodes(localPosition.x);
|
||||
|
||||
TrafficNode newNode = Instantiate(PrefabDictionary.instance.nodePrefab, transform).GetComponent<TrafficNode>();
|
||||
newNode.transform.localPosition = localPosition;
|
||||
|
||||
nearestNodes[0].RemoveNextNode(nearestNodes[1]);
|
||||
nearestNodes[0].AddNextNode(newNode);
|
||||
newNode.AddNextNode(nearestNodes[1]);
|
||||
nodes.Insert(nodes.IndexOf(nearestNodes[1]), newNode);
|
||||
return newNode;
|
||||
}
|
||||
|
||||
public void RemoveNode(TrafficNode node)
|
||||
{
|
||||
TrafficNode[] nearestNodes = getNearestNodes(node.transform.localPosition.x);
|
||||
|
||||
nearestNodes[0].RemoveNextNode(node);
|
||||
|
||||
foreach (TrafficNode n in node.GetNextTrafficNodes())
|
||||
{
|
||||
if (nodes.Contains(n))
|
||||
{
|
||||
nearestNodes[0].AddNextNode(n);
|
||||
}
|
||||
}
|
||||
|
||||
nodes.Remove(node);
|
||||
Destroy(node.gameObject);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/TrafficSystem/Street.cs.meta
Normal file
11
Assets/Scripts/TrafficSystem/Street.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b794f37801ab66d4496dd16ee2294dee
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -4,14 +4,24 @@ using UnityEngine;
|
||||
public class TrafficNode : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private List<TrafficNode> nextTrafficNodes;
|
||||
private List<TrafficNode> nextTrafficNodes = new List<TrafficNode>();
|
||||
|
||||
[SerializeField]
|
||||
private float speed = 5.0f;
|
||||
|
||||
public float GetSpeed() { return speed; }
|
||||
public List<TrafficNode> GetNextTrafficNodes() { return nextTrafficNodes; }
|
||||
|
||||
public void AddNextNode(TrafficNode node)
|
||||
public void AddNextNode(TrafficNode newNode)
|
||||
{
|
||||
nextTrafficNodes.Add(node);
|
||||
nextTrafficNodes.Add(newNode);
|
||||
}
|
||||
|
||||
public void RemoveNextNode(TrafficNode node)
|
||||
{
|
||||
nextTrafficNodes.Remove(node);
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
Gizmos.color = Color.green;
|
||||
|
||||
@@ -8,8 +8,7 @@ public class TrafficParticipant : MonoBehaviour
|
||||
[SerializeField]
|
||||
private TrafficNode currentTrafficNode;
|
||||
|
||||
[SerializeField]
|
||||
private float speed = 5f;
|
||||
private float speed = 1f;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
@@ -20,6 +19,7 @@ public class TrafficParticipant : MonoBehaviour
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
speed = currentTrafficNode.GetSpeed();
|
||||
if (nextNode != null)
|
||||
{
|
||||
transform.position = Vector3.MoveTowards(transform.position, nextNode.transform.position, speed * Time.deltaTime); if (transform.position == nextNode.transform.position)
|
||||
@@ -32,9 +32,6 @@ public class TrafficParticipant : MonoBehaviour
|
||||
{
|
||||
ChooseNextNode();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void Arrived()
|
||||
@@ -46,7 +43,7 @@ public class TrafficParticipant : MonoBehaviour
|
||||
|
||||
private void ChooseNextNode()
|
||||
{
|
||||
Debug.Log("Choose next node");
|
||||
//Debug.Log("Choose next node");
|
||||
if (nextNode == null)
|
||||
{
|
||||
nextNode = currentTrafficNode.GetNextTrafficNodes()[Random.Range(0, (currentTrafficNode.GetNextTrafficNodes().Count))];
|
||||
|
||||
Reference in New Issue
Block a user