修复了一些bug

This commit is contained in:
NianGao Dev
2026-09-19 14:21:08 +08:00
parent db81873409
commit 6c23eb4c62
73 changed files with 7354 additions and 2205 deletions

View File

@@ -31,6 +31,7 @@ type inputState struct {
breakDown bool
breakHeld bool
placeDown bool
placeHeld bool
selectSlot int // -1 无
toggleInv bool // E 打开/关闭物品栏
dropItem bool // Q 丢弃选中槽 1 个物品
@@ -39,7 +40,13 @@ type inputState struct {
cursorX, cursorY float64
clickX, clickY float32
clicked bool
clickedRight bool
escPressed bool
toggleFS bool // F11 / Alt+Enter(渲染线程消费,渲染.md §5.4)
textMode bool
chars []rune
backspace bool
}
// newInputState 创建输入状态并注册回调。
@@ -49,6 +56,7 @@ func newInputState(win *glfw.Window) *inputState {
win.SetMouseButtonCallback(in.mouseButtonCallback)
win.SetCursorPosCallback(in.cursorCallback)
win.SetScrollCallback(in.scrollCallback)
win.SetCharCallback(in.charCallback)
return in
}
@@ -63,7 +71,7 @@ func (in *inputState) SetSensitivity(s float64) {
func (in *inputState) Sensitivity() float64 { return in.sens }
// keyCallback 键盘回调(动作映射:WASD/空格/Shift/Ctrl/Esc)。
func (in *inputState) keyCallback(_ *glfw.Window, key glfw.Key, _ int, action glfw.Action, _ glfw.ModifierKey) {
func (in *inputState) keyCallback(_ *glfw.Window, key glfw.Key, _ int, action glfw.Action, mods glfw.ModifierKey) {
down := action != glfw.Release
switch key {
case glfw.KeyW:
@@ -84,6 +92,10 @@ func (in *inputState) keyCallback(_ *glfw.Window, key glfw.Key, _ int, action gl
if action == glfw.Press {
in.escPressed = true // 暂停/继续由屏幕状态机处理
}
case glfw.KeyBackspace:
if action != glfw.Release && in.textMode {
in.backspace = true
}
case glfw.Key1, glfw.Key2, glfw.Key3, glfw.Key4, glfw.Key5, glfw.Key6, glfw.Key7, glfw.Key8, glfw.Key9:
if action == glfw.Press {
in.selectSlot = int(key) - int(glfw.Key1)
@@ -96,9 +108,24 @@ func (in *inputState) keyCallback(_ *glfw.Window, key glfw.Key, _ int, action gl
if action == glfw.Press {
in.dropItem = true // 丢弃物品(UI与输入.md §5)
}
case glfw.KeyF11:
if action == glfw.Press {
in.toggleFS = true
}
case glfw.KeyEnter, glfw.KeyKPEnter:
if action == glfw.Press && mods&glfw.ModAlt != 0 {
in.toggleFS = true
}
}
}
func (in *inputState) charCallback(_ *glfw.Window, char rune) {
if !in.textMode {
return
}
in.chars = append(in.chars, char)
}
// mouseButtonCallback 鼠标按键:左键破坏(按住累积)、右键放置(UI与输入.md §6 长按)。
func (in *inputState) mouseButtonCallback(_ *glfw.Window, button glfw.MouseButton, action glfw.Action, _ glfw.ModifierKey) {
switch button {
@@ -113,7 +140,11 @@ func (in *inputState) mouseButtonCallback(_ *glfw.Window, button glfw.MouseButto
case glfw.MouseButtonRight:
if action == glfw.Press {
in.placeDown = true
in.clickedRight = true
in.clickX = float32(in.cursorX)
in.clickY = float32(in.cursorY)
}
in.placeHeld = action != glfw.Release
}
}
@@ -184,10 +215,15 @@ func (in *inputState) apply(s *game.Session) {
wish[1] -= r[2]
}
speed := 4.3
if in.sprint {
if p.InWater {
speed = 2.2
if in.sprint && in.forward {
speed = 5.4
}
} else if in.sprint {
speed *= 1.3
}
if in.sneak {
if in.sneak && !p.InWater {
speed *= 0.3
}
l := math.Sqrt(wish[0]*wish[0] + wish[1]*wish[1])
@@ -199,17 +235,25 @@ func (in *inputState) apply(s *game.Session) {
p.Vel[2] = 0
}
// 跳跃
if in.jump && p.OnGround {
p.Jump()
// 跳跃 / 游泳(物理与碰撞.md §4:水中空格上浮,不要求落地)
if in.jump {
if p.InWater {
p.SwimUp()
} else if p.OnGround {
p.Jump()
}
}
if in.sneak && p.InWater {
p.SwimDown()
}
p.SetPose(s.World, in.sneak && !p.InWater, in.sprint && in.forward && p.InWater)
// 热键栏切换
// 热键栏切换(UI与输入.md §6:上滚槽位−1,下滚+1,对齐原版 Java)
switch in.selectSlot {
case -2:
s.Inv.Selected = (s.Inv.Selected + 1) % 9
case -3:
s.Inv.Selected = (s.Inv.Selected + 8) % 9
case -3:
s.Inv.Selected = (s.Inv.Selected + 1) % 9
default:
if in.selectSlot >= 0 && in.selectSlot < 9 {
s.Inv.Selected = in.selectSlot
@@ -217,20 +261,28 @@ func (in *inputState) apply(s *game.Session) {
}
in.selectSlot = -1
// 左键:优先攻击实体(动物体系.md §5),未命中再破坏方块(按住累积)
// 左键:按下边沿攻击一次;按住连续破坏(方块交互与动画.md §3)
if in.breakDown {
if !s.Attack() {
if _, ok := s.Cast(5); !ok {
s.SwingMiss()
}
s.BeginBreak()
}
in.breakDown = false
}
if !in.breakHeld && s.BreakProgress() != 0 {
} else if in.breakHeld {
if !s.Breaking() {
s.BeginBreak()
} else {
s.ContinueBreak()
}
} else if s.Breaking() || s.BreakProgress() != 0 {
s.EndBreak()
}
// 放置(右键;命中工作台则打开合成界面,工作台与合成表.md §5)
// 放置(右键;命中工作台/熔炉/箱子则打开界面)
if in.placeDown {
if !s.TryOpenCrafting() {
if !s.TryOpenCrafting() && !s.TryOpenFurnace() && !s.TryOpenChest() && !s.TryFlickLever() && !s.TryUseEntity() && !s.TryEat() {
s.PlaceSelected()
}
in.placeDown = false