diff --git a/scripts/FootSensor.cs b/scripts/FootSensor.cs index d982236..15b9a72 100644 --- a/scripts/FootSensor.cs +++ b/scripts/FootSensor.cs @@ -8,60 +8,62 @@ public partial class FootSensor : CsgMesh3D private GameConnection _gameConnection; private Area3D _sensor; - private Marker3D MinMarker; - private Marker3D MaxMarker; - private Dictionary _activeSensors = new Dictionary(); + private Marker3D _minMarker; + private Marker3D _maxMarker; + private Dictionary _activeSensors = new(); 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) { - List touchCommands = new List(); + //"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 touchCommands = new(); + //Reset the "touching" bool, + //if it's still touching it'll be set to true again later foreach (var pair in _activeSensors) _activeSensors[pair.Key] = false; + //See if any SensorTrigger is touching the foot sensor area foreach (Area3D area in _sensor.GetOverlappingAreas()) { var sensorTrigger = area as SensorTrigger; RayCast3D raycast = area.GetNode("RayCast3D") as RayCast3D; 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)) { _activeSensors[sensorTrigger.sensorId] = true; - //GD.Print($"move event on {sensorTrigger.sensorId}!"); - //move event! - touchCommands.Add(new TouchCommand(sensorTrigger.sensorId, (int)TouchEvents.TOUCH_MOVE, GetLocalTouchPosition(new Vector2(raycast.GetCollisionPoint().X, raycast.GetCollisionPoint().Z)), defaultTouchSize)); - + touchCommands.Add(new TouchCommand(sensorTrigger.sensorId, (int)TouchEvents.TOUCH_MOVE, GetLocalTouchPosition(new Vector2(raycast.GetCollisionPoint().X, raycast.GetCollisionPoint().Z)), _defaultTouchSize)); } else { _activeSensors.Add(sensorTrigger.sensorId, true); - //GD.Print($"down event on {sensorTrigger.sensorId}!"); - //down event! - touchCommands.Add(new TouchCommand(sensorTrigger.sensorId, (int)TouchEvents.TOUCH_DOWN, GetLocalTouchPosition(new Vector2(raycast.GetCollisionPoint().X, raycast.GetCollisionPoint().Z)), defaultTouchSize)); + 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) { if (pair.Value == false) { _activeSensors.Remove(pair.Key); - //GD.Print($"up event on {pair.Key}!"); - //up event! - touchCommands.Add(new TouchCommand(pair.Key, (int)TouchEvents.TOUCH_UP, new Vector2(0, 0), defaultTouchSize)); + touchCommands.Add(new TouchCommand(pair.Key, (int)TouchEvents.TOUCH_UP, new Vector2(0, 0), _defaultTouchSize)); } } _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) { - 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 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); return new Vector2(1 - x, 1 - y); } @@ -69,8 +71,8 @@ public partial class FootSensor : CsgMesh3D public override void _Ready() { _sensor = GetNode("Sensor") as Area3D; - MinMarker = GetNode("Sensor/MinMarker") as Marker3D; - MaxMarker = GetNode("Sensor/MaxMarker") as Marker3D; + _minMarker = GetNode("Sensor/MinMarker") as Marker3D; + _maxMarker = GetNode("Sensor/MaxMarker") as Marker3D; } // Called every frame. 'delta' is the elapsed time since the previous frame. diff --git a/scripts/GameConnection.cs b/scripts/GameConnection.cs index 7c46b8c..a5107f4 100644 --- a/scripts/GameConnection.cs +++ b/scripts/GameConnection.cs @@ -10,14 +10,14 @@ public partial class GameConnection : Node [Export] private FootSensor _footSensor; - private WebSocketPeer wsPeer = new WebSocketPeer(); - double lightingGetInterval = 0.0166; + private WebSocketPeer _wsPeer = new(); + readonly double lightingGetInterval = 0.0166; double lightingGetTimer = 0; public void getLighting() { - if (wsPeer.GetReadyState() == WebSocketPeer.State.Open) - wsPeer.Send(Encoding.UTF8.GetBytes("{\"id\":0,\"module\":\"drs\",\"function\":\"tapeled_get\",\"params\":[]}")); + if (_wsPeer.GetReadyState() == WebSocketPeer.State.Open) + _wsPeer.Send(Encoding.UTF8.GetBytes("{\"id\":0,\"module\":\"drs\",\"function\":\"tapeled_get\",\"params\":[]}")); } public void setTouch(List touchCommands) @@ -25,7 +25,7 @@ public partial class GameConnection : Node if (touchCommands.Count == 0) return; - if (wsPeer.GetReadyState() == WebSocketPeer.State.Open) + if (_wsPeer.GetReadyState() == WebSocketPeer.State.Open) { string touchCommandString = "{\"id\":1,\"module\":\"drs\",\"function\":\"touch_set\",\"params\":["; foreach (var touchCommand in touchCommands) @@ -34,7 +34,7 @@ public partial class GameConnection : Node } touchCommandString = touchCommandString.Remove(touchCommandString.Length - 1); 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() { GD.Print("Connecting WebSocket..."); - wsPeer.ConnectToUrl(_websocket_url); - wsPeer.EncodeBufferMaxSize = 16 * 1024 * 1024; - wsPeer.InboundBufferSize = 16 * 1024 * 1024; - wsPeer.OutboundBufferSize = 16 * 1024 * 1024; + _wsPeer.ConnectToUrl(_websocket_url); //this shit takes so goddamn long to connect, and I CANNOT FATHOM WHY + _wsPeer.EncodeBufferMaxSize = 16 * 1024 * 1024; //give the buffer sizes a generous increase. it's a lot of data + _wsPeer.InboundBufferSize = 16 * 1024 * 1024; + _wsPeer.OutboundBufferSize = 16 * 1024 * 1024; } // Called every frame. 'delta' is the elapsed time since the previous frame. public override void _Process(double delta) { lightingGetTimer += delta; - wsPeer.Poll(); - var state = wsPeer.GetReadyState(); + _wsPeer.Poll(); + var state = _wsPeer.GetReadyState(); switch (state) { case WebSocketPeer.State.Open: @@ -63,13 +63,13 @@ public partial class GameConnection : Node 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()); - if (packetText.Contains("id")) + string packetText = Encoding.UTF8.GetString(_wsPeer.GetPacket()); + if (packetText.Contains("id")) //questionable way to check if the message is valid { 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(); //The LED data is in the format of [r, g, b, r, g, b, ...] @@ -84,8 +84,8 @@ public partial class GameConnection : Node break; case WebSocketPeer.State.Closed: GD.Print("WebSocket closed."); - var code = wsPeer.GetCloseCode(); - var reason = wsPeer.GetCloseReason(); + var code = _wsPeer.GetCloseCode(); + var reason = _wsPeer.GetCloseReason(); GD.Print("WebSocket closed with code: %d, reason %s. Clean: %s", code, reason, code != -1); SetProcess(false); diff --git a/scripts/GameInfo.cs b/scripts/GameInfo.cs index d2474cb..25cde9c 100644 --- a/scripts/GameInfo.cs +++ b/scripts/GameInfo.cs @@ -9,13 +9,15 @@ public enum TouchEvents TOUCH_MOVE = 2, } -public class TouchCommand { +public class TouchCommand +{ public int sensorId; public int touchEvent; public Vector2 touchPosition; 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.touchEvent = touchEvent; this.touchPosition = touchPosition; diff --git a/scripts/RootInit.cs b/scripts/RootInit.cs index 6b9d8f8..8105858 100644 --- a/scripts/RootInit.cs +++ b/scripts/RootInit.cs @@ -7,7 +7,7 @@ public partial class RootInit : Node3D public override void _Ready() { _xrInterface = XRServer.FindInterface("OpenXR"); - if(_xrInterface != null && _xrInterface.IsInitialized()) + if (_xrInterface != null && _xrInterface.IsInitialized()) { GD.Print("OpenXR initialized successfully");