2024-05-31 14:26:38 +00:00
|
|
|
|
using ColdMint.scripts.character;
|
|
|
|
|
using ColdMint.scripts.debug;
|
|
|
|
|
using ColdMint.scripts.map.events;
|
|
|
|
|
using Godot;
|
|
|
|
|
|
|
|
|
|
namespace ColdMint.scripts.map;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>PlayerSpawn</para>
|
|
|
|
|
/// <para>玩家出生点</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class PlayerSpawn : Marker2D
|
|
|
|
|
{
|
|
|
|
|
private PackedScene? _playerPackedScene;
|
|
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
|
{
|
|
|
|
|
base._Ready();
|
|
|
|
|
_playerPackedScene = GD.Load<PackedScene>("res://prefab/entitys/Character.tscn");
|
2024-06-03 14:58:59 +00:00
|
|
|
|
EventManager.MapGenerationCompleteEvent += MapGenerationCompleteEvent;
|
2024-06-04 14:23:06 +00:00
|
|
|
|
EventManager.GameReplayEvent += GameReplayEvent;
|
2024-05-31 14:26:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-04 14:23:06 +00:00
|
|
|
|
private void GameReplayEvent(GameReplayEvent gameReplayEvent)
|
2024-05-31 14:26:38 +00:00
|
|
|
|
{
|
|
|
|
|
if (GameSceneNodeHolder.Player != null)
|
|
|
|
|
{
|
2024-06-08 15:59:24 +00:00
|
|
|
|
GameSceneNodeHolder.Player.ProcessMode = ProcessModeEnum.Inherit;
|
2024-05-31 14:26:38 +00:00
|
|
|
|
GameSceneNodeHolder.Player.Position = GlobalPosition;
|
2024-06-04 14:23:06 +00:00
|
|
|
|
GameSceneNodeHolder.Player.Revive(GameSceneNodeHolder.Player.MaxHp);
|
2024-05-31 14:26:38 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2024-06-04 14:23:06 +00:00
|
|
|
|
SpawnPlayer();
|
|
|
|
|
}
|
2024-06-02 14:25:07 +00:00
|
|
|
|
|
2024-06-04 14:23:06 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Generate player instance</para>
|
|
|
|
|
/// <para>生成玩家实例</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void SpawnPlayer()
|
|
|
|
|
{
|
2024-05-31 14:26:38 +00:00
|
|
|
|
if (GameSceneNodeHolder.PlayerContainer == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_playerPackedScene == null)
|
|
|
|
|
{
|
|
|
|
|
LogCat.LogError("player_packed_scene_not_exist");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var playerNode = _playerPackedScene.Instantiate();
|
|
|
|
|
if (playerNode is not Player player)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-02 14:25:07 +00:00
|
|
|
|
player.ItemContainer = GameSceneNodeHolder.HotBar;
|
2024-05-31 14:26:38 +00:00
|
|
|
|
GameSceneNodeHolder.PlayerContainer.AddChild(player);
|
|
|
|
|
GameSceneNodeHolder.Player = player;
|
|
|
|
|
player.Position = GlobalPosition;
|
|
|
|
|
LogCat.LogWithFormat("player_spawn_debug", player.ReadOnlyCharacterName, player.Position);
|
|
|
|
|
}
|
2024-06-03 14:58:59 +00:00
|
|
|
|
|
2024-06-04 14:23:06 +00:00
|
|
|
|
private void MapGenerationCompleteEvent(MapGenerationCompleteEvent mapGenerationCompleteEvent)
|
|
|
|
|
{
|
|
|
|
|
//After the map is generated, create the player instance.
|
|
|
|
|
//当地图生成完成后,创建玩家实例。
|
|
|
|
|
if (GameSceneNodeHolder.Player != null)
|
|
|
|
|
{
|
|
|
|
|
//An existing player instance will not be created.
|
|
|
|
|
//已经存在玩家实例,不再创建。
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SpawnPlayer();
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 14:58:59 +00:00
|
|
|
|
public override void _ExitTree()
|
|
|
|
|
{
|
|
|
|
|
base._ExitTree();
|
|
|
|
|
EventManager.MapGenerationCompleteEvent -= MapGenerationCompleteEvent;
|
2024-06-04 14:23:06 +00:00
|
|
|
|
EventManager.GameReplayEvent -= GameReplayEvent;
|
2024-06-03 14:58:59 +00:00
|
|
|
|
}
|
2024-05-31 14:26:38 +00:00
|
|
|
|
}
|