SupaLidlGame/Utils/World.cs

156 lines
3.8 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;
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
2023-06-03 18:21:46 -07:00
namespace SupaLidlGame.Utils;
public partial class World : Node2D
2023-03-22 21:22:51 -07:00
{
2023-06-03 18:21:46 -07:00
[Export]
public PackedScene StartingArea { get; set; }
2023-03-22 21:22:51 -07:00
2023-06-03 18:21:46 -07:00
[Export]
public Map CurrentMap { get; protected set; }
2023-03-22 21:22:51 -07:00
2023-06-03 18:21:46 -07:00
[Export]
public Player CurrentPlayer { get; set; }
2023-03-22 21:22:51 -07:00
2023-06-03 18:21:46 -07:00
private Dictionary<string, Map> _maps;
2023-03-22 21:22:51 -07:00
2023-06-03 18:21:46 -07:00
private string _currentConnector;
2023-03-22 21:22:51 -07:00
2023-06-03 18:21:46 -07:00
private string _currentMapResourcePath;
2023-03-26 10:53:45 -07:00
2023-06-06 18:39:23 -07:00
private Entities.Campfire _lastCampfire = null;
2023-06-03 18:21:46 -07:00
private const string PLAYER_PATH = "res://Characters/Player.tscn";
private PackedScene _playerScene;
2023-03-26 10:53:45 -07:00
2023-06-03 18:21:46 -07:00
public World()
{
_maps = new Dictionary<string, Map>();
_playerScene = ResourceLoader.Load<PackedScene>(PLAYER_PATH);
}
public override void _Ready()
{
if (StartingArea is not null)
2023-03-22 21:22:51 -07:00
{
2023-06-03 18:21:46 -07:00
LoadScene(StartingArea);
2023-03-22 21:22:51 -07:00
}
2023-06-03 18:21:46 -07:00
// spawn the player in
CreatePlayer();
2023-03-26 10:53:45 -07:00
2023-06-06 18:39:23 -07:00
CurrentPlayer.Death += (Events.HealthChangedArgs args) =>
{
// TODO: respawn the player at the last campfire.
};
2023-06-03 18:21:46 -07:00
base._Ready();
}
2023-03-26 10:53:45 -07:00
2023-06-03 18:21:46 -07:00
public void LoadScene(PackedScene scene)
{
GD.Print("Loading map " + scene.ResourcePath);
2023-03-22 21:22:51 -07:00
2023-06-03 18:21:46 -07:00
Map map;
if (_maps.ContainsKey(scene.ResourcePath))
2023-03-26 10:53:45 -07:00
{
2023-06-03 18:21:46 -07:00
map = _maps[scene.ResourcePath];
}
else
{
map = scene.Instantiate<Map>();
_maps.Add(scene.ResourcePath, map);
}
2023-03-26 10:53:45 -07:00
2023-06-03 18:21:46 -07:00
if (CurrentMap is not null)
{
CurrentMap.Entities.RemoveChild(CurrentPlayer);
RemoveChild(CurrentMap);
CurrentMap.Active = false;
}
2023-03-26 13:03:45 -07:00
2023-06-03 18:21:46 -07:00
AddChild(map);
InitTilemap(map);
2023-03-26 10:53:45 -07:00
2023-06-03 18:21:46 -07:00
CurrentMap = map;
CurrentMap.Active = true;
2023-03-26 10:53:45 -07:00
2023-06-03 18:21:46 -07:00
if (CurrentPlayer is not null)
2023-03-22 21:22:51 -07:00
{
2023-03-26 10:53:45 -07:00
CurrentMap.Entities.AddChild(CurrentPlayer);
2023-03-22 21:22:51 -07:00
}
2023-06-03 18:21:46 -07:00
}
2023-03-22 21:22:51 -07:00
2023-06-03 18:21:46 -07:00
public void CreatePlayer()
{
CurrentPlayer = _playerScene.Instantiate<Player>();
CurrentMap.Entities.AddChild(CurrentPlayer);
}
2023-03-22 21:22:51 -07:00
2023-06-03 18:21:46 -07:00
private void InitTilemap(Map map)
{
var children = map.Areas.GetChildren();
foreach (Node node in children)
2023-03-26 10:53:45 -07:00
{
2023-06-03 18:21:46 -07:00
if (node is BoundingBoxes.ConnectorBox connector)
2023-03-26 10:53:45 -07:00
{
2023-06-03 18:21:46 -07:00
// this reconnects the EventHandler if it is connected
connector.RequestedEnter -= _on_area_2d_requested_enter;
connector.RequestedEnter += _on_area_2d_requested_enter;
}
2023-03-26 10:53:45 -07:00
}
2023-06-03 18:21:46 -07:00
}
2023-03-26 10:53:45 -07:00
2023-06-03 18:21:46 -07:00
private void MovePlayerToConnector(string name)
{
// find the first connector with the specified name
var connector = CurrentMap.Areas.GetChildren().First((child) =>
2023-03-26 10:53:45 -07:00
{
2023-06-03 18:21:46 -07:00
if (child is BoundingBoxes.ConnectorBox connector)
2023-03-26 10:53:45 -07:00
{
2023-06-03 18:21:46 -07:00
return connector.Identifier == name;
2023-03-26 10:53:45 -07:00
}
2023-06-03 18:21:46 -07:00
return false;
}) as BoundingBoxes.ConnectorBox;
CurrentPlayer.GlobalPosition = connector.GlobalPosition;
}
2023-03-26 10:53:45 -07:00
2023-06-03 18:21:46 -07:00
public void MoveToArea(string path, string connector)
{
_currentConnector = connector;
if (path != _currentMapResourcePath)
2023-03-25 10:21:24 -07:00
{
2023-06-03 18:21:46 -07:00
var scene = ResourceLoader.Load<PackedScene>(path);
LoadScene(scene);
_currentMapResourcePath = path;
2023-03-26 10:53:45 -07:00
}
2023-06-03 18:21:46 -07:00
// after finished loading, move our player to the connector
MovePlayerToConnector(connector);
}
2023-03-26 10:53:45 -07:00
2023-06-03 18:21:46 -07:00
public void _on_area_2d_requested_enter(
BoundingBoxes.ConnectorBox box,
Player player)
{
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-22 21:22:51 -07:00
}
}