engine

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package engine contains generic implementations of game logic and asset management.

Index

Constants

View Source
const (
	N = 1 << iota
	E
	S
	W

	NE = N | E
	NW = N | W
	SE = S | E
	SW = S | W
)

Cardinal directions.

View Source
const (
	// FlagResizable indicates that the viewport may be resized.
	FlagResizable = 1 << iota

	// FlagRunsInBackground indicates that logic will continue when the game is unfocused.
	FlagRunsInBackground
)
View Source
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.

View Source
const (
	MouseButtonLeft = iota
	MouseButtonRight
	MouseButtonMiddle
)

Mouse buttons.

Variables

View Source
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

func AngleToCardinal(angle float64) byte

AngleToCardinal convert an angle to a cardinal direction.

Types

type Animation

type Animation interface {
	SetState(string)

	SetTickCount(int)
	Play()
	Pause()
	Reset()

	Image
}

Animation is a series of frames that play in sequence.

type Asset

type Asset interface {
	ToImage() Image
	ToAtlas() Atlas
	ToAnimation() Animation
	ToSound() Sound
}

Asset is a generic asset container.

type Atlas

type Atlas interface {
	GetImage(string) Image
}

Atlas is a set of named Images.

type Camera

type Camera interface {
	LookAt(float64, float64, float64)
	Position() (float64, float64)
}

Camera manages the area being viewed.

type Collider

type Collider interface {
	SetTilemap(Tilemap)
	Resolve(Vec2, Vec2) Vec2
}

Collider resolves collisions.

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

type Context struct {
	Renderer
	Collider
	// contains filtered or unexported fields
}

Context is a rendering context.

func NewContext

func NewContext(renderer Renderer, collider Collider) *Context

NewContext creates a Context with the given Renderer and Collider.

func (*Context) AddEntity

func (c *Context) AddEntity(entities ...Entity)

AddEntity adds Entities to the Context.

func (*Context) GetEntities

func (c *Context) GetEntities(class string) []Entity

GetEntities gets the Context's Entities.

func (*Context) Tick

func (c *Context) Tick()

Tick updates the Context's internal state.

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.

func (*CoreEntity) Tick

func (e *CoreEntity) Tick()

Tick updates the CoreEntity's position.

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

type IsoRenderer interface {
	Renderer
	SetTilemap(Tilemap)
}

IsoRenderer is an isometric renderer.

type PartitionEntry

type PartitionEntry interface {
	IsDisposed() bool
	Position() Vec2
	Class() string
}

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

type TileOverlapEvent func(bool, Image, interface{}) interface{}

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.

func (Vec2) AngleTo

func (v Vec2) AngleTo(v2 Vec2) float64

AngleTo returns the angle in radians between two vectors.

func (Vec2) Distance

func (v Vec2) Distance(v2 Vec2) float64

Distance returns the distance between two vectors.

func (Vec2) Lerp

func (v Vec2) Lerp(v2 Vec2, t float64) Vec2

Lerp returns the linear interpolation between two vectors by percent t.

func (Vec2) Translate

func (v Vec2) Translate(d float64, m float64) Vec2

Translate returns the translation of a vector towards direction d by magnitude m.

Jump to

Keyboard shortcuts

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