Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a headless client #5

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions client/Assets/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class GameManager : MonoBehaviour
public FoodController foodPrefab;
public GameObject deathScreen;
public PlayerController playerPrefab;

public static Color[] colorPalette = new[]
{
(Color)new Color32(248, 72, 245, 255),
Expand All @@ -30,20 +30,20 @@ public class GameManager : MonoBehaviour
(Color)new Color32(247, 26, 37, 255),
(Color)new Color32(253, 121, 43, 255),
};

public static GameManager instance;
public static Camera localCamera;
public static Dictionary<uint, PlayerController> playerIdToPlayerController =
new Dictionary<uint, PlayerController>();

public static Identity? localIdentity;
public static DbConnection conn;

private void Start()
{
instance = this;
Application.targetFrameRate = 60;

// Now that we’ve registered all our callbacks, lets connect to spacetimedb
conn = DbConnection.Builder().OnConnect((_conn, identity, token) => {
// Called when we connect to SpacetimeDB and receive our client identity
Expand All @@ -57,36 +57,36 @@ private void Start()
conn.Db.Food.OnInsert += FoodOnInsert;
conn.Db.Player.OnInsert += PlayerOnInsert;
conn.Db.Player.OnDelete += PlayerOnDelete;

// Request all tables
conn.SubscriptionBuilder().OnApplied(ctx =>
{
Debug.Log("Subscription applied!");
}).Subscribe("SELECT * FROM *");
}).OnConnectError((status, message) =>
}).OnConnectError((message) =>
{
// Called when we have an error connecting to SpacetimeDB
Debug.LogError($"Connection error: {status} {message}");
}).OnDisconnect((_conn, closeStatus, error) =>
Debug.LogError($"Connection error: {message}");
}).OnDisconnect((_conn, error) =>
{
// Called when we are disconnected from SpacetimeDB
Debug.Log("Disconnected.");
}).WithUri("http://localhost:3000")
.WithModuleName("untitled-circle-game")
.Build();

#pragma warning disable CS0612 // Type or member is obsolete
conn.onUnhandledReducerError += InstanceOnUnhandledReducerError;
#pragma warning restore CS0612 // Type or member is obsolete

localCamera = Camera.main;
}

private void InstanceOnUnhandledReducerError(ReducerEvent<Reducer> reducerEvent)
{
Debug.LogError("There was an error!");
}

private void PlayerOnDelete(EventContext context, Player deletedvalue)
{
if (playerIdToPlayerController.TryGetValue(deletedvalue.PlayerId, out var playerController))
Expand All @@ -101,7 +101,7 @@ private void PlayerOnInsert(EventContext context, Player insertedPlayer)
{
// We have a player, but no circle, let's respawn
Respawn();
}
}
}

private void EntityOnUpdate(EventContext context, Entity oldEntity, Entity newEntity)
Expand All @@ -111,7 +111,7 @@ private void EntityOnUpdate(EventContext context, Entity oldEntity, Entity newEn
{
return;
}

var player = GetOrCreatePlayer(circle.PlayerId);
player.CircleUpdate(oldEntity, newEntity);
}
Expand All @@ -121,11 +121,11 @@ private void CircleOnDelete(EventContext context, Circle deletedCircle)
var player = GetOrCreatePlayer(deletedCircle.PlayerId);
player.DespawnCircle(deletedCircle);
}

private void CircleOnInsert(EventContext context, Circle insertedValue)
{
var player = GetOrCreatePlayer(insertedValue.PlayerId);
// Spawn the new circle
// Spawn the new circle
player.SpawnCircle(insertedValue, circlePrefab);
}

Expand All @@ -143,7 +143,7 @@ PlayerController GetOrCreatePlayer(uint playerId)

return playerController;
}

private void FoodOnInsert(EventContext context, Food insertedValue)
{
// Spawn the new food
Expand Down
Loading