mirror of
https://github.com/DerTyp7/traffic-unity.git
synced 2025-10-30 05:07:10 +01:00
dfg
This commit is contained in:
@@ -13,12 +13,10 @@ public class NodePlacer : MonoBehaviour
|
||||
{
|
||||
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
|
||||
|
||||
@@ -3,14 +3,15 @@ using UnityEngine;
|
||||
public class StreetBuilding : MonoBehaviour
|
||||
{
|
||||
|
||||
GameObject currentStreetPO;
|
||||
GameObject currentStreetPSO;
|
||||
Vector3 startPosition;
|
||||
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.H))
|
||||
{
|
||||
if (currentStreetPO == null)
|
||||
if (currentStreetPSO == null)
|
||||
{
|
||||
StartBuilding();
|
||||
}
|
||||
@@ -20,36 +21,41 @@ public class StreetBuilding : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
if (currentStreetPO != null && Input.GetKeyDown(KeyCode.J))
|
||||
if (currentStreetPSO != null)
|
||||
{
|
||||
if (currentStreetPO.GetComponent<PlaceableObject>().placeable)
|
||||
if (Input.GetKeyDown(KeyCode.J))
|
||||
{
|
||||
PlaceBuilding();
|
||||
if (currentStreetPSO.GetComponent<PlaceableStreetObject>().placeable)
|
||||
{
|
||||
PlaceBuilding();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void StartBuilding()
|
||||
{
|
||||
startPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
startPosition.z = 0;
|
||||
|
||||
currentStreetPO = Instantiate(PrefabDictionary.instance.oneWayStreetPO, startPosition, Quaternion.identity);
|
||||
currentStreetPSO = Instantiate(PrefabDictionary.instance.oneWayStreetPSO, startPosition, Quaternion.identity);
|
||||
}
|
||||
|
||||
private void StopBuilding()
|
||||
{
|
||||
Destroy(currentStreetPO);
|
||||
currentStreetPO = null;
|
||||
Destroy(currentStreetPSO);
|
||||
currentStreetPSO = null;
|
||||
startPosition = Vector3.zero;
|
||||
}
|
||||
|
||||
private void PlaceBuilding()
|
||||
{
|
||||
Transform placedTransform = currentStreetPO.transform;
|
||||
Transform placedTransform = currentStreetPSO.transform;
|
||||
|
||||
Instantiate(PrefabDictionary.instance.oneWayStreet, placedTransform.position, placedTransform.rotation).transform.localScale = placedTransform.localScale;
|
||||
GameObject newStreet = Instantiate(PrefabDictionary.instance.oneWayStreet, placedTransform.position, placedTransform.rotation);
|
||||
newStreet.transform.localScale = placedTransform.localScale;
|
||||
newStreet.GetComponent<StreetContainer>().Place();
|
||||
|
||||
StopBuilding();
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
89
Assets/Scripts/PlaceableStreetObject.cs
Normal file
89
Assets/Scripts/PlaceableStreetObject.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class PlaceableStreetObject : MonoBehaviour
|
||||
{
|
||||
public GameObject childObject;
|
||||
public bool placeable = true;
|
||||
TrafficNode snappingNode;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
UpdateSnappingNode();
|
||||
ScaleOnMousePosition();
|
||||
RotateToMousePosition();
|
||||
placeable = childObject.GetComponent<PlaceableStreetObjectChild>().placeable;
|
||||
}
|
||||
|
||||
float AngleBetweenTwoPoints(Vector3 a, Vector3 b)
|
||||
{
|
||||
return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
|
||||
}
|
||||
|
||||
private void UpdateSnappingNode()
|
||||
{
|
||||
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
RaycastHit2D[] hits = Physics2D.RaycastAll(mousePosition, Vector2.zero);
|
||||
|
||||
|
||||
// Snapping
|
||||
TrafficNode foundNode = null;
|
||||
foreach (RaycastHit2D hit in hits)
|
||||
{
|
||||
if (hit.collider != null)
|
||||
{
|
||||
if (hit.collider.gameObject.tag == "TrafficNode")
|
||||
{
|
||||
foundNode = hit.collider.gameObject.GetComponent<TrafficNode>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (snappingNode != null)
|
||||
{
|
||||
snappingNode.gizmoSphereColor = Color.green;
|
||||
}
|
||||
|
||||
snappingNode = foundNode;
|
||||
|
||||
if (snappingNode != null)
|
||||
{
|
||||
snappingNode.gizmoSphereColor = Color.red;
|
||||
}
|
||||
|
||||
childObject.GetComponent<PlaceableStreetObjectChild>().snappingNode = snappingNode;
|
||||
}
|
||||
|
||||
|
||||
Vector3 GetEndPosition()
|
||||
{
|
||||
PlaceableStreetObjectChild child = childObject.GetComponent<PlaceableStreetObjectChild>();
|
||||
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
|
||||
Vector3 endPosition = mousePosition;
|
||||
|
||||
|
||||
// Snapping
|
||||
if (snappingNode != null)
|
||||
{
|
||||
endPosition.x = snappingNode.gameObject.transform.position.x;
|
||||
endPosition.y = snappingNode.gameObject.transform.position.y;
|
||||
}
|
||||
|
||||
endPosition.z = 0;
|
||||
return endPosition;
|
||||
}
|
||||
|
||||
void RotateToMousePosition()
|
||||
{
|
||||
Vector3 endPosition = GetEndPosition();
|
||||
float angle = AngleBetweenTwoPoints(transform.position, endPosition);
|
||||
transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));
|
||||
}
|
||||
void ScaleOnMousePosition()
|
||||
{
|
||||
Vector3 endPosition = GetEndPosition();
|
||||
transform.localScale = new Vector3(-Vector3.Distance(endPosition, transform.position), 1, 1);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
95
Assets/Scripts/PlaceableStreetObjectChild.cs
Normal file
95
Assets/Scripts/PlaceableStreetObjectChild.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlaceableStreetObjectChild : MonoBehaviour
|
||||
{
|
||||
|
||||
public bool placeable = true;
|
||||
public List<Collision2D> currentCollisions = new List<Collision2D>();
|
||||
public TrafficNode snappingNode = null;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
transform.localPosition = new Vector3(0.5f, 0f, 0f);
|
||||
CheckPlaceable();
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (placeable)
|
||||
{
|
||||
GetComponent<SpriteRenderer>().color = Color.blue;
|
||||
}
|
||||
else
|
||||
{
|
||||
GetComponent<SpriteRenderer>().color = Color.red;
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckPlaceable()
|
||||
{
|
||||
// DEBUG LOG
|
||||
Debug.Log("--------------------------------------");
|
||||
foreach (Collision2D collision in currentCollisions)
|
||||
{
|
||||
Debug.Log(collision.gameObject.name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// If a snapping node is present -> its allowed to collide with all node of the SAME street
|
||||
if (snappingNode != null)
|
||||
{
|
||||
bool foundBadCollision = false;
|
||||
foreach (Collision2D collision in currentCollisions)
|
||||
{
|
||||
if (collision.collider)
|
||||
{
|
||||
if (collision.collider.transform.tag == "TrafficNode")
|
||||
{
|
||||
if (collision.collider.GetComponent<TrafficNode>().GetParentStreet() != snappingNode.GetParentStreet())
|
||||
{
|
||||
foundBadCollision = true;
|
||||
}
|
||||
}
|
||||
else if (collision.collider.transform.tag == "Street")
|
||||
{
|
||||
if (collision.collider.GetComponent<Street>() != snappingNode.GetParentStreet())
|
||||
{
|
||||
foundBadCollision = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("No Street or node");
|
||||
foundBadCollision = true;
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("No collider");
|
||||
foundBadCollision = true;
|
||||
}
|
||||
}
|
||||
placeable = !foundBadCollision;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (currentCollisions.Count > 0)
|
||||
{
|
||||
placeable = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCollisionEnter2D(Collision2D c)
|
||||
{
|
||||
currentCollisions.Add(c);
|
||||
}
|
||||
|
||||
private void OnCollisionExit2D(Collision2D c)
|
||||
{
|
||||
currentCollisions.Remove(c);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ public class PrefabDictionary : MonoBehaviour
|
||||
public static PrefabDictionary instance;
|
||||
|
||||
public GameObject nodePrefab;
|
||||
public GameObject oneWayStreetPO;
|
||||
public GameObject oneWayStreetPSO;
|
||||
public GameObject oneWayStreet;
|
||||
|
||||
void Awake()
|
||||
|
||||
@@ -3,23 +3,40 @@ using UnityEngine;
|
||||
|
||||
public class Street : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
GameObject nodePrefab;
|
||||
|
||||
private List<TrafficNode> nodes = new List<TrafficNode>();
|
||||
|
||||
private void Start()
|
||||
public void Place()
|
||||
{
|
||||
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);
|
||||
CreateStartEndNodes();
|
||||
|
||||
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));*/
|
||||
float parentScaleX = Mathf.Abs(transform.parent.transform.localScale.x);
|
||||
float nodeSize = 0.5f;
|
||||
|
||||
int nodeCount = Mathf.RoundToInt(parentScaleX / nodeSize);
|
||||
|
||||
int i = 1; // starts at 1 so the first gets ignored (start node already exists)
|
||||
while (i < nodeCount) // NOT <= so endnode does not get created twice ^
|
||||
{
|
||||
float xPosition = ((i * nodeSize) / parentScaleX) - 0.5f;// weird calc because street is child of object and always has scale of 1f ....
|
||||
CreateNode(new Vector3(xPosition, 0, 0));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateStartEndNodes()
|
||||
{
|
||||
nodes.Add(Instantiate(nodePrefab, transform).GetComponent<TrafficNode>());
|
||||
nodes.Add(Instantiate(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].SetParentStreet(this);
|
||||
nodes[1].SetParentStreet(this);
|
||||
nodes[0].AddNextNode(nodes[1]);
|
||||
}
|
||||
private TrafficNode[] getNearestNodes(float positionX)
|
||||
{
|
||||
TrafficNode[] nearestNodes = new TrafficNode[2];
|
||||
@@ -42,15 +59,22 @@ public class Street : MonoBehaviour
|
||||
return nearestNodes;
|
||||
}
|
||||
|
||||
// Dont create anything behind the endnode or before the startnode
|
||||
public TrafficNode CreateNode(Vector3 localPosition)
|
||||
{
|
||||
TrafficNode[] nearestNodes = getNearestNodes(localPosition.x);
|
||||
|
||||
TrafficNode newNode = Instantiate(PrefabDictionary.instance.nodePrefab, transform).GetComponent<TrafficNode>();
|
||||
TrafficNode newNode = Instantiate(nodePrefab, transform).GetComponent<TrafficNode>();
|
||||
newNode.SetParentStreet(this);
|
||||
newNode.transform.localPosition = localPosition;
|
||||
|
||||
nearestNodes[0].RemoveNextNode(nearestNodes[1]);
|
||||
nearestNodes[0].AddNextNode(newNode);
|
||||
|
||||
if (nearestNodes[0] != nodes[nodes.Count - 1])
|
||||
{
|
||||
nearestNodes[0].AddNextNode(newNode);
|
||||
}
|
||||
|
||||
newNode.AddNextNode(nearestNodes[1]);
|
||||
nodes.Insert(nodes.IndexOf(nearestNodes[1]), newNode);
|
||||
return newNode;
|
||||
|
||||
@@ -3,7 +3,8 @@ guid: b794f37801ab66d4496dd16ee2294dee
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
defaultReferences:
|
||||
- nodePrefab: {fileID: 487205225613997102, guid: def66282a1c498e4c906a02f6bbd214e, type: 3}
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
|
||||
@@ -3,4 +3,9 @@ using UnityEngine;
|
||||
public class StreetContainer : MonoBehaviour
|
||||
{
|
||||
public GameObject streetObject;
|
||||
|
||||
public void Place()
|
||||
{
|
||||
streetObject.GetComponent<Street>().Place();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,15 @@ public class TrafficNode : MonoBehaviour
|
||||
[SerializeField]
|
||||
private float speed = 5.0f;
|
||||
|
||||
|
||||
public Color gizmoSphereColor = Color.green;
|
||||
|
||||
private Street parentStreet;
|
||||
|
||||
public float GetSpeed() { return speed; }
|
||||
public List<TrafficNode> GetNextTrafficNodes() { return nextTrafficNodes; }
|
||||
public void SetParentStreet(Street newParentStreet) { parentStreet = newParentStreet; }
|
||||
public Street GetParentStreet() { return parentStreet; }
|
||||
|
||||
public void AddNextNode(TrafficNode newNode)
|
||||
{
|
||||
@@ -22,9 +29,20 @@ public class TrafficNode : MonoBehaviour
|
||||
nextTrafficNodes.Remove(node);
|
||||
}
|
||||
|
||||
public void LateUpdate()
|
||||
{
|
||||
if (transform.parent != null)
|
||||
{
|
||||
Vector3 parentScale = transform.parent.lossyScale;
|
||||
transform.localScale = new Vector3(1f / parentScale.x, 1f / parentScale.y,
|
||||
1f / parentScale.z);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
Gizmos.color = Color.green;
|
||||
Gizmos.color = gizmoSphereColor;
|
||||
Gizmos.DrawSphere(transform.position, 0.2f);
|
||||
foreach (TrafficNode node in nextTrafficNodes)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user