Documentation ¶
Overview ¶
Package engine contains generic implementations of game logic and asset management.
Index ¶
- Constants
- Variables
- func AngleToCardinal(angle float64) byte
- type Animation
- type Asset
- type Atlas
- type Camera
- type Collider
- type Component
- type Context
- type CoreEntity
- type CursorMode
- type Entity
- type Game
- type Hitbox
- type Image
- type Input
- type IsoRenderer
- type PartitionEntry
- type PartitionMap
- type Renderer
- type Sound
- type SoundControl
- type StateMachine
- func (sm *StateMachine) HandleCallbacks()
- func (sm *StateMachine) Is(state uint64) bool
- func (sm *StateMachine) IsOnly(state uint64) bool
- func (sm *StateMachine) RemoveCallback(state uint64)
- func (sm *StateMachine) SetAll(active bool)
- func (sm *StateMachine) SetCallback(state uint64, cb func())
- func (sm *StateMachine) SetState(state uint64, active bool)
- func (sm *StateMachine) State() uint64
- func (sm *StateMachine) UpdateState(state uint64, active bool)
- type TileOverlapEvent
- type Tilemap
- type Vec2
Constants ¶
Cardinal directions.
const ( // FlagResizable indicates that the viewport may be resized. FlagResizable = 1 << iota // FlagRunsInBackground indicates that logic will continue when the game is unfocused. FlagRunsInBackground )
const ( Key0 = iota Key1 Key2 Key3 Key4 Key5 Key6 Key7 Key8 Key9 KeyA KeyB KeyC KeyD KeyE KeyF KeyG KeyH KeyI KeyJ KeyK KeyL KeyM KeyN KeyO KeyP KeyQ KeyR KeyS KeyT KeyU KeyV KeyW KeyX KeyY KeyZ KeyApostrophe KeyBackslash KeyBackspace KeyCapsLock KeyComma KeyDelete KeyDown KeyEnd KeyEnter KeyEqual KeyEscape KeyF1 KeyF2 KeyF3 KeyF4 KeyF5 KeyF6 KeyF7 KeyF8 KeyF9 KeyF10 KeyF11 KeyF12 KeyGraveAccent KeyHome KeyInsert KeyKP0 KeyKP1 KeyKP2 KeyKP3 KeyKP4 KeyKP5 KeyKP6 KeyKP7 KeyKP8 KeyKP9 KeyKPAdd KeyKPDecimal KeyKPDivide KeyKPEnter KeyKPEqual KeyKPMultiply KeyKPSubtract KeyLeft KeyLeftBracket KeyMenu KeyMinus KeyNumLock KeyPageDown KeyPageUp KeyPause KeyPeriod KeyPrintScreen KeyRight KeyRightBracket KeyScrollLock KeySemicolon KeySlash KeySpace KeyTab KeyUp KeyAlt KeyControl KeyShift )
Keyboard keys.
const ( MouseButtonLeft = iota MouseButtonRight MouseButtonMiddle )
Mouse buttons.
Variables ¶
var CardinalToAngle = map[byte]float64{ N: 3 * math.Pi / 2, E: 0, S: math.Pi / 2, W: math.Pi, SE: math.Pi/6 - diag, SW: 5*math.Pi/6 + diag, NE: -(math.Pi / 6) + diag, NW: math.Pi + math.Pi/6 - diag, }
CardinalToAngle is a map of cardinal directions to angles in dimetric space.
Functions ¶
func AngleToCardinal ¶
AngleToCardinal convert an angle to a cardinal direction.
Types ¶
type Component ¶
type Component interface { NewAssetFromPath(string) (Asset, error) NewImageFromPath(string) (Image, error) NewImageFromAssetPath(string) (Image, error) NewImageFromImage(image.Image) Image NewTextImage(string, int, int, font.Face, color.Color) Image NewAtlasFromAssetPath(string) (Atlas, error) NewAnimationFromAssetPath(string) (Animation, error) NewSoundFromAssetPath(string) (Sound, error) NewRenderer() Renderer NewIsoRenderer() IsoRenderer NewTilemap(int, [2][][]int, map[int]Image, TileOverlapEvent) Tilemap NewCamera() Camera NewCollider() Collider }
Component produces engine components.
type Context ¶
Context is a rendering context.
func NewContext ¶
NewContext creates a Context with the given Renderer and Collider.
func (*Context) GetEntities ¶
GetEntities gets the Context's Entities.
type CoreEntity ¶
type CoreEntity struct { Vec2 // contains filtered or unexported fields }
CoreEntity is a default Entity implementation.
func (*CoreEntity) AddImage ¶
func (e *CoreEntity) AddImage(image ...Image)
AddImage adds an Image to the CoreEntity.
func (*CoreEntity) Dispose ¶
func (e *CoreEntity) Dispose()
Dispose marks the CoreEntity as disposed, and disposes its Images.
func (*CoreEntity) Images ¶
func (e *CoreEntity) Images() []Image
Images gets the CoreEntity's Images.
func (*CoreEntity) IsDisposed ¶
func (e *CoreEntity) IsDisposed() bool
IsDisposed checks if the CoreEntity has been disposed.
func (*CoreEntity) Position ¶
func (e *CoreEntity) Position() Vec2
Position gets the CoreEntity's current position.
func (*CoreEntity) SetCollider ¶
func (e *CoreEntity) SetCollider(collider Collider)
SetCollider sets the CoreEntity's Collider.
type CursorMode ¶
type CursorMode byte
CursorMode indicates a cursor display mode.
const ( // CursorModeVisible indicates normal cursor display. CursorModeVisible CursorMode = 1 << iota // CursorModeHidden indicates a hidden cursor that may escape the window. CursorModeHidden // CursorModeCaptured indicates a hidden cursor that may not escape the window. CursorModeCaptured )
type Entity ¶
type Entity interface { Tick() SetCollider(Collider) Position() Vec2 AddImage(...Image) Images() []Image Class() string Dispose() IsDisposed() bool }
Entity is a basic game entity.
type Game ¶
type Game interface { // Run starts running the game. Run() error // AddRenderer adds a renderer // to the game's draw stack. Renderers will // be applied in the order they are added. AddRenderer(...Renderer) // IsFullscreen returns the fullscreen state of the game. IsFullscreen() bool // SetFullscreen sets the fullscreen state of the game. SetFullscreen(bool) // IsVsync returns the vsync state of the game. IsVsync() bool // SetVsync sets the vsync state of the game. SetVsync(bool) // IsFocused returns the focused state of the game. IsFocused() bool Component Input SoundControl }
Game is an engine instance.
type Hitbox ¶
type Hitbox interface { Position() Vec2 HitBounds() image.Rectangle // TODO upgrade to float based approach? HitClasses() []string Hit(Entity) }
Hitbox is an object with collision support.
type Image ¶
type Image interface { // Translate sets the x y translation of the image relative to the origin. Translate(float64, float64) // Offset applies an offset to the image translation. // This can be useful for relative positioning to a parent translation. Offset(float64, float64) // Scale sets the x y scale of the image relative to the origin. Scale(float64, float64) // Rotate sets the rotation in radians relative to the origin. Rotate(float64) // Origin sets the coordinate origin of the image in percent ranging from 0.0 to 1.0 Origin(float64, float64) // SetZDepth sets a z value to override draw order. SetZDepth(int) // Tint scales the image colors by a factor of each value. Tint(float64, float64, float64) // Alpha sets the image's alpha channel with a range of 0.0 to 1.0 Alpha(float64) // SetRenderable sets whether or not an image should be rendered. SetRenderable(bool) // IsRenderable indicates whether or not an image should be rendered. IsRenderable() bool // RoundTranslations sets the rounding state for image translations. // If set to true, image translations are rounded to the nearest integer. // Defaults to true. RoundTranslations(bool) // TriggersTileOverlapEvent determines whether tile overlap events will occur. // A tile overlap is when an image is behind a tile in the isometric renderer. TriggersTileOverlapEvent(bool) // Size returns the size of the image. Size() (int, int) // Dispose marks the image to be disposed. Dispose() // IsDisposed indicates if the image has been disposed. IsDisposed() bool Position() Vec2 Class() string }
An Image represents a single unchanging image that can be applied to a renderer.
type Input ¶
type Input interface { // Keyboard IsAnyKeyPressed() bool IsAnyKeyJustPressed() bool IsKeyPressed(int) bool IsKeyJustPressed(int) bool IsKeyJustReleased(int) bool // Mouse IsMouseButtonPressed(int) bool IsMouseButtonJustPressed(int) bool IsMouseButtonJustReleased(int) bool CursorPosition() (int, int) SetCursorBounds(int, int, int, int) SetCursorMode(CursorMode) }
Input describes various input detection methods.
type IsoRenderer ¶
IsoRenderer is an isometric renderer.
type PartitionEntry ¶
PartitionEntry is a type that can be used in a PartitionMap.
type PartitionMap ¶
type PartitionMap struct {
// contains filtered or unexported fields
}
PartitionMap handles spatial partitioning of PartitionEntry.
func NewPartitionMap ¶
func NewPartitionMap(partitionSize, bucketCount int) *PartitionMap
NewPartitionMap returns a PartitionMap with a given range interval, and initial map bucket count.
func (*PartitionMap) Add ¶
func (pm *PartitionMap) Add(e PartitionEntry) [2]int
Add inserts a PartitionEntry into the PartitionMap, and returns the map key.
func (*PartitionMap) Class ¶
func (pm *PartitionMap) Class(class string) []PartitionEntry
Class returns all entries of a given class in the current buffer.
func (*PartitionMap) Tick ¶
func (pm *PartitionMap) Tick( pos Vec2, size int, tickFunc func([]PartitionEntry), )
Tick updates partitions around a position.
type Renderer ¶
type Renderer interface { // AddImage adds one or more images to the renderer's draw stack. // Images are drawn in the order they are added. AddImage(...Image) SetCamera(Camera) ScreenToWorld(Vec2) Vec2 SetViewport(int, int) Viewport() image.Rectangle // Tick is called by the Game engine each tick. Tick should not be invoked manually Tick() }
A Renderer is a basic context for drawing images.
type Sound ¶
type Sound interface { // Play plays the audio from the current // position to the end. An error may be returned // if a sound cannot be properly decoded. Play() error // Loop plays the audio from the current // position, and repeats from the start // after reaching the end. An error may be returned // if a sound cannot be properly decoded. Loop() error // Pause stops the sound for playing, // keeping the current position. Pause() // Reset seeks to the start of the sound. // Reset will also pause the track. Reset() // Close releases the underlying audio assets. // The user must call Close when they are done using the Sound. // Close does not need to be called when reusing // a given sound. Close() }
Sound represents music or a sound effect.
A Sound may have multiple options for its audio track, randomly selection one each time the Sound is played.
Sounds also belong to a specified control group. Groups can be modified in batches by a SoundControl.
type SoundControl ¶
type SoundControl interface { // SetVolume sets the playback volume // for a given sound group, between 0.0 and 1.0 // inclusively. Using an empty string for the sound // group will apply the volume to all groups. SetVolume(string, float64) // Volume returns the playback volume // for a given sound group. Volume(string) float64 }
SoundControl is a global control for all sounds.
type StateMachine ¶
type StateMachine struct {
// contains filtered or unexported fields
}
StateMachine handles states and their associated callbacks. States are represented as bitmasks, allowing multiple states to be active at the same time.
func NewStateMachine ¶
func NewStateMachine() *StateMachine
NewStateMachine returns an instantiated StateMachine.
func (*StateMachine) HandleCallbacks ¶
func (sm *StateMachine) HandleCallbacks()
HandleCallbacks runs all callbacks for every active state.
func (*StateMachine) Is ¶
func (sm *StateMachine) Is(state uint64) bool
Is indicates whether a given state is active.
func (*StateMachine) IsOnly ¶
func (sm *StateMachine) IsOnly(state uint64) bool
IsOnly indicates whether a given state is active, and that it is the only active state.
func (*StateMachine) RemoveCallback ¶
func (sm *StateMachine) RemoveCallback(state uint64)
RemoveCallback removes a callback for a given state.
func (*StateMachine) SetAll ¶
func (sm *StateMachine) SetAll(active bool)
SetAll marks all states as active or not.
func (*StateMachine) SetCallback ¶
func (sm *StateMachine) SetCallback(state uint64, cb func())
SetCallback sets a given callback function for a given state.
func (*StateMachine) SetState ¶
func (sm *StateMachine) SetState(state uint64, active bool)
SetState deactivates all previous states and only sets the state specified.
func (*StateMachine) State ¶
func (sm *StateMachine) State() uint64
State returns the full state bitmask.
func (*StateMachine) UpdateState ¶
func (sm *StateMachine) UpdateState(state uint64, active bool)
UpdateState marks a given state as active or not.
type TileOverlapEvent ¶
TileOverlapEvent updates renderer state in the case of a tile overlap.
type Tilemap ¶
type Tilemap interface{}
Tilemap is a placeholder for various tilemap implementations.
type Vec2 ¶
type Vec2 struct {
X, Y float64
}
Vec2 represents a point in 2D space.