初次提交
This commit is contained in:
10
Script/Base/IThing.cs
Normal file
10
Script/Base/IThing.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
namespace Cosmobox
|
||||
{
|
||||
public interface IThing
|
||||
{
|
||||
public void Update(double delta);
|
||||
}
|
||||
}
|
1
Script/Base/IThing.cs.uid
Normal file
1
Script/Base/IThing.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://m301eht3eac3
|
9
Script/Base/IThingPhysics.cs
Normal file
9
Script/Base/IThingPhysics.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using Godot;
|
||||
using System;
|
||||
namespace Cosmobox
|
||||
{
|
||||
public interface IThingPhysics
|
||||
{
|
||||
public void PhysicsUpdate(double delta);
|
||||
}
|
||||
}
|
1
Script/Base/IThingPhysics.cs.uid
Normal file
1
Script/Base/IThingPhysics.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://c6hg67pd2oxuc
|
9
Script/Base/IThingUI.cs
Normal file
9
Script/Base/IThingUI.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using Godot;
|
||||
using System;
|
||||
namespace Cosmobox
|
||||
{
|
||||
public interface IThingUI
|
||||
{
|
||||
public void UpdateUI();
|
||||
}
|
||||
}
|
1
Script/Base/IThingUI.cs.uid
Normal file
1
Script/Base/IThingUI.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://bqkle0sd1vwbk
|
89
Script/Base/InputControl.cs
Normal file
89
Script/Base/InputControl.cs
Normal file
@ -0,0 +1,89 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
namespace Cosmobox
|
||||
{
|
||||
public partial class InputControl : Node
|
||||
{
|
||||
[Export] public TickControl tickControl; // 控制游戏暂停的节点
|
||||
[Export] public Player player; // 玩家节点
|
||||
[Export] public BagUI bagUI; // 背包 UI
|
||||
[Export] public Control devUI; // 开发者 UI
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
// 初始化状态
|
||||
UpdatePauseState();
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
// 监听 "bag" 按键
|
||||
if (Input.IsActionJustPressed("bag"))
|
||||
{
|
||||
ToggleBagUI();
|
||||
}
|
||||
|
||||
// 监听开发者 UI 的切换(假设通过某个按键触发)
|
||||
if (Input.IsActionJustPressed("dev_ui"))
|
||||
{
|
||||
ToggleDevUI();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 切换背包 UI 的打开/关闭状态
|
||||
/// </summary>
|
||||
private void ToggleBagUI()
|
||||
{
|
||||
if (bagUI.IsOpen)
|
||||
{
|
||||
bagUI.CloseBag(); // 关闭背包
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果开发者 UI 已打开,则先关闭开发者 UI
|
||||
if (devUI.Visible)
|
||||
{
|
||||
devUI.Visible = false;
|
||||
}
|
||||
bagUI.OpenBag(); // 打开背包
|
||||
}
|
||||
|
||||
// 更新暂停状态
|
||||
UpdatePauseState();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 切换开发者 UI 的可见性
|
||||
/// </summary>
|
||||
private void ToggleDevUI()
|
||||
{
|
||||
if (devUI.Visible)
|
||||
{
|
||||
devUI.Visible = false; // 关闭开发者 UI
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果背包已打开,则先关闭背包
|
||||
if (bagUI.IsOpen)
|
||||
{
|
||||
bagUI.CloseBag();
|
||||
}
|
||||
devUI.Visible = true; // 打开开发者 UI
|
||||
}
|
||||
|
||||
// 更新暂停状态
|
||||
UpdatePauseState();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新游戏的暂停状态
|
||||
/// </summary>
|
||||
private void UpdatePauseState()
|
||||
{
|
||||
// 如果背包或开发者 UI 任一打开,则暂停游戏
|
||||
tickControl.pause = bagUI.IsOpen || devUI.Visible;
|
||||
}
|
||||
}
|
||||
}
|
1
Script/Base/InputControl.cs.uid
Normal file
1
Script/Base/InputControl.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://cgesjwgws6bmr
|
67
Script/Base/ItemResourceManager.cs
Normal file
67
Script/Base/ItemResourceManager.cs
Normal file
@ -0,0 +1,67 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
namespace Cosmobox
|
||||
{
|
||||
public static class ItemResourceManager
|
||||
{
|
||||
private static Dictionary<string, ItemResource> _items;
|
||||
private static bool _initialized = false;
|
||||
|
||||
// 初始化物品数据
|
||||
private static void Initialize()
|
||||
{
|
||||
if (_initialized) return;
|
||||
|
||||
_items = new Dictionary<string, ItemResource>();
|
||||
|
||||
// 加载 res://Resource/Item/ 目录下的所有 ItemResource
|
||||
var dir = DirAccess.Open("res://Resource/Item/");
|
||||
if (dir != null)
|
||||
{
|
||||
dir.ListDirBegin();
|
||||
string fileName = dir.GetNext();
|
||||
while (fileName != "")
|
||||
{
|
||||
if (!dir.CurrentIsDir() && fileName.EndsWith(".tres"))
|
||||
{
|
||||
var resourcePath = $"res://Resource/Item/{fileName}";
|
||||
var item = GD.Load<ItemResource>(resourcePath);
|
||||
if (item != null)
|
||||
{
|
||||
_items[item.ItemName] = item;
|
||||
}
|
||||
}
|
||||
fileName = dir.GetNext();
|
||||
}
|
||||
}
|
||||
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
// 获取所有物品
|
||||
public static Dictionary<string, ItemResource> GetAllItems()
|
||||
{
|
||||
Initialize();
|
||||
return _items;
|
||||
}
|
||||
|
||||
// 通过名称获取物品
|
||||
public static ItemResource GetItem(string itemName)
|
||||
{
|
||||
Initialize();
|
||||
if (_items.TryGetValue(itemName, out var item))
|
||||
{
|
||||
return item;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 获取物品数量
|
||||
public static int GetItemCount()
|
||||
{
|
||||
Initialize();
|
||||
return _items.Count;
|
||||
}
|
||||
}
|
||||
}
|
1
Script/Base/ItemResourceManager.cs.uid
Normal file
1
Script/Base/ItemResourceManager.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://cla8a7157wf4v
|
135
Script/Base/PerlinNoise.cs
Normal file
135
Script/Base/PerlinNoise.cs
Normal file
@ -0,0 +1,135 @@
|
||||
using System;
|
||||
namespace Cosmobox
|
||||
{
|
||||
public class PerlinNoise
|
||||
{
|
||||
private int[] p; // 混淆表
|
||||
|
||||
// 构造函数:初始化混淆表
|
||||
public PerlinNoise(int seed)
|
||||
{
|
||||
// 初始化为0-255的随机排列
|
||||
p = new int[512]; // 混淆表加倍以方便使用
|
||||
int[] permutation = new int[256];
|
||||
Random random = new Random(seed);
|
||||
|
||||
// 填充数组为0-255
|
||||
for (int i = 0; i < 256; i++)
|
||||
{
|
||||
permutation[i] = i;
|
||||
}
|
||||
|
||||
// 使用Fisher-Yates算法打乱数组
|
||||
for (int i = 0; i < 256; i++)
|
||||
{
|
||||
int swapIndex = random.Next(256);
|
||||
int temp = permutation[i];
|
||||
permutation[i] = permutation[swapIndex];
|
||||
permutation[swapIndex] = temp;
|
||||
}
|
||||
|
||||
// 将打乱后的数组复制两次,生成512个元素的混淆表
|
||||
for (int i = 0; i < 256; i++)
|
||||
{
|
||||
p[i] = permutation[i];
|
||||
p[i + 256] = permutation[i];
|
||||
}
|
||||
}
|
||||
|
||||
// 平滑函数 (6t^5 - 15t^4 + 10t^3)
|
||||
private double Fade(double t)
|
||||
{
|
||||
return t * t * t * (t * (t * 6 - 15) + 10);
|
||||
}
|
||||
|
||||
// 线性插值
|
||||
private double Lerp(double t, double a, double b)
|
||||
{
|
||||
return a + t * (b - a);
|
||||
}
|
||||
|
||||
// 计算梯度向量和距离向量的点积
|
||||
private double Grad(int hash, double x, double y, double z)
|
||||
{
|
||||
// 根据hash值确定使用哪个梯度向量
|
||||
// 12个梯度向量由以下组合构成:(+/-1, +/-1, 0), (+/-1, 0, +/-1), (0, +/-1, +/-1)
|
||||
switch (hash & 0xF) // 取hash值的最后4位
|
||||
{
|
||||
case 0x0: return x + y;
|
||||
case 0x1: return -x + y;
|
||||
case 0x2: return x - y;
|
||||
case 0x3: return -x - y;
|
||||
case 0x4: return x + z;
|
||||
case 0x5: return -x + z;
|
||||
case 0x6: return x - z;
|
||||
case 0x7: return -x - z;
|
||||
case 0x8: return y + z;
|
||||
case 0x9: return -y + z;
|
||||
case 0xA: return y - z;
|
||||
case 0xB: return -y - z;
|
||||
case 0xC: return y + x; // 这四个是重复的,但Ken Perlin的原始实现中包含它们。
|
||||
case 0xD: return -y + x; // 它们对噪声质量影响不大,但保持了表格的一致性。
|
||||
case 0xE: return y - x;
|
||||
case 0xF: return -y - x;
|
||||
default: return 0; // 不应该发生
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 为给定的(x, y, z)坐标生成3D Perlin噪声。
|
||||
/// 输出值通常在-1到1之间。
|
||||
/// </summary>
|
||||
public double Noise(double x, double y, double z)
|
||||
{
|
||||
// 找到包含该点的单位立方体
|
||||
int X = (int)Math.Floor(x) & 255;
|
||||
int Y = (int)Math.Floor(y) & 255;
|
||||
int Z = (int)Math.Floor(z) & 255;
|
||||
|
||||
// 找到该点在立方体内的相对x, y, z坐标
|
||||
x -= Math.Floor(x);
|
||||
y -= Math.Floor(y);
|
||||
z -= Math.Floor(z);
|
||||
|
||||
// 计算x, y, z的平滑曲线
|
||||
double u = Fade(x);
|
||||
double v = Fade(y);
|
||||
double w = Fade(z);
|
||||
|
||||
// 对立方体的8个角进行哈希计算
|
||||
int A = p[X] + Y;
|
||||
int AA = p[A] + Z;
|
||||
int AB = p[A + 1] + Z;
|
||||
int B = p[X + 1] + Y;
|
||||
int BA = p[B] + Z;
|
||||
int BB = p[B + 1] + Z;
|
||||
|
||||
// 获取所有8个角的哈希值
|
||||
int H000 = p[AA];
|
||||
int H100 = p[BA];
|
||||
int H010 = p[AB];
|
||||
int H110 = p[BB];
|
||||
int H001 = p[AA + 1];
|
||||
int H101 = p[BA + 1];
|
||||
int H011 = p[AB + 1];
|
||||
int H111 = p[BB + 1];
|
||||
|
||||
// 计算所有8个角的点积并插值
|
||||
double x0, x1, y0, y1;
|
||||
|
||||
x0 = Lerp(u, Grad(H000, x, y, z), // (0,0,0)
|
||||
Grad(H100, x - 1, y, z)); // (1,0,0)
|
||||
x1 = Lerp(u, Grad(H010, x, y - 1, z), // (0,1,0)
|
||||
Grad(H110, x - 1, y - 1, z)); // (1,1,0)
|
||||
y0 = Lerp(v, x0, x1);
|
||||
|
||||
x0 = Lerp(u, Grad(H001, x, y, z - 1), // (0,0,1)
|
||||
Grad(H101, x - 1, y, z - 1)); // (1,0,1)
|
||||
x1 = Lerp(u, Grad(H011, x, y - 1, z - 1), // (0,1,1)
|
||||
Grad(H111, x - 1, y - 1, z - 1)); // (1,1,1)
|
||||
y1 = Lerp(v, x0, x1);
|
||||
|
||||
return Lerp(w, y0, y1);
|
||||
}
|
||||
}
|
||||
}
|
1
Script/Base/PerlinNoise.cs.uid
Normal file
1
Script/Base/PerlinNoise.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://cnom22n5jbp2
|
74
Script/Base/TickControl.cs
Normal file
74
Script/Base/TickControl.cs
Normal file
@ -0,0 +1,74 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
namespace Cosmobox
|
||||
{
|
||||
public partial class TickControl : Node
|
||||
{
|
||||
[Export] public bool pause = false;
|
||||
|
||||
private List<IThing> things = new List<IThing>();
|
||||
private List<IThingPhysics> thingsPhysics = new List<IThingPhysics>();
|
||||
private List<IThingUI> thingsUI = new List<IThingUI>();
|
||||
public override void _Ready()
|
||||
{
|
||||
// 从场景树的根节点开始查找实现了 IThing 接口的节点
|
||||
FindThingsInScene(GetTree().GetRoot());
|
||||
}
|
||||
|
||||
private void FindThingsInScene(Node node)
|
||||
{
|
||||
// 检查当前节点是否实现了 IThing 接口
|
||||
if (node is IThing thing)
|
||||
{
|
||||
things.Add(thing);
|
||||
}
|
||||
|
||||
// 检查当前节点是否实现了 IThingPhysics 接口
|
||||
if (node is IThingPhysics physicsThing)
|
||||
{
|
||||
thingsPhysics.Add(physicsThing);
|
||||
}
|
||||
|
||||
// 检查当前节点是否实现了 IThingUI 接口
|
||||
if (node is IThingUI uiThing)
|
||||
{
|
||||
thingsUI.Add(uiThing);
|
||||
}
|
||||
|
||||
// 递归查找子节点
|
||||
foreach (Node child in node.GetChildren())
|
||||
{
|
||||
FindThingsInScene(child);
|
||||
}
|
||||
}
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (!pause)
|
||||
{
|
||||
foreach (var thing in things)
|
||||
{
|
||||
thing.Update(delta);
|
||||
}
|
||||
}
|
||||
foreach (var thing in thingsUI)
|
||||
{
|
||||
thing.UpdateUI();
|
||||
}
|
||||
}
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
if (!pause)
|
||||
{
|
||||
foreach (var thing in thingsPhysics)
|
||||
{
|
||||
thing.PhysicsUpdate(delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
1
Script/Base/TickControl.cs.uid
Normal file
1
Script/Base/TickControl.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://ctbyhk40owvej
|
Reference in New Issue
Block a user