Commit ea743dbd authored by Mikko Lainio's avatar Mikko Lainio 🇺🇦
Browse files

Replaced boxes with ragdoll men. Added percentage of well pop visible.

parent 7e98f4bc
fileFormatVersion: 2
guid: bb6569a682674d6429de1b9a9eaf4327
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 3653bf778ef537c4cabd156f2f246dfd
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
......@@ -4,12 +4,14 @@ using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
public class CubeController : MonoBehaviour
public class HangerController : MonoBehaviour
{
public enum CubePosition
{
None,
Left,
Right
Right,
Mid
}
public KeyCode MoveLeft = KeyCode.A;
......@@ -18,22 +20,44 @@ public class CubeController : MonoBehaviour
public CubePosition cPosition;
private Vector3 m_defaultPos;
private Rigidbody m_rb;
public TMPro.TextMeshProUGUI textObj;
private float m_heightTarget;
private bool m_noMove = false;
// Start is called before the first frame update
void Awake()
{
QuantumDemoManager.RegisterController(this, cPosition);
m_defaultPos = transform.position;
m_rb = GetComponent<Rigidbody>();
}
private void OnEnable()
{
transform.position = m_defaultPos;
StartCoroutine(LerpMove(m_defaultPos, 0.5f));
UpdateTextObject();
}
private IEnumerator LerpMove(Vector3 p, float time)
{
m_noMove = true;
float t = 0;
Vector3 start = transform.position;
while (t < time)
{
yield return new WaitForEndOfFrame();
t += Time.deltaTime;
float pt = t / time;
transform.position = (Vector3.Lerp(start, p, pt));
}
m_noMove = false;
}
// Update is called once per frame
void Update()
{
......@@ -46,10 +70,21 @@ public class CubeController : MonoBehaviour
Move(1f);
}
UpdateTextObject();
}
private void FixedUpdate()
{
if (m_noMove)
return;
Vector3 v = m_rb.position;
v.y = m_heightTarget;
Vector3 deltaMove = v - m_rb.position;
m_rb.MovePosition(m_rb.position + deltaMove * Time.fixedDeltaTime);
}
private void UpdateTextObject()
public void UpdateTextObject()
{
if (textObj != null)
{
......@@ -57,10 +92,23 @@ public class CubeController : MonoBehaviour
}
}
public void UpdateTextObject(float p)
{
if (textObj != null)
{
textObj.text = cPosition + $"\n{MoveLeft} <-> {MoveRight}\n" + CurrentInputPosition + "\n" +
p.ToString("P0");
}
}
private void Move(float dir)
{
if (m_noMove)
return;
transform.position += Vector3.right * dir * Time.deltaTime;
//clamp the position
float x = transform.position.x - m_defaultPos.x;
x = Mathf.Min(1f, Mathf.Max(x, -1f));
Vector3 v = transform.position;
......@@ -69,4 +117,11 @@ public class CubeController : MonoBehaviour
}
public float CurrentInputPosition => transform.position.x - m_defaultPos.x;
public void SetHeight(float h)
{
m_heightTarget = h;
}
}
......@@ -4,16 +4,37 @@ using UnityEngine;
public class MaterialColorChanger : MonoBehaviour
{
[SerializeField] private Renderer m_renderer;
[SerializeField] private GameObject parentObject;
private Renderer[] m_renderers;
private Renderer m_singleRenderer;
public bool FindAll = false;
// Start is called before the first frame update
void Start()
{
if (FindAll)
{
m_renderers = parentObject.GetComponentsInChildren<Renderer>();
}
else
{
m_singleRenderer = parentObject.GetComponentInChildren<Renderer>();
}
}
public void SetColor(Color c)
{
m_renderer.material.SetColor("_Color", c);
if (m_singleRenderer != null)
m_singleRenderer.material.SetColor("_Color", c);
if (m_renderers.Length > 0)
{
foreach (Renderer r in m_renderers)
{
r.material.SetColor("_Color", c);
}
}
}
}
......@@ -23,9 +23,9 @@ public class QuantumDemoManager : MonoBehaviour
}
}
private CubeController m_left;
private CubeController m_right;
[SerializeField] private Transform m_mid;
private HangerController m_left;
private HangerController m_right;
private HangerController m_mid;
private StirapEnv m_env;
......@@ -158,11 +158,13 @@ public class QuantumDemoManager : MonoBehaviour
/// <returns></returns>
private IEnumerator GameCoroutine()
{
float time, delta;
int step = 0;
ResetGrowthMeasures();
// Run the stirap env step at intervals determined by UpdateFrequency, until the result states Done.
yield return new WaitForSeconds(1f);
do
{
time = Time.time;
......@@ -211,49 +213,31 @@ public class QuantumDemoManager : MonoBehaviour
result = m_env.Step(left, right);
}
Vector3 leftScale = m_left.transform.localScale;
Vector3 rightScale = m_right.transform.localScale;
Vector3 midScale = m_mid.localScale;
float lastRightScale = rightScale.y;
float lastMidScale = midScale.y;
float lastLeftScale = leftScale.y;
leftScale.y = result.Observation[0] * 3f;
rightScale.y = result.Observation[1] * 3f;
midScale.y = 3f - (leftScale.y + rightScale.y);
m_growthRight.Update(rightScale.y - lastRightScale);
m_growthMid.Update(midScale.y - lastMidScale);
m_growthLeft.Update(leftScale.y - lastLeftScale);
// if (m_growthRight.Count > m_growthBuffer)
// m_growthRight.RemoveAt(0);
// m_growthRight.Add(growth);
//
// growth = 0f;
// foreach (float g in m_growthRight)
// {
// growth += g;
// }
//
// growth /= m_growthRight.Count;
m_left.transform.localScale = leftScale;
m_right.transform.localScale = rightScale;
m_mid.localScale = midScale;
float leftPop = result.LeftPopulation;
float rightPop = result.RightPopulation;
float midPop = 1f - (leftPop + rightPop);
float lPos = result.Observation[2];
float rPos = result.Observation[3];
//m_left.transform.position = new Vector3(lPos * 2, 0f, 0f) + Vector3.left * 2f;
//m_right.transform.position = new Vector3(rPos * 2, 0f, 0f) + Vector3.right * 2f;
UpdateWell(m_right, rightPop, m_growthRight);
UpdateWell(m_left, leftPop, m_growthLeft);
UpdateWell(m_mid, midPop, m_growthMid);
RenderPlot(result);
SetScore(result.RightPopulation, step);
return result.Done;
}
private void UpdateWell(HangerController t, float value, RingBuffer growthMeasure)
{
Vector3 p = t.transform.position;
float last = p.y;
p.y = value * 5f;
growthMeasure.Update(p.y - last);
t.SetHeight(p.y);
t.UpdateTextObject(value);
}
private void RenderPlot(StirapEnv.StepResult result)
{
if (m_plotRenderer == null)
......@@ -299,7 +283,7 @@ public class QuantumDemoManager : MonoBehaviour
// }
//float v = growth / MaxGradientThreshold;
float scale = 10000;
float scale = 1;
float r = Mathf.Max(0f, Mathf.Min(1f, 0.5f + m_growthRight.Average * scale / MaxGradientThreshold));
float l = Mathf.Max(0f, Mathf.Min(1f, 0.5f + m_growthLeft.Average * scale / MaxGradientThreshold));
float m = Mathf.Max(0f, Mathf.Min(1f, 0.5f + m_growthMid.Average * scale/2f / MaxGradientThreshold));
......@@ -307,6 +291,7 @@ public class QuantumDemoManager : MonoBehaviour
Color right = m_rewardGradient.Evaluate(r);
Color left = m_rewardGradient.Evaluate(1-l);
Color mid = m_rewardGradient.Evaluate(m);
m_leftColor.SetColor(left);
m_rightColor.SetColor(right);
m_midColor.SetColor(mid);
......@@ -314,13 +299,15 @@ public class QuantumDemoManager : MonoBehaviour
}
public static void RegisterController(CubeController ctrl, CubeController.CubePosition cPosition)
public static void RegisterController(HangerController ctrl, HangerController.CubePosition cPosition)
{
switch (cPosition)
{
case CubeController.CubePosition.Left: Instance.m_left = ctrl;
case HangerController.CubePosition.Left: Instance.m_left = ctrl;
break;
case HangerController.CubePosition.Right: Instance.m_right = ctrl;
break;
case CubeController.CubePosition.Right: Instance.m_right = ctrl;
case HangerController.CubePosition.Mid: Instance.m_mid = ctrl;
break;
}
}
......
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