Initial commit
This commit is contained in:
49
internal/game/game.go
Normal file
49
internal/game/game.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"edgaru089.ml/go/gl01/internal/entity"
|
||||
"edgaru089.ml/go/gl01/internal/render"
|
||||
"edgaru089.ml/go/gl01/internal/util/itype"
|
||||
"edgaru089.ml/go/gl01/internal/world"
|
||||
"github.com/inkyblackness/imgui-go/v4"
|
||||
|
||||
_ "edgaru089.ml/go/gl01/internal/entity/entities"
|
||||
)
|
||||
|
||||
// Game holds a game scene.
|
||||
type Game struct {
|
||||
world *world.World
|
||||
|
||||
player *entity.Entity
|
||||
|
||||
view *render.View
|
||||
worldrender *render.WorldRenderer
|
||||
|
||||
prevCursorPos itype.Vec2d
|
||||
cameraPos itype.Vec3d
|
||||
rotY itype.Angle
|
||||
rotZ float32 // Degrees in range (-90, 90)
|
||||
|
||||
fbSize itype.Vec2i
|
||||
|
||||
io imgui.IO
|
||||
paused bool
|
||||
}
|
||||
|
||||
// NewGame creates a new, empty Game.
|
||||
func NewGame() (g *Game) {
|
||||
return &Game{
|
||||
world: world.NewWorld(),
|
||||
player: entity.NewEntity("player", itype.Vec3d{18, 80, 18}),
|
||||
worldrender: &render.WorldRenderer{},
|
||||
cameraPos: itype.Vec3d{18, 80, 18},
|
||||
rotY: 0,
|
||||
rotZ: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// LoadGameFromPath loads a saved game from a filesystem folder in a read-write fashion.
|
||||
func LoadGameFromPath(path string) (g *Game, err error) {
|
||||
|
||||
return
|
||||
}
|
333
internal/game/logic.go
Normal file
333
internal/game/logic.go
Normal file
@@ -0,0 +1,333 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image/color"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"edgaru089.ml/go/gl01/internal/asset"
|
||||
"edgaru089.ml/go/gl01/internal/igwrap"
|
||||
"edgaru089.ml/go/gl01/internal/io"
|
||||
"edgaru089.ml/go/gl01/internal/render"
|
||||
"edgaru089.ml/go/gl01/internal/util/itype"
|
||||
"edgaru089.ml/go/gl01/internal/world"
|
||||
"edgaru089.ml/go/gl01/internal/world/worldgen"
|
||||
"github.com/go-gl/gl/all-core/gl"
|
||||
"github.com/go-gl/glfw/v3.3/glfw"
|
||||
"github.com/go-gl/mathgl/mgl64"
|
||||
"github.com/inkyblackness/imgui-go/v4"
|
||||
)
|
||||
|
||||
var logs string
|
||||
|
||||
type actions struct {
|
||||
loadChunkFile string
|
||||
loadChunkID [2]int32
|
||||
|
||||
saveChunkFile string
|
||||
saveChunkID [2]int32
|
||||
}
|
||||
|
||||
var action *actions
|
||||
var logFollow *bool
|
||||
|
||||
func init() {
|
||||
action = &actions{
|
||||
loadChunkFile: "chunk.gob",
|
||||
loadChunkID: [2]int32{0, 0},
|
||||
saveChunkFile: "chunk.gob",
|
||||
saveChunkID: [2]int32{0, 0},
|
||||
}
|
||||
logFollow = new(bool)
|
||||
(*logFollow) = true
|
||||
}
|
||||
|
||||
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{}
|
||||
g.worldrender = &render.WorldRenderer{}
|
||||
|
||||
err := g.worldrender.Init(g.world)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var seed int64 = time.Now().Unix()
|
||||
gensync := make(chan struct{})
|
||||
gensynccnt := 0
|
||||
for i := -8; i <= 8; i++ {
|
||||
for j := -8; j <= 8; j++ {
|
||||
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) {
|
||||
win.SetCursorPos(float64(width)/2, float64(height)/2)
|
||||
g.view.Aspect(float32(width) / float32(height))
|
||||
io.DisplaySize = itype.Vec2i{width, height}
|
||||
})
|
||||
|
||||
err = render.Framewire.Init()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
imgui.CreateContext(nil)
|
||||
g.io = imgui.CurrentIO()
|
||||
|
||||
cfg := imgui.NewFontConfig()
|
||||
cfg.SetOversampleH(1)
|
||||
cfg.SetOversampleV(1)
|
||||
cfg.SetPixelSnapH(true)
|
||||
g.io.Fonts().AddFontFromMemoryTTFV(asset.Unifont, 16, cfg, g.io.Fonts().GlyphRangesChineseFull())
|
||||
igwrap.Init(win)
|
||||
|
||||
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 {
|
||||
igwrap.MouseButtonCallback(button, action)
|
||||
}
|
||||
})
|
||||
|
||||
win.SetKeyCallback(func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
|
||||
if g.paused {
|
||||
igwrap.KeyCallback(key, action)
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
win.SetCharCallback(func(w *glfw.Window, char rune) {
|
||||
if g.paused {
|
||||
igwrap.InputCallback(char)
|
||||
}
|
||||
})
|
||||
|
||||
win.SetScrollCallback(func(w *glfw.Window, xpos, ypos float64) {
|
||||
if g.paused {
|
||||
igwrap.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) {
|
||||
igwrap.NewFrame()
|
||||
imgui.ShowDemoWindow(nil)
|
||||
|
||||
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))
|
||||
|
||||
render.Framewire.PushBox(g.player.WorldHitbox().ToFloat32(), color.White)
|
||||
|
||||
if g.player.Position()[1] < -100 {
|
||||
g.player.SetPosition(itype.Vec3d{18, 80, 18})
|
||||
g.player.SetSpeed(itype.Vec3d{})
|
||||
}
|
||||
|
||||
if imgui.BeginV("Player", nil, imgui.WindowFlagsAlwaysAutoResize) {
|
||||
pos := g.player.Position()
|
||||
vel := g.player.Speed()
|
||||
imgui.Text(fmt.Sprintf("Pos: (%.5f, %.5f, %.5f), Vel: (%.5f, %.5f, %.5f)", pos[0], pos[1], pos[2], vel[0], vel[1], vel[2]))
|
||||
}
|
||||
imgui.End()
|
||||
|
||||
if imgui.BeginV("Go Runtime", nil, imgui.WindowFlagsAlwaysAutoResize) {
|
||||
imgui.Text(fmt.Sprintf("%s/%s, compiler: %s", runtime.GOOS, runtime.GOARCH, runtime.Compiler))
|
||||
imgui.Text(fmt.Sprintf("NumCPU=%d, NumGOMAXPROCS=%d", runtime.NumCPU(), runtime.GOMAXPROCS(0)))
|
||||
imgui.Text(fmt.Sprintf("NumCgoCalls=%d, NumGoroutine=%d", runtime.NumCgoCall(), runtime.NumGoroutine()))
|
||||
imgui.Spacing()
|
||||
if imgui.ButtonV("!!! PANIC !!!", imgui.Vec2{X: -2, Y: 0}) {
|
||||
panic("Manual Panic")
|
||||
}
|
||||
}
|
||||
imgui.End()
|
||||
|
||||
if imgui.BeginV("Logs", nil, imgui.WindowFlagsMenuBar) {
|
||||
if imgui.BeginMenuBar() {
|
||||
if imgui.Button("Clear") {
|
||||
logs = ""
|
||||
}
|
||||
if imgui.RadioButton("Follow", *logFollow) {
|
||||
(*logFollow) = !(*logFollow)
|
||||
}
|
||||
imgui.EndMenuBar()
|
||||
}
|
||||
|
||||
var size *float32
|
||||
if size == nil {
|
||||
size = new(float32)
|
||||
}
|
||||
imgui.BeginChildV("LogScroll", imgui.Vec2{}, true, 0)
|
||||
imgui.Text(logs)
|
||||
if (*size) != imgui.ScrollMaxY() && (*logFollow) {
|
||||
imgui.SetScrollY(imgui.ScrollMaxY())
|
||||
}
|
||||
(*size) = imgui.ScrollMaxY()
|
||||
imgui.EndChild()
|
||||
}
|
||||
imgui.End()
|
||||
|
||||
if imgui.Begin("Actions") {
|
||||
|
||||
imgui.Text("Chunks")
|
||||
imgui.Separator()
|
||||
imgui.InputText("Load Filename", &action.loadChunkFile)
|
||||
imgui.SliderInt2("Load ID", &action.loadChunkID, -10, 10)
|
||||
if imgui.ButtonV("Load", imgui.Vec2{X: -2, Y: 0}) {
|
||||
c := &world.Chunk{}
|
||||
f, err := os.Open(action.loadChunkFile)
|
||||
if err != nil {
|
||||
log.Print("LoadChunk: ", err)
|
||||
} else {
|
||||
c.LoadFromGobIndexed(f, int(action.loadChunkID[0]), int(action.loadChunkID[1]))
|
||||
g.world.SetChunk(int(action.loadChunkID[0]), int(action.loadChunkID[1]), c)
|
||||
}
|
||||
}
|
||||
imgui.Separator()
|
||||
imgui.InputText("Save Filename", &action.saveChunkFile)
|
||||
imgui.SliderInt2("Save ID", &action.saveChunkID, -10, 10)
|
||||
if imgui.ButtonV("Save", imgui.Vec2{X: -2, Y: 0}) {
|
||||
c := g.world.Chunks[itype.Vec2i{int(action.saveChunkID[0]), int(action.saveChunkID[1])}]
|
||||
f, _ := os.Create(action.saveChunkFile)
|
||||
c.WriteToGob(f)
|
||||
f.Close()
|
||||
}
|
||||
imgui.Separator()
|
||||
|
||||
}
|
||||
imgui.End()
|
||||
}
|
||||
|
||||
// Render, called with a OpenGL context, renders the game.
|
||||
func (g *Game) Render(win *glfw.Window) {
|
||||
gl.Viewport(0, 0, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]))
|
||||
g.worldrender.Render(g.world, g.view)
|
||||
render.Framewire.Render(g.view)
|
||||
|
||||
igwrap.Render(win)
|
||||
}
|
115
internal/game/logic.go.old
Executable file
115
internal/game/logic.go.old
Executable file
@@ -0,0 +1,115 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/Edgaru089/gl01/internal/render"
|
||||
"github.com/Edgaru089/gl01/internal/util/itype"
|
||||
"github.com/Edgaru089/gl01/internal/world"
|
||||
"github.com/Edgaru089/gl01/internal/world/worldgen"
|
||||
"github.com/go-gl/gl/all-core/gl"
|
||||
"github.com/go-gl/glfw/v3.3/glfw"
|
||||
"github.com/go-gl/mathgl/mgl32"
|
||||
)
|
||||
|
||||
// Init initializes the game.
|
||||
func (g *Game) Init(win *glfw.Window) {
|
||||
|
||||
g.world = world.NewWorld()
|
||||
g.view = &render.View{}
|
||||
g.worldrender = &render.WorldRenderer{}
|
||||
|
||||
var seed int64 = time.Now().Unix()
|
||||
for i := -2; i <= 3; i++ {
|
||||
for j := -2; j <= 3; j++ {
|
||||
c := &world.Chunk{}
|
||||
g.world.SetChunk(i, j, c)
|
||||
worldgen.Chunk(c, g.world, seed)
|
||||
}
|
||||
}
|
||||
|
||||
width, height := win.GetSize()
|
||||
g.view.Aspect(float32(width)/float32(height)).FovY(60).LookAt(g.cameraPos.ToFloat32(), g.rotY, g.rotZ)
|
||||
|
||||
err := g.worldrender.Init(g.world)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
g.fbSize[0], g.fbSize[1] = win.GetFramebufferSize()
|
||||
win.SetSizeCallback(func(w *glfw.Window, width, height int) {
|
||||
win.SetCursorPos(float64(width)/2, float64(height)/2)
|
||||
g.view.Aspect(float32(width) / float32(height))
|
||||
g.fbSize[0], g.fbSize[1] = w.GetFramebufferSize()
|
||||
})
|
||||
|
||||
err = render.Framewire.Init()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
win.SetCursorPos(float64(width)/2, float64(height)/2)
|
||||
|
||||
win.SetInputMode(glfw.CursorMode, glfw.CursorDisabled)
|
||||
win.SetCursorPosCallback(func(w *glfw.Window, xpos, ypos float64) {
|
||||
width, height := w.GetSize()
|
||||
centerX, centerY := float64(width)/2, float64(height)/2
|
||||
deltaX, deltaY := xpos-centerX, ypos-centerY
|
||||
g.rotY -= float32(deltaX / 10)
|
||||
g.rotZ -= float32(deltaY / 10)
|
||||
if g.rotZ > 89.9 {
|
||||
g.rotZ = 89.9
|
||||
}
|
||||
if g.rotZ < -89.9 {
|
||||
g.rotZ = -89.9
|
||||
}
|
||||
win.SetCursorPos(centerX, centerY)
|
||||
})
|
||||
}
|
||||
|
||||
// Update updates the game state, not necessarily in the main thread.
|
||||
func (g *Game) Update(win *glfw.Window, delta time.Duration) {
|
||||
if win.GetKey(glfw.KeyLeft) == glfw.Press {
|
||||
g.rotY += float32(delta.Seconds()) * 100
|
||||
}
|
||||
if win.GetKey(glfw.KeyRight) == glfw.Press {
|
||||
g.rotY -= float32(delta.Seconds()) * 100
|
||||
}
|
||||
if win.GetKey(glfw.KeyUp) == glfw.Press {
|
||||
g.rotZ += float32(delta.Seconds()) * 100
|
||||
if g.rotZ > 89.9 {
|
||||
g.rotZ = 89.9
|
||||
}
|
||||
}
|
||||
if win.GetKey(glfw.KeyDown) == glfw.Press {
|
||||
g.rotZ -= float32(delta.Seconds()) * 100
|
||||
if g.rotZ < -89.9 {
|
||||
g.rotZ = -89.9
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
right := forward.Cross(itype.Vec3d{0, 1, 0})
|
||||
right = right.Multiply(1 / right.Length()).Multiply(delta.Seconds() * 8)
|
||||
if win.GetKey(glfw.KeyW) == glfw.Press {
|
||||
g.cameraPos = g.cameraPos.Add(forward)
|
||||
}
|
||||
if win.GetKey(glfw.KeyS) == glfw.Press {
|
||||
g.cameraPos = g.cameraPos.Add(forward.Negative())
|
||||
}
|
||||
if win.GetKey(glfw.KeyD) == glfw.Press {
|
||||
g.cameraPos = g.cameraPos.Add(right)
|
||||
}
|
||||
if win.GetKey(glfw.KeyA) == glfw.Press {
|
||||
g.cameraPos = g.cameraPos.Add(right.Negative())
|
||||
}
|
||||
|
||||
g.view.LookAt(g.cameraPos.ToFloat32(), g.rotY, g.rotZ)
|
||||
}
|
||||
|
||||
// Render, called with a OpenGL context, renders the game.
|
||||
func (g *Game) Render() {
|
||||
gl.Viewport(0, 0, int32(g.fbSize[0]), int32(g.fbSize[1]))
|
||||
g.worldrender.Render(g.world, g.view)
|
||||
render.Framewire.Render(g.view)
|
||||
}
|
285
internal/game/logic.go.old2
Executable file
285
internal/game/logic.go.old2
Executable file
@@ -0,0 +1,285 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image/color"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/Edgaru089/gl01/internal/asset"
|
||||
"github.com/Edgaru089/gl01/internal/igwrap"
|
||||
"github.com/Edgaru089/gl01/internal/render"
|
||||
"github.com/Edgaru089/gl01/internal/util"
|
||||
"github.com/Edgaru089/gl01/internal/util/itype"
|
||||
"github.com/Edgaru089/gl01/internal/world"
|
||||
"github.com/Edgaru089/gl01/internal/world/worldgen"
|
||||
"github.com/go-gl/gl/all-core/gl"
|
||||
"github.com/go-gl/glfw/v3.3/glfw"
|
||||
"github.com/go-gl/mathgl/mgl64"
|
||||
"github.com/inkyblackness/imgui-go/v4"
|
||||
)
|
||||
|
||||
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{}
|
||||
g.worldrender = &render.WorldRenderer{}
|
||||
|
||||
var seed int64 = time.Now().Unix()
|
||||
gensync := make(chan struct{})
|
||||
gensynccnt := 0
|
||||
for i := -1; i <= 1; i++ {
|
||||
for j := -1; j <= 1; j++ {
|
||||
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(60).LookAt(g.cameraPos.ToFloat32(), g.rotY, g.rotZ)
|
||||
|
||||
err := g.worldrender.Init(g.world)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
g.fbSize[0], g.fbSize[1] = win.GetFramebufferSize()
|
||||
win.SetSizeCallback(func(w *glfw.Window, width, height int) {
|
||||
win.SetCursorPos(float64(width)/2, float64(height)/2)
|
||||
g.view.Aspect(float32(width) / float32(height))
|
||||
g.fbSize[0], g.fbSize[1] = w.GetFramebufferSize()
|
||||
})
|
||||
|
||||
err = render.Framewire.Init()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
imgui.CreateContext(nil)
|
||||
g.io = imgui.CurrentIO()
|
||||
|
||||
cfg := imgui.NewFontConfig()
|
||||
cfg.SetOversampleH(1)
|
||||
cfg.SetOversampleV(1)
|
||||
cfg.SetPixelSnapH(true)
|
||||
g.io.Fonts().AddFontFromMemoryTTFV(asset.Unifont, 16, cfg, g.io.Fonts().GlyphRangesChineseFull())
|
||||
igwrap.Init(win)
|
||||
|
||||
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 -= float32(deltaX / 10)
|
||||
g.rotZ -= float32(deltaY / 10)
|
||||
if g.rotZ > 89.9 {
|
||||
g.rotZ = 89.9
|
||||
}
|
||||
if g.rotZ < -89.9 {
|
||||
g.rotZ = -89.9
|
||||
}
|
||||
g.rotY = util.FloatModf(g.rotY, 360)
|
||||
win.SetCursorPos(centerX, centerY)
|
||||
})
|
||||
|
||||
win.SetMouseButtonCallback(func(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
|
||||
if g.paused {
|
||||
igwrap.MouseButtonCallback(button, action)
|
||||
}
|
||||
})
|
||||
|
||||
win.SetKeyCallback(func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
|
||||
if g.paused {
|
||||
igwrap.KeyCallback(key, action)
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
win.SetCharCallback(func(w *glfw.Window, char rune) {
|
||||
if g.paused {
|
||||
igwrap.InputCallback(char)
|
||||
}
|
||||
})
|
||||
|
||||
win.SetScrollCallback(func(w *glfw.Window, xpos, ypos float64) {
|
||||
if g.paused {
|
||||
igwrap.MouseScrollCallback(xpos, ypos)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Update updates the game state, not necessarily in the main thread.
|
||||
func (g *Game) Update(win *glfw.Window, delta time.Duration) {
|
||||
igwrap.NewFrame()
|
||||
imgui.ShowDemoWindow(nil)
|
||||
|
||||
if !g.paused {
|
||||
|
||||
if win.GetKey(glfw.KeyLeft) == glfw.Press {
|
||||
g.rotY += float32(delta.Seconds()) * 100
|
||||
}
|
||||
if win.GetKey(glfw.KeyRight) == glfw.Press {
|
||||
g.rotY -= float32(delta.Seconds()) * 100
|
||||
}
|
||||
if win.GetKey(glfw.KeyUp) == glfw.Press {
|
||||
g.rotZ += float32(delta.Seconds()) * 100
|
||||
if g.rotZ > 89.9 {
|
||||
g.rotZ = 89.9
|
||||
}
|
||||
}
|
||||
if win.GetKey(glfw.KeyDown) == glfw.Press {
|
||||
g.rotZ -= float32(delta.Seconds()) * 100
|
||||
if g.rotZ < -89.9 {
|
||||
g.rotZ = -89.9
|
||||
}
|
||||
}
|
||||
|
||||
//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 = 8
|
||||
}
|
||||
|
||||
forward := itype.Vec3d(mgl64.Rotate3DY(mgl64.DegToRad(float64(g.rotY))).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, g.rotZ)
|
||||
|
||||
render.Framewire.PushBox(g.player.WorldHitbox().ToFloat32(), color.White)
|
||||
|
||||
if g.player.Position()[1] < -100 {
|
||||
g.player.SetPosition(itype.Vec3d{18, 80, 18})
|
||||
g.player.SetSpeed(itype.Vec3d{})
|
||||
}
|
||||
|
||||
if imgui.BeginV("Player", nil, imgui.WindowFlagsAlwaysAutoResize) {
|
||||
pos := g.player.Position()
|
||||
vel := g.player.Speed()
|
||||
imgui.Text(fmt.Sprintf("Pos: (%.5f, %.5f, %.5f), Vel: (%.5f, %.5f, %.5f)", pos[0], pos[1], pos[2], vel[0], vel[1], vel[2]))
|
||||
}
|
||||
imgui.End()
|
||||
|
||||
if imgui.BeginV("Go Runtime", nil, imgui.WindowFlagsAlwaysAutoResize) {
|
||||
imgui.Text(fmt.Sprintf("%s/%s, compiler: %s", runtime.GOOS, runtime.GOARCH, runtime.Compiler))
|
||||
imgui.Text(fmt.Sprintf("NumCPU=%d, NumGOMAXPROCS=%d", runtime.NumCPU(), runtime.GOMAXPROCS(0)))
|
||||
imgui.Text(fmt.Sprintf("NumCgoCalls=%d, NumGoroutine=%d", runtime.NumCgoCall(), runtime.NumGoroutine()))
|
||||
imgui.Spacing()
|
||||
if imgui.ButtonV("!!! PANIC !!!", imgui.Vec2{X: -2, Y: 0}) {
|
||||
panic("Manual Panic")
|
||||
}
|
||||
}
|
||||
imgui.End()
|
||||
|
||||
var logFollow *bool
|
||||
if logFollow == nil {
|
||||
logFollow = new(bool)
|
||||
(*logFollow) = true
|
||||
}
|
||||
if imgui.BeginV("Logs", nil, imgui.WindowFlagsMenuBar) {
|
||||
if imgui.BeginMenuBar() {
|
||||
if imgui.Button("Clear") {
|
||||
logs = ""
|
||||
}
|
||||
if imgui.RadioButton("Follow", *logFollow) {
|
||||
(*logFollow) = !(*logFollow)
|
||||
}
|
||||
imgui.EndMenuBar()
|
||||
}
|
||||
|
||||
var size *float32
|
||||
if size == nil {
|
||||
size = new(float32)
|
||||
}
|
||||
imgui.BeginChildV("LogScroll", imgui.Vec2{}, true, 0)
|
||||
imgui.Text(logs)
|
||||
if (*size) != imgui.ScrollMaxY() && (*logFollow) {
|
||||
imgui.SetScrollY(imgui.ScrollMaxY())
|
||||
}
|
||||
(*size) = imgui.ScrollMaxY()
|
||||
imgui.EndChild()
|
||||
}
|
||||
imgui.End()
|
||||
}
|
||||
|
||||
// Render, called with a OpenGL context, renders the game.
|
||||
func (g *Game) Render(win *glfw.Window) {
|
||||
gl.Viewport(0, 0, int32(g.fbSize[0]), int32(g.fbSize[1]))
|
||||
g.worldrender.Render(g.world, g.view)
|
||||
render.Framewire.Render(g.view)
|
||||
|
||||
igwrap.Render(win)
|
||||
}
|
Reference in New Issue
Block a user