Files
ngzz-mc/internal/config/config.go
NianGao Dev f25ae15077 feat(game): 音频接入、原版掉落、动物攻击、村庄村民、种子/灵敏度配置与设置界面
- 音频(音频.md §2):Session.Sound 事件回调,破坏/放置/攻击/村民环境音接入,
  客户端 audio.Manager 3D 衰减 + 监听器跟随 + 音量可调
- 原版掉落:草方块→泥土、石头→圆石、树叶无掉落(no_drop);掉落物实体
  用物品图标小广告牌渲染(物品纹理并入实体图集)
- 动物攻击(动物体系.md §5):左键优先射线命中实体(半径 0.6 球体判定),
  伤害/击退/死亡移除+肉类掉落;僵尸掉腐肉
- 村庄村民(村民系统.md §2):Generator.VillageAt 定位村庄中心,会话扫描
  已加载区块在村庄生成 3 名村民
- 种子与灵敏度配置:client.yaml world.seed/name、mouse_sensitivity;
  存档与种子联动;鼠标左右反转修复(右移应右转)
- 设置界面(UI与输入.md §6):屏幕状态 4,灵敏度/音量滑条、世界种子显示、
  按键教程多行列表(cjkfont 支持 \\n)、完成按钮;主菜单/暂停菜单入口
- 准星指向方块名:左下角 CJK 显示(lang 本地化,tick 光栅化渲染线程上传)
- 截图统一存项目根目录 jietu/(paths.RootDir;dev 构建识别 bin/ 布局)
2026-08-16 20:18:52 +08:00

122 lines
3.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package config 解析 client.yaml / server.yaml 配置(设计.md §14)。
package config
import (
"fmt"
"os"
"gopkg.in/yaml.v3"
)
// ClientConfig 客户端配置(设计.md §14.2)。
type ClientConfig struct {
Client struct {
WindowWidth int `yaml:"window_width"`
WindowHeight int `yaml:"window_height"`
WindowTitle string `yaml:"window_title"`
VSync bool `yaml:"vsync"`
FOV float64 `yaml:"fov"`
RenderDistance int `yaml:"render_distance"`
Language string `yaml:"language"`
MouseSensitivity float64 `yaml:"mouse_sensitivity"`
World struct {
Seed int64 `yaml:"seed"` // 世界生成种子(世界生成.md §2 确定性)
Name string `yaml:"name"`
} `yaml:"world"`
Graphics struct {
SmoothLighting bool `yaml:"smooth_lighting"`
Mipmap bool `yaml:"mipmap"`
Anisotropic int `yaml:"anisotropic"`
} `yaml:"graphics"`
Audio struct {
Volume float64 `yaml:"volume"`
Music bool `yaml:"music"`
} `yaml:"audio"`
} `yaml:"client"`
}
// DefaultClient 客户端默认配置。
func DefaultClient() *ClientConfig {
c := &ClientConfig{}
c.Client.WindowWidth = 1280
c.Client.WindowHeight = 720
c.Client.WindowTitle = "年糕历险记"
c.Client.VSync = true
c.Client.FOV = 70
c.Client.RenderDistance = 12
c.Client.Language = "zh_cn"
c.Client.MouseSensitivity = 0.0025
c.Client.World.Seed = 123456789
c.Client.World.Name = "NewWorld"
c.Client.Graphics.SmoothLighting = true
c.Client.Graphics.Mipmap = true
c.Client.Graphics.Anisotropic = 4
c.Client.Audio.Volume = 1.0
c.Client.Audio.Music = true
return c
}
// ServerConfig 服务器配置(设计.md §14.1)。
type ServerConfig struct {
Server struct {
Port int `yaml:"port"`
MaxPlayers int `yaml:"max_players"`
ViewDistance int `yaml:"view_distance"`
TickRate int `yaml:"tick_rate"`
} `yaml:"server"`
World struct {
Seed int64 `yaml:"seed"`
Name string `yaml:"name"`
Generator struct {
BiomeScale float64 `yaml:"biome_scale"`
HeightScale float64 `yaml:"height_scale"`
CaveDensity float64 `yaml:"cave_density"`
} `yaml:"generator"`
} `yaml:"world"`
Mods struct {
Enabled bool `yaml:"enabled"`
Directory string `yaml:"directory"`
AllowScripts bool `yaml:"allow_scripts"`
ScriptTimeoutMS int `yaml:"script_timeout_ms"`
HotReload bool `yaml:"hot_reload"`
} `yaml:"mods"`
}
// DefaultServer 服务器默认配置。
func DefaultServer() *ServerConfig {
c := &ServerConfig{}
c.Server.Port = 25565
c.Server.MaxPlayers = 20
c.Server.ViewDistance = 10
c.Server.TickRate = 20
c.World.Seed = 123456789
c.World.Name = "NewWorld"
c.World.Generator.BiomeScale = 0.005
c.World.Generator.HeightScale = 0.01
c.World.Generator.CaveDensity = 0.1
c.Mods.Enabled = true
c.Mods.Directory = "mods"
c.Mods.AllowScripts = true
c.Mods.ScriptTimeoutMS = 5
return c
}
// Load 读取 YAML 配置;文件不存在时返回 defaults(零拷贝不可行则回退默认构造)。
func Load[T any](path string, defaults *T) (*T, error) {
b, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return defaults, nil
}
return nil, fmt.Errorf("config.Load %s: %w", path, err)
}
out := new(T)
if defaults != nil {
*out = *defaults
}
if err := yaml.Unmarshal(b, out); err != nil {
return nil, fmt.Errorf("config.Load %s: %w", path, err)
}
return out, nil
}