place blocks
This commit is contained in:
@ -19,7 +19,7 @@ const (
|
||||
// BlockAppearance describes basic appearance of a kind of block.
|
||||
type BlockAppearance struct {
|
||||
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)
|
||||
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.
|
||||
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 {
|
||||
@ -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 {
|
||||
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.
|
||||
func BlockBehaviourStatic(app BlockAppearance) BlockBehaviour {
|
||||
@ -172,6 +176,9 @@ type Block struct {
|
||||
// Appearance is a shortcut for Behaviour.Appearance().
|
||||
// It returns the Appearance of the block with the given parameters.
|
||||
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)
|
||||
if !app.NotSolid && len(app.Hitbox) == 0 {
|
||||
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 {
|
||||
return false
|
||||
}
|
||||
func (WaterBehaviour) Break(position itype.Vec3i, aux int, data itype.Dataset, w *world.World) {}
|
||||
|
@ -4,6 +4,8 @@ import (
|
||||
"encoding/gob"
|
||||
"io"
|
||||
"log"
|
||||
|
||||
"edgaru089.ml/go/gl01/internal/util/itype"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -40,6 +42,27 @@ func (c *Chunk) SetBlock(x, y, z int, id, aux int) {
|
||||
c.Id[x][y][z] = uint16(id)
|
||||
c.Aux[x][y][z] = uint16(aux)
|
||||
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
|
||||
|
@ -1,48 +1,55 @@
|
||||
package world
|
||||
|
||||
import "edgaru089.ml/go/gl01/internal/util/itype"
|
||||
import (
|
||||
"edgaru089.ml/go/gl01/internal/util/itype"
|
||||
)
|
||||
|
||||
// CastViewRay
|
||||
func (w *World) CastViewRay(from, dir itype.Vec3d, maxlen float64) (ok bool, blockcoord itype.Vec3i, face itype.Direction, where itype.Vec3d, dist float64) {
|
||||
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) {
|
||||
|
||||
bfrom := from.Floor()
|
||||
bfromdir := itype.Direction(-1)
|
||||
for bfrom.ToFloat64().Addv(0.5, 0.5, 0.5).Add(from.Negative()).Length() < maxlen {
|
||||
for todir := itype.Direction(0); todir < 6; todir++ {
|
||||
if todir == bfromdir {
|
||||
continue
|
||||
// Length test
|
||||
if current.ToFloat64().Addv(0.5, 0.5, 0.5).Add(from.Negative()).Length() > maxlen+1 {
|
||||
return
|
||||
}
|
||||
|
||||
// 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])
|
||||
block := w.Block(bto)
|
||||
// Test the directions
|
||||
for i := itype.Direction(0); i < 6; i++ {
|
||||
if i == skipdir {
|
||||
continue
|
||||
}
|
||||
|
||||
if block.Id != 0 {
|
||||
outbox := itype.Boxd{
|
||||
OffX: float64(bto[0]),
|
||||
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, blockcoord, face, where, dist = w.castworker(from, dir, maxlen, current.Add(itype.DirectionVeci[i]), i.Opposite())
|
||||
if ok {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ok = false
|
||||
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
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (w *World) Block(pos itype.Vec3i) Block {
|
||||
cx, cz := BlockPosToChunk(pos)
|
||||
|
Reference in New Issue
Block a user