place blocks

This commit is contained in:
2022-02-24 20:20:33 +08:00
parent 9195dd7c3f
commit 32b06810e2
17 changed files with 309 additions and 55 deletions

View File

@@ -1,6 +1,8 @@
package game
import (
"time"
"edgaru089.ml/go/gl01/internal/entity"
"edgaru089.ml/go/gl01/internal/render"
"edgaru089.ml/go/gl01/internal/util/itype"
@@ -28,6 +30,8 @@ type Game struct {
fullscreen bool
lastPos, lastSize itype.Vec2i // Window size before entering fullscreen
runtime time.Duration
io imgui.IO
gui guiState
paused bool

View File

@@ -74,6 +74,11 @@ func (g *Game) imgui() {
pos := g.player.Position()
igwrap.TextBackground("Player: (%.3f, %.5f, %.3f) (Y%.2f, Z%.2f)", pos[0], pos[1], pos[2], g.rotY.Degrees(), g.rotZ)
if ok, bc, face, _, _ := g.world.CastViewRay(io.ViewPos, io.ViewDir.Normalize(), 10); ok {
igwrap.TextBackground("Looking At: (%d %d %d) facing %s", bc[0], bc[1], bc[2], itype.DirectionName[face])
}
imgui.End()
}
}
@@ -138,4 +143,43 @@ func (g *Game) imgui() {
}
imgui.End()
imgui.SetNextWindowPosV(imgui.Vec2{float32(io.DisplaySize[0] / 2), float32(io.DisplaySize[1]) + 1}, imgui.ConditionAlways, imgui.Vec2{0.5, 1})
if igwrap.Begin("InventoryBar", nil, imgui.WindowFlagsAlwaysAutoResize|imgui.WindowFlagsNoNavFocus|imgui.WindowFlagsNoNavInputs|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoBringToFrontOnFocus|imgui.WindowFlagsNoSavedSettings|imgui.WindowFlagsNoFocusOnAppearing) {
imgui.PushStyleColor(imgui.StyleColorBorder, imgui.Vec4{0.8, 0.8, 0.8, 1})
for i, id := range placeId {
if i != 0 {
imgui.SameLineV(0, 1)
}
if i == placei {
imgui.PushStyleVarFloat(imgui.StyleVarFrameBorderSize, 2)
}
app := world.GetBlockBehaviour(id).Appearance(itype.Vec3i{}, 0, nil, g.world)
var name string
switch app.RenderType {
case world.OneTexture:
name = app.Name + ".png"
case world.ThreeTexture:
name = app.Name + "_top.png"
case world.SixTexture:
name = app.Name + "_y+.png"
}
igwrap.ImageButtonV(
g.render.texture.Handle(),
itype.Vec2f{32, 32},
asset.WorldTextureAtlas.RectNormalized(name),
0, itype.Vec4f{}, itype.Vec4f{1, 1, 1, 1},
)
if i == placei {
imgui.PopStyleVar()
}
}
imgui.PopStyleColor()
imgui.End()
}
}

View File

@@ -12,12 +12,21 @@ import (
"edgaru089.ml/go/gl01/internal/util"
"edgaru089.ml/go/gl01/internal/util/itype"
"edgaru089.ml/go/gl01/internal/world"
"edgaru089.ml/go/gl01/internal/world/blocks"
"edgaru089.ml/go/gl01/internal/world/worldgen"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/go-gl/mathgl/mgl64"
"github.com/inkyblackness/imgui-go/v4"
)
const (
PlayerBreakCooldown = 200 * time.Millisecond
PlayerPlaceCooldown = 200 * time.Millisecond
)
var placeId = [10]int{blocks.Stone, blocks.Dirt, blocks.Grass, blocks.LogOak, blocks.PlanksOak, blocks.LeavesOak, blocks.Debug, blocks.DebugDir, blocks.Bedrock, blocks.Water}
var placei = 0
var logs string
type logger struct{}
@@ -43,6 +52,9 @@ func (g *Game) Init(win *glfw.Window) {
panic(err)
}
g.player.SetDatasetI("LastBreak", 0)
g.player.SetDatasetI("LastPlace", 0)
var seed int64 = time.Now().Unix()
gensync := make(chan struct{})
gensynccnt := 0
@@ -163,6 +175,18 @@ func (g *Game) Init(win *glfw.Window) {
win.SetScrollCallback(func(w *glfw.Window, xpos, ypos float64) {
if g.paused {
backend.MouseScrollCallback(xpos, ypos)
} else {
if ypos > 0 {
placei--
} else if ypos < 0 {
placei++
}
if placei < 0 {
placei += len(placeId)
}
if placei >= len(placeId) {
placei -= len(placeId)
}
}
})
@@ -172,6 +196,7 @@ 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()
@@ -242,6 +267,42 @@ func (g *Game) Update(win *glfw.Window, delta time.Duration) {
g.player.SetSpeed(itype.Vec3d{})
}
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)
}
// 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
}
}
}
if canplace {
g.world.SetBlock(bc.Add(itype.DirectionVeci[dir]), placeId[placei], 0, nil)
g.player.SetDatasetI("LastPlace", int64(g.runtime))
}
}
}
io.Diagnostics.Times.Logic = clock.Restart()
imgui.ShowDemoWindow(nil)

View File

@@ -479,4 +479,5 @@ func (g *Game) Render(win *glfw.Window) {
}
backend.Render(win)
}