Browse Source

Moved creation of game objects for the world to GameState, which is where the bridge between Quake's game logic and Unity's game objects will happen. Also includes destruction of said game objects, so that the world is properly destroyed and recreated on map change.

console
Nico de Poel 5 years ago
parent
commit
453664031c
  1. 2
      Assets/Scripts/Game/GameAssets.cs
  2. 54
      Assets/Scripts/Game/GameState.cs
  3. 39
      Assets/Scripts/Modules/RenderModule.cs
  4. 4
      Assets/Scripts/UniQuake.cs

2
Assets/Scripts/Game/GameAssets.cs

@ -52,7 +52,7 @@ public class GameAssets
public void SetWorldModel(BrushModel brushModel) public void SetWorldModel(BrushModel brushModel)
{ {
worldModel?.Dispose(); // TODO: also need to clean up any GameObjects made from this
worldModel?.Dispose();
worldModel = brushModel; worldModel = brushModel;
} }

54
Assets/Scripts/Game/GameState.cs

@ -1,13 +1,67 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.Rendering;
public class GameState public class GameState
{ {
private readonly UniQuake uq; private readonly UniQuake uq;
private GameObject worldGameObject;
public GameState(UniQuake uniQuake) public GameState(UniQuake uniQuake)
{ {
uq = uniQuake; uq = uniQuake;
} }
public void Destroy()
{
DestroyWorld();
}
public void NewMap(BrushModel worldModel)
{
DestroyWorld();
// DEBUG - we'll want to instantiate prefabs for each submodel and assign materials from a preset collection
worldGameObject = new GameObject(worldModel.Name);
for (int i = 0; i < worldModel.SubModelCount; ++i)
{
var subModel = worldModel.GetSubModel(i);
var subModelGO = new GameObject($"SubModel_{i}");
subModelGO.transform.SetParent(worldGameObject.transform);
foreach (var surfaceMesh in subModel.SurfaceMeshes)
{
var meshGO = new GameObject(surfaceMesh.Mesh.name);
meshGO.transform.SetParent(subModelGO.transform);
var mf = meshGO.AddComponent<MeshFilter>();
mf.sharedMesh = surfaceMesh.Mesh;
var material = new Material(Shader.Find("Universal Render Pipeline/Simple Lit"));
uint texNum = surfaceMesh.Texture.TextureNum;
if (uq.GameAssets.TryGetTexture(texNum, out var texture))
{
material.mainTexture = texture;
}
var mr = meshGO.AddComponent<MeshRenderer>();
mr.sharedMaterial = material;
mr.shadowCastingMode = ShadowCastingMode.Off;
mr.receiveShadows = false;
mr.lightProbeUsage = LightProbeUsage.Off;
mr.reflectionProbeUsage = ReflectionProbeUsage.Off;
}
}
}
private void DestroyWorld()
{
if (worldGameObject != null)
{
Object.Destroy(worldGameObject);
worldGameObject = null;
}
}
} }

39
Assets/Scripts/Modules/RenderModule.cs

@ -107,42 +107,11 @@ public partial class RenderModule
{ {
Debug.Log($"World model '{model.name}' with {model.numVertices} vertices, {model.numEdges} edges, {model.numSurfaces} surfaces"); Debug.Log($"World model '{model.name}' with {model.numVertices} vertices, {model.numEdges} edges, {model.numSurfaces} surfaces");
var brushModel = new BrushModel(model.name);
brushModel.ImportMeshData(model);
uq.GameAssets.SetWorldModel(brushModel);
// DEBUG
var worldGO = new GameObject(model.name);
for (int i = 0; i < brushModel.SubModelCount; ++i)
{
var subModel = brushModel.GetSubModel(i);
var subModelGO = new GameObject($"SubModel_{i}");
subModelGO.transform.SetParent(worldGO.transform);
foreach (var surfaceMesh in subModel.SurfaceMeshes)
{
var meshGO = new GameObject(surfaceMesh.Mesh.name);
meshGO.transform.SetParent(subModelGO.transform);
var mf = meshGO.AddComponent<MeshFilter>();
mf.sharedMesh = surfaceMesh.Mesh;
var material = new Material(Shader.Find("Universal Render Pipeline/Simple Lit"));
uint texNum = surfaceMesh.Texture.TextureNum;
if (uq.GameAssets.TryGetTexture(texNum, out var texture))
{
material.mainTexture = texture;
}
var worldModel = new BrushModel(model.name);
worldModel.ImportMeshData(model);
var mr = meshGO.AddComponent<MeshRenderer>();
mr.sharedMaterial = material;
mr.shadowCastingMode = ShadowCastingMode.Off;
mr.receiveShadows = false;
mr.lightProbeUsage = LightProbeUsage.Off;
mr.reflectionProbeUsage = ReflectionProbeUsage.Off;
}
}
uq.GameAssets.SetWorldModel(worldModel);
uq.GameState.NewMap(worldModel);
return 1; return 1;
} }

4
Assets/Scripts/UniQuake.cs

@ -35,7 +35,7 @@ public partial class UniQuake: MonoBehaviour
public GameState GameState { get; private set; } public GameState GameState { get; private set; }
private Action<string> logHandler; private Action<string> logHandler;
private StringBuilder logBuffer = new StringBuilder();
private readonly StringBuilder logBuffer = new StringBuilder();
void Start() void Start()
{ {
@ -158,8 +158,10 @@ public partial class UniQuake: MonoBehaviour
private void OnDestroy() private void OnDestroy()
{ {
GameState.Destroy();
GameAssets.Destroy(); GameAssets.Destroy();
gameModule.Destroy();
renderModule.Destroy(); renderModule.Destroy();
systemModule.Destroy(); systemModule.Destroy();

Loading…
Cancel
Save