2022-01-20 21:58:50 +08:00
|
|
|
package game
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image/color"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2024-08-02 18:38:33 +08:00
|
|
|
"edgaru089.ink/go/gl01/internal/igwrap/backend"
|
|
|
|
"edgaru089.ink/go/gl01/internal/io"
|
|
|
|
"edgaru089.ink/go/gl01/internal/render"
|
|
|
|
"edgaru089.ink/go/gl01/internal/util"
|
|
|
|
"edgaru089.ink/go/gl01/internal/util/itype"
|
|
|
|
"edgaru089.ink/go/gl01/internal/world"
|
|
|
|
"edgaru089.ink/go/gl01/internal/world/blocks"
|
|
|
|
"edgaru089.ink/go/gl01/internal/world/worldgen"
|
2022-01-20 21:58:50 +08:00
|
|
|
"github.com/go-gl/glfw/v3.3/glfw"
|
|
|
|
"github.com/go-gl/mathgl/mgl64"
|
|
|
|
)
|
|
|
|
|
2022-02-24 20:20:33 +08:00
|
|
|
const (
|
|
|
|
PlayerBreakCooldown = 200 * time.Millisecond
|
|
|
|
PlayerPlaceCooldown = 200 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
2022-10-12 08:24:00 +08:00
|
|
|
var placeId = [10]int{blocks.Stone, blocks.Slab, blocks.Grass, blocks.LogOak, blocks.PlanksOak, blocks.LeavesOak, blocks.Glass, blocks.DebugDir, blocks.Bedrock, blocks.Water}
|
|
|
|
var placeAux = [10]int{0, blocks.PlanksOak, 0, 0, 0, 0, 0, 0, 0, 0}
|
2022-02-24 20:20:33 +08:00
|
|
|
var placei = 0
|
|
|
|
|
2022-01-20 21:58:50 +08:00
|
|
|
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) {
|
|
|
|
|
|
|
|
g.world = world.NewWorld()
|
|
|
|
g.view = &render.View{}
|
|
|
|
|
2022-02-24 13:18:05 +08:00
|
|
|
err := g.initRender()
|
2022-01-20 21:58:50 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-02-24 20:20:33 +08:00
|
|
|
g.player.SetDatasetI("LastBreak", 0)
|
|
|
|
g.player.SetDatasetI("LastPlace", 0)
|
|
|
|
|
2022-01-20 21:58:50 +08:00
|
|
|
var seed int64 = time.Now().Unix()
|
|
|
|
gensync := make(chan struct{})
|
|
|
|
gensynccnt := 0
|
2022-01-29 00:43:11 +08:00
|
|
|
for i := -4; i <= 4; i++ {
|
|
|
|
for j := -4; j <= 4; j++ {
|
2022-01-20 21:58:50 +08:00
|
|
|
c := &world.Chunk{}
|
|
|
|
g.world.SetChunk(i, j, c)
|
|
|
|
go func() {
|
|
|
|
worldgen.Chunk(c, g.world, seed)
|
|
|
|
gensync <- struct{}{}
|
|
|
|
}()
|
|
|
|
gensynccnt++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < gensynccnt; i++ {
|
|
|
|
<-gensync
|
|
|
|
}
|
|
|
|
|
|
|
|
width, height := win.GetSize()
|
|
|
|
g.view.Aspect(float32(width)/float32(height)).FovY(itype.Degrees(60)).LookAt(g.cameraPos.ToFloat32(), g.rotY, itype.Degrees(g.rotZ))
|
|
|
|
|
|
|
|
io.DisplaySize[0], io.DisplaySize[1] = win.GetFramebufferSize()
|
|
|
|
win.SetSizeCallback(func(w *glfw.Window, width, height int) {
|
2022-01-28 15:24:03 +08:00
|
|
|
//win.SetCursorPos(float64(width)/2, float64(height)/2)
|
2022-01-20 21:58:50 +08:00
|
|
|
g.view.Aspect(float32(width) / float32(height))
|
|
|
|
io.DisplaySize = itype.Vec2i{width, height}
|
|
|
|
})
|
|
|
|
|
|
|
|
err = render.Framewire.Init()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-01-24 22:40:53 +08:00
|
|
|
g.initImgui(win)
|
2022-01-20 21:58:50 +08:00
|
|
|
|
|
|
|
win.SetCursorPos(float64(width)/2, float64(height)/2)
|
|
|
|
|
|
|
|
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
|
|
|
|
g.rotY -= itype.Degrees(float32(deltaX) / 10)
|
|
|
|
g.rotZ -= float32(deltaY) / 10
|
|
|
|
if g.rotZ > 89.99 {
|
|
|
|
g.rotZ = 89.99
|
|
|
|
}
|
|
|
|
if g.rotZ < -89.99 {
|
|
|
|
g.rotZ = -89.99
|
|
|
|
}
|
|
|
|
win.SetCursorPos(centerX, centerY)
|
|
|
|
})
|
|
|
|
|
|
|
|
win.SetMouseButtonCallback(func(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
|
|
|
|
if g.paused {
|
2022-02-10 19:58:51 +08:00
|
|
|
backend.MouseButtonCallback(button, action)
|
2022-01-20 21:58:50 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
win.SetKeyCallback(func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
|
|
|
|
if g.paused {
|
2022-02-10 19:58:51 +08:00
|
|
|
backend.KeyCallback(key, action)
|
2022-01-20 21:58:50 +08:00
|
|
|
}
|
|
|
|
if action == glfw.Press {
|
|
|
|
if g.paused {
|
|
|
|
if key == glfw.KeyEscape && !g.io.WantCaptureKeyboard() {
|
|
|
|
g.paused = false
|
|
|
|
win.SetInputMode(glfw.CursorMode, glfw.CursorDisabled)
|
|
|
|
width, height := w.GetSize()
|
|
|
|
win.SetCursorPos(float64(width)/2, float64(height)/2)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if key == glfw.KeyEscape {
|
|
|
|
g.paused = true
|
|
|
|
win.SetInputMode(glfw.CursorMode, glfw.CursorNormal)
|
|
|
|
width, height := w.GetSize()
|
|
|
|
win.SetCursorPos(float64(width)/2, float64(height)/2)
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 23:31:40 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-01-27 01:09:45 +08:00
|
|
|
win.MakeContextCurrent()
|
|
|
|
glfw.SwapInterval(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if key == glfw.KeyF3 {
|
|
|
|
io.ShowDebugInfo = !io.ShowDebugInfo
|
2022-01-26 23:31:40 +08:00
|
|
|
}
|
2022-01-20 21:58:50 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
win.SetCharCallback(func(w *glfw.Window, char rune) {
|
|
|
|
if g.paused {
|
2022-02-10 19:58:51 +08:00
|
|
|
backend.InputCallback(char)
|
2022-01-20 21:58:50 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
win.SetScrollCallback(func(w *glfw.Window, xpos, ypos float64) {
|
|
|
|
if g.paused {
|
2022-02-10 19:58:51 +08:00
|
|
|
backend.MouseScrollCallback(xpos, ypos)
|
2022-02-24 20:20:33 +08:00
|
|
|
} else {
|
|
|
|
if ypos > 0 {
|
|
|
|
placei--
|
|
|
|
} else if ypos < 0 {
|
|
|
|
placei++
|
|
|
|
}
|
|
|
|
if placei < 0 {
|
|
|
|
placei += len(placeId)
|
|
|
|
}
|
|
|
|
if placei >= len(placeId) {
|
|
|
|
placei -= len(placeId)
|
|
|
|
}
|
2022-01-20 21:58:50 +08:00
|
|
|
}
|
|
|
|
})
|
2022-02-01 23:48:39 +08:00
|
|
|
|
2022-01-20 21:58:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-02-24 20:20:33 +08:00
|
|
|
g.runtime += delta
|
2022-02-10 19:58:51 +08:00
|
|
|
backend.NewFrame()
|
2022-02-21 12:24:05 +08:00
|
|
|
|
|
|
|
clock := util.NewClock()
|
2022-01-20 21:58:50 +08:00
|
|
|
|
|
|
|
if !g.paused {
|
|
|
|
|
|
|
|
if win.GetKey(glfw.KeyLeft) == glfw.Press {
|
|
|
|
g.rotY += itype.Degrees(float32(delta.Seconds()) * 100)
|
|
|
|
}
|
|
|
|
if win.GetKey(glfw.KeyRight) == glfw.Press {
|
|
|
|
g.rotY -= itype.Degrees(float32(delta.Seconds()) * 100)
|
|
|
|
}
|
|
|
|
if win.GetKey(glfw.KeyUp) == glfw.Press {
|
|
|
|
g.rotZ += float32(delta.Seconds()) * 100
|
|
|
|
if g.rotZ > 89.99 {
|
|
|
|
g.rotZ = 89.99
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if win.GetKey(glfw.KeyDown) == glfw.Press {
|
|
|
|
g.rotZ -= float32(delta.Seconds()) * 100
|
|
|
|
if g.rotZ < -89.99 {
|
|
|
|
g.rotZ = -89.99
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//forward := itype.Vec3f(mgl32.Rotate3DY(mgl32.DegToRad(g.rotY)).Mul3(mgl32.Rotate3DZ(mgl32.DegToRad(g.rotZ))).Mul3x1(mgl32.Vec3{1, 0, 0})).ToFloat64().Multiply(delta.Seconds()*8)
|
|
|
|
var walkaccel float64
|
|
|
|
if g.player.OnGround() {
|
|
|
|
walkaccel = 48
|
|
|
|
} else {
|
|
|
|
walkaccel = 16
|
|
|
|
}
|
|
|
|
|
|
|
|
forward := itype.Vec3d(mgl64.Rotate3DY(float64(g.rotY.Radians())).Mul3x1(mgl64.Vec3{1, 0, 0}))
|
|
|
|
right := forward.Cross(itype.Vec3d{0, 1, 0})
|
|
|
|
accel := itype.Vec3d{0, 0, 0}
|
|
|
|
if win.GetKey(glfw.KeyW) == glfw.Press {
|
|
|
|
accel = accel.Add(forward.Multiply(walkaccel * delta.Seconds()))
|
|
|
|
}
|
|
|
|
if win.GetKey(glfw.KeyS) == glfw.Press {
|
|
|
|
accel = accel.Add(forward.Multiply(walkaccel * delta.Seconds()).Negative())
|
|
|
|
}
|
|
|
|
if win.GetKey(glfw.KeyD) == glfw.Press {
|
|
|
|
accel = accel.Add(right.Multiply(walkaccel * delta.Seconds()))
|
|
|
|
}
|
|
|
|
if win.GetKey(glfw.KeyA) == glfw.Press {
|
|
|
|
accel = accel.Add(right.Multiply(walkaccel * delta.Seconds()).Negative())
|
|
|
|
}
|
|
|
|
|
|
|
|
if win.GetKey(glfw.KeySpace) == glfw.Press && g.player.OnGround() {
|
|
|
|
//log.Print("Jump!")
|
|
|
|
accel = accel.Addv(0, 8, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
g.player.Accelerate(accel[0], accel[1], accel[2])
|
|
|
|
}
|
|
|
|
|
|
|
|
g.player.Update(g.world, delta)
|
|
|
|
|
|
|
|
g.view.LookAt(g.player.EyePosition().ToFloat32(), g.rotY, itype.Degrees(g.rotZ))
|
2022-01-27 01:09:45 +08:00
|
|
|
io.ViewPos = g.player.EyePosition()
|
|
|
|
io.ViewDir = itype.Vec3d(mgl64.Rotate3DY(float64(g.rotY.Radians())).Mul3(mgl64.Rotate3DZ(float64(itype.Degrees(g.rotZ)))).Mul3x1(mgl64.Vec3{1, 0, 0}))
|
2022-01-20 21:58:50 +08:00
|
|
|
|
2022-02-01 23:48:39 +08:00
|
|
|
render.Framewire.PushBox(g.player.WorldHitbox()[0].ToFloat32(), color.White)
|
2022-01-20 21:58:50 +08:00
|
|
|
|
|
|
|
if g.player.Position()[1] < -100 {
|
|
|
|
g.player.SetPosition(itype.Vec3d{18, 80, 18})
|
|
|
|
g.player.SetSpeed(itype.Vec3d{})
|
|
|
|
}
|
|
|
|
|
2022-02-24 20:20:33 +08:00
|
|
|
if ok, bc, dir, _, _ := g.world.CastViewRay(io.ViewPos, io.ViewDir.Normalize(), 6); ok {
|
|
|
|
ba := g.world.Block(bc).Appearance(bc)
|
|
|
|
for _, r := range ba.Lookbox {
|
|
|
|
render.Framewire.PushBox(r.GrowEven(itype.Vec3d{0.03125, 0.03125, 0.03125}).Offset(bc.ToFloat64()).ToFloat32(), color.White)
|
|
|
|
}
|
|
|
|
|
2022-02-25 21:15:44 +08:00
|
|
|
if !g.paused {
|
|
|
|
// Break/Place block
|
|
|
|
if win.GetMouseButton(glfw.MouseButtonLeft) == glfw.Press && g.runtime-time.Duration(g.player.DatasetI("LastBreak")) >= PlayerBreakCooldown {
|
|
|
|
// Break
|
|
|
|
g.world.Break(bc)
|
|
|
|
g.player.SetDatasetI("LastBreak", int64(g.runtime))
|
|
|
|
} else if win.GetMouseButton(glfw.MouseButtonRight) == glfw.Press && g.runtime-time.Duration(g.player.DatasetI("LastPlace")) >= PlayerBreakCooldown {
|
|
|
|
// Place
|
|
|
|
// Check hitbox
|
|
|
|
tobehit := world.GetBlockBehaviour(placeId[placei]).Appearance(bc.Add(itype.DirectionVeci[dir]), 0, nil, g.world).Hitbox
|
|
|
|
if len(tobehit) == 0 {
|
|
|
|
tobehit = []itype.Boxd{{OffX: 0, OffY: 0, OffZ: 0, SizeX: 1, SizeY: 1, SizeZ: 1}}
|
|
|
|
}
|
|
|
|
canplace := true
|
|
|
|
outer:
|
|
|
|
for _, pb := range g.player.WorldHitbox() {
|
|
|
|
for _, b := range tobehit {
|
|
|
|
if ok, _ := b.Offset(bc.Add(itype.DirectionVeci[dir]).ToFloat64()).Intersect(pb); ok {
|
|
|
|
canplace = false
|
|
|
|
break outer
|
|
|
|
}
|
2022-02-24 20:20:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 21:15:44 +08:00
|
|
|
if canplace {
|
2022-10-12 08:24:00 +08:00
|
|
|
aux := placeAux[placei]
|
|
|
|
if win.GetKey(glfw.KeyLeftShift) == glfw.Press {
|
|
|
|
aux = -aux
|
|
|
|
}
|
|
|
|
g.world.SetBlock(bc.Add(itype.DirectionVeci[dir]), placeId[placei], aux, nil)
|
2022-02-25 21:15:44 +08:00
|
|
|
g.player.SetDatasetI("LastPlace", int64(g.runtime))
|
|
|
|
}
|
2022-02-24 20:20:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:24:05 +08:00
|
|
|
io.Diagnostics.Times.Logic = clock.Restart()
|
|
|
|
|
2022-01-24 22:40:53 +08:00
|
|
|
g.imgui()
|
2022-02-21 12:24:05 +08:00
|
|
|
io.Diagnostics.Times.GUI = clock.Restart()
|
2022-01-20 21:58:50 +08:00
|
|
|
}
|