Skip to content
Snippets Groups Projects
Commit 2e818a0c authored by gawen.ackerman's avatar gawen.ackerman :robot:
Browse files

ADD : V1, LogManager

parent 38deac06
Branches
No related tags found
No related merge requests found
This diff is collapsed.
...@@ -924,6 +924,50 @@ Transform: ...@@ -924,6 +924,50 @@ Transform:
m_Children: [] m_Children: []
m_Father: {fileID: 1244371183} m_Father: {fileID: 1244371183}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &488291749
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 488291751}
- component: {fileID: 488291750}
m_Layer: 0
m_Name: LogManager
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &488291750
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 488291749}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5a2b03e912995c74fb5d8b8c41c3d3e1, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &488291751
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 488291749}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0.12605426, y: 0.77100605, z: 2.3938558}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!4 &528685368 stripped --- !u!4 &528685368 stripped
Transform: Transform:
m_CorrespondingSourceObject: {fileID: 8951808350050416116, guid: 8b61e12ffefeb4e6abf74d83fd582466, type: 3} m_CorrespondingSourceObject: {fileID: 8951808350050416116, guid: 8b61e12ffefeb4e6abf74d83fd582466, type: 3}
...@@ -3776,3 +3820,4 @@ SceneRoots: ...@@ -3776,3 +3820,4 @@ SceneRoots:
- {fileID: 1434339810} - {fileID: 1434339810}
- {fileID: 284084581} - {fileID: 284084581}
- {fileID: 1244371183} - {fileID: 1244371183}
- {fileID: 488291751}
fileFormatVersion: 2
guid: 8776b387cc1b526438389005ddf47ab4
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class LogEntry
{
public float timestamp;
public Vector3 position;
public Vector3 velocity;
public string movementDirection;
public float distanceSinceLastFrame;
public float totalDistance;
public float timeSinceLastCollectible;
public int totalCollectibles;
public int currentScore;
public string rawUDPInput;
public override string ToString()
{
string FormatVector(Vector3 v) => $"({v.x:F2}, {v.y:F2}, {v.z:F2})";
return
$"--- Log Entry ---\n" +
$"Timestamp : {timestamp:F2} s\n" +
$"Position : {FormatVector(position)}\n" +
$"Velocity : {FormatVector(velocity)}\n" +
$"Movement Direction : {movementDirection}\n" +
$"Distance Since Last Frame : {distanceSinceLastFrame:F2} units\n" +
$"Total Distance : {totalDistance:F2} units\n" +
$"Time Since Last Collectible : {timeSinceLastCollectible:F2} s\n" +
$"Total Collectibles : {totalCollectibles}\n" +
$"Current Score : {currentScore}\n" +
$"Raw UDP Input : \"{rawUDPInput}\"\n" +
$"----------------------------";
}
}
public class LogManager : MonoBehaviour
{
public static LogManager Instance;
private List<LogEntry> logs = new List<LogEntry>();
private Vector3 lastPosition;
private float totalDistance = 0f;
private float lastCollectibleTime = 0f;
private int totalCollectibles = 0;
private int currentScore = 0;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
lastPosition = Vector3.zero;
}
public void Log(Vector3 currentPosition, Vector3 velocity, string direction, string rawUDP)
{
float timestamp = Time.time;
float distance = Vector3.Distance(currentPosition, lastPosition);
totalDistance += distance;
var entry = new LogEntry
{
timestamp = timestamp,
position = currentPosition,
velocity = velocity,
movementDirection = direction,
distanceSinceLastFrame = distance,
totalDistance = totalDistance,
timeSinceLastCollectible = timestamp - lastCollectibleTime,
totalCollectibles = totalCollectibles,
currentScore = currentScore,
rawUDPInput = rawUDP
};
logs.Add(entry);
lastPosition = currentPosition;
Debug.Log(entry.ToString());
}
public void RegisterCollectible(int scoreValue)
{
currentScore += scoreValue;
lastCollectibleTime = Time.time;
}
public void RegisterTotalCollectible(int value)
{
totalCollectibles = value;
}
public void SaveLogsToFile(string filename)
{
string json = JsonUtility.ToJson(new LogWrapper { logs = logs }, true);
System.IO.File.WriteAllText(Application.dataPath + "/" + filename, json);
}
[System.Serializable]
private class LogWrapper
{
public List<LogEntry> logs;
}
}
fileFormatVersion: 2
guid: 5a2b03e912995c74fb5d8b8c41c3d3e1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using UnityEngine; using UnityEngine;
using UnityEngine.XR; using UnityEngine.XR;
...@@ -27,6 +28,9 @@ public class PlayerController : MonoBehaviour ...@@ -27,6 +28,9 @@ public class PlayerController : MonoBehaviour
private UdpClientController playerUdpClient; private UdpClientController playerUdpClient;
private Vector3 lastPosition;
void Awake() void Awake()
{ {
playerUdpClient = new UdpClientController(5000); playerUdpClient = new UdpClientController(5000);
...@@ -34,7 +38,8 @@ public class PlayerController : MonoBehaviour ...@@ -34,7 +38,8 @@ public class PlayerController : MonoBehaviour
void Start() void Start()
{ {
ApplyPlayerPrefs(); ApplyPlayerPrefs();
lastPosition = transform.position;
} }
void Update() void Update()
...@@ -132,51 +137,95 @@ public class PlayerController : MonoBehaviour ...@@ -132,51 +137,95 @@ public class PlayerController : MonoBehaviour
/// Define the player controls according if there is a connection to the UDP server or not /// Define the player controls according if there is a connection to the UDP server or not
/// </summary> /// </summary>
/// <param name="isConnectedToUdpServer">True if the player is connected to the UDP server</param> /// <param name="isConnectedToUdpServer">True if the player is connected to the UDP server</param>
private void WatchPlayerControls() //private void WatchPlayerControls()
{ //{
if (playerUdpClient.IsConnected()) // Vector3 movement = new Vector3();
{ // if (playerUdpClient.IsConnected())
PositionManager pm = this.GetComponent<PositionManager>(); // {
float[] newPosition = pm.updatePosition(playerUdpClient?.data); // PositionManager pm = this.GetComponent<PositionManager>();
// float[] newPosition = pm.updatePosition(playerUdpClient?.data);
float x = newPosition[0];
float z = newPosition[1]; // float x = newPosition[0];
float y = newPosition[2]; // because it's sending only x,y coords and in Unity on flat the coords are x,z // float z = newPosition[1];
// float y = newPosition[2]; // because it's sending only x,y coords and in Unity on flat the coords are x,z
float theta_x = newPosition[3];
float theta_y = newPosition[4] * rotationSensibility; // float theta_x = newPosition[3];
float theta_z = newPosition[5]; // float theta_y = newPosition[4] * rotationSensibility;
// float theta_z = newPosition[5];
StringBuilder stringBuilder = new StringBuilder();
//stringBuilder.AppendLine($"PositionManager : [x {x}], [y {y}], [z {z}] - [theta_x {theta_x}], [theta_y {theta_y}], [theta_z {theta_z}]"); // StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine($"\nTransform P : \t\t[x {this.transform.position.x}], [y {this.transform.position.y}], [z {this.transform.position.z}]"); // movement = new Vector3(x, 0, z);
Vector3 movement = new Vector3(x, 0, z); // this.transform.position += movement * Time.deltaTime * movementSensibility;
this.transform.position += movement * Time.deltaTime * movementSensibility;
stringBuilder.AppendLine($"Updated Transform P : \t[x {this.transform.position.x}], [y {this.transform.position.y}], [z {this.transform.position.z}]"); // Quaternion newRotation = Quaternion.Euler(theta_x, theta_y, theta_z);
//stringBuilder.AppendLine($"Transform R : [x {this.transform.rotation.x}], [y {this.transform.rotation.y}], [z {this.transform.rotation.z}]"); // this.transform.rotation = newRotation;
// }
Quaternion newRotation = Quaternion.Euler(theta_x, theta_y, theta_z); // else
// {
this.transform.rotation = newRotation; // // Shift the player with the keyboard, according to the camera facing direction
//stringBuilder.AppendLine($"Updated Transform R : [x {this.transform.rotation.x} ], [y {this.transform.rotation.y} ], [z {this.transform.rotation.z}]"); // float horizontal = Input.GetAxis("Horizontal");
// float vertical = Input.GetAxis("Vertical");
print(stringBuilder.ToString()); // movement = this.transform.right * horizontal + this.transform.forward * vertical;
} // this.transform.position += movement * Time.deltaTime * movementSensibility;
else
{ // // Rotate the player with the mouse (horizontal axis only)
// Shift the player with the keyboard, according to the camera facing direction // float mouseX = Input.GetAxis("Mouse X");
float horizontal = Input.GetAxis("Horizontal"); // Vector3 rotation = this.transform.rotation.eulerAngles;
float vertical = Input.GetAxis("Vertical"); // this.transform.rotation = Quaternion.Euler(rotation.x, rotation.y + mouseX * rotationSensibility, rotation.z);
Vector3 movement = this.transform.right * horizontal + this.transform.forward * vertical; // }
this.transform.position += movement * Time.deltaTime * movementSensibility; //}
// Rotate the player with the mouse (horizontal axis only) private void WatchPlayerControls()
float mouseX = Input.GetAxis("Mouse X"); {
Vector3 rotation = this.transform.rotation.eulerAngles; Vector3 movement = new Vector3();
this.transform.rotation = Quaternion.Euler(rotation.x, rotation.y + mouseX * rotationSensibility, rotation.z); string direction = "Idle";
} string rawUDP = "";
Vector3 currentPosition = transform.position;
if (playerUdpClient.IsConnected())
{
PositionManager pm = this.GetComponent<PositionManager>();
float[] newPosition = pm.updatePosition(playerUdpClient?.data);
float x = newPosition[0];
float z = newPosition[1];
float y = newPosition[2];
float theta_x = newPosition[3];
float theta_y = newPosition[4] * rotationSensibility;
float theta_z = newPosition[5];
movement = new Vector3(x, 0, z);
transform.position += movement * Time.deltaTime * movementSensibility;
Quaternion newRotation = Quaternion.Euler(theta_x, theta_y, theta_z);
transform.rotation = newRotation;
direction = InterpretDirection(movement);
rawUDP = playerUdpClient?.data;
}
else
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
movement = this.transform.right * horizontal + this.transform.forward * vertical;
transform.position += movement * Time.deltaTime * movementSensibility;
float mouseX = Input.GetAxis("Mouse X");
Vector3 rotation = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler(rotation.x, rotation.y + mouseX * rotationSensibility, rotation.z);
direction = InterpretDirection(movement);
rawUDP = "Keyboard/Mouse Input";
}
currentPosition = transform.position;
Vector3 velocity = (currentPosition - lastPosition) / Time.deltaTime;
LogManager.Instance.Log(currentPosition, velocity, direction, rawUDP);
lastPosition = currentPosition;
} }
/// <summary> /// <summary>
/// Define the XR controls if the VR headset is active /// Define the XR controls if the VR headset is active
/// </summary> /// </summary>
...@@ -223,4 +272,14 @@ public class PlayerController : MonoBehaviour ...@@ -223,4 +272,14 @@ public class PlayerController : MonoBehaviour
return device; return device;
} }
private string InterpretDirection(Vector3 delta)
{
if (delta.magnitude < 0.01f)
return "Idle";
else if (Mathf.Abs(delta.z) > Mathf.Abs(delta.x))
return delta.z > 0 ? "Forward" : "Backward";
else
return delta.x > 0 ? "Right" : "Left";
}
} }
...@@ -100,17 +100,17 @@ public class PositionManager : MonoBehaviour ...@@ -100,17 +100,17 @@ public class PositionManager : MonoBehaviour
float min = coordinates_inputRange[0]; float min = coordinates_inputRange[0];
float max = coordinates_inputRange[1]; float max = coordinates_inputRange[1];
string a = ""; //string a = "";
// Normalize the values between -1 and 1 // Normalize the values between -1 and 1
for (int i = 0; i < coordinates.Length; i++) for (int i = 0; i < coordinates.Length; i++)
{ {
if (coordinates[i] > max) coordinates_inputRange[1] = coordinates[i]; if (coordinates[i] > max) coordinates_inputRange[1] = coordinates[i];
float minmaxNormalized_value = 2 * ((coordinates[i] - min) / (max - min)) - 1; float minmaxNormalized_value = 2 * ((coordinates[i] - min) / (max - min)) - 1;
a += $"[{i}] minmaxNormalized_value = 2 * (({coordinates[i]} - {min}) / ({max} - {min})) - 1 = {minmaxNormalized_value}\n"; //a += $"[{i}] minmaxNormalized_value = 2 * (({coordinates[i]} - {min}) / ({max} - {min})) - 1 = {minmaxNormalized_value}\n";
coordinates[i] = minmaxNormalized_value * coordinates_sensibility; coordinates[i] = minmaxNormalized_value * coordinates_sensibility;
} }
print(a); //print(a);
} }
//public void NormalizeValues() //public void NormalizeValues()
......
...@@ -53,6 +53,7 @@ public class UIScoreController : MonoBehaviour ...@@ -53,6 +53,7 @@ public class UIScoreController : MonoBehaviour
public void IncrementCount() public void IncrementCount()
{ {
score++; score++;
LogManager.Instance.RegisterCollectible(score);
SetCurrentCount(score); SetCurrentCount(score);
} }
} }
...@@ -43,6 +43,7 @@ public class CollectibleGenerator : MonoBehaviour ...@@ -43,6 +43,7 @@ public class CollectibleGenerator : MonoBehaviour
UIScoreController uiScoreController = FindAnyObjectByType<UIScoreController>(); UIScoreController uiScoreController = FindAnyObjectByType<UIScoreController>();
uiScoreController.SetCurrentCount(0); uiScoreController.SetCurrentCount(0);
uiScoreController.SetTotalCount(collectibles.Count); uiScoreController.SetTotalCount(collectibles.Count);
LogManager.Instance.RegisterTotalCollectible(collectibles.Count);
} }
private Transform SelectSlotWithPreference(List<Transform> slots, int preferenceCode) private Transform SelectSlotWithPreference(List<Transform> slots, int preferenceCode)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment