2024-06-19 16:02:32 +00:00
|
|
|
|
using System;
|
|
|
|
|
using ColdMint.scripts.character;
|
2024-05-31 14:26:38 +00:00
|
|
|
|
using ColdMint.scripts.debug;
|
|
|
|
|
using ColdMint.scripts.map.events;
|
2024-10-08 08:11:51 +00:00
|
|
|
|
using ColdMint.scripts.map.room;
|
2024-06-10 13:05:18 +00:00
|
|
|
|
using ColdMint.scripts.utils;
|
2024-05-31 14:26:38 +00:00
|
|
|
|
using Godot;
|
|
|
|
|
|
|
|
|
|
namespace ColdMint.scripts.map;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>PlayerSpawn</para>
|
|
|
|
|
/// <para>玩家出生点</para>
|
|
|
|
|
/// </summary>
|
2024-10-08 14:04:37 +00:00
|
|
|
|
public partial class PlayerSpawn : Marker2D, ISpawnMarker
|
2024-05-31 14:26:38 +00:00
|
|
|
|
{
|
|
|
|
|
private PackedScene? _playerPackedScene;
|
2024-10-08 14:04:37 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>The player's generated wave count</para>
|
|
|
|
|
/// <para>玩家的生成波数</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly int PlayerWaveNumber = 1;
|
2024-05-31 14:26:38 +00:00
|
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
|
{
|
|
|
|
|
base._Ready();
|
2024-10-09 02:49:10 +00:00
|
|
|
|
_playerPackedScene = ResourceLoader.Load<PackedScene>("res://prefab/entitys/Character.tscn");
|
2024-09-06 15:36:21 +00:00
|
|
|
|
EventBus.MapGenerationCompleteEvent += MapGenerationCompleteEvent;
|
|
|
|
|
EventBus.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
|
|
|
|
{
|
2024-09-01 15:24:35 +00:00
|
|
|
|
if (GameSceneDepend.Player != null)
|
2024-05-31 14:26:38 +00:00
|
|
|
|
{
|
2024-09-13 15:04:39 +00:00
|
|
|
|
GameSceneDepend.Player.FullHpRevive();
|
2024-09-01 15:24:35 +00:00
|
|
|
|
GameSceneDepend.Player.GlobalPosition = GlobalPosition;
|
2024-05-31 14:26:38 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2024-10-08 14:04:37 +00:00
|
|
|
|
Spawn(PlayerWaveNumber);
|
2024-10-08 08:11:51 +00:00
|
|
|
|
}
|
2024-10-08 14:04:37 +00:00
|
|
|
|
|
2024-10-08 08:11:51 +00:00
|
|
|
|
|
|
|
|
|
private void MapGenerationCompleteEvent(MapGenerationCompleteEvent mapGenerationCompleteEvent)
|
|
|
|
|
{
|
|
|
|
|
//After the map is generated, create the player instance.
|
|
|
|
|
//当地图生成完成后,创建玩家实例。
|
|
|
|
|
if (GameSceneDepend.Player != null)
|
|
|
|
|
{
|
|
|
|
|
//An existing player instance will not be created.
|
|
|
|
|
//已经存在玩家实例,不再创建。
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-08 14:04:37 +00:00
|
|
|
|
Spawn(PlayerWaveNumber);
|
2024-10-08 08:11:51 +00:00
|
|
|
|
}
|
2024-06-10 13:05:18 +00:00
|
|
|
|
|
2024-10-08 08:11:51 +00:00
|
|
|
|
public override void _ExitTree()
|
|
|
|
|
{
|
|
|
|
|
base._ExitTree();
|
|
|
|
|
EventBus.MapGenerationCompleteEvent -= MapGenerationCompleteEvent;
|
|
|
|
|
EventBus.GameReplayEvent -= GameReplayEvent;
|
2024-06-04 14:23:06 +00:00
|
|
|
|
}
|
2024-06-02 14:25:07 +00:00
|
|
|
|
|
2024-10-08 14:04:37 +00:00
|
|
|
|
|
|
|
|
|
public Node2D? Spawn(int waveNumber)
|
2024-06-04 14:23:06 +00:00
|
|
|
|
{
|
2024-10-08 14:04:37 +00:00
|
|
|
|
if (waveNumber != PlayerWaveNumber)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-09-01 15:24:35 +00:00
|
|
|
|
if (GameSceneDepend.PlayerContainer == null)
|
2024-05-31 14:26:38 +00:00
|
|
|
|
{
|
2024-10-08 08:11:51 +00:00
|
|
|
|
return null;
|
2024-05-31 14:26:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_playerPackedScene == null)
|
|
|
|
|
{
|
|
|
|
|
LogCat.LogError("player_packed_scene_not_exist");
|
2024-10-08 08:11:51 +00:00
|
|
|
|
return null;
|
2024-05-31 14:26:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 13:05:18 +00:00
|
|
|
|
var playerNode =
|
2024-06-24 14:19:12 +00:00
|
|
|
|
NodeUtils.InstantiatePackedScene<Player>(_playerPackedScene);
|
2024-06-10 13:05:18 +00:00
|
|
|
|
if (playerNode == null)
|
2024-05-31 14:26:38 +00:00
|
|
|
|
{
|
2024-10-08 08:11:51 +00:00
|
|
|
|
return null;
|
2024-05-31 14:26:38 +00:00
|
|
|
|
}
|
2024-06-19 16:02:32 +00:00
|
|
|
|
|
2024-09-01 15:24:35 +00:00
|
|
|
|
//The player's parent node must be GameSceneDepend PlayerContainer.
|
|
|
|
|
//玩家的父节点必须是GameSceneDepend.PlayerContainer。
|
|
|
|
|
NodeUtils.CallDeferredAddChild(GameSceneDepend.PlayerContainer, playerNode);
|
|
|
|
|
var itemContainer = GameSceneDepend.HotBar?.GetItemContainer();
|
2024-06-19 16:02:32 +00:00
|
|
|
|
if (itemContainer == null)
|
|
|
|
|
{
|
|
|
|
|
//Throws an exception when the item container is empty.
|
|
|
|
|
//当物品容器为空时,抛出异常。
|
|
|
|
|
throw new NullReferenceException(TranslationServerUtils.Translate("log_item_container_is_null"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
playerNode.ItemContainer = itemContainer;
|
2024-09-01 15:24:35 +00:00
|
|
|
|
GameSceneDepend.Player = playerNode;
|
2024-06-25 14:11:19 +00:00
|
|
|
|
playerNode.GlobalPosition = GlobalPosition;
|
2024-10-08 08:11:51 +00:00
|
|
|
|
return playerNode;
|
2024-05-31 14:26:38 +00:00
|
|
|
|
}
|
2024-06-03 14:58:59 +00:00
|
|
|
|
|
2024-10-08 14:04:37 +00:00
|
|
|
|
public int GetMaxWaveNumber()
|
|
|
|
|
{
|
|
|
|
|
return PlayerWaveNumber;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-08 08:11:51 +00:00
|
|
|
|
public bool CanQueueFree()
|
2024-06-04 14:23:06 +00:00
|
|
|
|
{
|
2024-10-08 08:11:51 +00:00
|
|
|
|
return false;
|
2024-06-04 14:23:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-08 08:11:51 +00:00
|
|
|
|
public void DoQueueFree()
|
2024-06-03 14:58:59 +00:00
|
|
|
|
{
|
2024-10-08 08:11:51 +00:00
|
|
|
|
QueueFree();
|
2024-06-03 14:58:59 +00:00
|
|
|
|
}
|
2024-05-31 14:26:38 +00:00
|
|
|
|
}
|