Skip to content

Commit

Permalink
component updates...
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Seeley committed Jun 19, 2015
1 parent 4d46de8 commit 0d745eb
Showing 1 changed file with 40 additions and 23 deletions.
63 changes: 40 additions & 23 deletions unity/LockstepIO/LockstepIOComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ public class LockstepIOComponent : MonoBehaviour
private Dictionary<long, JSONObject> CommandQueue;
private float SyncRateSec = 1f / 15f;
private int SyncPoolSize = 15;
public delegate void ExecuteCommandSignature (JSONObject Command);
public ExecuteCommandSignature ExecuteCommandFunction;
public string LastLockstepReadyString;
public long LastServerNow;
public long LastLocalNow;
public long LastSyncOffset;
public long LastSyncRoundTrip;
public bool LastSyncStatus;
public bool LockstepReady;
public long CommandDelay;
public long LocalNow
{
Expand Down Expand Up @@ -76,27 +74,49 @@ public bool IsSynched

public void Start ()
{
// First call Sync(executeCallbacK)
Sync ((JSONObject j) => {
// Debug log executed commands
Debug.Log(j.ToString());
});
// Synchronize lockstep with the server first
Sync ();
}

public void Update ()
{
if (Input.GetKeyDown(KeyCode.Space))
// Ensure lockstep is ready before issuing commands
if (LockstepReady)
{
JSONObject j = new JSONObject();
j.AddField("my test data", 420);
j.AddField("move unit 234 to x, y", "see easy peasy");
IssueCommand(j);
// On spacebar
if (Input.GetKeyDown(KeyCode.Space))
{
// Create a fake command, commands can be any JSON object
// after it is issued, it will be handed back to your code as is
// from the server in the ExecuteCommand function
JSONObject j = new JSONObject();
j.AddField("my test data", 420);
j.AddField("move unit 234 to x, y", "see easy peasy");

// Issue the fake command
IssueCommand(j);
}
}
}

public void Sync (ExecuteCommandSignature executeCommandFunction)
public void IssueCommand(JSONObject Command)
{
// Attach the command delay automatically to the command
Command.AddField("atLockstep", (double)(LockStepTime + CommandDelay));
// Send the command to the server
Socket.Emit("lockstep.io:cmd:issue", Command);
}

public void ExecuteCommand(JSONObject Command)
{
// Commands come back from the server, and are executed here
Debug.Log("Execute Command: " + Command.ToString());
// Add your own custom code to attach commands to GameObjects
}


public void Sync ()
{
ExecuteCommandFunction = executeCommandFunction;
SyncOffsets = new List<long>();
SyncRoundTrips = new List<long>();
CommandQueue = new Dictionary<long, JSONObject>();
Expand All @@ -108,6 +128,7 @@ public void Sync (ExecuteCommandSignature executeCommandFunction)
InvokeRepeating("LockstepSync", 0f, SyncRateSec);
}


private void OnLockstepSeed (SocketIOEvent evt)
{
int randomSeed = (int)evt.EventData.GetField("randomSeed").n;
Expand Down Expand Up @@ -142,8 +163,8 @@ private void OnLockstepSync(SocketIOEvent evt)
LastSyncOffset = SyncOffset;
LastSyncRoundTrip = SyncRoundTrip;
LastServerNow = t1;
LastSyncStatus = IsSynched;
if (LastSyncStatus)
LockstepReady = IsSynched;
if (LockstepReady)
{
JSONObject ready = new JSONObject();
ready.AddField("localNow", (double)LastLocalNow);
Expand Down Expand Up @@ -208,12 +229,8 @@ private void OnCommandExecute ()

JSONObject closestCommand = CommandQueue[closest];
CommandQueue.Remove(closest);
ExecuteCommandFunction(closestCommand);
ExecuteCommand(closestCommand);
}

public void IssueCommand(JSONObject Command)
{
Command.AddField("atLockstep", (double)(LockStepTime + CommandDelay));
Socket.Emit("lockstep.io:cmd:issue", Command);
}

}

0 comments on commit 0d745eb

Please sign in to comment.