map switching

item-info
John Montagu, the 4th Earl of Sandvich 2023-03-26 10:53:45 -07:00
parent 72a1ace2f1
commit df6b75613c
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
7 changed files with 2530 additions and 1207 deletions

View File

@ -17,12 +17,15 @@ namespace SupaLidlGame.BoundingBoxes
[Export]
public string ToConnector { get; set; }
[Export]
public string Identifier { get; set; }
/// <summary>
/// Determines if the connector requires the user to interact to enter
/// the connector
/// </summary>
[Export]
public bool RequiresInteraction { get; set; } = true;
public bool RequiresInteraction { get; set; } = false;
[Export]
public CollisionShape2D Collision { get; set; }
@ -55,10 +58,13 @@ namespace SupaLidlGame.BoundingBoxes
public override void _Process(double delta)
{
if (Input.IsActionJustPressed("interact"))
if (Input.IsActionJustReleased("interact"))
{
if (_player is not null)
{
EmitSignal(SignalName.RequestedEnter, this, _player);
}
}
base._Process(delta);
}

1159
Scenes/BaseMap.tscn 100644

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

48
Scenes/Map.cs 100644
View File

@ -0,0 +1,48 @@
using Godot;
using System;
namespace SupaLidlGame.Scenes
{
public partial class Map : TileMap
{
[Export]
public Node2D Entities { get; set; }
[Export]
public Node2D Areas { get; set; }
[Export]
public Node2D Spawners { get; set; }
[Export]
public Vector2 CameraLowerBound { get; set; }
[Export]
public Vector2 CameraUpperBound { get; set; }
private bool _active;
public bool Active
{
get
{
return _active;
}
set
{
_active = Visible = value;
SetProcess(value);
}
}
public override void _Ready()
{
Active = true;
}
public override void _Process(double delta)
{
base._Process(delta);
}
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,56 +1,149 @@
using Godot;
using SupaLidlGame.Characters;
using SupaLidlGame.Scenes;
using SupaLidlGame.Extensions;
using System.Collections.Generic;
using System.Linq;
namespace SupaLidlGame.Utils
{
public partial class World : Node2D
{
[Export]
public string StartingArea { get; set; }
public PackedScene StartingArea { get; set; }
[Export]
public string CurrentArea { get; protected set; }
public Map CurrentMap { get; protected set; }
[Export]
public Player CurrentPlayer { get; set; }
private Dictionary<string, TileMap> maps;
private Dictionary<string, Map> _maps;
private string _currentConnector;
private string _currentMapResourcePath;
private const string PLAYER_PATH = "res://Characters/Player.tscn";
private PackedScene _playerScene;
public World()
{
maps = new Dictionary<string, TileMap>();
_maps = new Dictionary<string, Map>();
_playerScene = ResourceLoader.Load<PackedScene>(PLAYER_PATH);
}
public override void _Ready()
{
if (StartingArea is not null)
{
LoadScene(StartingArea);
}
// spawn the player in
CreatePlayer();
base._Ready();
}
public override void _Process(double delta)
public void LoadScene(PackedScene scene)
{
base._Process(delta);
if (CurrentMap is not null)
{
CurrentMap.Entities.RemoveChild(CurrentPlayer);
RemoveChild(CurrentMap);
CurrentMap.Active = false;
}
public void MoveToArea(string area, string connector, Player player)
Map map;
if (_maps.ContainsKey(scene.ResourcePath))
{
if (area != CurrentArea)
map = _maps[scene.ResourcePath];
}
else
{
// remove current map and load in the new map
TileMap map = GetNode<TileMap>(CurrentArea);
CurrentArea = area;
map = scene.Instantiate<Map>();
_maps.Add(scene.ResourcePath, map);
}
GD.Print("Loading " + scene.ResourcePath);
CurrentMap = map;
AddChild(map);
InitTilemap(map);
CurrentMap.Active = true;
if (CurrentPlayer is not null)
{
CurrentMap.Entities.AddChild(CurrentPlayer);
}
}
public void CreatePlayer()
{
CurrentPlayer = _playerScene.Instantiate<Player>();
CurrentMap.Entities.AddChild(CurrentPlayer);
}
private void InitTilemap(Map map)
{
var children = map.Areas.GetChildren();
foreach (Node node in children)
{
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;
}
}
}
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)
{
_currentConnector = connector;
if (path != _currentMapResourcePath)
{
var scene = ResourceLoader.Load<PackedScene>(path);
LoadScene(scene);
_currentMapResourcePath = path;
}
// after finished loading, move our player to the connector
MovePlayerToConnector(connector);
}
public void _on_area_2d_requested_enter(
BoundingBoxes.ConnectorBox box,
Player player)
{
MoveToArea(box.ToArea, box.ToConnector, 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();
}
}
}