Skip to content

Commit

Permalink
solves issue with timer in GAMA
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaillandier committed Aug 21, 2024
1 parent d645103 commit 3a06b33
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected override void HandleReceivedMessage(object sender, MessageEventArgs e)
if (e.IsText)
{

Debug.Log("e.Data: " + e.Data);
// Debug.Log("e.Data: " + e.Data);
JObject jsonObj = JObject.Parse(e.Data);
string type = (string)jsonObj["type"];

Expand Down
55 changes: 15 additions & 40 deletions Assets/Scripts/Gama Provider/Simulation/SimulationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class SimulationManager : MonoBehaviour
[SerializeField] protected float GamaCRSOffsetX = 0.0f;
[SerializeField] protected float GamaCRSOffsetY = 0.0f;

GAMAMessage message = null;


// Z offset and scale
Expand Down Expand Up @@ -134,24 +133,10 @@ void FixedUpdate()
if (TimerSendPosition <= 0.0f)
UpdatePlayerPosition();
}
if (message != null)
{
initPlayer();
message = null;
}

}



private void initPlayer()
{
Dictionary<string, string> args = new Dictionary<string, string> {
{"id",ConnectionManager.Instance.getUseMiddleware() ? ConnectionManager.Instance.GetConnectionId() : ("\"" + ConnectionManager.Instance.GetConnectionId() + "\"") }
};
ConnectionManager.Instance.SendExecutableAsk("active_player", args);
}


private void Update()
{
if (TimerSendPosition > 0)
Expand Down Expand Up @@ -262,7 +247,17 @@ private void UpdatePlayerPosition()


ConnectionManager.Instance.SendExecutableAsk("move_player_external", args);



Dictionary<string, string> args2 = new Dictionary<string, string> {
{"id",ConnectionManager.Instance.getUseMiddleware() ? ConnectionManager.Instance.GetConnectionId() : ("\"" + ConnectionManager.Instance.GetConnectionId() + "\"") },
{"time_remaining", "" + ((int)(Timer.Instance.timeRemaining))},

};


ConnectionManager.Instance.SendExecutableAsk("update_player_timer", args2);

TimerSendPosition = TimeSendPosition;
}

Expand All @@ -281,8 +276,7 @@ private void HandleConnectionStateChanged(ConnectionState state)

private async void HandleServerMessageReceived(String firstKey, String content)
{
Debug.Log("HandleServerMessageReceived: " + content);


if (content == null || content.Equals("{}")) return;
if (firstKey == null)
{
Expand All @@ -293,10 +287,7 @@ private async void HandleServerMessageReceived(String firstKey, String content)
}
else if (content.Contains("precision"))
firstKey = "precision";
else if (content.Contains("start_exploration"))
firstKey = "start_exploration";

else
else
{
return;
}
Expand All @@ -320,9 +311,7 @@ private async void HandleServerMessageReceived(String firstKey, String content)


break;
case "start_exploration":
message = GAMAMessage.CreateFromJSON(content);
break;

}
}

Expand Down Expand Up @@ -398,17 +387,3 @@ public static bool TryGetComponent<T>(this GameObject obj, T result) where T : C
return (result = obj.GetComponent<T>()) != null;
}
}

[System.Serializable]
public class GAMAMessage
{


public bool start_exploration;

public static GAMAMessage CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<GAMAMessage>(jsonString);
}

}
25 changes: 12 additions & 13 deletions Assets/Scripts/Utils/Timer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,33 @@ public class Timer : MonoBehaviour

private bool timerRunning = false;
private float midTime;
private float timeRemaining;
public float timeRemaining;

public bool InfoSentToGAMA = false;

public static Timer Instance = null;

// ############################################################


private void Awake()
{
Instance = this;

}


void Start() {
timeRemaining = timerDuration;
midTime = timeRemaining / 2;
DisplayTime(timeRemaining-1);
timerRunning = true;
InfoSentToGAMA = false;
}


private void initPlayer()
{
Dictionary<string, string> args = new Dictionary<string, string> {
{"id",ConnectionManager.Instance.getUseMiddleware() ? ConnectionManager.Instance.GetConnectionId() : ("\"" + ConnectionManager.Instance.GetConnectionId() + "\"") }
};
ConnectionManager.Instance.SendExecutableAsk("active_player", args);
}



void Update() {

if (!InfoSentToGAMA)
initPlayer();
if (timerRunning)
{
if (timeRemaining > 0)
Expand Down

0 comments on commit 3a06b33

Please sign in to comment.