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
void Awake()
{
square.allObjects.Add(this);
}
}
......@@ -8,149 +8,59 @@ public class Square : FlatLandObject
{
public LineRenderer pathRenderer; // About drawing paths.
public List<Vector2> pathList = new List<Vector2>();
public UIManager uiManager;
public List<Vector3> pathList = new List<Vector3>();
private float _zPosition => transform.position.z;
public List<FlatLandObject> allObjects = new List<FlatLandObject>(); // Evety FlatLandObject is automatically added with Awake().
public FlatLandObject attatchedObject = null;
private Vector2 _currentPathEnd;
// Start is called before the first frame update
void Start()
private void Awake()
{
_ResetPaths();
}
// Update is called once per frame
public void Update()
private void Update()
{
if(Input.GetMouseButtonDown(1)) // If right mouse button is clicked
if (Input.GetMouseButtonDown(1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit)) // If the click was on the background
RaycastHit hit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (Input.GetKey(KeyCode.LeftShift) && pathRenderer.GetPosition(1) != Vector3.zero) // If shift is clicked and path is already exists
AddPath(hit.point);
if (Input.GetKey(KeyCode.LeftShift))
{
Debug.Log("asdf");
_DrawMorePath(hit.point);
}
else
CreatePath(hit.point);
{
_DrawOnePath(hit.point);
}
}
}
if (Input.GetKeyDown(KeyCode.G))
Grab();
if (Input.GetKeyDown(KeyCode.H))
Detach();
}
public IEnumerator MoveSquare(Vector2 destination)
private void _DrawOnePath(Vector3 point)
{
speedVector = destination - (Vector2)transform.position;
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;
if (attatchedObject != null)
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.
{
pathRenderer.positionCount++;
pathRenderer.SetPosition(pathRenderer.positionCount - 1, point);
uiManager.UpdateUI();
}
public void Move()
{
StartCoroutine(_StartMovingPath());
}
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]));
}
AddTimes();
ResetPaths();
uiManager.UpdateUI();
uiManager.CheckVictory();
_ResetPaths();
point.z = _zPosition;
pathList[0] = transform.position;
pathList[1] = point;
pathRenderer.SetPositions(pathList.ToArray());
}
public void ResetPaths()
private void _ResetPaths()
{
pathRenderer.positionCount = 2;
pathRenderer.SetPositions(new Vector3[] { Vector3.zero, Vector3.zero });
pathList.Clear();
pathList.Add(transform.position);
pathList.Add(new Vector3(0, 0, _zPosition));
pathRenderer.SetPositions(pathList.ToArray());
}
public Vector2 getNthPath(int n) // returns movement vector stored in path renderer by index.
private void _DrawMorePath(Vector3 point)
{
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.
{
double result = 0;
for (int i = 0; i < pathRenderer.positionCount - 1; i++)
result += MovingTime(getNthPath(i));
return result / gameSpeed;
}
public void ModifySpeed(float d)
{
if (gameSpeed + d <= Constants.c && gameSpeed + d >= 0)
gameSpeed += d;
uiManager.UpdateUI();
}
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()
{
Debug.Log(CalculateEntireMovingTime());
foreach (var v in allObjects)
{
Debug.Log(v.gameObject.name);
v.properTime += CalculateEntireMovingTime();
}
square.properTime -= CalculateEntireMovingTime() - (square.CalculateEntireMovingTime() / Constants.Gamma(square.gameSpeed));
if (attatchedObject != null)
attatchedObject.properTime -= CalculateEntireMovingTime() - (square.CalculateEntireMovingTime() / Constants.Gamma(square.gameSpeed));
point.z = _zPosition;
pathList.Add(point);
pathRenderer.positionCount = pathList.Count();
pathRenderer.SetPositions(pathList.ToArray());
}
}
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