using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering; public class GameState { private readonly UniQuake uq; private GameObject worldGameObject; private readonly Dictionary entities = new Dictionary(); public GameState(UniQuake uniQuake) { uq = uniQuake; } public void Destroy() { DestroyEntities(); DestroyWorld(); } public void NewMap(BrushModel worldModel) { Destroy(); // 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) for (int i = 0; i < 1; ++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(); 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(); 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; } } public void SetEntityAliasModel(int entityNum, string modelName) { if (!entities.TryGetValue(entityNum, out var entity)) { entity = new Entity(entityNum); entities.Add(entityNum, entity); } if (!uq.GameAssets.TryGetAliasModel(modelName, out var aliasModel)) { Debug.LogWarning($"Unknown alias model name: {aliasModel}"); return; } entity.SetAliasModel(aliasModel); } public void SetEntityWorldModel(int entityNum, int subModelNum) { // TODO: obtain Mesh from brush submodel and assign it to MeshRenderer } public void UpdateEntityAnimation(int entityNum, int frameNum) { if (entities.TryGetValue(entityNum, out var entity)) { entity.UpdateAnimation(frameNum); } } public void SetEntityTransform(int entityNum, Vector3 position, Quaternion rotation) { if (entities.TryGetValue(entityNum, out var entity)) { entity.SetTransform(position, rotation); } } public void RemoveEntity(int entityNum) { if (entities.TryGetValue(entityNum, out var entity)) { entity.Destroy(); entities.Remove(entityNum); } } private void DestroyEntities() { foreach (var entity in entities.Values) { entity.Destroy(); } entities.Clear(); } }