feat: update method communicate with python
parent
e19dad4b7d
commit
ef010cbc37
|
@ -267,7 +267,7 @@ PrefabInstance:
|
||||||
m_Modifications:
|
m_Modifications:
|
||||||
- target: {fileID: 5011611157645887697, guid: 7a832c1c97ca15e40bad68fa0ddb5007, type: 3}
|
- target: {fileID: 5011611157645887697, guid: 7a832c1c97ca15e40bad68fa0ddb5007, type: 3}
|
||||||
propertyPath: m_Name
|
propertyPath: m_Name
|
||||||
value: Data Receive
|
value: Upb Socket
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 7936842462643178004, guid: 7a832c1c97ca15e40bad68fa0ddb5007, type: 3}
|
- target: {fileID: 7936842462643178004, guid: 7a832c1c97ca15e40bad68fa0ddb5007, type: 3}
|
||||||
propertyPath: m_LocalEulerAnglesHint.x
|
propertyPath: m_LocalEulerAnglesHint.x
|
||||||
|
|
|
@ -5,10 +5,12 @@ using UnityEngine;
|
||||||
|
|
||||||
namespace GadGame.MiniGame
|
namespace GadGame.MiniGame
|
||||||
{
|
{
|
||||||
public class Bomb : MonoBehaviour, ICollect
|
public class Bomb : MonoBehaviour, ICollect, IPoolable
|
||||||
{
|
{
|
||||||
[SerializeField] private int _reduceScore;
|
[SerializeField] private int _reduceScore;
|
||||||
[SerializeField] private Rigidbody2D _rb;
|
[SerializeField] private Rigidbody2D _rb;
|
||||||
|
|
||||||
|
private bool _inUsed;
|
||||||
|
|
||||||
public void Init(float gravityScale = 1)
|
public void Init(float gravityScale = 1)
|
||||||
{
|
{
|
||||||
|
@ -17,7 +19,7 @@ namespace GadGame.MiniGame
|
||||||
|
|
||||||
private void FixedUpdate()
|
private void FixedUpdate()
|
||||||
{
|
{
|
||||||
if (_rb.position.y <= -10)
|
if (_inUsed && _rb.position.y <= -10)
|
||||||
{
|
{
|
||||||
this.Release();
|
this.Release();
|
||||||
}
|
}
|
||||||
|
@ -28,5 +30,15 @@ namespace GadGame.MiniGame
|
||||||
GameManager.Instance.UpdateScore(-_reduceScore);
|
GameManager.Instance.UpdateScore(-_reduceScore);
|
||||||
this.Release();
|
this.Release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void OnGet()
|
||||||
|
{
|
||||||
|
_inUsed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnRelease()
|
||||||
|
{
|
||||||
|
_inUsed = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -4,10 +4,12 @@ using UnityEngine;
|
||||||
|
|
||||||
namespace GadGame.MiniGame
|
namespace GadGame.MiniGame
|
||||||
{
|
{
|
||||||
public class Item : MonoBehaviour, ICollect
|
public class Item : MonoBehaviour, ICollect, IPoolable
|
||||||
{
|
{
|
||||||
[SerializeField] private int _score;
|
[SerializeField] private int _score;
|
||||||
[SerializeField] private Rigidbody2D _rb;
|
[SerializeField] private Rigidbody2D _rb;
|
||||||
|
|
||||||
|
private bool _inUsed;
|
||||||
|
|
||||||
public void Init(float gravityScale = 1)
|
public void Init(float gravityScale = 1)
|
||||||
{
|
{
|
||||||
|
@ -16,7 +18,7 @@ namespace GadGame.MiniGame
|
||||||
|
|
||||||
private void LateUpdate()
|
private void LateUpdate()
|
||||||
{
|
{
|
||||||
if (_rb.position.y <= -10)
|
if (_inUsed && _rb.position.y <= -10)
|
||||||
{
|
{
|
||||||
this.Release();
|
this.Release();
|
||||||
}
|
}
|
||||||
|
@ -27,5 +29,15 @@ namespace GadGame.MiniGame
|
||||||
GameManager.Instance.UpdateScore(_score);
|
GameManager.Instance.UpdateScore(_score);
|
||||||
this.Release();
|
this.Release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void OnGet()
|
||||||
|
{
|
||||||
|
_inUsed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnRelease()
|
||||||
|
{
|
||||||
|
_inUsed = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -72,7 +72,7 @@ namespace GadGame.MiniGame
|
||||||
public void PlayerControl()
|
public void PlayerControl()
|
||||||
{
|
{
|
||||||
//640x480;
|
//640x480;
|
||||||
var inputData = DataReceiver.Instance.DataReceived.PosPoint;
|
var inputData = UdpSocket.Instance.DataReceived.PosPoint;
|
||||||
var inputNormalize = new Vector2((inputData.x - 213.33f)/ 213.33f, inputData.y / 480);
|
var inputNormalize = new Vector2((inputData.x - 213.33f)/ 213.33f, inputData.y / 480);
|
||||||
var input = new Vector2
|
var input = new Vector2
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,101 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Net;
|
|
||||||
using System.Net.Sockets;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading;
|
|
||||||
using GadGame.Singleton;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
namespace GadGame.Network
|
|
||||||
{
|
|
||||||
public class DataReceiver : PersistentSingleton<DataReceiver>
|
|
||||||
{
|
|
||||||
[SerializeField] private int _port = 3000;
|
|
||||||
[SerializeField] private ReceiverData _dataReceived;
|
|
||||||
|
|
||||||
private bool _receiving = true;
|
|
||||||
public ReceiverData DataReceived => _dataReceived;
|
|
||||||
|
|
||||||
private Thread _receiveThread;
|
|
||||||
private TcpListener _listener;
|
|
||||||
|
|
||||||
|
|
||||||
private void Start()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_listener = new TcpListener(IPAddress.Any, _port);
|
|
||||||
_listener.Start();
|
|
||||||
_receiving = true;
|
|
||||||
Debug.Log("Listening for data from Python...");
|
|
||||||
// Start a new thread to handle incoming data
|
|
||||||
Thread receiveThread = new Thread(GetReceiveData)
|
|
||||||
{
|
|
||||||
IsBackground = true
|
|
||||||
};
|
|
||||||
receiveThread.Start();
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Debug.LogError("Error starting listener: " + e.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GetReceiveData()
|
|
||||||
{
|
|
||||||
while (_receiving)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var client = _listener.AcceptTcpClient();
|
|
||||||
var stream = client.GetStream();
|
|
||||||
var buffer = new byte[1024];
|
|
||||||
var bytesRead = stream.Read(buffer, 0, buffer.Length);
|
|
||||||
var jsonData = Encoding.ASCII.GetString(buffer, 0, bytesRead);
|
|
||||||
_dataReceived = JsonConvert.DeserializeObject<ReceiverData>(jsonData, new JsonSerializerSettings
|
|
||||||
{
|
|
||||||
NullValueHandling = NullValueHandling.Ignore,
|
|
||||||
MissingMemberHandling = MissingMemberHandling.Ignore
|
|
||||||
});
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Debug.Log(e);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SendDataToPython(string data)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
string jsonData = JsonConvert.SerializeObject(data);
|
|
||||||
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, _port);
|
|
||||||
|
|
||||||
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
||||||
socket.Connect(endPoint);
|
|
||||||
|
|
||||||
// Convert string data to bytes
|
|
||||||
byte[] byteData = Encoding.UTF8.GetBytes(jsonData);
|
|
||||||
|
|
||||||
// Send data to Python
|
|
||||||
socket.Send(byteData);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Debug.LogError("Error sending data to Python: " + e.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OnDestroy()
|
|
||||||
{
|
|
||||||
_receiving = false;
|
|
||||||
if (_listener != null)
|
|
||||||
{
|
|
||||||
_listener.Stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,92 @@
|
||||||
|
using System;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using GadGame.Singleton;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace GadGame.Network
|
||||||
|
{
|
||||||
|
public class UdpSocket : PersistentSingleton<UdpSocket>
|
||||||
|
{
|
||||||
|
[SerializeField] private string _ip = "127.0.0.1";
|
||||||
|
[SerializeField] private int _receivePort = 8000;
|
||||||
|
[SerializeField] private int _sendPort = 8001;
|
||||||
|
[SerializeField] private ReceiverData _dataReceived;
|
||||||
|
|
||||||
|
private bool _receiving = true;
|
||||||
|
public ReceiverData DataReceived => _dataReceived;
|
||||||
|
|
||||||
|
private Thread _receiveThread;
|
||||||
|
private UdpClient _client;
|
||||||
|
private IPEndPoint _remoteEndPoint;
|
||||||
|
|
||||||
|
|
||||||
|
private void Start()
|
||||||
|
{
|
||||||
|
// Create remote endpoint
|
||||||
|
_remoteEndPoint = new IPEndPoint(IPAddress.Parse(_ip), _sendPort);
|
||||||
|
|
||||||
|
// Create local client
|
||||||
|
_client = new UdpClient(_receivePort);
|
||||||
|
|
||||||
|
// local endpoint define (where messages are received)
|
||||||
|
// Create a new thread for reception of incoming messages
|
||||||
|
_receiveThread = new Thread(new ThreadStart(ReceiveData))
|
||||||
|
{
|
||||||
|
IsBackground = true
|
||||||
|
};
|
||||||
|
_receiveThread.Start();
|
||||||
|
_receiving = true;
|
||||||
|
// Initialize (seen in comments window)
|
||||||
|
Debug.Log("UDP Comms Initialised");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ReceiveData()
|
||||||
|
{
|
||||||
|
while (_receiving)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
|
||||||
|
byte[] data = _client.Receive(ref anyIP);
|
||||||
|
string jsonData = Encoding.UTF8.GetString(data);
|
||||||
|
_dataReceived = JsonConvert.DeserializeObject<ReceiverData>(jsonData, new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
NullValueHandling = NullValueHandling.Ignore,
|
||||||
|
MissingMemberHandling = MissingMemberHandling.Ignore
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.Log(e);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SendDataToPython(string message)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
byte[] data = Encoding.UTF8.GetBytes(message);
|
||||||
|
_client.Send(data, data.Length, _remoteEndPoint);
|
||||||
|
}
|
||||||
|
catch (Exception err)
|
||||||
|
{
|
||||||
|
print(err.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnDestroy()
|
||||||
|
{
|
||||||
|
_receiving = false;
|
||||||
|
if (_receiveThread != null)
|
||||||
|
_receiveThread.Abort();
|
||||||
|
|
||||||
|
_client.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,7 +14,7 @@ namespace GadGame.State.MainFlowState
|
||||||
|
|
||||||
public override void Update(float time)
|
public override void Update(float time)
|
||||||
{
|
{
|
||||||
if (!DataReceiver.Instance.DataReceived.PassBy)
|
if (!UdpSocket.Instance.DataReceived.PassBy)
|
||||||
{
|
{
|
||||||
_noPassByTimer += Time.deltaTime;
|
_noPassByTimer += Time.deltaTime;
|
||||||
if (_noPassByTimer >= 10)
|
if (_noPassByTimer >= 10)
|
||||||
|
|
|
@ -19,7 +19,7 @@ namespace GadGame.State.MainFlowState
|
||||||
{
|
{
|
||||||
if (time >= 2)
|
if (time >= 2)
|
||||||
{
|
{
|
||||||
if (!DataReceiver.Instance.DataReceived.PassBy)
|
if (!UdpSocket.Instance.DataReceived.PassBy)
|
||||||
{
|
{
|
||||||
Runner.SetState<IdleState>();
|
Runner.SetState<IdleState>();
|
||||||
return;
|
return;
|
||||||
|
@ -27,20 +27,20 @@ namespace GadGame.State.MainFlowState
|
||||||
|
|
||||||
switch (_warned)
|
switch (_warned)
|
||||||
{
|
{
|
||||||
case false when !DataReceiver.Instance.DataReceived.Engage:
|
case false when !UdpSocket.Instance.DataReceived.Engage:
|
||||||
_warned = true;
|
_warned = true;
|
||||||
PopupManager.Instance.Show("Come Back", 5, () =>
|
PopupManager.Instance.Show("Come Back", 5, () =>
|
||||||
{
|
{
|
||||||
Runner.SetState<ViewedState>();
|
Runner.SetState<ViewedState>();
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
case true when DataReceiver.Instance.DataReceived.Engage:
|
case true when UdpSocket.Instance.DataReceived.Engage:
|
||||||
_warned = false;
|
_warned = false;
|
||||||
PopupManager.Instance.Hide();
|
PopupManager.Instance.Hide();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!DataReceiver.Instance.DataReceived.Ready) _readyTimer = 0;
|
if (!UdpSocket.Instance.DataReceived.Ready) _readyTimer = 0;
|
||||||
_readyTimer += Time.deltaTime;
|
_readyTimer += Time.deltaTime;
|
||||||
if (_readyTimer >= 5)
|
if (_readyTimer >= 5)
|
||||||
{
|
{
|
||||||
|
|
|
@ -14,7 +14,7 @@ namespace GadGame.State.MainFlowState
|
||||||
public override void Update(float time)
|
public override void Update(float time)
|
||||||
{
|
{
|
||||||
if(time < 2) return;
|
if(time < 2) return;
|
||||||
if (DataReceiver.Instance.DataReceived.PassBy)
|
if (UdpSocket.Instance.DataReceived.PassBy)
|
||||||
{
|
{
|
||||||
Runner.SetState<PassByState>();
|
Runner.SetState<PassByState>();
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,12 +14,12 @@ namespace GadGame.State.MainFlowState
|
||||||
public override void Update(float time)
|
public override void Update(float time)
|
||||||
{
|
{
|
||||||
if (time < 2f) return;
|
if (time < 2f) return;
|
||||||
if (!DataReceiver.Instance.DataReceived.PassBy)
|
if (!UdpSocket.Instance.DataReceived.PassBy)
|
||||||
{
|
{
|
||||||
Runner.SetState<IdleState>();
|
Runner.SetState<IdleState>();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (DataReceiver.Instance.DataReceived.OnVision)
|
if (UdpSocket.Instance.DataReceived.OnVision)
|
||||||
{
|
{
|
||||||
Runner.SetState<ViewedState>();
|
Runner.SetState<ViewedState>();
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ namespace GadGame.State.MainFlowState
|
||||||
|
|
||||||
public override async void Enter()
|
public override async void Enter()
|
||||||
{
|
{
|
||||||
DataReceiver.Instance.SendDataToPython("{playingGame: true}");
|
UdpSocket.Instance.SendDataToPython("{playingGame: true}");
|
||||||
await LoadSceneManager.Instance.LoadSceneWithTransitionAsync(Runner.SceneFlowConfig.GameScene.ScenePath);
|
await LoadSceneManager.Instance.LoadSceneWithTransitionAsync(Runner.SceneFlowConfig.GameScene.ScenePath);
|
||||||
_gameManager = GameManager.Instance;
|
_gameManager = GameManager.Instance;
|
||||||
_gameManager.OnEnd += OnEndGame;
|
_gameManager.OnEnd += OnEndGame;
|
||||||
|
@ -22,7 +22,7 @@ namespace GadGame.State.MainFlowState
|
||||||
{
|
{
|
||||||
switch (_warned)
|
switch (_warned)
|
||||||
{
|
{
|
||||||
case false when !DataReceiver.Instance.DataReceived.Engage:
|
case false when !UdpSocket.Instance.DataReceived.Engage:
|
||||||
{
|
{
|
||||||
_leaveTimer += Time.deltaTime;
|
_leaveTimer += Time.deltaTime;
|
||||||
if ( _leaveTimer >= 5)
|
if ( _leaveTimer >= 5)
|
||||||
|
@ -34,7 +34,7 @@ namespace GadGame.State.MainFlowState
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
case true when DataReceiver.Instance.DataReceived.Engage:
|
case true when UdpSocket.Instance.DataReceived.Engage:
|
||||||
_warned = false;
|
_warned = false;
|
||||||
_gameManager.Resume();
|
_gameManager.Resume();
|
||||||
PopupManager.Instance.Hide();
|
PopupManager.Instance.Hide();
|
||||||
|
@ -44,7 +44,7 @@ namespace GadGame.State.MainFlowState
|
||||||
|
|
||||||
public override void Exit()
|
public override void Exit()
|
||||||
{
|
{
|
||||||
DataReceiver.Instance.SendDataToPython("{playingGame: false}");
|
UdpSocket.Instance.SendDataToPython("{playingGame: false}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnEndGame()
|
private void OnEndGame()
|
||||||
|
|
|
@ -13,12 +13,12 @@ namespace GadGame.State.MainFlowState
|
||||||
public override void Update(float time)
|
public override void Update(float time)
|
||||||
{
|
{
|
||||||
if(time < 2) return;
|
if(time < 2) return;
|
||||||
if (!DataReceiver.Instance.DataReceived.PassBy)
|
if (!UdpSocket.Instance.DataReceived.PassBy)
|
||||||
{
|
{
|
||||||
Runner.SetState<IdleState>();
|
Runner.SetState<IdleState>();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (DataReceiver.Instance.DataReceived.Engage)
|
if (UdpSocket.Instance.DataReceived.Engage)
|
||||||
{
|
{
|
||||||
Runner.SetState<EngageState>();
|
Runner.SetState<EngageState>();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue