prevent reparsing the map when entering playmode

This commit is contained in:
2022-06-09 21:15:14 +03:00
parent 8987915bfe
commit adf9cf1edc

View File

@@ -6,6 +6,7 @@ using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FlaxEditor;
using FlaxEngine;
using FlaxEngine.Assertions;
using Console = Game.Console;
@@ -55,6 +56,7 @@ namespace Game
private float fallOffExponent_ = 2.0f;
private float saturationMultiplier_ = 1.0f;
private List<MapEntity> lightEnts = new List<MapEntity>();
private Actor worldSpawnActor = null;
[Range(0.1f, 4f)]
public float BrightnessMultiplier
@@ -227,92 +229,57 @@ namespace Game
vertices = new Vector3[0];
}
#if false //FLAX_EDITOR
[OnSerializing]
internal void OnSerializing(StreamingContext context)
{
Debug.Log("OnSerializing: " + Editor.IsPlayMode);
}
#if FLAX_EDITOR
private void OnEditorPlayModeStart()
{
try
{
if (worldSpawnActor != null)
worldSpawnActor.HideFlags &= ~HideFlags.DontSave;
}
catch (Exception e)
{
FlaxEngine.Debug.Log("OnEditorPlayModeStart error: " + e.Message);
}
}
[OnSerialized]
internal void OnSerialized(StreamingContext context)
{
Debug.Log("OnSerialized: " + Editor.IsPlayMode);
}
private void OnEditorPlayModeEnd()
{
try
{
if (worldSpawnActor != null)
worldSpawnActor.HideFlags |= HideFlags.DontSave;
}
catch (Exception e)
{
FlaxEngine.Debug.Log("OnEditorPlayModeEnd error: " + e.Message);
}
}
[OnDeserializing]
internal void OnDeserializing(StreamingContext context)
{
Debug.Log("OnDeserializing: " + Editor.IsPlayMode);
}
public override void OnEnable()
{
Editor.Instance.PlayModeBeginning += OnEditorPlayModeStart;
Editor.Instance.PlayModeEnding += OnEditorPlayModeEnd;
}
[OnDeserialized]
internal void OnDeserialized(StreamingContext context)
{
Debug.Log("OnDeserialized: " + Editor.IsPlayMode);
}
public override void OnDisable()
{
Editor.Instance.PlayModeBeginning -= OnEditorPlayModeStart;
Editor.Instance.PlayModeEnding -= OnEditorPlayModeEnd;
}
#endif
private ConcurrentBag<Model> sdfModels = new ConcurrentBag<Model>();
private Task task;
public override void OnStart()
{
#if false
Action onScriptsReloadBegin = null;
onScriptsReloadBegin = () =>
{
Debug.Log("LoadMap ScriptsReloadEnd");
Actor worldSpawnActor = Actor.GetChild("WorldSpawn");
if (worldSpawnActor != null)
{
Debug.Log("LoadMap: removing DontSave flag");
worldSpawnActor.HideFlags &= ~HideFlags.DontSave;
}
ScriptsBuilder.ScriptsReloadBegin -= onScriptsReloadBegin;
};
ScriptsBuilder.ScriptsReloadBegin += onScriptsReloadBegin;
Action onScriptsReloadEnd = null;
onScriptsReloadEnd = () =>
{
Debug.Log("LoadMap ScriptsReloadEnd");
Actor worldSpawnActor = Actor.GetChild("WorldSpawn");
if (worldSpawnActor != null)
{
Debug.Log("LoadMap: restoring DontSave flag");
worldSpawnActor.HideFlags |= HideFlags.DontSave;
}
ScriptsBuilder.ScriptsReloadEnd -= onScriptsReloadEnd;
};
ScriptsBuilder.ScriptsReloadEnd += onScriptsReloadEnd;
#endif
//Debug.Log("LoadMap");
LoadMap(false);
if (generateSdf && Graphics.EnableGlobalSDF && sdfModels.Count > 1)
{
task = Task.Run(() =>
{
Stopwatch sw = Stopwatch.StartNew();
Console.Print($"Generating level SDF ({sdfModels.Count} models)...");
Parallel.ForEach(sdfModels, new ParallelOptions() { MaxDegreeOfParallelism = 1 }, model =>
{
if (model.WaitForLoaded())
throw new Exception("model was not loaded");
model.GenerateSDF();
});
Console.Print($"Generated level SDF in {sw.Elapsed.TotalMilliseconds}ms");
});
}
}
public override void OnUpdate()
{
if (dirtyLights)
{
Actor worldSpawnActor = Actor.GetChild("WorldSpawn");
if (worldSpawnActor == null || !worldSpawnActor || root == null)
return;
foreach (var light in worldSpawnActor.GetChildren<Light>())
Destroy(light);
@@ -323,10 +290,10 @@ namespace Game
{
case "light":
if (importLights)
ParseLight(entity, worldSpawnActor, ref lightIndex);
ParseLight(entity, ref lightIndex);
break;
//case "info_player_deathmatch":
// ParsePlayerSpawn(entity, worldSpawnActor, ref playerSpawnIndex);
// ParsePlayerSpawn(entity, ref playerSpawnIndex);
// break;
}
@@ -336,19 +303,23 @@ namespace Game
private void LoadMap(bool forceLoad)
{
Actor worldSpawnActor = Actor.GetChild("WorldSpawn");
worldSpawnActor = Actor.FindActor("WorldSpawn");
if (worldSpawnActor != null)
{
if (forceLoad)
{
FlaxEngine.Debug.Log("Destroying children");
worldSpawnActor.DestroyChildren();
}
else
{
Console.Print("Map already loaded in the scene");
FlaxEngine.Debug.Log("Map already loaded in the scene");
dirtyLights = false;
return;
}
}
else
FlaxEngine.Debug.Log("Loading map");
{
string workDir = Directory.GetCurrentDirectory();
@@ -357,6 +328,8 @@ namespace Game
missingMaterial = Content.Load<MaterialBase>(assetPath);
}
ConcurrentBag<Model> sdfModels = new ConcurrentBag<Model>();
Stopwatch sw = Stopwatch.StartNew();
byte[] mapChars = File.ReadAllBytes(mapPath);
root = MapParser.Parse(mapChars);
@@ -828,19 +801,39 @@ namespace Game
{
case "light":
if (importLights)
ParseLight(entity, worldSpawnActor, ref lightIndex);
ParseLight(entity, ref lightIndex);
lightEnts.Add(entity);
break;
case "info_player_deathmatch":
ParsePlayerSpawn(entity, worldSpawnActor, ref playerSpawnIndex);
ParsePlayerSpawn(entity, ref playerSpawnIndex);
break;
}
Console.Print("entity parsing time: " + sw.Elapsed.TotalMilliseconds + "ms");
}
if (generateSdf && Graphics.EnableGlobalSDF && sdfModels.Count > 1)
{
var task = Task.Run(() =>
{
Stopwatch sw = Stopwatch.StartNew();
Console.Print($"Generating level SDF ({sdfModels.Count} models)...");
Parallel.ForEach(sdfModels, new ParallelOptions() { MaxDegreeOfParallelism = 1 }, model =>
{
if (model.WaitForLoaded())
throw new Exception("model was not loaded");
model.GenerateSDF();
});
Console.Print($"Generated level SDF in {sw.Elapsed.TotalMilliseconds}ms");
});
}
}
private void ParseLight(MapEntity entity, Actor worldSpawnActor, ref int lightIndex)
private void ParseLight(MapEntity entity, ref int lightIndex)
{
int preset = 3;
@@ -917,7 +910,7 @@ namespace Game
lightIndex++;
}
private void ParsePlayerSpawn(MapEntity entity, Actor worldSpawnActor, ref int playerSpawnIndex)
private void ParsePlayerSpawn(MapEntity entity, ref int playerSpawnIndex)
{
Actor spawn = worldSpawnActor.AddChild<Actor>();
spawn.Name = "PlayerSpawn_" + playerSpawnIndex;