state

package
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2023 License: MIT Imports: 12 Imported by: 0

README

State

ECS-Inspired, table based state management for handling game state data

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddToWorld

func AddToWorld(w *GameWorld, obj any) int

Adds a filled schema to the world. Creates the proper entity, etc.

func AddToWorldSpecific

func AddToWorldSpecific(w *GameWorld, entity int, obj any) int

Core add a struct to a world on a specific entity

func AreBigTileOriginPositionsAdjacent

func AreBigTileOriginPositionsAdjacent(pos1 Pos, pos2 Pos, tileWidth int) bool

func ArePositionsAdjacent

func ArePositionsAdjacent(pos1 Pos, pos2 Pos) bool

func ArrayIntersectionWithContext

func ArrayIntersectionWithContext(ctx *QueryContext, nums1, nums2 []int) []int

func CalculateWorldStateRootHash added in v0.1.11

func CalculateWorldStateRootHash(w *GameWorld) string

func ContainsPositions

func ContainsPositions(posList []Pos, pos Pos) bool

func ConvertStructToString added in v0.1.11

func ConvertStructToString(inputStruct interface{}) string

func CreateTypeRegistrationMapping

func CreateTypeRegistrationMapping() map[string]reflect.Type

Create type map registration

func EncodeTableUpdateArrayToBytes

func EncodeTableUpdateArrayToBytes(tableUpdates []TableUpdate) ([]byte, error)

func Filter

func Filter[T any](w *GameWorld, filter T, fieldNames []string) []int

func HashStringsTogether added in v0.1.11

func HashStringsTogether(str1, str2 string) string

func IncludesString

func IncludesString(arr []string, value string) bool

func SamePos

func SamePos(pos1 Pos, pos2 Pos) bool

func Set

func Set[T any](w *GameWorld, entity int, obj T) int

func SortInts added in v0.1.11

func SortInts(input []int) []int

func SortStrings added in v0.1.11

func SortStrings(input []string) []string

deep copy and sort array of strings

func WithinDistance

func WithinDistance(pos1 Pos, pos2 Pos, distance int) bool

Types

type EntityManager

type EntityManager struct {
	*SparseSet
	// contains filtered or unexported fields
}

func NewEntityMananger

func NewEntityMananger() *EntityManager

func (*EntityManager) GetNextAvailableEntity added in v0.1.0

func (e *EntityManager) GetNextAvailableEntity() int

type GameWorld

type GameWorld struct {

	// all tables that stores data
	Tables map[string]Table

	// array of all table updates
	TableUpdates TableUpdateArray
	// contains filtered or unexported fields
}

Master game world holding entities and tables All data belong to a table (aka component in ECS) that have different schemas (ex: Cat{}, Dog{})

func NewWorld

func NewWorld() *GameWorld

Initialize new game world

func (*GameWorld) Add

func (w *GameWorld) Add(obj interface{}, tableName string) int

func (*GameWorld) AddEntity added in v0.1.0

func (w *GameWorld) AddEntity() int

Add entity to world. Fetches the next available entity

func (*GameWorld) AddSpecific

func (w *GameWorld) AddSpecific(entity int, obj interface{}, tableName string) int

func (*GameWorld) AddSpecificEntity added in v0.1.0

func (w *GameWorld) AddSpecificEntity(entity int)

Add a specific entity to the world This is useful in debugging and assigning constant entities to constant numbers

func (*GameWorld) AddTable

func (w *GameWorld) AddTable(table ITable)

Add single table to game world

func (*GameWorld) AddTableUpdate

func (w *GameWorld) AddTableUpdate(tableUpdate TableUpdate)

Add table updates to game world

func (*GameWorld) AddTables

func (w *GameWorld) AddTables(tables ...ITable)

Add multiple tables to the game world

func (*GameWorld) ClearTableUpdates

func (w *GameWorld) ClearTableUpdates()

Clear game world's table updates

func (*GameWorld) Delete

func (w *GameWorld) Delete(entity int, tableName string) int

func (*GameWorld) Entities

func (w *GameWorld) Entities(tableName string) []int

func (*GameWorld) Filter

func (w *GameWorld) Filter(filter interface{}, fieldNames []string, tableName string) []int

func (*GameWorld) Get

func (w *GameWorld) Get(entity int, tableName string) (any, bool)

func (*GameWorld) GetAndClearTableUpdates

func (w *GameWorld) GetAndClearTableUpdates() []TableUpdate

Get and clear game world's table updates

func (*GameWorld) GetTableUpdates

func (w *GameWorld) GetTableUpdates() TableUpdateArray

func (*GameWorld) Set

func (w *GameWorld) Set(entity int, obj interface{}, tableName string) interface{}

type ITable

type ITable interface {
	Name() string
	Type() reflect.Type
}

Table interface

type IWorld

type IWorld interface {
	Add(obj interface{}, name string) int
	AddSpecific(entity int, obj interface{}, name string) int
	Get(entity int, tableName string) (any, bool)
	Set(entity int, obj interface{}, name string) interface{}
	Delete(entity int, tableName string) int
	Filter(filter interface{}, fieldNames []string, name string) []int
	Entities(string) []int
	GetTableUpdates() TableUpdateArray
}

The World Interface Implemented by base world state and buffer world to enable atomic transactions

type Pos

type Pos struct {
	X int `json:"x"`
	Y int `json:"y"`
}

Universal position struct

func GetBottomPos

func GetBottomPos(pos Pos) Pos

func GetLeftPos

func GetLeftPos(pos Pos) Pos

func GetRightPos

func GetRightPos(pos Pos) Pos

func GetTopPos

func GetTopPos(pos Pos) Pos

type QueryContext

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

func NewQueryContext

func NewQueryContext() *QueryContext

func (*QueryContext) Clear

func (ctx *QueryContext) Clear()

type SparseSet

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

func ArrayToSparseSet

func ArrayToSparseSet(array []int) *SparseSet

func NewSparseSet

func NewSparseSet() *SparseSet

func (*SparseSet) Add

func (s *SparseSet) Add(value int)

func (*SparseSet) Contains

func (s *SparseSet) Contains(value int) bool

func (*SparseSet) DeepCopy

func (s *SparseSet) DeepCopy() *SparseSet

func (*SparseSet) GetAll

func (s *SparseSet) GetAll() []int

func (*SparseSet) GetRandomElement

func (s *SparseSet) GetRandomElement() int

func (*SparseSet) Remove

func (s *SparseSet) Remove(value int)

func (*SparseSet) Size

func (s *SparseSet) Size() int

type Table

type Table struct {
	GameWorld *GameWorld

	// table name
	Name string

	// table type
	Type reflect.Type

	// entities
	Entities *SparseSet

	// entity int => value
	EntityToValue map[int]any

	// fieldName => fieldVal => entities sparse set
	//
	// ex: Person {name string, age int}
	// "name" => "Alice" => [1, 2, 3]
	// "age" => "20" => [3, 5, 11] // json representation of int
	Indexes map[string]map[string]*SparseSet // TODO create a struct that locks this
	// contains filtered or unexported fields
}

Each table corresponds to a schema (ex: Player, Animal, Tile)

func NewTable

func NewTable(w *GameWorld, table ITable) Table

Initialize a new table that points to a world

func (*Table) All

func (t *Table) All() []int

Get all entities numbers of a table

func (*Table) Filter

func (t *Table) Filter(filter any, fieldNames []string) []int

Core filter query function

func (*Table) Get

func (t *Table) Get(entity int) (any, bool)

Get from table with entity

func (*Table) RemoveEntity

func (t *Table) RemoveEntity(w *GameWorld, entity int)

Remove entity from world

func (*Table) Set

func (t *Table) Set(w *GameWorld, entity int, value any) any

Core table set function

func (*Table) TableInterface

func (t *Table) TableInterface() ITable

type TableBaseAccessor

type TableBaseAccessor[T any] struct {
	TableName  string
	SchemaType reflect.Type
}

Table accessors are used to access table data with proper types of its schema

func NewTableAccessor

func NewTableAccessor[T any]() *TableBaseAccessor[T]

Initialize new table accessor

func (*TableBaseAccessor[T]) Add

func (c *TableBaseAccessor[T]) Add(w IWorld, obj T) int

Add the object to the world and returns the assigned entity number

func (*TableBaseAccessor[T]) AddSpecific

func (c *TableBaseAccessor[T]) AddSpecific(w IWorld, entity int, obj T) int

Adds the object to the world with a specific entity ID

func (*TableBaseAccessor[T]) Entities

func (c *TableBaseAccessor[T]) Entities(w IWorld) []int

Get all entities for a schema table

func (*TableBaseAccessor[T]) Filter

func (c *TableBaseAccessor[T]) Filter(w IWorld, filter T, fieldNames []string) []int

Query by filtering the fields. returns list of entities

func (*TableBaseAccessor[T]) Get

func (c *TableBaseAccessor[T]) Get(w IWorld, entity int) T

Get object using entity from a world

func (*TableBaseAccessor[T]) Name

func (c *TableBaseAccessor[T]) Name() string

Get the table name

func (*TableBaseAccessor[T]) RemoveEntity

func (c *TableBaseAccessor[T]) RemoveEntity(w IWorld, entity int) int

Remove entity from world

func (*TableBaseAccessor[T]) Set

func (c *TableBaseAccessor[T]) Set(w IWorld, entity int, value T) T

Set entity and its object value to world

func (*TableBaseAccessor[T]) Type

func (c *TableBaseAccessor[T]) Type() reflect.Type

Receive the reflect.Type value of a table's schema

type TableOperationType

type TableOperationType string
var (
	// key indicating that the entity is being removed
	RemovalOP TableOperationType = "removal"

	// key indicating that the entity value is being set
	UpdateOP TableOperationType = "set"

	// key indicating that the entity is being added
	AddEntityOP TableOperationType = "add"
)

Available op codes to indicate what type of update it is

type TableUpdate

type TableUpdate struct {
	// "op codes" that represent what type of operation it is
	OP TableOperationType `json:"op"`

	// the entity
	Entity int `json:"entity"`

	// the name of the table
	Table string `json:"table"`

	// the value that's being updated
	Value interface{} `json:"value"`

	// unix timestamp
	Time int64 `json:"time"`
}

func CopyTableUpdates

func CopyTableUpdates(updates []TableUpdate) []TableUpdate

Deep copy table table updates

type TableUpdateArray

type TableUpdateArray []TableUpdate

"table update" represents 1 single table update to the state

type WorldUpdateBuffer

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

func NewWorldUpdateBuffer

func NewWorldUpdateBuffer(currentWorld *GameWorld) *WorldUpdateBuffer

TODO we can also make the register tables function a part of IWorld

func (*WorldUpdateBuffer) Add

func (w *WorldUpdateBuffer) Add(obj interface{}, tableName string) int

func (*WorldUpdateBuffer) AddSpecific

func (w *WorldUpdateBuffer) AddSpecific(entity int, obj interface{}, tableName string) int

func (*WorldUpdateBuffer) ApplyUpdates

func (w *WorldUpdateBuffer) ApplyUpdates()

func (*WorldUpdateBuffer) Delete

func (w *WorldUpdateBuffer) Delete(entity int, tableName string) int

func (*WorldUpdateBuffer) Entities

func (w *WorldUpdateBuffer) Entities(tableName string) []int

func (*WorldUpdateBuffer) Filter

func (w *WorldUpdateBuffer) Filter(filter interface{}, fieldNames []string, tableName string) []int

func (*WorldUpdateBuffer) Get

func (w *WorldUpdateBuffer) Get(entity int, tableName string) (any, bool)

func (*WorldUpdateBuffer) GetTableUpdates

func (w *WorldUpdateBuffer) GetTableUpdates() TableUpdateArray

func (*WorldUpdateBuffer) Set

func (w *WorldUpdateBuffer) Set(entity int, obj interface{}, tableName string) interface{}

Jump to

Keyboard shortcuts

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