mirror of
https://github.com/clockworklabs/SpacetimeDB.git
synced 2026-05-20 14:42:10 -04:00
Merged ArenaController into GameManager for simplicity and cleaned up a few things
This commit is contained in:
@@ -477,7 +477,6 @@ GameObject:
|
||||
- component: {fileID: 207278860}
|
||||
- component: {fileID: 207278863}
|
||||
- component: {fileID: 207278859}
|
||||
- component: {fileID: 207278862}
|
||||
- component: {fileID: 207278864}
|
||||
m_Layer: 0
|
||||
m_Name: GameManager
|
||||
@@ -498,6 +497,10 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: aaf311e6a809fdf4d874741c6fd6db10, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
backgroundInstance: {fileID: 1774890158}
|
||||
borderThickness: 2
|
||||
borderMaterial: {fileID: 2100000, guid: eb000c9de35d73e4d81c6a0831a9e7bc, type: 2}
|
||||
starBackgroundPrefab: {fileID: 0}
|
||||
--- !u!4 &207278860
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -517,22 +520,6 @@ Transform:
|
||||
- {fileID: 1854949949}
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &207278862
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 207278858}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: dab6414c7b228714bb7700728f85482c, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
backgroundInstance: {fileID: 1774890158}
|
||||
borderThickness: 2
|
||||
borderMaterial: {fileID: 2100000, guid: eb000c9de35d73e4d81c6a0831a9e7bc, type: 2}
|
||||
starBackgroundPrefab: {fileID: 0}
|
||||
--- !u!114 &207278863
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using SpacetimeDB.Types;
|
||||
using UnityEngine;
|
||||
|
||||
public class ArenaController : MonoBehaviour
|
||||
{
|
||||
public SpriteRenderer backgroundInstance;
|
||||
public float borderThickness = 10;
|
||||
public Material borderMaterial;
|
||||
public ParallaxBackground starBackgroundPrefab;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// Once we receive the world size from the server, we can set up the borders
|
||||
// of the arena.
|
||||
GameManager.Conn.Db.Config.OnInsert += (ctx, value) =>
|
||||
{
|
||||
var worldSize = value.WorldSize;
|
||||
CreateBorderCube(new Vector2(worldSize / 2.0f, worldSize + borderThickness / 2),
|
||||
new Vector2(worldSize + borderThickness * 2.0f, borderThickness)); //North
|
||||
CreateBorderCube(new Vector2(worldSize / 2.0f, -borderThickness / 2),
|
||||
new Vector2(worldSize + borderThickness * 2.0f, borderThickness)); //South
|
||||
CreateBorderCube(new Vector2(worldSize + borderThickness / 2, worldSize / 2.0f),
|
||||
new Vector2(borderThickness, worldSize + borderThickness * 2.0f)); //East
|
||||
CreateBorderCube(new Vector2(-borderThickness / 2, worldSize / 2.0f),
|
||||
new Vector2(borderThickness, worldSize + borderThickness * 2.0f)); //West
|
||||
|
||||
backgroundInstance.gameObject.SetActive(true); ;
|
||||
var size = worldSize / backgroundInstance.transform.localScale.x;
|
||||
backgroundInstance.size = new Vector2(size, size);
|
||||
backgroundInstance.transform.position = new Vector3((float)worldSize / 2, (float)worldSize / 2);
|
||||
|
||||
// Start the camera in the middle of the screen for setup, but only if we have no player
|
||||
if (PlayerController.Local == null)
|
||||
{
|
||||
Camera.main.transform.position = new Vector3((float)worldSize / 2, (float)worldSize / 2, -10.0f);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void CreateBorderCube(Vector2 position, Vector2 scale)
|
||||
{
|
||||
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
||||
cube.name = "Border";
|
||||
cube.transform.localScale = new Vector3(scale.x, scale.y, 1);
|
||||
cube.transform.position = new Vector3(position.x, position.y, 1);
|
||||
cube.GetComponent<MeshRenderer>().material = borderMaterial;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dab6414c7b228714bb7700728f85482c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -4,22 +4,32 @@ using UnityEngine;
|
||||
|
||||
public class CameraController : MonoBehaviour
|
||||
{
|
||||
public static float WorldSize = 0.0f;
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
var arenaCenterTransform = new Vector3(WorldSize / 2, WorldSize / 2, -10.0f);
|
||||
if (PlayerController.Local == null || !GameManager.IsConnected())
|
||||
{
|
||||
// Set the camera to be in middle of the arena if we are not connected or
|
||||
// there is no local player
|
||||
transform.position = arenaCenterTransform;
|
||||
return;
|
||||
}
|
||||
|
||||
var centerOfMass = PlayerController.Local.CenterOfMass();
|
||||
if (centerOfMass.HasValue)
|
||||
{
|
||||
// Set the camera to be the center of mass of the local player
|
||||
// if the local player has one
|
||||
transform.position = new Vector3
|
||||
{
|
||||
x = centerOfMass.Value.x,
|
||||
y = centerOfMass.Value.y,
|
||||
z = transform.position.z
|
||||
};
|
||||
} else {
|
||||
transform.position = arenaCenterTransform;
|
||||
}
|
||||
|
||||
float targetCameraSize = CalculateCameraSize(PlayerController.Local);
|
||||
|
||||
@@ -13,6 +13,11 @@ public class GameManager : MonoBehaviour
|
||||
public static event Action OnConnected;
|
||||
public static event Action OnSubscriptionApplied;
|
||||
|
||||
public SpriteRenderer backgroundInstance;
|
||||
public float borderThickness = 2;
|
||||
public Material borderMaterial;
|
||||
public ParallaxBackground starBackgroundPrefab;
|
||||
|
||||
public static GameManager Instance { get; private set; }
|
||||
public static Identity LocalIdentity { get; private set; }
|
||||
public static DbConnection Conn { get; private set; }
|
||||
@@ -65,14 +70,13 @@ public class GameManager : MonoBehaviour
|
||||
conn.Db.Food.OnInsert += FoodOnInsert;
|
||||
conn.Db.Player.OnInsert += PlayerOnInsert;
|
||||
conn.Db.Player.OnDelete += PlayerOnDelete;
|
||||
|
||||
OnConnected?.Invoke();
|
||||
|
||||
// Request all tables
|
||||
Conn.SubscriptionBuilder().OnApplied(ctx =>
|
||||
{
|
||||
Debug.Log("Subscription applied!");
|
||||
OnSubscriptionApplied?.Invoke();
|
||||
}).Subscribe("SELECT * FROM *");
|
||||
Conn.SubscriptionBuilder()
|
||||
.OnApplied(HandleSubscriptionApplied)
|
||||
.Subscribe("SELECT * FROM *");
|
||||
}
|
||||
|
||||
void HandleConnectError(Exception ex)
|
||||
@@ -89,6 +93,46 @@ public class GameManager : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleSubscriptionApplied(EventContext ctx)
|
||||
{
|
||||
Debug.Log("Subscription applied!");
|
||||
OnSubscriptionApplied?.Invoke();
|
||||
|
||||
// Once we have the initial subscription sync'd to the client cache
|
||||
// Get the world size from the config table and set up the arena
|
||||
var worldSize = Conn.Db.Config.Id.Find(0).WorldSize;
|
||||
SetupArena(worldSize);
|
||||
}
|
||||
|
||||
private void SetupArena(float worldSize)
|
||||
{
|
||||
CreateBorderCube(new Vector2(worldSize / 2.0f, worldSize + borderThickness / 2),
|
||||
new Vector2(worldSize + borderThickness * 2.0f, borderThickness)); //North
|
||||
CreateBorderCube(new Vector2(worldSize / 2.0f, -borderThickness / 2),
|
||||
new Vector2(worldSize + borderThickness * 2.0f, borderThickness)); //South
|
||||
CreateBorderCube(new Vector2(worldSize + borderThickness / 2, worldSize / 2.0f),
|
||||
new Vector2(borderThickness, worldSize + borderThickness * 2.0f)); //East
|
||||
CreateBorderCube(new Vector2(-borderThickness / 2, worldSize / 2.0f),
|
||||
new Vector2(borderThickness, worldSize + borderThickness * 2.0f)); //West
|
||||
|
||||
backgroundInstance.gameObject.SetActive(true); ;
|
||||
var size = worldSize / backgroundInstance.transform.localScale.x;
|
||||
backgroundInstance.size = new Vector2(size, size);
|
||||
backgroundInstance.transform.position = new Vector3((float)worldSize / 2, (float)worldSize / 2);
|
||||
|
||||
// Set the world size for the camera controller
|
||||
CameraController.WorldSize = worldSize;
|
||||
}
|
||||
|
||||
private void CreateBorderCube(Vector2 position, Vector2 scale)
|
||||
{
|
||||
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
||||
cube.name = "Border";
|
||||
cube.transform.localScale = new Vector3(scale.x, scale.y, 1);
|
||||
cube.transform.position = new Vector3(position.x, position.y, 1);
|
||||
cube.GetComponent<MeshRenderer>().material = borderMaterial;
|
||||
}
|
||||
|
||||
private static void CircleOnInsert(EventContext context, Circle insertedValue)
|
||||
{
|
||||
var player = GetOrCreatePlayer(insertedValue.PlayerId);
|
||||
@@ -144,10 +188,9 @@ public class GameManager : MonoBehaviour
|
||||
return playerController;
|
||||
}
|
||||
|
||||
/* BEGIN: not in tutorial */
|
||||
private void InstanceOnUnhandledReducerError(ReducerEvent<Reducer> reducerEvent)
|
||||
public static bool IsConnected()
|
||||
{
|
||||
Debug.LogError($"There was an error!\r\n{(reducerEvent.Status as Status.Failed)?.Failed_}");
|
||||
return Conn != null && Conn.IsActive;
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
@@ -156,9 +199,10 @@ public class GameManager : MonoBehaviour
|
||||
Conn = null;
|
||||
}
|
||||
|
||||
public static bool IsConnected()
|
||||
/* BEGIN: not in tutorial */
|
||||
private void InstanceOnUnhandledReducerError(ReducerEvent<Reducer> reducerEvent)
|
||||
{
|
||||
return Conn != null && Conn.IsActive;
|
||||
Debug.LogError($"There was an error!\r\n{(reducerEvent.Status as Status.Failed)?.Failed_}");
|
||||
}
|
||||
/* END: not in tutorial */
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user