Commit 8099dd9d authored by Chae Ho Shin's avatar Chae Ho Shin

added length contraction feature, minor UI and bug fixes.

parent 8dfd1a8d
This diff is collapsed.
......@@ -4,13 +4,14 @@ using UnityEngine;
public class FlatLandObject : MonoBehaviour
{
public Square square;
public Square square = null;
public Vector2 speedVector = new Vector2(0, 0);
public double gameSpeed = 0;
public double properTime = 0;
void Awake()
{
square.allObjects.Add(this);
if(square != null)
square.allObjects.Add(this);
}
}
......@@ -2,14 +2,18 @@
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.SceneManagement;
using System.Linq;
public class Square : FlatLandObject
{
public LineRenderer pathRenderer; // About drawing paths.
public List<GameObject> pathPositionObjects = new List<GameObject>();
public List<Vector2> pathList = new List<Vector2>();
public UIManager uiManager;
public GameObject Background;
public float backgroundSize = 400.0f;
public List<FlatLandObject> allObjects = new List<FlatLandObject>(); // Evety FlatLandObject is automatically added with Awake().
public FlatLandObject attatchedObject = null;
......@@ -29,7 +33,7 @@ public class Square : FlatLandObject
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
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
if (Input.GetKey(KeyCode.LeftShift) && pathPositionObjects.Count >= 1) // If shift is clicked and path is already exists
AddPath(hit.point);
else
CreatePath(hit.point);
......@@ -43,24 +47,106 @@ public class Square : FlatLandObject
Detach();
}
public IEnumerator MoveSquare(Vector2 destination)
public IEnumerator MoveSquare(GameObject destination)
{
speedVector = destination - (Vector2)transform.position;
Vector2 dest = new Vector2(destination.transform.position.x, destination.transform.position.y);
Vector2 start = new Vector2(transform.position.x, transform.position.y);
speedVector = dest - start;
Vector2 prevFrameSpeedVector = speedVector;
//Vector2 ndest = (Vector2)transform.position + (speedVector / (float)Constants.Gamma(gameSpeed));
Vector3 scaledVector = speedVector * Time.deltaTime;
while (speedVector.x * (destination.x - transform.position.x) > 0 || speedVector.y * (destination.y - transform.position.y) > 0)
LengthContraction(scaledVector);
dest = new Vector2(destination.transform.position.x, destination.transform.position.y);
start = new Vector2(transform.position.x, transform.position.y);
speedVector = dest - start;
scaledVector = speedVector * Time.deltaTime;
while (speedVector.x * (destination.transform.position.x - transform.position.x) > 0 || speedVector.y * (destination.transform.position.y - transform.position.y) > 0)
{
transform.position += scaledVector;
if (attatchedObject != null)
attatchedObject.gameObject.transform.position += scaledVector;
yield return null;
}
Background.transform.localScale = new Vector3(backgroundSize, backgroundSize, 1.0F);
Background.transform.position = new Vector3(Background.transform.localPosition.x, Background.transform.localPosition.y, 0.0f);
//RemoveFromBackground();
Background.transform.DetachChildren();
//pathRenderer.transform.position = new Vector3(0.0f, 0.0f, -2.0f);
Background.transform.position = destination.transform.position;
//Background.transform.localPosition += (Vector3)speedVector;
//Background.transform.localPosition += (Vector3)speedVector;
AddtoBackground();
Background.transform.position = transform.position;
Background.transform.position = new Vector3(Background.transform.position.x, Background.transform.position.y, 0.0f);
//Background.transform.DetachChildren();
//Background.transform.localEulerAngles = new Vector3(0.0F, 0.0F, 0.0F);
//Background.transform.LookAt(-Vector3.forward);
//AddtoBackground();
//RemoveFromBackground();
Background.transform.DetachChildren();
Background.transform.position = new Vector3(Background.transform.position.x, Background.transform.position.y, 0.0f);
//pathRenderer.transform.position = new Vector3(0.0f, 0.0f, -2.0f);
yield return null;
}
public void LengthContraction(Vector3 scaledVector) // length contraction - find all flatlandobjects not grabbed by the square, add them to the rotated background and squeeze
{
//Background.transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, Background.transform.localPosition.z);
Background.transform.rotation = Quaternion.LookRotation(Vector3.forward, scaledVector);
//Background.transform.LookAt(new Vector3(0.0f,0.0f,1.0f),scaledVector);
AddtoBackground();
Background.transform.localScale = new Vector3(backgroundSize, (float)(1 / Constants.Gamma(gameSpeed)) * backgroundSize, 1.0F);
}
public void AddtoBackground()
{
List<GameObject> rootObjects = new List<GameObject>();
Scene scene = SceneManager.GetActiveScene();
scene.GetRootGameObjects(rootObjects);
foreach (GameObject anobject in rootObjects)
{
if (anobject.GetComponent<FlatLandObject>() != null && anobject.GetComponent<Square>() == null)
{
if (attatchedObject != null && anobject == attatchedObject.gameObject)
continue;
else
{
anobject.transform.parent = Background.transform;
}
}
}
pathRenderer.transform.parent = Background.transform;
}
public void RemoveFromBackground()
{
foreach(Transform t in Background.transform)
{
Vector3 realposition = t.position;
Quaternion realrotation = t.rotation;
Vector3 realscale = t.lossyScale;
Transform a = t;
t.parent = null;
a.localPosition = realposition;
a.localRotation = realrotation;
a.localScale = realscale;
}
}
public void CreatePath(Vector3 point) // Creates the fitst path, and updates Proper Time UI.
{
ResetPaths();
pathRenderer.positionCount = 2;
pathRenderer.SetPositions(new Vector3[] { transform.position, point });
pathRenderer.material.color = Color.green;
GameObject wow = new GameObject("Point1"); //turn the destination point into a gameobject for length contraction scaling
wow.transform.parent = null;
wow.transform.localPosition = new Vector3(point.x, point.y, 0.0f);
wow.AddComponent<FlatLandObject>();
pathPositionObjects.Add(wow);
uiManager.UpdateUI();
}
......@@ -68,6 +154,12 @@ public class Square : FlatLandObject
{
pathRenderer.positionCount++;
pathRenderer.SetPosition(pathRenderer.positionCount - 1, point);
GameObject wow = new GameObject("Point" + (pathRenderer.positionCount - 1), typeof(FlatLandObject));
wow.transform.parent = null;
wow.transform.localPosition = new Vector3(point.x, point.y, 0.0f);
pathPositionObjects.Add(wow);
uiManager.UpdateUI();
}
......@@ -79,11 +171,18 @@ public class Square : FlatLandObject
public IEnumerator _StartMovingPath()
{
Vector3[] tomoveList = new Vector3[pathRenderer.positionCount];
pathRenderer.GetPositions(tomoveList);
for(int i = 1; i < pathRenderer.positionCount; i++)
//Vector3[] tomoveList = new Vector3[pathRenderer.positionCount];
//pathRenderer.GetPositions(tomoveList);
//Vector3[] tomoveList = new Vector3[pathRenderer.positionCount - 1];
//for(int i = 1; i < pathRenderer.positionCount; i++)
//{
//yield return StartCoroutine(MoveSquare(tomoveList[i]));
//}
foreach(GameObject wow in pathPositionObjects)
{
yield return StartCoroutine(MoveSquare(tomoveList[i]));
yield return StartCoroutine(MoveSquare(wow));
//Background.transform.position = transform.position;
}
AddTimes();
......@@ -96,12 +195,18 @@ public class Square : FlatLandObject
{
pathRenderer.positionCount = 2;
pathRenderer.SetPositions(new Vector3[] { Vector3.zero, Vector3.zero });
foreach(GameObject j in pathPositionObjects)
{
Destroy(j);
}
pathPositionObjects = new List<GameObject>();
pathRenderer.transform.localPosition = 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.");
throw new InvalidOperationException(n + "the path is not stored.");
return pathRenderer.GetPosition(n + 1) - pathRenderer.GetPosition(n);
}
......@@ -132,7 +237,16 @@ public class Square : FlatLandObject
var lst = Physics.OverlapSphere(transform.position, 10);
if (lst.Length > 0)
attatchedObject = lst[0].gameObject.GetComponent<FlatLandObject>();
{
foreach(Collider thing in lst)
{
if (thing.gameObject.GetComponent<FlatLandObject>() != null)
{
attatchedObject = thing.gameObject.GetComponent<FlatLandObject>();
break;
}
}
}
}
public void Detach()
......
......@@ -10,24 +10,36 @@ public class UIManager : MonoBehaviour
public Square square;
public FlatLandObject clock1, clock2;
public GameObject victoryScreen;
float curZoomPos = 95.0f;
public void UpdateUI() // it just updates UI.
{
string newPropertime, prevPropertime;
double 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();
prevPropertime = square.CalculateEntireMovingTime();
newPropertime = (square.CalculateEntireMovingTime() / Constants.Gamma(square.gameSpeed));
properTime.text = "New Proper Time : +" + newPropertime + " seconds\nPrev Proper Time : +" + prevPropertime + " seconds";
properTime.text = "ETA(you) : " + newPropertime.ToString("+0;-#") + " seconds\nETA(start frame) : " + prevPropertime.ToString("+0;-#") + " seconds\nΔt : " + (newPropertime - prevPropertime).ToString("+0;-#") + " seconds";
clockText.text = "Clock1 : " + clock1.properTime + " second\nClock2 : " + clock2.properTime + " second";
clockText.text = "Clock1 : " + clock1.properTime + " seconds\nClock2 : " + clock2.properTime + " seconds";
}
public void Update()
{
float y = Input.mouseScrollDelta.y;
curZoomPos -= y*20;
curZoomPos = Mathf.Clamp(curZoomPos, 40f, 400f);
// Makes the actual change to Field Of View
Camera.main.orthographicSize = curZoomPos;
}
public void CheckVictory()
{
if ((clock1.transform.position - clock2.transform.position).sqrMagnitude < 10 && Mathf.Abs((float)(clock1.properTime - clock2.properTime)) < 1)
if ((clock1.transform.position - clock2.transform.position).sqrMagnitude < 20 && Mathf.Abs((float)(clock1.properTime - clock2.properTime)) < 1)
victoryScreen.SetActive(true);
}
}
......@@ -31,6 +31,9 @@ GraphicsSettings:
m_AlwaysIncludedShaders:
- {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 10770, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16000, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16001, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 17000, guid: 0000000000000000f000000000000000, type: 0}
m_PreloadedShaders: []
m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000,
type: 0}
......
......@@ -4,7 +4,7 @@
UnityConnectSettings:
m_ObjectHideFlags: 0
serializedVersion: 1
m_Enabled: 0
m_Enabled: 1
m_TestMode: 0
m_EventOldUrl: https://api.uca.cloud.unity3d.com/v1/events
m_EventUrl: https://cdp.cloud.unity3d.com/v1/events
......
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