game

package
v0.0.0-...-9debb76 Latest Latest
Warning

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

Go to latest
Published: May 9, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package game contains the logic for game features such as worlds and entities.

The entire game state is managed by a Game instance. This type has a Run function that is responsible for updating ('ticking') the world and entities. All game related logic should execute on the same goroutine that called Run. If a different goroutine needs to execute a game-related job, it can use the Game.Schedule function to schedule a job to run on the correct goroutine.

Index

Constants

View Source
const PlayerEyeHeight = 1.62

Variables

This section is empty.

Functions

This section is empty.

Types

type Chunk

type Chunk struct {
	// contains filtered or unexported fields
}

Chunk represents a 16x16x256 area in a World.

func NewChunk

func NewChunk() *Chunk

NewChunk constructs a new Chunk.

func (*Chunk) Broadcast

func (c *Chunk) Broadcast(msg interface{})

Broadcast broadcasts a message to all subscribers of this Chunk.

func (*Chunk) SetBlock

func (c *Chunk) SetBlock(x, y, z uint8, data block.Data)

SetBlock changes a block in the chunk. Note that the coordinates are relative to the chunk, not world coordinates. Coordinates must all be within the range [0,15] or the function will panic.

func (*Chunk) Subscribe

func (c *Chunk) Subscribe(id uint32, ch chan<- interface{})

Subscribe registers the specified channel to receive updates for this Chunk. The specified ID must be unique to the subscriber.

func (*Chunk) Unsubscribe

func (c *Chunk) Unsubscribe(id uint32)

Unsubscribe cancels the subscription associated with the specified ID.

type ChunkPos

type ChunkPos struct {
	X, Z int32
}

ChunkPos contains a pair of chunk coordinates.

func ChunkPosFromWorldCoords

func ChunkPosFromWorldCoords(x, z float64) ChunkPos

ChunkPosFromWorldCoords returns the ChunkPos for the given world coordinates.

func (ChunkPos) Dist

func (p ChunkPos) Dist(other ChunkPos) int32

Dist returns the distance between two ChunkPos values, in chunks. Note that this is not the euclidean distance, instead it is computed as max(abs(x1-x2), abs(z1-z2)).

type Config

type Config struct {
	// MaxJobs specifies how many jobs can be queued at the same time.
	MaxJobs int
	// TickInterval specifies how often the game state should be updated.
	TickInterval time.Duration
}

Config is used to configure a Game instance.

type Entity

type Entity interface {
	// EntityID returns the ID for this entity. This function may be called concurrently.
	EntityID() ID
	// contains filtered or unexported methods
}

Entity represents a Minecraft entity, such as a player or a dropped item.

type Game

type Game struct {
	// contains filtered or unexported fields
}

Game manages the state for game related things, such as worlds and entities.

func NewGame

func NewGame(worlds []*World, cfg Config) *Game

NewGame constructs a new Game. Panics if len(worlds)==0.

func (*Game) Close

func (g *Game) Close() error

Close releases resources associated with the Game. Any ongoing Run calls will exit. This function may only be called once and always returns nil.

func (*Game) DefaultWorld

func (g *Game) DefaultWorld() *World

DefaultWorld returns the default World.

func (*Game) Run

func (g *Game) Run() error

Run will process game updates until Close is called.

func (*Game) Schedule

func (g *Game) Schedule(job func())

Schedule schedules a job to be executed in the same goroutine that called Run.

type ID

type ID int32

ID is a unique identifier for an Entity.

type Player

type Player struct {
	// contains filtered or unexported fields
}

Player represents a player entity.

func NewPlayer

func NewPlayer(name string, uid uuid.UUID, conn PlayerConn) *Player

NewPlayer constructs a new Player. The created Player will not be associated with any World yet.

func (*Player) Close

func (p *Player) Close() error

Close releases resources associated with the Player.

func (*Player) EntityID

func (p *Player) EntityID() ID

EntityID implements Entity.EntityID.

func (*Player) HandlePacket

func (p *Player) HandlePacket(pk packet.Inbound)

HandlePacket processes a packet sent by the player. This function should only be used by the server itself.

func (*Player) SetWorld

func (p *Player) SetWorld(w *World)

SetWorld moves the player to a different World.

func (*Player) Teleport

func (p *Player) Teleport(pos Pos)

Teleport moves the player to a new Pos.

type PlayerConn

type PlayerConn interface {
	// WritePacket sends a packet to the player.
	WritePacket(pk packet.Outbound)
	// Disconnect kicks the player from the server.
	Disconnect(reason *chat.Msg)
}

PlayerConn represents a player's network connection.

type Pos

type Pos struct {
	X     float64
	Y     float64
	Z     float64
	Yaw   float32
	Pitch float32
}

Pos represents a position within a world, containing coordinates and look angles.

type World

type World struct {
	// contains filtered or unexported fields
}

World represents a world within the server.

func NewWorld

func NewWorld(chunks map[ChunkPos]*Chunk) *World

NewWorld constructs a new World containing predefined chunks.

func (*World) AddEntity

func (w *World) AddEntity(e Entity)

AddEntity adds an Entity to the world.

func (*World) GetChunk

func (w *World) GetChunk(pos ChunkPos) *Chunk

GetChunk gets the Chunk at the specified position, or nil if it does not exist.

func (*World) RemoveEntity

func (w *World) RemoveEntity(id ID)

RemoveEntity removes the entity associated with the specified id. If no such entity exists, this function does nothing.

Jump to

Keyboard shortcuts

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