rpg2d

package
v0.0.0-...-73b4bbd Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2021 License: MIT Imports: 13 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMustProvideAQuadtree = errors.New("user must provide a quad tree to a simulation defination")
View Source
var ErrMustProvideATerrainMap = errors.New("user must provide a terrain map to a simulation defination")

Functions

This section is empty.

Types

type Actor

type Actor interface {
	Id() ActorId

	// Returns an entity that represents the
	// actor in the simulation's world.
	Entity() entity.Entity

	// Enables the simulation to send the
	// state of the world to the actor
	WriteState(WorldState)
}

type ActorId

type ActorId int64

type HaltedSimulation

type HaltedSimulation interface {
	Quad() quad.Quad
}

type RunningSimulation

type RunningSimulation interface {
	ConnectActor(Actor)
	RemoveActor(Actor)
	Halt() (HaltedSimulation, error)
}

type SimulationDef

type SimulationDef struct {
	// The target FPS for the simulation to calculate at
	FPS int

	// Initial World State
	Now        stime.Time
	QuadTree   quad.Quad
	TerrainMap TerrainMap

	// User defined update phase handler
	quad.UpdatePhaseHandler

	// User defined input application phase
	InputPhaseHandler quad.InputPhaseHandler

	// User defined the narrow phase
	NarrowPhaseHandler quad.NarrowPhaseHandler
}

A SimulationDef used to configure a simulation to define the how the simulation will behave.

func (SimulationDef) Begin

func (s SimulationDef) Begin() (RunningSimulation, error)

Implement engine/sim.UnstartedSimulation

type TerrainMap

type TerrainMap struct {
	Bounds coord.Bounds
	// y, x
	TerrainTypes TerrainType2dArray
}

A terrain map is a dense store of terrain state. Ever cell in the world has a terrain type.

func JoinTerrain

func JoinTerrain(newBounds coord.Bounds, maps ...TerrainMap) (TerrainMap, error)

func NewTerrainMap

func NewTerrainMap(bounds coord.Bounds, s string) (TerrainMap, error)

TODO extract the errors this constructor returns into static error values.

func (TerrainMap) Cell

func (m TerrainMap) Cell(c coord.Cell) TerrainType

Return the terrain type in a given cell.

func (TerrainMap) Clone

func (m TerrainMap) Clone() (TerrainMap, error)

Create a copy of the terrain map. The copy will not share memory with the source.

func (*TerrainMap) MergeDiff

func (m *TerrainMap) MergeDiff(newBounds coord.Bounds, slices ...TerrainMapStateSlice) error

MergeDiff will merge the slices of terrain into the TerrainMap. TerrainMap will have the bounds of newBounds once the operation is complete. If slices are unmergable MergeDiff will return an error.

func (*TerrainMap) SetType

func (m *TerrainMap) SetType(t TerrainType, c coord.Cell)

func (TerrainMap) Slice

func (m TerrainMap) Slice(bounds coord.Bounds) TerrainMap

Return a slice of terrain within a given bounds. This method doesn't copy any memory. The slice is viewport into the same memeory as the map it is sliced from.

func (TerrainMap) String

func (m TerrainMap) String() string

Produce a string representation of the terrain map.

func (TerrainMap) ToState

func (m TerrainMap) ToState() *TerrainMapState

Produce a terrain map state with the given terrain map.

type TerrainMapState

type TerrainMapState struct {
	TerrainMap
}

Used to calculate diff's

func (*TerrainMapState) Clone

func (m *TerrainMapState) Clone() (*TerrainMapState, error)

func (*TerrainMapState) Diff

func (*TerrainMapState) IsEmpty

func (m *TerrainMapState) IsEmpty() bool

func (TerrainMapState) MarshalBinary

func (m TerrainMapState) MarshalBinary() ([]byte, error)

func (TerrainMapState) MarshalJSON

func (m TerrainMapState) MarshalJSON() ([]byte, error)

func (*TerrainMapState) UnmarshalBinary

func (m *TerrainMapState) UnmarshalBinary(data []byte) error

type TerrainMapStateDiff

type TerrainMapStateDiff struct {
	Bounds  coord.Bounds        `json:"bounds"`
	Changes []TerrainTypeChange `json:"changes"`
}

type TerrainMapStateSlice

type TerrainMapStateSlice struct {
	Bounds  coord.Bounds `json:"bounds"`
	Terrain string       `json:"terrain"`
}

func (TerrainMapStateSlice) IsEmpty

func (m TerrainMapStateSlice) IsEmpty() bool

type TerrainType

type TerrainType rune

Represents a type of terrain in the world.

const (
	TT_UNKNOWN TerrainType = 'U'
	TT_GRASS   TerrainType = 'G'
	TT_DIRT    TerrainType = 'D'
	TT_ROCK    TerrainType = 'R'
)

type TerrainType2dArray

type TerrainType2dArray [][]TerrainType

func NewTerrainArray

func NewTerrainArray(bounds coord.Bounds, s string) (TerrainType2dArray, error)

func (TerrainType2dArray) String

func (a TerrainType2dArray) String() string

type TerrainTypeChange

type TerrainTypeChange struct {
	Cell        coord.Cell  `json:"cell"`
	TerrainType TerrainType `json:"type"`
}

A change to the terrain type of a cell.

type UnstartedSimulation

type UnstartedSimulation interface {
	Begin() (RunningSimulation, error)
}

type World

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

func NewWorld

func NewWorld(now stime.Time, quad quad.Quad, terrain TerrainMap) *World

func (*World) Insert

func (w *World) Insert(e entity.Entity)

func (*World) Remove

func (w *World) Remove(e entity.Entity)

func (World) ToState

func (world World) ToState() WorldState

type WorldState

type WorldState struct {
	Time   stime.Time   `json:"time"`
	Bounds coord.Bounds `json:"bounds"`

	Entities          entity.StateSlice `json:"entities"`
	EntitiesRemoved   entity.StateSlice `json:"entitiesRemoved"`
	EntitiesNew       entity.StateSlice `json:"entitiesNew"`
	EntitiesChanged   entity.StateSlice `json:"entitiesChanged"`
	EntitiesUnchanged entity.StateSlice `json:"entitiesUnchanged"`

	TerrainMap *TerrainMapState `json:"terrainMap,omitempty"`
}

func (*WorldState) Apply

func (state *WorldState) Apply(diff WorldStateDiff)

Modifies the world state with the changes in a world state diff.

func (WorldState) Clone

func (s WorldState) Clone() WorldState

func (WorldState) Cull

func (s WorldState) Cull(bounds coord.Bounds) (other WorldState)

Returns a world state that only contains entities and terrain within bounds. Does NOT change world state type.

func (WorldState) CullForInitialState

func (s WorldState) CullForInitialState(bounds coord.Bounds) (result WorldState)

func (WorldState) CullInto

func (s WorldState) CullInto(other WorldState, bounds coord.Bounds) (result WorldState)

func (WorldState) Diff

func (prev WorldState) Diff(next WorldState) (diff WorldStateDiff)

Returns a world state that only contains entities and terrain that is different such that state + diff == other. Diff is therefor the changes necessary to get from state to other.

type WorldStateDiff

type WorldStateDiff struct {
	Time   stime.Time   `json:"time"`
	Bounds coord.Bounds `json:"bounds"`

	Entities entity.StateSlice `json:"entities"`
	Removed  entity.StateSlice `json:"removed"`

	TerrainMapSlices []TerrainMapStateSlice `json:"terrainMapSlices,omitempty"`
}

func (*WorldStateDiff) Between

func (diff *WorldStateDiff) Between(prev, next WorldState)

TODO Figure out a way to reuse the maps

Directories

Path Synopsis
Package coord implements math primitives that are used in a 2d cell/tile based game.
Package coord implements math primitives that are used in a 2d cell/tile based game.

Jump to

Keyboard shortcuts

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