You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
3.3 KiB
118 lines
3.3 KiB
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<int, Entity> entities = new Dictionary<int, Entity>();
|
|
|
|
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<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;
|
|
}
|
|
}
|
|
|
|
public Entity GetEntity(int entityNum)
|
|
{
|
|
entities.TryGetValue(entityNum, out var entity);
|
|
return entity;
|
|
}
|
|
|
|
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: {modelName}");
|
|
return;
|
|
}
|
|
|
|
entity.SetAliasModel(aliasModel);
|
|
}
|
|
|
|
public void SetEntityWorldModel(int entityNum, int subModelNum)
|
|
{
|
|
// TODO: obtain Mesh from brush submodel and assign it to MeshRenderer
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|