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

@@ -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)