SupaLidlGame/Utils/World.cs

151 lines
4.1 KiB
C#
Raw Normal View History

2023-03-22 21:22:51 -07:00
using Godot;
using SupaLidlGame.Characters;
2023-03-26 10:53:45 -07:00
using SupaLidlGame.Scenes;
using SupaLidlGame.Extensions;
2023-03-22 21:22:51 -07:00
using System.Collections.Generic;
2023-03-26 10:53:45 -07:00
using System.Linq;
2023-03-22 21:22:51 -07:00
namespace SupaLidlGame.Utils
{
public partial class World : Node2D
{
[Export]
2023-03-26 10:53:45 -07:00
public PackedScene StartingArea { get; set; }
2023-03-22 21:22:51 -07:00
[Export]
2023-03-26 10:53:45 -07:00
public Map CurrentMap { get; protected set; }
2023-03-22 21:22:51 -07:00
[Export]
public Player CurrentPlayer { get; set; }
2023-03-26 10:53:45 -07:00
private Dictionary<string, Map> _maps;
2023-03-22 21:22:51 -07:00
private string _currentConnector;
2023-03-26 10:53:45 -07:00
private string _currentMapResourcePath;
private const string PLAYER_PATH = "res://Characters/Player.tscn";
private PackedScene _playerScene;
2023-03-22 21:22:51 -07:00
public World()
{
2023-03-26 10:53:45 -07:00
_maps = new Dictionary<string, Map>();
_playerScene = ResourceLoader.Load<PackedScene>(PLAYER_PATH);
2023-03-22 21:22:51 -07:00
}
public override void _Ready()
{
2023-03-26 10:53:45 -07:00
if (StartingArea is not null)
{
LoadScene(StartingArea);
}
// spawn the player in
CreatePlayer();
2023-03-22 21:22:51 -07:00
base._Ready();
}
2023-03-26 10:53:45 -07:00
public void LoadScene(PackedScene scene)
{
2023-03-26 13:03:45 -07:00
GD.Print("Loading map " + scene.ResourcePath);
2023-03-26 10:53:45 -07:00
Map map;
if (_maps.ContainsKey(scene.ResourcePath))
{
map = _maps[scene.ResourcePath];
}
else
{
map = scene.Instantiate<Map>();
_maps.Add(scene.ResourcePath, map);
}
2023-03-26 13:03:45 -07:00
if (CurrentMap is not null)
{
CurrentMap.Entities.RemoveChild(CurrentPlayer);
RemoveChild(CurrentMap);
CurrentMap.Active = false;
}
2023-03-26 10:53:45 -07:00
AddChild(map);
InitTilemap(map);
2023-03-26 13:03:45 -07:00
CurrentMap = map;
2023-03-26 10:53:45 -07:00
CurrentMap.Active = true;
if (CurrentPlayer is not null)
{
CurrentMap.Entities.AddChild(CurrentPlayer);
}
}
public void CreatePlayer()
2023-03-22 21:22:51 -07:00
{
2023-03-26 10:53:45 -07:00
CurrentPlayer = _playerScene.Instantiate<Player>();
CurrentMap.Entities.AddChild(CurrentPlayer);
2023-03-22 21:22:51 -07:00
}
2023-03-26 10:53:45 -07:00
private void InitTilemap(Map map)
2023-03-22 21:22:51 -07:00
{
2023-03-26 10:53:45 -07:00
var children = map.Areas.GetChildren();
foreach (Node node in children)
2023-03-22 21:22:51 -07:00
{
2023-03-26 10:53:45 -07:00
if (node is BoundingBoxes.ConnectorBox connector)
{
// this reconnects the EventHandler if it is connected
connector.RequestedEnter -= _on_area_2d_requested_enter;
connector.RequestedEnter += _on_area_2d_requested_enter;
}
2023-03-22 21:22:51 -07:00
}
2023-03-26 10:53:45 -07:00
}
2023-03-22 21:22:51 -07:00
2023-03-26 10:53:45 -07:00
private void MovePlayerToConnector(string name)
{
// find the first connector with the specified name
var connector = CurrentMap.Areas.GetChildren().First((child) =>
{
if (child is BoundingBoxes.ConnectorBox connector)
{
return connector.Identifier == name;
}
return false;
}) as BoundingBoxes.ConnectorBox;
CurrentPlayer.GlobalPosition = connector.GlobalPosition;
}
public void MoveToArea(string path, string connector)
{
2023-03-22 21:22:51 -07:00
_currentConnector = connector;
2023-03-26 10:53:45 -07:00
if (path != _currentMapResourcePath)
{
var scene = ResourceLoader.Load<PackedScene>(path);
LoadScene(scene);
_currentMapResourcePath = path;
}
// after finished loading, move our player to the connector
MovePlayerToConnector(connector);
2023-03-22 21:22:51 -07:00
}
2023-03-25 10:21:24 -07:00
public void _on_area_2d_requested_enter(
BoundingBoxes.ConnectorBox box,
Player player)
{
2023-03-26 10:53:45 -07:00
GD.Print("Requesting to enter " + box.ToConnector);
MoveToArea(box.ToArea, box.ToConnector);
}
public void SaveGame()
{
throw new System.NotImplementedException();
}
public void LoadGame()
{
throw new System.NotImplementedException();
2023-03-25 10:21:24 -07:00
}
2023-03-22 21:22:51 -07:00
}
}