129 lines
2.9 KiB
Go
129 lines
2.9 KiB
Go
package game
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"edgaru089.ink/go/gl01/internal/igwrap/backend"
|
|
"edgaru089.ink/go/gl01/internal/io"
|
|
"edgaru089.ink/go/gl01/internal/util"
|
|
"edgaru089.ink/go/gl01/internal/util/itype"
|
|
"github.com/go-gl/glfw/v3.3/glfw"
|
|
)
|
|
|
|
const (
|
|
PlayerBreakCooldown = 200 * time.Millisecond
|
|
PlayerPlaceCooldown = 200 * time.Millisecond
|
|
)
|
|
|
|
var logs string
|
|
|
|
type logger struct{}
|
|
|
|
func (logger) Write(b []byte) (n int, err error) {
|
|
logs = logs + string(b)
|
|
n, err = os.Stderr.Write(b)
|
|
return
|
|
}
|
|
|
|
func init() {
|
|
log.Default().SetOutput(logger{})
|
|
}
|
|
|
|
// Init initializes the game.
|
|
func (g *Game) Init(win *glfw.Window) {
|
|
|
|
width, height := win.GetSize()
|
|
|
|
io.DisplaySize[0], io.DisplaySize[1] = win.GetFramebufferSize()
|
|
win.SetSizeCallback(func(w *glfw.Window, width, height int) {
|
|
//win.SetCursorPos(float64(width)/2, float64(height)/2)
|
|
io.DisplaySize = itype.Vec2i{width, height}
|
|
})
|
|
|
|
g.initImgui(win)
|
|
|
|
win.SetCursorPos(float64(width)/2, float64(height)/2)
|
|
|
|
io.ShowDebugInfo = true
|
|
g.paused = true
|
|
//win.SetInputMode(glfw.CursorMode, glfw.CursorDisabled)
|
|
win.SetCursorPosCallback(func(w *glfw.Window, xpos, ypos float64) {
|
|
if g.paused { // GUI
|
|
return
|
|
}
|
|
|
|
width, height := w.GetSize()
|
|
centerX, centerY := float64(width)/2, float64(height)/2
|
|
//deltaX, deltaY := xpos-centerX, ypos-centerY
|
|
_, _ = xpos-centerX, ypos-centerY
|
|
win.SetCursorPos(centerX, centerY)
|
|
})
|
|
|
|
win.SetMouseButtonCallback(func(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
|
|
if g.paused {
|
|
backend.MouseButtonCallback(button, action)
|
|
}
|
|
})
|
|
|
|
win.SetKeyCallback(func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
|
|
if g.paused {
|
|
backend.KeyCallback(key, action)
|
|
}
|
|
if action == glfw.Press {
|
|
|
|
if key == glfw.KeyF11 {
|
|
if g.fullscreen {
|
|
win.SetMonitor(nil, g.lastPos[0], g.lastPos[1], g.lastSize[0], g.lastSize[1], glfw.DontCare)
|
|
g.fullscreen = false
|
|
} else {
|
|
g.lastPos[0], g.lastPos[1] = win.GetPos()
|
|
g.lastSize[0], g.lastSize[1] = win.GetSize()
|
|
|
|
monitor := glfw.GetPrimaryMonitor()
|
|
videoMode := monitor.GetVideoMode()
|
|
|
|
win.SetMonitor(monitor, 0, 0, videoMode.Width, videoMode.Height, glfw.DontCare)
|
|
|
|
g.fullscreen = true
|
|
}
|
|
win.MakeContextCurrent()
|
|
glfw.SwapInterval(1)
|
|
}
|
|
|
|
if key == glfw.KeyF3 {
|
|
io.ShowDebugInfo = !io.ShowDebugInfo
|
|
}
|
|
}
|
|
})
|
|
|
|
win.SetCharCallback(func(w *glfw.Window, char rune) {
|
|
if g.paused {
|
|
backend.InputCallback(char)
|
|
}
|
|
})
|
|
|
|
win.SetScrollCallback(func(w *glfw.Window, xpos, ypos float64) {
|
|
if g.paused {
|
|
backend.MouseScrollCallback(xpos, ypos)
|
|
}
|
|
})
|
|
|
|
}
|
|
|
|
const airAccel = 0.1
|
|
|
|
// Update updates the game state, not necessarily in the main thread.
|
|
func (g *Game) Update(win *glfw.Window, delta time.Duration) {
|
|
g.runtime += delta
|
|
backend.NewFrame()
|
|
|
|
clock := util.NewClock()
|
|
|
|
io.Diagnostics.Times.Logic = clock.Restart()
|
|
|
|
g.imgui()
|
|
io.Diagnostics.Times.GUI = clock.Restart()
|
|
}
|