wecqs

package module
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2022 License: MIT Imports: 33 Imported by: 2

README

WECQS

WECQS logo

WECQS (/wɛks/) is an experimental ECS library built on top of Ebitengine, designed to be both simple to use and extremely fast. It achieves this by making heavy use of unsafe.Pointer and various other reflect-like hacks of the Go runtime, without directly using reflect. The drawbacks here are there are no guarantees of safety, portability, or compatibility with past or future versions of Go. As such, WECQS currently only currently supports Go 1.19.

Take a look at these other ECS packages, which may fit your use-case better and may also less likely to randomly break:

License

The wecqs package is distributed under the MIT License. See this page for more details.

Documentation

Overview

Package wecqs is an ECS framework for Go game development.

Index

Constants

View Source
const (
	InputKeyboard inputMode = iota
	InputMouseButton
	InputGamepadButton
	InputGamepadAxis
)

Variables

View Source
var (
	// ErrShutdown is an error which can be returned by an UpdateSystem to gracefully stop the game.
	ErrShutdown = errors.New("game shutdown")

	ErrWorldUnavailable = errors.New("world is not available")
	ErrWorldNoneActive  = errors.New("no world is currently active")

	ErrSceneUnavailable = errors.New("scene is not available")
	ErrSceneNoneActive  = errors.New("no scene is currently active")

	ErrInputsUnavailable    = errors.New("input mapping is not available")
	ErrInputsRebindDisabled = errors.New("input mapping has rebinding disabled")

	ErrAssetUnavailable = errors.New("asset is not available")
	ErrExpectedFolder   = errors.New("expected path to folder")
	ErrUnhandledType    = errors.New("unhandled type")
)

Functions

func LogDebug added in v0.5.0

func LogDebug(layout string, v ...any)

func LogError added in v0.5.0

func LogError(layout string, v ...any)

func LogInfo added in v0.5.0

func LogInfo(layout string, v ...any)

func LogTrace added in v0.5.0

func LogTrace(layout string, v ...any)

func LogWarn added in v0.5.0

func LogWarn(layout string, v ...any)

func SetLogger added in v0.3.0

func SetLogger(logger Logger)

SetLogger sets the global package logger. The default value is SilentLogger, which does not log anything. Passing nil defaults the logger back to SilentLogger.

Types

type AssetReader added in v0.5.0

type AssetReader func(path string) (fs.FS, error)

type DrawSystem

type DrawSystem interface {
	Draw(state *State, canvas *ebitengine.Image)
}

type Entity

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

Entity is a general-purpose collection of components and tags which queries can be run on.

func NewEntity

func NewEntity(components ...any) *Entity

NewEntity creates a new Entity with the given components.

func (*Entity) AddComponents

func (entity *Entity) AddComponents(components ...any) *Entity

AddComponents adds the given components to this entity and returns it.

func (*Entity) AddTags

func (entity *Entity) AddTags(tags ...string) *Entity

AddTags adds the given tags to the Entity and returns it.

func (*Entity) Component

func (entity *Entity) Component(dst any) bool

Component gets a component from this entity and stores it in the given destination. Generally this method wouldn't be used, as in regular usage a component would be extracted using a Has query instead. But for completeness’ sake, it's here anyway.

func (*Entity) Components added in v0.4.0

func (entity *Entity) Components() []any

Components returns a slice of all the components in this entity.

func (*Entity) HasTag

func (entity *Entity) HasTag(tag string) bool

HasTag returns true if the Entity has the given tag.

func (*Entity) ID

func (entity *Entity) ID() ID

ID of the Entity.

func (*Entity) String added in v0.3.0

func (entity *Entity) String() string

String returns the ID of the entity as a string. This is here to satisfy fmt.Stringer.

func (*Entity) Tags

func (entity *Entity) Tags() []string

Tags which belong to the Entity.

type ErrInputsConflict

type ErrInputsConflict struct{ ConflictsWith map[string]*Input }

func (ErrInputsConflict) Error

func (e ErrInputsConflict) Error() string

type Game added in v0.5.0

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

func NewGame added in v0.5.0

func NewGame(title string) *Game

NewGame returns a new game with the given title.

func (*Game) AddIcons added in v0.5.0

func (game *Game) AddIcons(icons ...image.Image) *Game

AddIcons adds window icons to the game. Adding multiple icons allows for the one of the closest desired size to be chosen automatically (see ebiten.SetWindowIcon).

It returns the game, which makes it useful for initialising by chaining method calls together. This has no effect when running on non-desktops.

func (*Game) AddWorlds added in v0.5.0

func (game *Game) AddWorlds(worlds ...*World) *Game

AddWorlds adds all the given worlds to the game state.

It returns the game, which makes it useful for initialising by chaining method calls together.

func (*Game) AllowFullscreen added in v0.5.0

func (game *Game) AllowFullscreen(enable bool) *Game

AllowFullscreen sets if native desktop fullscreen is allowed or not. This currently only has an effect on macOS (see ebiten.WindowResizingModeOnlyFullscreenEnabled).

It returns the game, which makes it useful for initialising by chaining method calls together.

func (*Game) AllowHighDPI added in v0.5.2

func (game *Game) AllowHighDPI(enable bool) *Game

AllowHighDPI sets if the game should acknowledge the high-DPI settings of the host system.

It returns the game, which makes it useful for initialising by chaining method calls together.

func (*Game) AllowResize added in v0.5.0

func (game *Game) AllowResize(enable bool) *Game

AllowResize sets if window resizing is enabled or disabled.

It returns the game, which makes it useful for initialising by chaining method calls together. This has no effect when running on non-desktops.

func (*Game) Run added in v0.5.0

func (game *Game) Run(worldName, sceneName string) error

Run initialises the game with a starting world and scene and runs it.

func (*Game) SetFullscreen added in v0.5.0

func (game *Game) SetFullscreen(enable bool) *Game

SetFullscreen sets if the game should be fullscreen. This does not have an effect on macOS if fullscreen has been enabled natively by the desktop.

It returns the game, which makes it useful for initialising by chaining method calls together. This has no effect when running on non-desktops.

func (*Game) SetResolution added in v0.5.0

func (game *Game) SetResolution(width, height int) *Game

SetResolution sets the canvas resolution of the game.

It returns the game, which makes it useful for initialising by chaining method calls together. This has no effect when running on non-desktops.

func (*Game) SetResolutionLimits added in v0.5.0

func (game *Game) SetResolutionLimits(minWidth, minHeight, maxWidth, maxHeight int) *Game

SetResolutionLimits sets the resolution limits of the game window. A negative value means the size is unlimited.

It returns the game, which makes it useful for initialising by chaining method calls together. This has no effect when running on non-desktops.

func (*Game) SetScale added in v0.5.3

func (game *Game) SetScale(scale int) *Game

SetScale sets the canvas scale of the game.

It returns the game, which makes it useful for initialising by chaining method calls together.

func (*Game) SetTPS added in v0.5.0

func (game *Game) SetTPS(tps int) *Game

SetTPS sets the maximum TPS (ticks per second). By default, this is set to 60. Setting the TPS to a negative number results in ticks being synced to the framerate (see ebiten.SetTPS).

It returns the game, which makes it useful for initialising by chaining method calls together.

func (*Game) SetVsync added in v0.5.0

func (game *Game) SetVsync(enable bool) *Game

SetVsync sets if vsync is enabled or disabled.

It returns the game, which makes it useful for initialising by chaining method calls together.

type ID

type ID uint64

ID is a unique identifier for an Entity.

func NewID

func NewID() ID

NewID generates a new ID.

func (ID) String

func (id ID) String() string

type Input

type Input struct {
	Mode inputMode

	KeyPrimary    ebitengine.Key
	KeySecondary  ebitengine.Key
	MouseButton   ebitengine.MouseButton
	GamepadID     ebitengine.GamepadID
	GamepadButton ebitengine.StandardGamepadButton
	GamepadAxis   ebitengine.StandardGamepadAxis
	// contains filtered or unexported fields
}

func NewInput

func NewInput() *Input

NewInput returns a new Input with all the input values set to -1 by default, meaning it will not react to any inputs.

func (*Input) AddGamepadAxis

func (input *Input) AddGamepadAxis(gamepadAxis ebitengine.StandardGamepadAxis) *Input

func (*Input) AddGamepadButton

func (input *Input) AddGamepadButton(gamepadButton ebitengine.StandardGamepadButton) *Input

func (*Input) AddGamepadID

func (input *Input) AddGamepadID(gamepadID ebitengine.GamepadID) *Input

func (*Input) AddKeys

func (input *Input) AddKeys(keys ...ebitengine.Key) *Input

AddKeys sets the input keys and returns the input. If more than 2 keys are given, the rest are ignored.

func (*Input) AddMode

func (input *Input) AddMode(mode inputMode) *Input

AddMode sets the input mode and returns the input.

func (*Input) AddMouseButton

func (input *Input) AddMouseButton(mouseButton ebitengine.MouseButton) *Input

func (*Input) DisableRebind

func (input *Input) DisableRebind() *Input

func (*Input) IsJustPressed

func (input *Input) IsJustPressed() bool

func (*Input) IsJustReleased

func (input *Input) IsJustReleased() bool

func (*Input) IsPressed

func (input *Input) IsPressed() bool

func (*Input) ModeAvailable

func (input *Input) ModeAvailable() bool

func (*Input) PressDuration

func (input *Input) PressDuration() int

func (*Input) Value

func (input *Input) Value() float64

type LayoutSystem

type LayoutSystem interface {
	Layout(state *State, canvas *ebitengine.Image)
}

type LoadSystem

type LoadSystem interface {
	Load(state *State) error
}

type Logger added in v0.3.0

type Logger interface {
	Trace(layout string, v ...any)
	Debug(layout string, v ...any)
	Info(layout string, v ...any)
	Warn(layout string, v ...any)
	Error(layout string, v ...any)
}

type Query

type Query interface {
	// Match returns true if the Entity does not match the query, else returns false.
	Match(*Entity) bool
}

func All

func All(queries ...Query) Query

All implements a Query for if every underlying query successfully matches.

func Any

func Any(queries ...Query) Query

Any implements a Query for if at least 1 of the underlying queries successfully matches.

func Cache

func Cache(query Query) Query

Cache wraps the query and caches the results for faster subsequent runs.

func Has

func Has(dst any) Query

Has implements a Query for if the entity has a particular component.

func Not

func Not(query Query) Query

Not implements a Query which returns an error if the underlying query successfully matches. This query effectively discards errors when used, which may make debugging more difficult.

func Tagged

func Tagged(tag string) Query

Tagged implements a Query for if the entity has a particular tag.

type Scene

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

func NewScene

func NewScene(name string, entities ...*Entity) *Scene

NewScene creates a new Scene with the given name and entities.

func (*Scene) AddEntities

func (scene *Scene) AddEntities(entities ...*Entity) *Scene

AddEntities adds all the given entities.

It returns the scene, which makes it useful for initialising scenes by chaining method calls together.

func (*Scene) Entity

func (scene *Scene) Entity(id ID) *Entity

Entity returns the entity with the given ID, or nil if none exists.

func (*Scene) Name

func (scene *Scene) Name() string

Name returns the scene name.

func (*Scene) Query

func (scene *Scene) Query(query Query, fn func(*Entity) bool)

Query runs a given function on all matching entities that match a given query. If false is returned from the given function, iteration will stop.

func (*Scene) RemoveEntities

func (scene *Scene) RemoveEntities(ids ...ID) *Scene

RemoveEntities removes all entities with the given IDs. This uses a fast method for removing elements from a slice which does not preserve order.

It returns the scene, which makes it useful for initialising scenes by chaining method calls together.

func (*Scene) String added in v0.3.0

func (scene *Scene) String() string

String returns the scene name. This is here to satisfy fmt.Stringer.

type SilentLogger added in v0.3.0

type SilentLogger struct{}

SilentLogger is a levelled logger implementation which does nothing.

func (SilentLogger) Debug added in v0.3.0

func (s SilentLogger) Debug(string, ...any)

func (SilentLogger) Error added in v0.3.0

func (s SilentLogger) Error(string, ...any)

func (SilentLogger) Info added in v0.3.0

func (s SilentLogger) Info(string, ...any)

func (SilentLogger) Trace added in v0.3.0

func (s SilentLogger) Trace(string, ...any)

func (SilentLogger) Warn added in v0.3.0

func (s SilentLogger) Warn(string, ...any)

type SimpleLogger added in v0.3.0

type SimpleLogger struct {
	// Level specifies what sort of logs should be outputted.
	// -1: none
	// 0: errors
	// 1: warnings
	// 2: informational logs
	// 3: debug logging (verbose)
	// 4: trace logging (extremely verbose)
	Level int
	// contains filtered or unexported fields
}

SimpleLogger is a simple levelled logger implementation, which prints all logs to the console (stderr).

func (*SimpleLogger) Debug added in v0.3.0

func (l *SimpleLogger) Debug(layout string, v ...any)

func (*SimpleLogger) Error added in v0.3.0

func (l *SimpleLogger) Error(layout string, v ...any)

func (*SimpleLogger) Info added in v0.3.0

func (l *SimpleLogger) Info(layout string, v ...any)

func (*SimpleLogger) Trace added in v0.3.0

func (l *SimpleLogger) Trace(layout string, v ...any)

func (*SimpleLogger) Warn added in v0.3.0

func (l *SimpleLogger) Warn(layout string, v ...any)

type State added in v0.2.0

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

State stores the current game state.

func (*State) AddAssetReader added in v0.4.0

func (state *State) AddAssetReader(ext string, assetReader AssetReader) *State

AddAssetReader adds an asset handler function to the state, allowing it to read assets from a new file type. The function takes a path to the asset file as the only argument and returns an fs.FS and an error.

It returns the state, which makes it useful for initialising the state by chaining method calls together.

func (*State) AddWorlds added in v0.3.0

func (state *State) AddWorlds(worlds ...*World)

AddWorlds adds all the given worlds to the game state.

func (*State) CanvasScale added in v0.5.3

func (state *State) CanvasScale() int

CanvasScale returns the current canvas scale.

func (*State) CanvasSize added in v0.3.2

func (state *State) CanvasSize() (int, int)

CanvasSize returns the current canvas size.

func (*State) CurrentScene added in v0.2.0

func (state *State) CurrentScene() *Scene

CurrentScene returns the current scene.

func (*State) CurrentWorld added in v0.2.0

func (state *State) CurrentWorld() *World

CurrentWorld returns the current world.

func (*State) GetAsset added in v0.2.0

func (state *State) GetAsset(name string) (raw []byte, err error)

GetAsset reads a given asset for this world and returns its raw bytes.

func (*State) GetAudio added in v0.2.0

func (state *State) GetAudio(name string) (s io.ReadSeeker, err error)

GetAudio returns an MP3, WAV, or Vorbis audio file as an io.ReadSeeker. Usually, NewAudioPlayer, NewLoopAudioPlayer, or NewLoopWithIntroAudioPlayer would be preferred.

func (*State) GetFont added in v0.2.0

func (state *State) GetFont(name string, size float64) (font.Face, error)

GetFont returns an OpenType font with the given name.

func (*State) GetImage added in v0.2.0

func (state *State) GetImage(name string) (img *ebitengine.Image, i image.Image, err error)

GetImage returns an image with the given name.

func (*State) GetJSON added in v0.2.0

func (state *State) GetJSON(name string, dst any) (err error)

func (*State) InputJustPressed added in v0.2.0

func (state *State) InputJustPressed(name string) bool

InputJustPressed returns true if the input with the given name was just pressed.

func (*State) InputJustReleased added in v0.2.0

func (state *State) InputJustReleased(name string) bool

InputJustReleased returns true if the input with the given name was just released.

func (*State) InputPressDuration added in v0.2.0

func (state *State) InputPressDuration(name string) int

InputPressDuration returns the number of update cycles the input with the given name has been pressed for.

func (*State) InputPressed added in v0.2.0

func (state *State) InputPressed(name string) bool

InputPressed returns true if the input with the given name is currently pressed.

func (*State) InputValue added in v0.2.0

func (state *State) InputValue(name string) float64

InputValue returns the value of the input with the given name, between 0.0 and 1.0.

func (*State) NewAudioPlayer added in v0.2.0

func (state *State) NewAudioPlayer(name string) (*audio.Player, error)

func (*State) NewLoopAudioPlayer added in v0.2.0

func (state *State) NewLoopAudioPlayer(name string, length int64) (*audio.Player, error)

func (*State) NewLoopWithIntroAudioPlayer added in v0.2.0

func (state *State) NewLoopWithIntroAudioPlayer(name string, introLength, loopLength int64) (*audio.Player, error)

func (*State) Query added in v0.2.0

func (state *State) Query(query Query, fn func(*Entity) bool)

Query iterates over all entities in the current scene, running a given function.

func (*State) RebindInput added in v0.2.0

func (state *State) RebindInput(name string, newInput *Input, force bool) error

RebindInput changes an existing input mapping with conflict detection. When force is true, conflict detection is skipped, and mappings are created regardless of if they existed already. Error will still be returned if input mapping has rebinding disabled, regardless of the value of force.

func (*State) RegisterInput added in v0.2.0

func (state *State) RegisterInput(name string, newInput *Input)

RegisterInput adds a new input mapping with the given name if it has not already been added. If an input mapping with this name already exists, nothing is done. This makes it good for initialising input mappings per-system.

func (*State) SetFullscreen added in v0.3.0

func (state *State) SetFullscreen(enable bool)

SetFullscreen sets if the game should be fullscreen. This does not have an effect on macOS if fullscreen has been enabled natively by the desktop.

This has no effect when running on non-desktops.

func (*State) SetResolution added in v0.3.0

func (state *State) SetResolution(width, height int)

SetResolution sets the resolution of the game.

This has no effect when running on non-desktops.

func (*State) SetVsync added in v0.3.0

func (state *State) SetVsync(enable bool)

SetVsync sets if vsync is enabled or disabled.

func (*State) Switch added in v0.2.0

func (state *State) Switch(worldName, sceneName string) error

Switch switches the currently active world and/or scene.

func (*State) World added in v0.2.0

func (state *State) World(name string) *World

World returns a world with the given name, or nil if none exist.

type UnloadSystem

type UnloadSystem interface {
	Unload(state *State) error
}

type UpdateSystem

type UpdateSystem interface {
	Update(state *State) error
}

type World

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

World is a collection of scenes and systems.

func NewWorld

func NewWorld(name string) *World

func (*World) AddScenes

func (world *World) AddScenes(scenes ...*Scene) *World

AddScenes all the given scenes to the world.

It returns the world, which makes it useful for initialising world by chaining method calls together.

func (*World) AddSystems

func (world *World) AddSystems(systems ...any) *World

AddSystems all the given systems to the world.

It returns the world, which makes it useful for initialising world by chaining method calls together.

func (*World) Name

func (world *World) Name() string

Name returns the world name.

func (*World) Scene

func (world *World) Scene(name string) *Scene

Scene gets a scene with the given name, or nil if it doesn't exist.

func (*World) SetAssets added in v0.4.0

func (world *World) SetAssets(assets any) *World

SetAssets sets the assets for this world. The given value can be a string, an fs.FS, or an embed.FS. If a string is provided, it must be a path to either a folder of assets, or an archive file containing the assets. By default, only .zip files are supported. Use (*State).AddAssetReader to add handlers for other archive types.

It returns the world, which makes it useful for initialising world by chaining method calls together.

func (*World) String added in v0.3.0

func (world *World) String() string

String returns the world name. This is here to satisfy fmt.Stringer.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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