mirror of
https://github.com/DerTyp7/traffic-unity.git
synced 2025-10-30 13:17:08 +01:00
place streets
This commit is contained in:
56
Assets/Scripts/BuildingSystem/StreetBuilding.cs
Normal file
56
Assets/Scripts/BuildingSystem/StreetBuilding.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class StreetBuilding : MonoBehaviour
|
||||
{
|
||||
|
||||
GameObject currentStreetPO;
|
||||
Vector3 startPosition;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.H))
|
||||
{
|
||||
if (currentStreetPO == null)
|
||||
{
|
||||
StartBuilding();
|
||||
}
|
||||
else
|
||||
{
|
||||
StopBuilding();
|
||||
}
|
||||
}
|
||||
|
||||
if (currentStreetPO != null && Input.GetKeyDown(KeyCode.J))
|
||||
{
|
||||
if (currentStreetPO.GetComponent<PlaceableObject>().placeable)
|
||||
{
|
||||
PlaceBuilding();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void StartBuilding()
|
||||
{
|
||||
startPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
startPosition.z = 0;
|
||||
|
||||
currentStreetPO = Instantiate(PrefabDictionary.instance.oneWayStreetPO, startPosition, Quaternion.identity);
|
||||
}
|
||||
|
||||
private void StopBuilding()
|
||||
{
|
||||
Destroy(currentStreetPO);
|
||||
currentStreetPO = null;
|
||||
startPosition = Vector3.zero;
|
||||
}
|
||||
|
||||
private void PlaceBuilding()
|
||||
{
|
||||
Transform placedTransform = currentStreetPO.transform;
|
||||
|
||||
Instantiate(PrefabDictionary.instance.oneWayStreet, placedTransform.position, placedTransform.rotation).transform.localScale = placedTransform.localScale;
|
||||
|
||||
StopBuilding();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/BuildingSystem/StreetBuilding.cs.meta
Normal file
11
Assets/Scripts/BuildingSystem/StreetBuilding.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ab8ef902838d7534bb1b8062f24d5f82
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
38
Assets/Scripts/PlaceableObject.cs
Normal file
38
Assets/Scripts/PlaceableObject.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class PlaceableObject : MonoBehaviour
|
||||
{
|
||||
public GameObject childObject;
|
||||
public bool placeable = true;
|
||||
|
||||
|
||||
private void Update()
|
||||
{
|
||||
ScaleOnMousePosition();
|
||||
RotateToMousePosition();
|
||||
|
||||
placeable = childObject.GetComponent<PlaceableObjectChild>().placeable;
|
||||
}
|
||||
|
||||
float AngleBetweenTwoPoints(Vector3 a, Vector3 b)
|
||||
{
|
||||
return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
|
||||
}
|
||||
|
||||
void RotateToMousePosition()
|
||||
{
|
||||
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
mousePosition.z = 0;
|
||||
float angle = AngleBetweenTwoPoints(transform.position, mousePosition);
|
||||
transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));
|
||||
}
|
||||
void ScaleOnMousePosition()
|
||||
{
|
||||
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
mousePosition.z = 0;
|
||||
|
||||
transform.localScale = new Vector3(-Vector3.Distance(mousePosition, transform.position), 1, 1);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
11
Assets/Scripts/PlaceableObject.cs.meta
Normal file
11
Assets/Scripts/PlaceableObject.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1eb76ac910e345a428730df4c72c835b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
31
Assets/Scripts/PlaceableObjectChild.cs
Normal file
31
Assets/Scripts/PlaceableObjectChild.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class PlaceableObjectChild : MonoBehaviour
|
||||
{
|
||||
|
||||
public bool placeable = true;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
transform.localPosition = new Vector3(0.5f, 0f, 0f);
|
||||
if (placeable)
|
||||
{
|
||||
GetComponent<SpriteRenderer>().color = Color.blue;
|
||||
}
|
||||
else
|
||||
{
|
||||
GetComponent<SpriteRenderer>().color = Color.red;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCollisionStay2D(Collision2D collision)
|
||||
{
|
||||
placeable = false;
|
||||
|
||||
}
|
||||
|
||||
private void OnCollisionExit2D(Collision2D collision)
|
||||
{
|
||||
placeable = true;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/PlaceableObjectChild.cs.meta
Normal file
11
Assets/Scripts/PlaceableObjectChild.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 69d68f14c2a51dc4d94160eafca27a7e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -4,6 +4,8 @@ public class PrefabDictionary : MonoBehaviour
|
||||
public static PrefabDictionary instance;
|
||||
|
||||
public GameObject nodePrefab;
|
||||
public GameObject oneWayStreetPO;
|
||||
public GameObject oneWayStreet;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
|
||||
@@ -5,7 +5,7 @@ public class Street : MonoBehaviour
|
||||
{
|
||||
private List<TrafficNode> nodes = new List<TrafficNode>();
|
||||
|
||||
private void Awake()
|
||||
private void Start()
|
||||
{
|
||||
nodes.Add(Instantiate(PrefabDictionary.instance.nodePrefab, transform).GetComponent<TrafficNode>());
|
||||
nodes.Add(Instantiate(PrefabDictionary.instance.nodePrefab, transform).GetComponent<TrafficNode>());
|
||||
@@ -14,10 +14,10 @@ public class Street : MonoBehaviour
|
||||
|
||||
nodes[0].AddNextNode(nodes[1]);
|
||||
|
||||
CreateNode(new Vector3(0, 0, 0));
|
||||
/*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));
|
||||
CreateNode(new Vector3(-0.12f, 0, 0));*/
|
||||
}
|
||||
|
||||
private TrafficNode[] getNearestNodes(float positionX)
|
||||
|
||||
6
Assets/Scripts/TrafficSystem/StreetContainer.cs
Normal file
6
Assets/Scripts/TrafficSystem/StreetContainer.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class StreetContainer : MonoBehaviour
|
||||
{
|
||||
public GameObject streetObject;
|
||||
}
|
||||
11
Assets/Scripts/TrafficSystem/StreetContainer.cs.meta
Normal file
11
Assets/Scripts/TrafficSystem/StreetContainer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 51e2ae474deec9241b41be37ee06f5cf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -6,47 +6,84 @@ public class TrafficParticipant : MonoBehaviour
|
||||
private TrafficNode nextNode;
|
||||
|
||||
[SerializeField]
|
||||
private TrafficNode currentTrafficNode;
|
||||
private TrafficNode currentNode;
|
||||
|
||||
private bool onNodeNetwork = false;
|
||||
|
||||
|
||||
private float speed = 1f;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (nextNode == null)
|
||||
{
|
||||
ChooseNextNode();
|
||||
}
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
speed = currentTrafficNode.GetSpeed();
|
||||
if (nextNode != null)
|
||||
if (currentNode)
|
||||
{
|
||||
transform.position = Vector3.MoveTowards(transform.position, nextNode.transform.position, speed * Time.deltaTime); if (transform.position == nextNode.transform.position)
|
||||
speed = currentNode.GetSpeed();
|
||||
|
||||
if (onNodeNetwork)
|
||||
{
|
||||
Arrived();
|
||||
ChooseNextNode();
|
||||
if (nextNode != null)
|
||||
{
|
||||
transform.position = Vector3.MoveTowards(transform.position, nextNode.transform.position, speed * Time.deltaTime);
|
||||
if (transform.position == nextNode.transform.position)
|
||||
{
|
||||
Arrived();
|
||||
ChooseNextNode();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ChooseNextNode();
|
||||
}
|
||||
}
|
||||
else // Align to node network
|
||||
{
|
||||
transform.position = Vector3.MoveTowards(transform.position, currentNode.transform.position, speed * Time.deltaTime);
|
||||
if (transform.position == currentNode.transform.position)
|
||||
{
|
||||
onNodeNetwork = true;
|
||||
ChooseNextNode();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
ChooseNextNode();
|
||||
currentNode = GetNearestNode();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private TrafficNode GetNearestNode()
|
||||
{
|
||||
GameObject[] allTrafficNodes = GameObject.FindGameObjectsWithTag("TrafficNode");
|
||||
|
||||
if (allTrafficNodes.Length > 0)
|
||||
{
|
||||
GameObject nearestNodeObject = allTrafficNodes[0];
|
||||
|
||||
foreach (GameObject nodeObject in GameObject.FindGameObjectsWithTag("TrafficNode"))
|
||||
{
|
||||
if ((transform.position - nodeObject.transform.position).sqrMagnitude < (transform.position - nearestNodeObject.transform.position).sqrMagnitude)
|
||||
{
|
||||
nearestNodeObject = nodeObject;
|
||||
}
|
||||
}
|
||||
return nearestNodeObject.GetComponent<TrafficNode>();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void Arrived()
|
||||
{
|
||||
Debug.Log("Arrived");
|
||||
currentTrafficNode = nextNode;
|
||||
currentNode = nextNode;
|
||||
nextNode = null;
|
||||
}
|
||||
|
||||
private void ChooseNextNode()
|
||||
{
|
||||
//Debug.Log("Choose next node");
|
||||
if (nextNode == null)
|
||||
{
|
||||
nextNode = currentTrafficNode.GetNextTrafficNodes()[Random.Range(0, (currentTrafficNode.GetNextTrafficNodes().Count))];
|
||||
nextNode = currentNode.GetNextTrafficNodes()[Random.Range(0, (currentNode.GetNextTrafficNodes().Count))];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +92,6 @@ public class TrafficParticipant : MonoBehaviour
|
||||
if (nextNode != null)
|
||||
{
|
||||
Gizmos.color = Color.red;
|
||||
|
||||
Gizmos.DrawLine(transform.position, nextNode.transform.position);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user