Commit 4abd5efc authored by Maxime's avatar Maxime
Browse files

levels are now loading and unloading properly

parent e95beb60
using System;
using System.IO;
using Urho;
using Urho.Urho2D;
using ZZZZZZ.Physics.CollisionBox;
......@@ -19,16 +20,17 @@ namespace ZZZZZZ.Worlds
public Level(string level, int x, int y)
{
Console.WriteLine($"ctor level {x} {y}");
WorldX=x;
WorldY=y;
Name = level.Split('.')[1];
Name = level;
ChildFromScene = GameManager.Current.Scene.CreateChild("Level");
ChildFromScene.AddComponent(this);
TileMap = Node.CreateComponent<TileMap2D>();
TileMap.TmxFile = Application.ResourceCache.GetTmxFile2D(level);
TileMap.Node.SetPosition2D(new Vector2(x * 7.65f, y * 7.65f));
TileMap.Node.SetPosition2D(new Vector2(y * 7.68f, x * 4.8f));
SetSpawn();
......
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Urho;
using Urho.Actions;
using ZZZZZZ.Physics;
......@@ -11,19 +13,7 @@ namespace ZZZZZZ.Worlds
{
public class LevelManager
{
private Level _current;
public Level Current
{
get => _current;
private set
{
_current = value;
LevelChanged?.Invoke(new LevelChangedEventArgs(_current.Name, _current.Spawn));
}
}
public Level Current { get; private set; }
public List<Level> Loaded { get; }
public event Action<LevelChangedEventArgs> LevelChanged;
......@@ -40,17 +30,41 @@ namespace ZZZZZZ.Worlds
{
Console.WriteLine($"entering area {obj.CameraPosition}");
GameManager.Current.Camera.Node.Position = new Vector3(obj.CameraPosition.Y * 7.68f + 3.84f, obj.CameraPosition.X * 4.8f + 2.4f, 0);
Current = Loaded.Find(x => x.WorldX == obj.CameraPosition.X && x.WorldY == obj.CameraPosition.Y);
var regex = new Regex("world([0-9*])_([0-9*])-([0-9*]).tmx");
var match = regex.Match(Current.Name).Groups;
if (match.Count >= 4)
{
var toLoad = Path.Combine(Path.GetDirectoryName(Current.Name), $"world{match[1]}_{match[2]}-{match[3]}.tmx");
Unload();
Load(toLoad);
}
else
{
Console.WriteLine($"problem with on fly loading of world {Current.Name}");
}
}
public void Unload()
{
foreach (var level in Loaded)
level.Dispose();
var toDelete = Loaded.Select(x => (x.WorldX, x.WorldY)).ToList();
for (int i = Current.WorldX - 1; i <= Current.WorldX + 1; i++)
for (int j = Current.WorldY - 1; j <= Current.WorldY + 1; j++)
toDelete.Remove((i, j));
foreach (var (WorldX, WorldY) in toDelete)
{
Console.WriteLine($"unloading {WorldX} {WorldY}");
var toDispose = Loaded.Find(x => x.WorldX == WorldX && x.WorldY == WorldY);
toDispose.Dispose();
Loaded.Remove(toDispose);
}
}
public void Load(string level)
{
//Unload();
var filename = Path.GetFileNameWithoutExtension(level);
var path = Path.GetDirectoryName(level);
......@@ -65,12 +79,12 @@ namespace ZZZZZZ.Worlds
for (int j = levelY - 1; j <= levelY + 1; j++)
{
if (!Loaded.Exists(x => x.WorldX == i && x.WorldY == j))
{
{
var filePathToLoad = Path.Combine(path, $"{splitted[0]}_{i}-{j}.tmx");
var tmxLevel = Application.Current.ResourceCache.GetTmxFile2D(filePathToLoad, false);
if (tmxLevel != null)
{
var _level = new Level(filePathToLoad, j, i);
var _level = new Level(filePathToLoad, i, j);
Loaded.Add(_level);
if (i == levelX && j == levelY)
Current = _level;
......@@ -82,7 +96,6 @@ namespace ZZZZZZ.Worlds
private void CollisionHandler_CollisionBoxTriggered(CollisionBoxTriggeredEventArgs obj)
{
//To do => unload unused levels in neighborhood
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment