place blocks
This commit is contained in:
parent
9195dd7c3f
commit
32b06810e2
Before Width: | Height: | Size: 528 B After Width: | Height: | Size: 528 B |
@ -64,6 +64,7 @@ func NewEntity(typename string, pos itype.Vec3d) *Entity {
|
|||||||
b: b,
|
b: b,
|
||||||
pos: pos,
|
pos: pos,
|
||||||
name: typename,
|
name: typename,
|
||||||
|
ds: make(itype.Dataset),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,3 +107,29 @@ func (e *Entity) EyeHeight() float64 {
|
|||||||
func (e *Entity) EyePosition() itype.Vec3d {
|
func (e *Entity) EyePosition() itype.Vec3d {
|
||||||
return e.pos.Addv(0, e.EyeHeight(), 0)
|
return e.pos.Addv(0, e.EyeHeight(), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Entity) DatasetI(name string) int64 {
|
||||||
|
return e.ds[name].(int64)
|
||||||
|
}
|
||||||
|
func (e *Entity) DatasetF(name string) float64 {
|
||||||
|
return e.ds[name].(float64)
|
||||||
|
}
|
||||||
|
func (e *Entity) DatasetS(name string) string {
|
||||||
|
return e.ds[name].(string)
|
||||||
|
}
|
||||||
|
func (e *Entity) DatasetB(name string) bool {
|
||||||
|
return e.ds[name].(bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Entity) SetDatasetI(name string, val int64) {
|
||||||
|
e.ds[name] = val
|
||||||
|
}
|
||||||
|
func (e *Entity) SetDatasetF(name string, val float64) {
|
||||||
|
e.ds[name] = val
|
||||||
|
}
|
||||||
|
func (e *Entity) SetDatasetS(name string, val string) {
|
||||||
|
e.ds[name] = val
|
||||||
|
}
|
||||||
|
func (e *Entity) SetDatasetB(name string, val bool) {
|
||||||
|
e.ds[name] = val
|
||||||
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package game
|
package game
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"edgaru089.ml/go/gl01/internal/entity"
|
"edgaru089.ml/go/gl01/internal/entity"
|
||||||
"edgaru089.ml/go/gl01/internal/render"
|
"edgaru089.ml/go/gl01/internal/render"
|
||||||
"edgaru089.ml/go/gl01/internal/util/itype"
|
"edgaru089.ml/go/gl01/internal/util/itype"
|
||||||
@ -28,6 +30,8 @@ type Game struct {
|
|||||||
fullscreen bool
|
fullscreen bool
|
||||||
lastPos, lastSize itype.Vec2i // Window size before entering fullscreen
|
lastPos, lastSize itype.Vec2i // Window size before entering fullscreen
|
||||||
|
|
||||||
|
runtime time.Duration
|
||||||
|
|
||||||
io imgui.IO
|
io imgui.IO
|
||||||
gui guiState
|
gui guiState
|
||||||
paused bool
|
paused bool
|
||||||
|
@ -74,6 +74,11 @@ func (g *Game) imgui() {
|
|||||||
|
|
||||||
pos := g.player.Position()
|
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)
|
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()
|
imgui.End()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -138,4 +143,43 @@ func (g *Game) imgui() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
imgui.End()
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,12 +12,21 @@ import (
|
|||||||
"edgaru089.ml/go/gl01/internal/util"
|
"edgaru089.ml/go/gl01/internal/util"
|
||||||
"edgaru089.ml/go/gl01/internal/util/itype"
|
"edgaru089.ml/go/gl01/internal/util/itype"
|
||||||
"edgaru089.ml/go/gl01/internal/world"
|
"edgaru089.ml/go/gl01/internal/world"
|
||||||
|
"edgaru089.ml/go/gl01/internal/world/blocks"
|
||||||
"edgaru089.ml/go/gl01/internal/world/worldgen"
|
"edgaru089.ml/go/gl01/internal/world/worldgen"
|
||||||
"github.com/go-gl/glfw/v3.3/glfw"
|
"github.com/go-gl/glfw/v3.3/glfw"
|
||||||
"github.com/go-gl/mathgl/mgl64"
|
"github.com/go-gl/mathgl/mgl64"
|
||||||
"github.com/inkyblackness/imgui-go/v4"
|
"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
|
var logs string
|
||||||
|
|
||||||
type logger struct{}
|
type logger struct{}
|
||||||
@ -43,6 +52,9 @@ func (g *Game) Init(win *glfw.Window) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g.player.SetDatasetI("LastBreak", 0)
|
||||||
|
g.player.SetDatasetI("LastPlace", 0)
|
||||||
|
|
||||||
var seed int64 = time.Now().Unix()
|
var seed int64 = time.Now().Unix()
|
||||||
gensync := make(chan struct{})
|
gensync := make(chan struct{})
|
||||||
gensynccnt := 0
|
gensynccnt := 0
|
||||||
@ -163,6 +175,18 @@ func (g *Game) Init(win *glfw.Window) {
|
|||||||
win.SetScrollCallback(func(w *glfw.Window, xpos, ypos float64) {
|
win.SetScrollCallback(func(w *glfw.Window, xpos, ypos float64) {
|
||||||
if g.paused {
|
if g.paused {
|
||||||
backend.MouseScrollCallback(xpos, ypos)
|
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.
|
// Update updates the game state, not necessarily in the main thread.
|
||||||
func (g *Game) Update(win *glfw.Window, delta time.Duration) {
|
func (g *Game) Update(win *glfw.Window, delta time.Duration) {
|
||||||
|
g.runtime += delta
|
||||||
backend.NewFrame()
|
backend.NewFrame()
|
||||||
|
|
||||||
clock := util.NewClock()
|
clock := util.NewClock()
|
||||||
@ -242,6 +267,42 @@ func (g *Game) Update(win *glfw.Window, delta time.Duration) {
|
|||||||
g.player.SetSpeed(itype.Vec3d{})
|
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()
|
io.Diagnostics.Times.Logic = clock.Restart()
|
||||||
|
|
||||||
imgui.ShowDemoWindow(nil)
|
imgui.ShowDemoWindow(nil)
|
||||||
|
@ -479,4 +479,5 @@ func (g *Game) Render(win *glfw.Window) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
backend.Render(win)
|
backend.Render(win)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -24,8 +24,5 @@ void main() {
|
|||||||
fragUV = uv;
|
fragUV = uv;
|
||||||
fragColor = color;
|
fragColor = color;
|
||||||
gl_Position = projection * vec4(pos.xy, 0, 1);
|
gl_Position = projection * vec4(pos.xy, 0, 1);
|
||||||
|
|
||||||
if ((flags & FLAG_FLIP_Y) != 0)
|
|
||||||
fragUV.y = 1 - fragUV.y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,11 +13,13 @@ import (
|
|||||||
// size is in pixels. texRange is in [0, 1].
|
// size is in pixels. texRange is in [0, 1].
|
||||||
func Image(tex uint32, size itype.Vec2f, texRange itype.Rectf) {
|
func Image(tex uint32, size itype.Vec2f, texRange itype.Rectf) {
|
||||||
min, max := texRange.MinPoint(), texRange.MaxPoint()
|
min, max := texRange.MinPoint(), texRange.MaxPoint()
|
||||||
|
if GetTextureFlag(tex)&TextureFlag_FlipY == 0 {
|
||||||
|
min[1], max[1] = max[1], min[1] // swap minY/maxY
|
||||||
|
}
|
||||||
imgui.ImageV(
|
imgui.ImageV(
|
||||||
imgui.TextureID(tex),
|
imgui.TextureID(tex),
|
||||||
Vec2(size),
|
Vec2(size),
|
||||||
imgui.Vec2{X: min[0], Y: max[1]},
|
Vec2(min), Vec2(max),
|
||||||
imgui.Vec2{X: max[0], Y: min[1]},
|
|
||||||
Color(255, 255, 255, 255),
|
Color(255, 255, 255, 255),
|
||||||
imgui.Vec4{},
|
imgui.Vec4{},
|
||||||
)
|
)
|
||||||
@ -28,17 +30,52 @@ func Image(tex uint32, size itype.Vec2f, texRange itype.Rectf) {
|
|||||||
//
|
//
|
||||||
// size is in pixels. texRange is in [0, 1].
|
// size is in pixels. texRange is in [0, 1].
|
||||||
func ImageV(tex uint32, size itype.Vec2f, texRange itype.Rectf, texColor itype.Vec4f, borderColor itype.Vec4f) {
|
func ImageV(tex uint32, size itype.Vec2f, texRange itype.Rectf, texColor itype.Vec4f, borderColor itype.Vec4f) {
|
||||||
|
min, max := texRange.MinPoint(), texRange.MaxPoint()
|
||||||
|
if GetTextureFlag(tex)&TextureFlag_FlipY == 0 {
|
||||||
|
min[1], max[1] = max[1], min[1] // swap minY/maxY
|
||||||
|
}
|
||||||
imgui.ImageV(
|
imgui.ImageV(
|
||||||
imgui.TextureID(tex),
|
imgui.TextureID(tex),
|
||||||
Vec2(size),
|
Vec2(size),
|
||||||
Vec2(texRange.MinPoint()),
|
Vec2(min), Vec2(max),
|
||||||
Vec2(texRange.MaxPoint()),
|
|
||||||
Vec4(texColor),
|
Vec4(texColor),
|
||||||
Vec4(borderColor),
|
Vec4(borderColor),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Per-texture flags. This is passed to the shader as-is and handled only there.
|
func ImageButton(tex uint32, size itype.Vec2f, texRange itype.Rectf) bool {
|
||||||
|
min, max := texRange.MinPoint(), texRange.MaxPoint()
|
||||||
|
if GetTextureFlag(tex)&TextureFlag_FlipY == 0 {
|
||||||
|
min[1], max[1] = max[1], min[1] // swap minY/maxY
|
||||||
|
}
|
||||||
|
return imgui.ImageButtonV(
|
||||||
|
imgui.TextureID(tex),
|
||||||
|
Vec2(size),
|
||||||
|
Vec2(min), Vec2(max),
|
||||||
|
-1,
|
||||||
|
imgui.Vec4{},
|
||||||
|
imgui.Vec4{1, 1, 1, 1},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ImageButtonV(tex uint32, size itype.Vec2f, texRange itype.Rectf, padding int, bgCol, tintCol itype.Vec4f) bool {
|
||||||
|
min, max := texRange.MinPoint(), texRange.MaxPoint()
|
||||||
|
if GetTextureFlag(tex)&TextureFlag_FlipY == 0 {
|
||||||
|
min[1], max[1] = max[1], min[1] // swap minY/maxY
|
||||||
|
}
|
||||||
|
return imgui.ImageButtonV(
|
||||||
|
imgui.TextureID(tex),
|
||||||
|
Vec2(size),
|
||||||
|
Vec2(min), Vec2(max),
|
||||||
|
padding,
|
||||||
|
Vec4(bgCol),
|
||||||
|
Vec4(tintCol),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Per-texture flags. This is passed to the shader as-is.
|
||||||
|
//
|
||||||
|
// FilpY is handled by the ImageXXX() call.
|
||||||
type TextureFlag int
|
type TextureFlag int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -75,7 +75,7 @@ func (f *FramewireRenderer) PushLine(p0, p1 itype.Vec3f, color0, color1 color.Co
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const FramewireSizeShrink = 1e-2
|
const FramewireSizeShrink = 1e-4
|
||||||
|
|
||||||
// PushBox pushes a 3D box into the vertex array.
|
// PushBox pushes a 3D box into the vertex array.
|
||||||
//
|
//
|
||||||
|
@ -4,7 +4,7 @@ package itype
|
|||||||
// This is the major way how complicated data is stored in blocks and entities.
|
// This is the major way how complicated data is stored in blocks and entities.
|
||||||
//
|
//
|
||||||
// The empty interface should only hold these 4 types:
|
// The empty interface should only hold these 4 types:
|
||||||
// - int
|
// - int64
|
||||||
// - float64
|
// - float64
|
||||||
// - bool
|
// - bool
|
||||||
// - string
|
// - string
|
||||||
|
@ -62,6 +62,10 @@ func (d Direction) Opposite() Direction {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d Direction) String() string {
|
||||||
|
return DirectionName[d]
|
||||||
|
}
|
||||||
|
|
||||||
// DirectionIs returns X/Y/Z for 0/1/2, and +/- for #pos/neg.
|
// DirectionIs returns X/Y/Z for 0/1/2, and +/- for #pos/neg.
|
||||||
func DirectionIs(index int, positive bool) Direction {
|
func DirectionIs(index int, positive bool) Direction {
|
||||||
switch index {
|
switch index {
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package itype
|
package itype
|
||||||
|
|
||||||
import "sort"
|
import (
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
// Recti is a 2D rectangle with int coordinates.
|
// Recti is a 2D rectangle with int coordinates.
|
||||||
type Recti struct {
|
type Recti struct {
|
||||||
@ -113,6 +115,32 @@ func (b Boxd) Offsetv(x, y, z float64) Boxd {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GrowEven grows the box on each axis by deltaSize,
|
||||||
|
// not moving its center.
|
||||||
|
func (b Boxd) GrowEven(deltaSize Vec3d) Boxd {
|
||||||
|
return Boxd{
|
||||||
|
OffX: b.OffX - deltaSize[0]/2,
|
||||||
|
OffY: b.OffY - deltaSize[1]/2,
|
||||||
|
OffZ: b.OffZ - deltaSize[2]/2,
|
||||||
|
SizeX: b.SizeX + deltaSize[0],
|
||||||
|
SizeY: b.SizeY + deltaSize[1],
|
||||||
|
SizeZ: b.SizeZ + deltaSize[2],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GrowOrigin grows the box on each axis by deltaSize,
|
||||||
|
// not moving its origin.
|
||||||
|
func (b Boxd) GrowOrigin(deltaSize Vec3d) Boxd {
|
||||||
|
return Boxd{
|
||||||
|
OffX: b.OffX,
|
||||||
|
OffY: b.OffY,
|
||||||
|
OffZ: b.OffZ,
|
||||||
|
SizeX: b.SizeX + deltaSize[0],
|
||||||
|
SizeY: b.SizeY + deltaSize[1],
|
||||||
|
SizeZ: b.SizeZ + deltaSize[2],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (b Boxd) MinPoint() Vec3d {
|
func (b Boxd) MinPoint() Vec3d {
|
||||||
return Vec3d{b.OffX, b.OffY, b.OffZ}
|
return Vec3d{b.OffX, b.OffY, b.OffZ}
|
||||||
}
|
}
|
||||||
@ -121,9 +149,9 @@ func (b Boxd) MaxPoint() Vec3d {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b Boxd) Contains(point Vec3d) bool {
|
func (b Boxd) Contains(point Vec3d) bool {
|
||||||
return point[0] >= b.OffX && point[0] <= b.OffX+b.SizeX &&
|
return point[0] > b.OffX && point[0] < b.OffX+b.SizeX &&
|
||||||
point[1] >= b.OffY && point[1] <= b.OffY+b.SizeY &&
|
point[1] > b.OffY && point[1] < b.OffY+b.SizeY &&
|
||||||
point[2] >= b.OffZ && point[2] <= b.OffZ+b.SizeZ
|
point[2] > b.OffZ && point[2] < b.OffZ+b.SizeZ
|
||||||
}
|
}
|
||||||
|
|
||||||
func pointIntersect(n, m, p, q float64) (min, len float64) {
|
func pointIntersect(n, m, p, q float64) (min, len float64) {
|
||||||
@ -157,7 +185,7 @@ func (box1 Boxd) Intersect(box2 Boxd) (ok bool, intersect Boxd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func between(val, min, max float64) bool {
|
func between(val, min, max float64) bool {
|
||||||
return min <= val && val <= max
|
return min < val && val < max
|
||||||
}
|
}
|
||||||
|
|
||||||
// IntersectRay computes if a Box intersects with a ray.
|
// IntersectRay computes if a Box intersects with a ray.
|
||||||
@ -174,14 +202,14 @@ func (box Boxd) IntersectRay(start, dir Vec3d, length float64) (ok bool, hitface
|
|||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
if dir[i] > 0 {
|
if dir[i] > 0 {
|
||||||
t[i] = (min[i] - start[i]) / dir[i]
|
t[i] = (min[i] - start[i]) / dir[i]
|
||||||
} else {
|
} else if dir[i] < 0 {
|
||||||
t[i] = (max[i] - start[i]) / dir[i]
|
t[i] = (max[i] - start[i]) / dir[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
maxi := 0
|
maxi := 0
|
||||||
for i, ti := range t {
|
for i := range t {
|
||||||
if ti > t[maxi] {
|
if t[i] > t[maxi] {
|
||||||
maxi = i
|
maxi = i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -190,7 +218,7 @@ func (box Boxd) IntersectRay(start, dir Vec3d, length float64) (ok bool, hitface
|
|||||||
pt := start.Add(dir.Multiply(t[maxi]))
|
pt := start.Add(dir.Multiply(t[maxi]))
|
||||||
|
|
||||||
o1 := (maxi + 1) % 3
|
o1 := (maxi + 1) % 3
|
||||||
o2 := (maxi + 1) % 3
|
o2 := (maxi + 2) % 3
|
||||||
|
|
||||||
if between(pt[o1], min[o1], max[o1]) && between(pt[o2], min[o2], max[o2]) {
|
if between(pt[o1], min[o1], max[o1]) && between(pt[o2], min[o2], max[o2]) {
|
||||||
ok = true
|
ok = true
|
||||||
@ -201,5 +229,6 @@ func (box Boxd) IntersectRay(start, dir Vec3d, length float64) (ok bool, hitface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ok = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ const (
|
|||||||
// BlockAppearance describes basic appearance of a kind of block.
|
// BlockAppearance describes basic appearance of a kind of block.
|
||||||
type BlockAppearance struct {
|
type BlockAppearance struct {
|
||||||
Name string // A short name, like "stone" or "dirt", used for texture lookups
|
Name string // A short name, like "stone" or "dirt", used for texture lookups
|
||||||
Transparent bool // Is block transparent?
|
Transparent bool // Is block transparent, i.e., does not block nearby blocks in rendering?
|
||||||
NotSolid bool // Is block not solid, i.e., has no hitbox at all? (this makes the zero value reasonable)
|
NotSolid bool // Is block not solid, i.e., has no hitbox at all? (this makes the zero value reasonable)
|
||||||
Light int // The light level it emits, 0 is none
|
Light int // The light level it emits, 0 is none
|
||||||
|
|
||||||
@ -72,6 +72,9 @@ type BlockBehaviour interface {
|
|||||||
//
|
//
|
||||||
// Return true if this block also changed state, false otherwise.
|
// Return true if this block also changed state, false otherwise.
|
||||||
BlockUpdate(position itype.Vec3i, aux int, data itype.Dataset, world *World) bool
|
BlockUpdate(position itype.Vec3i, aux int, data itype.Dataset, world *World) bool
|
||||||
|
|
||||||
|
// Break is called when the block is broken by natural means.
|
||||||
|
Break(position itype.Vec3i, aux int, data itype.Dataset, world *World)
|
||||||
}
|
}
|
||||||
|
|
||||||
type blockBehaviourStatic struct {
|
type blockBehaviourStatic struct {
|
||||||
@ -87,6 +90,7 @@ func (b blockBehaviourStatic) Appearance(position itype.Vec3i, aux int, data ity
|
|||||||
func (blockBehaviourStatic) BlockUpdate(position itype.Vec3i, aux int, data itype.Dataset, world *World) bool {
|
func (blockBehaviourStatic) BlockUpdate(position itype.Vec3i, aux int, data itype.Dataset, world *World) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
func (blockBehaviourStatic) Break(position itype.Vec3i, aux int, data itype.Dataset, world *World) {}
|
||||||
|
|
||||||
// BlockBehaviourStatic returns a Static BlockBehaviour that has the given BlockAppearance.
|
// BlockBehaviourStatic returns a Static BlockBehaviour that has the given BlockAppearance.
|
||||||
func BlockBehaviourStatic(app BlockAppearance) BlockBehaviour {
|
func BlockBehaviourStatic(app BlockAppearance) BlockBehaviour {
|
||||||
@ -172,6 +176,9 @@ type Block struct {
|
|||||||
// Appearance is a shortcut for Behaviour.Appearance().
|
// Appearance is a shortcut for Behaviour.Appearance().
|
||||||
// It returns the Appearance of the block with the given parameters.
|
// It returns the Appearance of the block with the given parameters.
|
||||||
func (b Block) Appearance(position itype.Vec3i) BlockAppearance {
|
func (b Block) Appearance(position itype.Vec3i) BlockAppearance {
|
||||||
|
if b.Behaviour == nil {
|
||||||
|
return BlockAppearance{}
|
||||||
|
}
|
||||||
app := b.Behaviour.Appearance(position, b.Aux, b.Dataset, b.World)
|
app := b.Behaviour.Appearance(position, b.Aux, b.Dataset, b.World)
|
||||||
if !app.NotSolid && len(app.Hitbox) == 0 {
|
if !app.NotSolid && len(app.Hitbox) == 0 {
|
||||||
app.Hitbox = []itype.Boxd{{
|
app.Hitbox = []itype.Boxd{{
|
||||||
|
@ -35,3 +35,4 @@ func (WaterBehaviour) Appearance(position itype.Vec3i, aux int, data itype.Datas
|
|||||||
func (WaterBehaviour) BlockUpdate(position itype.Vec3i, aux int, data itype.Dataset, w *world.World) bool {
|
func (WaterBehaviour) BlockUpdate(position itype.Vec3i, aux int, data itype.Dataset, w *world.World) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
func (WaterBehaviour) Break(position itype.Vec3i, aux int, data itype.Dataset, w *world.World) {}
|
||||||
|
@ -4,6 +4,8 @@ import (
|
|||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"edgaru089.ml/go/gl01/internal/util/itype"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -40,6 +42,27 @@ func (c *Chunk) SetBlock(x, y, z int, id, aux int) {
|
|||||||
c.Id[x][y][z] = uint16(id)
|
c.Id[x][y][z] = uint16(id)
|
||||||
c.Aux[x][y][z] = uint16(aux)
|
c.Aux[x][y][z] = uint16(aux)
|
||||||
c.renderChanged = true
|
c.renderChanged = true
|
||||||
|
|
||||||
|
switch x {
|
||||||
|
case 0:
|
||||||
|
if cb, ok := c.world.Chunks[itype.Vec2i{c.X - 1, c.Z}]; ok {
|
||||||
|
cb.InvalidateRender()
|
||||||
|
}
|
||||||
|
case ChunkSizeX - 1:
|
||||||
|
if cb, ok := c.world.Chunks[itype.Vec2i{c.X + 1, c.Z}]; ok {
|
||||||
|
cb.InvalidateRender()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch z {
|
||||||
|
case 0:
|
||||||
|
if cb, ok := c.world.Chunks[itype.Vec2i{c.X, c.Z - 1}]; ok {
|
||||||
|
cb.InvalidateRender()
|
||||||
|
}
|
||||||
|
case ChunkSizeZ - 1:
|
||||||
|
if cb, ok := c.world.Chunks[itype.Vec2i{c.X, c.Z + 1}]; ok {
|
||||||
|
cb.InvalidateRender()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// InvalidateRender should be called if a block inside the chunk has changed
|
// InvalidateRender should be called if a block inside the chunk has changed
|
||||||
|
@ -1,48 +1,55 @@
|
|||||||
package world
|
package world
|
||||||
|
|
||||||
import "edgaru089.ml/go/gl01/internal/util/itype"
|
import (
|
||||||
|
"edgaru089.ml/go/gl01/internal/util/itype"
|
||||||
|
)
|
||||||
|
|
||||||
// CastViewRay
|
func (w *World) castworker(from, dir itype.Vec3d, maxlen float64, current itype.Vec3i, skipdir itype.Direction) (ok bool, blockcoord itype.Vec3i, face itype.Direction, where itype.Vec3d, dist float64) {
|
||||||
func (w *World) CastViewRay(from, dir itype.Vec3d, maxlen float64) (ok bool, blockcoord itype.Vec3i, face itype.Direction, where itype.Vec3d, dist float64) {
|
|
||||||
|
|
||||||
bfrom := from.Floor()
|
// Length test
|
||||||
bfromdir := itype.Direction(-1)
|
if current.ToFloat64().Addv(0.5, 0.5, 0.5).Add(from.Negative()).Length() > maxlen+1 {
|
||||||
for bfrom.ToFloat64().Addv(0.5, 0.5, 0.5).Add(from.Negative()).Length() < maxlen {
|
return
|
||||||
for todir := itype.Direction(0); todir < 6; todir++ {
|
}
|
||||||
if todir == bfromdir {
|
|
||||||
continue
|
// Loose intersect test
|
||||||
|
if ok, _, _, _ = (itype.Boxd{
|
||||||
|
OffX: float64(current[0]),
|
||||||
|
OffY: float64(current[1]),
|
||||||
|
OffZ: float64(current[2]),
|
||||||
|
SizeX: 1, SizeY: 1, SizeZ: 1,
|
||||||
|
}).IntersectRay(from, dir, maxlen); !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Go through the bounding boxes
|
||||||
|
ba := w.Block(current).Appearance(current)
|
||||||
|
if !ba.NotSolid {
|
||||||
|
for _, b := range ba.Lookbox {
|
||||||
|
if ok, face, where, dist = b.Offset(current.ToFloat64()).IntersectRay(from, dir, maxlen); ok {
|
||||||
|
blockcoord = current
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bto := bfrom.Add(itype.DirectionVeci[todir])
|
// Test the directions
|
||||||
block := w.Block(bto)
|
for i := itype.Direction(0); i < 6; i++ {
|
||||||
|
if i == skipdir {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if block.Id != 0 {
|
ok, blockcoord, face, where, dist = w.castworker(from, dir, maxlen, current.Add(itype.DirectionVeci[i]), i.Opposite())
|
||||||
outbox := itype.Boxd{
|
if ok {
|
||||||
OffX: float64(bto[0]),
|
return
|
||||||
OffY: float64(bto[1]),
|
|
||||||
OffZ: float64(bto[2]),
|
|
||||||
SizeX: 1,
|
|
||||||
SizeY: 1,
|
|
||||||
SizeZ: 1,
|
|
||||||
}
|
|
||||||
var outface itype.Direction
|
|
||||||
if ok, outface, _, _ = outbox.IntersectRay(from, dir, maxlen); ok {
|
|
||||||
app := block.Appearance(bto)
|
|
||||||
for _, lb := range app.Lookbox {
|
|
||||||
if ok, face, where, dist = lb.IntersectRay(from, dir, maxlen); ok {
|
|
||||||
blockcoord = bto
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bfromdir = outface.Opposite()
|
|
||||||
bfrom = bto
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = false
|
ok = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CastViewRay
|
||||||
|
func (w *World) CastViewRay(from, dir itype.Vec3d, maxlen float64) (ok bool, blockcoord itype.Vec3i, face itype.Direction, where itype.Vec3d, dist float64) {
|
||||||
|
|
||||||
|
return w.castworker(from, dir, maxlen, from.Floor(), -1)
|
||||||
|
}
|
||||||
|
@ -84,6 +84,18 @@ func BlockPosToInChunk(blockpos itype.Vec3i) (x, y, z int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Break breaks the block.
|
||||||
|
func (w *World) Break(pos itype.Vec3i) {
|
||||||
|
cx, cz := BlockPosToChunk(pos)
|
||||||
|
cix, ciy, ciz := BlockPosToInChunk(pos)
|
||||||
|
c, ok := w.Chunks[itype.Vec2i{cx, cz}]
|
||||||
|
if !ok || ciy < 0 || ciy >= ChunkSizeY {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.SetBlock(cix, ciy, ciz, 0, 0)
|
||||||
|
}
|
||||||
|
|
||||||
// Block returns the block at the position [pos], or an empty Block{} if it does not exist.
|
// Block returns the block at the position [pos], or an empty Block{} if it does not exist.
|
||||||
func (w *World) Block(pos itype.Vec3i) Block {
|
func (w *World) Block(pos itype.Vec3i) Block {
|
||||||
cx, cz := BlockPosToChunk(pos)
|
cx, cz := BlockPosToChunk(pos)
|
||||||
|
Loading…
Reference in New Issue
Block a user