1
0
mirror of https://github.com/CLfamilaris/VRDOM.git synced 2024-12-18 20:25:52 +01:00

Add comments, rename variables

This commit is contained in:
RubberDuckShobe 2023-08-03 21:47:18 +02:00
parent a4b64f8275
commit 2e392206ab
4 changed files with 44 additions and 40 deletions

View File

@ -8,60 +8,62 @@ public partial class FootSensor : CsgMesh3D
private GameConnection _gameConnection; private GameConnection _gameConnection;
private Area3D _sensor; private Area3D _sensor;
private Marker3D MinMarker; private Marker3D _minMarker;
private Marker3D MaxMarker; private Marker3D _maxMarker;
private Dictionary<int, bool> _activeSensors = new Dictionary<int, bool>(); private Dictionary<int, bool> _activeSensors = new();
public Vector3[] gpu_led_data = new Vector3[1862]; public Vector3[] gpu_led_data = new Vector3[1862];
private Vector2 defaultTouchSize = new Vector2(0.15f, 0.15f); private Vector2 _defaultTouchSize = new(0.15f, 0.15f);
public override void _PhysicsProcess(double delta) public override void _PhysicsProcess(double delta)
{ {
List<TouchCommand> touchCommands = new List<TouchCommand>(); //"collect" touch commands so we can all send them in one WebSocket message
//needed so the multitouch doesn't brutally murder the WebSocket connection
List<TouchCommand> touchCommands = new();
//Reset the "touching" bool,
//if it's still touching it'll be set to true again later
foreach (var pair in _activeSensors) foreach (var pair in _activeSensors)
_activeSensors[pair.Key] = false; _activeSensors[pair.Key] = false;
//See if any SensorTrigger is touching the foot sensor area
foreach (Area3D area in _sensor.GetOverlappingAreas()) foreach (Area3D area in _sensor.GetOverlappingAreas())
{ {
var sensorTrigger = area as SensorTrigger; var sensorTrigger = area as SensorTrigger;
RayCast3D raycast = area.GetNode("RayCast3D") as RayCast3D; RayCast3D raycast = area.GetNode("RayCast3D") as RayCast3D;
if (raycast.IsColliding()) if (raycast.IsColliding())
{ {
//If the sensor is already in the dict, it's already touched before -> MOVE
//If it hasn't touched before -> DOWN
if (_activeSensors.ContainsKey(sensorTrigger.sensorId)) if (_activeSensors.ContainsKey(sensorTrigger.sensorId))
{ {
_activeSensors[sensorTrigger.sensorId] = true; _activeSensors[sensorTrigger.sensorId] = true;
//GD.Print($"move event on {sensorTrigger.sensorId}!"); touchCommands.Add(new TouchCommand(sensorTrigger.sensorId, (int)TouchEvents.TOUCH_MOVE, GetLocalTouchPosition(new Vector2(raycast.GetCollisionPoint().X, raycast.GetCollisionPoint().Z)), _defaultTouchSize));
//move event!
touchCommands.Add(new TouchCommand(sensorTrigger.sensorId, (int)TouchEvents.TOUCH_MOVE, GetLocalTouchPosition(new Vector2(raycast.GetCollisionPoint().X, raycast.GetCollisionPoint().Z)), defaultTouchSize));
} }
else else
{ {
_activeSensors.Add(sensorTrigger.sensorId, true); _activeSensors.Add(sensorTrigger.sensorId, true);
//GD.Print($"down event on {sensorTrigger.sensorId}!"); touchCommands.Add(new TouchCommand(sensorTrigger.sensorId, (int)TouchEvents.TOUCH_DOWN, GetLocalTouchPosition(new Vector2(raycast.GetCollisionPoint().X, raycast.GetCollisionPoint().Z)), _defaultTouchSize));
//down event!
touchCommands.Add(new TouchCommand(sensorTrigger.sensorId, (int)TouchEvents.TOUCH_DOWN, GetLocalTouchPosition(new Vector2(raycast.GetCollisionPoint().X, raycast.GetCollisionPoint().Z)), defaultTouchSize));
} }
} }
} }
//If touching bool is still false -> UP
foreach (var pair in _activeSensors) foreach (var pair in _activeSensors)
{ {
if (pair.Value == false) if (pair.Value == false)
{ {
_activeSensors.Remove(pair.Key); _activeSensors.Remove(pair.Key);
//GD.Print($"up event on {pair.Key}!"); touchCommands.Add(new TouchCommand(pair.Key, (int)TouchEvents.TOUCH_UP, new Vector2(0, 0), _defaultTouchSize));
//up event!
touchCommands.Add(new TouchCommand(pair.Key, (int)TouchEvents.TOUCH_UP, new Vector2(0, 0), defaultTouchSize));
} }
} }
_gameConnection.setTouch(touchCommands); _gameConnection.setTouch(touchCommands);
} }
//Turns the world position into X and Y values from 0 to 1 on the pad that we can use for Spice
public Vector2 GetLocalTouchPosition(Vector2 vec) public Vector2 GetLocalTouchPosition(Vector2 vec)
{ {
float x = 1 - (vec.X - MinMarker.GlobalPosition.X) / (MaxMarker.GlobalPosition.X - MinMarker.GlobalPosition.X); float x = 1 - (vec.X - _minMarker.GlobalPosition.X) / (_maxMarker.GlobalPosition.X - _minMarker.GlobalPosition.X);
float y = 1 - (vec.Y - MinMarker.GlobalPosition.Z) / (MaxMarker.GlobalPosition.Z - MinMarker.GlobalPosition.Z); float y = 1 - (vec.Y - _minMarker.GlobalPosition.Z) / (_maxMarker.GlobalPosition.Z - _minMarker.GlobalPosition.Z);
return new Vector2(1 - x, 1 - y); return new Vector2(1 - x, 1 - y);
} }
@ -69,8 +71,8 @@ public partial class FootSensor : CsgMesh3D
public override void _Ready() public override void _Ready()
{ {
_sensor = GetNode("Sensor") as Area3D; _sensor = GetNode("Sensor") as Area3D;
MinMarker = GetNode("Sensor/MinMarker") as Marker3D; _minMarker = GetNode("Sensor/MinMarker") as Marker3D;
MaxMarker = GetNode("Sensor/MaxMarker") as Marker3D; _maxMarker = GetNode("Sensor/MaxMarker") as Marker3D;
} }
// Called every frame. 'delta' is the elapsed time since the previous frame. // Called every frame. 'delta' is the elapsed time since the previous frame.

View File

@ -10,14 +10,14 @@ public partial class GameConnection : Node
[Export] [Export]
private FootSensor _footSensor; private FootSensor _footSensor;
private WebSocketPeer wsPeer = new WebSocketPeer(); private WebSocketPeer _wsPeer = new();
double lightingGetInterval = 0.0166; readonly double lightingGetInterval = 0.0166;
double lightingGetTimer = 0; double lightingGetTimer = 0;
public void getLighting() public void getLighting()
{ {
if (wsPeer.GetReadyState() == WebSocketPeer.State.Open) if (_wsPeer.GetReadyState() == WebSocketPeer.State.Open)
wsPeer.Send(Encoding.UTF8.GetBytes("{\"id\":0,\"module\":\"drs\",\"function\":\"tapeled_get\",\"params\":[]}")); _wsPeer.Send(Encoding.UTF8.GetBytes("{\"id\":0,\"module\":\"drs\",\"function\":\"tapeled_get\",\"params\":[]}"));
} }
public void setTouch(List<TouchCommand> touchCommands) public void setTouch(List<TouchCommand> touchCommands)
@ -25,7 +25,7 @@ public partial class GameConnection : Node
if (touchCommands.Count == 0) if (touchCommands.Count == 0)
return; return;
if (wsPeer.GetReadyState() == WebSocketPeer.State.Open) if (_wsPeer.GetReadyState() == WebSocketPeer.State.Open)
{ {
string touchCommandString = "{\"id\":1,\"module\":\"drs\",\"function\":\"touch_set\",\"params\":["; string touchCommandString = "{\"id\":1,\"module\":\"drs\",\"function\":\"touch_set\",\"params\":[";
foreach (var touchCommand in touchCommands) foreach (var touchCommand in touchCommands)
@ -34,7 +34,7 @@ public partial class GameConnection : Node
} }
touchCommandString = touchCommandString.Remove(touchCommandString.Length - 1); touchCommandString = touchCommandString.Remove(touchCommandString.Length - 1);
touchCommandString += "]}"; touchCommandString += "]}";
wsPeer.Send(Encoding.UTF8.GetBytes(touchCommandString)); _wsPeer.Send(Encoding.UTF8.GetBytes(touchCommandString));
} }
} }
@ -42,18 +42,18 @@ public partial class GameConnection : Node
public override void _Ready() public override void _Ready()
{ {
GD.Print("Connecting WebSocket..."); GD.Print("Connecting WebSocket...");
wsPeer.ConnectToUrl(_websocket_url); _wsPeer.ConnectToUrl(_websocket_url); //this shit takes so goddamn long to connect, and I CANNOT FATHOM WHY
wsPeer.EncodeBufferMaxSize = 16 * 1024 * 1024; _wsPeer.EncodeBufferMaxSize = 16 * 1024 * 1024; //give the buffer sizes a generous increase. it's a lot of data
wsPeer.InboundBufferSize = 16 * 1024 * 1024; _wsPeer.InboundBufferSize = 16 * 1024 * 1024;
wsPeer.OutboundBufferSize = 16 * 1024 * 1024; _wsPeer.OutboundBufferSize = 16 * 1024 * 1024;
} }
// Called every frame. 'delta' is the elapsed time since the previous frame. // Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta) public override void _Process(double delta)
{ {
lightingGetTimer += delta; lightingGetTimer += delta;
wsPeer.Poll(); _wsPeer.Poll();
var state = wsPeer.GetReadyState(); var state = _wsPeer.GetReadyState();
switch (state) switch (state)
{ {
case WebSocketPeer.State.Open: case WebSocketPeer.State.Open:
@ -63,13 +63,13 @@ public partial class GameConnection : Node
lightingGetTimer = 0; lightingGetTimer = 0;
} }
for (int i = 0; i < wsPeer.GetAvailablePacketCount(); i++) for (int i = 0; i < _wsPeer.GetAvailablePacketCount(); i++)
{ {
string packetText = Encoding.UTF8.GetString(wsPeer.GetPacket()); string packetText = Encoding.UTF8.GetString(_wsPeer.GetPacket());
if (packetText.Contains("id")) if (packetText.Contains("id")) //questionable way to check if the message is valid
{ {
var dict = Json.ParseString(packetText).AsGodotDictionary(); var dict = Json.ParseString(packetText).AsGodotDictionary();
if (dict.ContainsKey("data") && dict["id"].AsInt32() == 0) if (dict.ContainsKey("data") && dict["id"].AsInt32() == 0) //Only the lighting data responses will have an ID of 0
{ {
int[] ledData = dict["data"].AsGodotArray()[0].AsInt32Array(); int[] ledData = dict["data"].AsGodotArray()[0].AsInt32Array();
//The LED data is in the format of [r, g, b, r, g, b, ...] //The LED data is in the format of [r, g, b, r, g, b, ...]
@ -84,8 +84,8 @@ public partial class GameConnection : Node
break; break;
case WebSocketPeer.State.Closed: case WebSocketPeer.State.Closed:
GD.Print("WebSocket closed."); GD.Print("WebSocket closed.");
var code = wsPeer.GetCloseCode(); var code = _wsPeer.GetCloseCode();
var reason = wsPeer.GetCloseReason(); var reason = _wsPeer.GetCloseReason();
GD.Print("WebSocket closed with code: %d, reason %s. Clean: %s", code, reason, code != -1); GD.Print("WebSocket closed with code: %d, reason %s. Clean: %s", code, reason, code != -1);
SetProcess(false); SetProcess(false);

View File

@ -9,13 +9,15 @@ public enum TouchEvents
TOUCH_MOVE = 2, TOUCH_MOVE = 2,
} }
public class TouchCommand { public class TouchCommand
{
public int sensorId; public int sensorId;
public int touchEvent; public int touchEvent;
public Vector2 touchPosition; public Vector2 touchPosition;
public Vector2 touchSize; public Vector2 touchSize;
public TouchCommand(int sensorId, int touchEvent, Vector2 touchPosition, Vector2 touchSize) { public TouchCommand(int sensorId, int touchEvent, Vector2 touchPosition, Vector2 touchSize)
{
this.sensorId = sensorId; this.sensorId = sensorId;
this.touchEvent = touchEvent; this.touchEvent = touchEvent;
this.touchPosition = touchPosition; this.touchPosition = touchPosition;

View File

@ -7,7 +7,7 @@ public partial class RootInit : Node3D
public override void _Ready() public override void _Ready()
{ {
_xrInterface = XRServer.FindInterface("OpenXR"); _xrInterface = XRServer.FindInterface("OpenXR");
if(_xrInterface != null && _xrInterface.IsInitialized()) if (_xrInterface != null && _xrInterface.IsInitialized())
{ {
GD.Print("OpenXR initialized successfully"); GD.Print("OpenXR initialized successfully");