Commit 7c42f50b authored by 18김민수's avatar 18김민수

Main Development Initiation - path rendering update

parent 8dfd1a8d
This diff is collapsed.
...@@ -11,6 +11,5 @@ public class FlatLandObject : MonoBehaviour ...@@ -11,6 +11,5 @@ public class FlatLandObject : MonoBehaviour
void Awake() void Awake()
{ {
square.allObjects.Add(this);
} }
} }
...@@ -8,149 +8,59 @@ public class Square : FlatLandObject ...@@ -8,149 +8,59 @@ public class Square : FlatLandObject
{ {
public LineRenderer pathRenderer; // About drawing paths. public LineRenderer pathRenderer; // About drawing paths.
public List<Vector2> pathList = new List<Vector2>(); public List<Vector3> pathList = new List<Vector3>();
public UIManager uiManager; private float _zPosition => transform.position.z;
public List<FlatLandObject> allObjects = new List<FlatLandObject>(); // Evety FlatLandObject is automatically added with Awake(). private void Awake()
public FlatLandObject attatchedObject = null;
private Vector2 _currentPathEnd;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
public void Update()
{
if(Input.GetMouseButtonDown(1)) // If right mouse button is clicked
{ {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); _ResetPaths();
if (Physics.Raycast(ray, out RaycastHit hit)) // If the click was on the background
{
if (Input.GetKey(KeyCode.LeftShift) && pathRenderer.GetPosition(1) != Vector3.zero) // If shift is clicked and path is already exists
AddPath(hit.point);
else
CreatePath(hit.point);
}
}
if (Input.GetKeyDown(KeyCode.G))
Grab();
if (Input.GetKeyDown(KeyCode.H))
Detach();
} }
public IEnumerator MoveSquare(Vector2 destination) private void Update()
{ {
speedVector = destination - (Vector2)transform.position; if (Input.GetMouseButtonDown(1))
Vector3 scaledVector = speedVector * Time.deltaTime;
while (speedVector.x * (destination.x - transform.position.x) > 0 || speedVector.y * (destination.y - transform.position.y) > 0)
{ {
transform.position += scaledVector; RaycastHit hit;
if (attatchedObject != null) var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
attatchedObject.gameObject.transform.position += scaledVector;
yield return null;
}
}
public void CreatePath(Vector3 point) // Creates the fitst path, and updates Proper Time UI.
{
pathRenderer.positionCount = 2;
pathRenderer.SetPositions(new Vector3[] { transform.position, point });
uiManager.UpdateUI();
}
public void AddPath(Vector3 point) // Adds new path to current path, and updates Proper Time UI. if (Physics.Raycast(ray, out hit))
{ {
pathRenderer.positionCount++; if (Input.GetKey(KeyCode.LeftShift))
pathRenderer.SetPosition(pathRenderer.positionCount - 1, point);
uiManager.UpdateUI();
}
public void Move()
{ {
StartCoroutine(_StartMovingPath()); Debug.Log("asdf");
_DrawMorePath(hit.point);
} }
else
public IEnumerator _StartMovingPath()
{
Vector3[] tomoveList = new Vector3[pathRenderer.positionCount];
pathRenderer.GetPositions(tomoveList);
for(int i = 1; i < pathRenderer.positionCount; i++)
{ {
yield return StartCoroutine(MoveSquare(tomoveList[i])); _DrawOnePath(hit.point);
}
AddTimes();
ResetPaths();
uiManager.UpdateUI();
uiManager.CheckVictory();
} }
public void ResetPaths()
{
pathRenderer.positionCount = 2;
pathRenderer.SetPositions(new Vector3[] { Vector3.zero, Vector3.zero });
} }
public Vector2 getNthPath(int n) // returns movement vector stored in path renderer by index.
{
if (n >= pathRenderer.positionCount)
throw new InvalidOperationException(n + "th path is not stored.");
return pathRenderer.GetPosition(n + 1) - pathRenderer.GetPosition(n);
} }
public double MovingTime(Vector2 v) // How long it takes to move given vector with current speed.
{
return v.sqrMagnitude / gameSpeed;
} }
public double CalculateEntireMovingTime() // How long it takes to move current path all the way. private void _DrawOnePath(Vector3 point)
{ {
double result = 0; _ResetPaths();
for (int i = 0; i < pathRenderer.positionCount - 1; i++) point.z = _zPosition;
result += MovingTime(getNthPath(i)); pathList[0] = transform.position;
pathList[1] = point;
return result / gameSpeed; pathRenderer.SetPositions(pathList.ToArray());
} }
public void ModifySpeed(float d) private void _ResetPaths()
{ {
if (gameSpeed + d <= Constants.c && gameSpeed + d >= 0) pathRenderer.positionCount = 2;
gameSpeed += d; pathList.Clear();
uiManager.UpdateUI(); pathList.Add(transform.position);
} pathList.Add(new Vector3(0, 0, _zPosition));
pathRenderer.SetPositions(pathList.ToArray());
public void Grab()
{
var lst = Physics.OverlapSphere(transform.position, 10);
if (lst.Length > 0)
attatchedObject = lst[0].gameObject.GetComponent<FlatLandObject>();
}
public void Detach()
{
attatchedObject = null;
} }
public void AddTimes() private void _DrawMorePath(Vector3 point)
{
Debug.Log(CalculateEntireMovingTime());
foreach (var v in allObjects)
{ {
Debug.Log(v.gameObject.name); point.z = _zPosition;
v.properTime += CalculateEntireMovingTime(); pathList.Add(point);
} pathRenderer.positionCount = pathList.Count();
square.properTime -= CalculateEntireMovingTime() - (square.CalculateEntireMovingTime() / Constants.Gamma(square.gameSpeed)); pathRenderer.SetPositions(pathList.ToArray());
if (attatchedObject != null)
attatchedObject.properTime -= CalculateEntireMovingTime() - (square.CalculateEntireMovingTime() / Constants.Gamma(square.gameSpeed));
} }
} }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIManager : MonoBehaviour
{
public Text properTime;
public Text currentSpeed;
public Text clockText;
public Square square;
public FlatLandObject clock1, clock2;
public GameObject victoryScreen;
public void UpdateUI() // it just updates UI.
{
string newPropertime, prevPropertime;
currentSpeed.text = "Current Speed : " + (square.gameSpeed / Constants.c).ToString("F2") + "c";
prevPropertime = square.CalculateEntireMovingTime().ToString();
newPropertime = (square.CalculateEntireMovingTime() / Constants.Gamma(square.gameSpeed)).ToString();
properTime.text = "New Proper Time : +" + newPropertime + " seconds\nPrev Proper Time : +" + prevPropertime + " seconds";
clockText.text = "Clock1 : " + clock1.properTime + " second\nClock2 : " + clock2.properTime + " second";
}
public void CheckVictory()
{
if ((clock1.transform.position - clock2.transform.position).sqrMagnitude < 10 && Mathf.Abs((float)(clock1.properTime - clock2.properTime)) < 1)
victoryScreen.SetActive(true);
}
}
fileFormatVersion: 2
guid: 2c85c5f34b599bd47bda7810cd6b95a6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment