chunk

package
v0.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2017 License: BSD-2-Clause Imports: 7 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// ChunkSize controls the cubic dimensions of Chunk structures
	ChunkSize = 16

	// ChunkSizeF is the size of the Chunk in float
	ChunkSizeF = float32(16.0)

	// ChunkSize2 is chunkSize squared
	ChunkSize2 = ChunkSize * ChunkSize

	// ChunkSize3 is chunkSize cubed
	ChunkSize3 = ChunkSize2 * ChunkSize
)
View Source
const (
	BlockTypeEmpty  = BlockType(0)
	BlockTypeGrass  = BlockType(1)
	BlockTypeStones = BlockType(2)
	BlockTypeDirt   = BlockType(3)
)

TODO: these are temporary fixes until the chunk manager has a map of block types to descriptors.

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block struct {
	// Type determines the type of block that the Block struct represents
	// NOTE: a Type of 0 has special significance in that it's assumed
	// to be an empty space.
	Type BlockType
}

Block is the basic building block of the landscape terrain.

func (*Block) IsVisible

func (b *Block) IsVisible() bool

IsVisible returns true if the block is a type of block that can be visualized normally. Basically: if it's not air.

type BlockType

type BlockType uint32

BlockType is a type alias for the variable that will determine what type a landscape Block is in the Chunk.

type Chunk

type Chunk struct {
	// X is the location of Chunk along the X axis in multiples of ChunkSize.
	// (e.g. a value of Chunk.X==2 for ChunkSize==16 means a world position of 32)
	X int

	// Y is the location of Chunk along the Y axis in multiples of ChunkSize.
	// (e.g. a value of Chunk.Y==2 for ChunkSize==16 means a world position of 32)
	Y int

	// Z is the location of Chunk along the Z axis in multiples of ChunkSize.
	// (e.g. a value of Chunk.Z==2 for ChunkSize==16 means a world position of 32)
	Z int

	// Blocks is a flat array [y*x*z] Block values. You can think
	// of it in +Z first ordering, meaning it fills a depth of Z, then moves in +X
	// and fills another depth of Z.
	//
	// Viz:
	//  Y
	//  ^ Z
	//  |/__> X
	//
	// Further examples for RegionDepth of 16:
	//   (0,0,0) = [0]
	//   (0,0,5) = [5]
	//   (1,0,0) = [16]
	//   (1,0,5) = [21]
	//   (0,1,0) = [256] (16*16)
	Blocks [ChunkSize3]Block

	// Renderable is the drawable OpenGL object for the chunk. In a server-side
	// only server, this pointer will be nil.
	Renderable *fizzle.Renderable

	// AABBCollider is an axis aligned bounding box collider for the chunk for
	// use in simple collision checks with the space of the entire chunk,
	// regarldless of the contents (i.e. will still collide on 'empty' blocks
	// and contains the entierty of the chunk).
	// Note: coordinates used in the collider are in world space.
	AABBCollider *glider.AABBox

	// colliders is a slice of calculate physics colliders for the chunk geometry.
	// This is generated on calls to UpdateColliders() which should be called
	// when the Blocks array has value changes.
	Colliders []physics.Collider

	// BoxColliders is a slice of AABBoxes that mirrors the physics colliders.
	// This can be used for faster raycasts since the voxels aren't rotating.
	BoxColliders []*glider.AABBox

	// Owner is the owning landscape Manager class. This will be nil if the Chunk
	// has not been registered yet with a manager.
	Owner *Manager
}

Chunk is the data structure that groups together Blocks.

func NewChunk

func NewChunk(chunkX, chunkY, chunkZ int) *Chunk

NewChunk returns a newly created chunk

func (*Chunk) BlockAt

func (c *Chunk) BlockAt(x, y, z int) *Block

BlockAt returns the Block object at a given offset within the Chunk. NOTE: not to be confused with coordinate -- this is the offset in the Cubes array.

func (*Chunk) Clone

func (c *Chunk) Clone() *Chunk

Clone creates a new Chunk object with the core data but does not duplicate the Renderable or physics colliders

func (*Chunk) Destroy

func (c *Chunk) Destroy()

Destroy tells the chunk to release any special data.

func (*Chunk) GetTheRenderable

func (c *Chunk) GetTheRenderable(textureIndexes fizzle.TextureArrayIndexes) *fizzle.Renderable

GetTheRenderable returns the already crafted Renderable object or makes one, caches it and returns a pointer to it.

func (*Chunk) IsBlockMovementBlocking

func (c *Chunk) IsBlockMovementBlocking(x, y, z int) bool

IsBlockMovementBlocking checks to see if a block location blocks movement from entities. Having this separate from IsVisible makes the mechanic being checked more explicit.

func (*Chunk) IsBlockVisible

func (c *Chunk) IsBlockVisible(x, y, z int) bool

IsBlockVisible tests whether or not the block is visible. x,y,z are specified in local coordinates. NOTE: blocks on the outside of the chunk are always considered visible.

func (*Chunk) SetBlock

func (c *Chunk) SetBlock(x, y, z int, ty BlockType)

SetBlock sets the attribute of a block at a given coordiante. X,Y,Z should be within range of [0..ChunkSize-1].

func (*Chunk) UpdateColliders

func (c *Chunk) UpdateColliders()

UpdateColliders should be called whenever the Blocks of the Chunk change which could result in the pathing being different.

type Manager

type Manager struct {
	// Chunks is the internal splice of all of the chunks registered in the Manager.
	// Access to this will depend on the Size parameter.
	Chunks []*Chunk

	// Size is the number of chunks in each direction to manage.
	Size int

	// X is the location of Manager along the X axis in multiples of ChunkSize.
	// (e.g. a value of Manager.X==2 for ChunkSize==16 means a world position of 32)
	X int

	// Y is the location of Manager along the Y axis in multiples of ChunkSize.
	// (e.g. a value of Manager.Y==2 for ChunkSize==16 means a world position of 32)
	Y int

	// Z is the location of Manager along the Z axis in multiples of ChunkSize.
	// (e.g. a value of Manager.Z==2 for ChunkSize==16 means a world position of 32)
	Z int
}

Manager is the landscape manager type structure that manages access to landscape data.

func NewManager

func NewManager(length int, x, y, z int) *Manager

NewManager returns a new voxel manager object that manages a cube region of chunks.

func (*Manager) GetBlockAt

func (m *Manager) GetBlockAt(worldX, worldY, worldZ int) (*Block, *Chunk)

GetBlockAt returns the block at a given coorindate and, as an added bonus to faithful callers, it will also return the chunk. A two for one! NOTE: restrictions apply! If world xyz doesn't exist in the manger, (nil, nil) is returned.

func (*Manager) GetHeightAt

func (m *Manager) GetHeightAt(localX, localZ int) int

GetHeightAt returns the local height of the land at the local coordinate X,Z passed in.

func (*Manager) RegisterChunk

func (m *Manager) RegisterChunk(c *Chunk) bool

RegisterChunk fits the new chunk into the chunks slice of the Manager

func (*Manager) RegisterChunks

func (m *Manager) RegisterChunks(cs []*Chunk)

RegisterChunks registers all of the chunks in the slice passed in.

func (*Manager) SetBlockAt

func (m *Manager) SetBlockAt(worldX, worldY, worldZ int, bt BlockType) *Chunk

SetBlockAt sets the block type and color for a given locaiton in the world. It will create a new chunk if necessary. It returns the chunk for the block that was set. NOTE: Does not update colliders. This was left to the calling client so that multiple updates at a time don't needlessly recreate colliders.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL