engine

package
v0.0.0-...-1d1e069 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultLayoutFunc

func DefaultLayoutFunc(w, h int) (int, int)

DefaultLayoutFunc uses 1:1 scaling.

Types

type Clock

type Clock struct {
	// The time in real world seconds.
	Time float64

	// The game time. This is the time that Update() was last called. This should be
	// used for game logic instead of Time.
	GameTime float64

	// The amount of time spent on the previous tick.
	DeltaTime float64
}

Clock represents the game clock.

type Component

type Component interface{}

Component is an empty interface for any component type. A component can be anything, though typically it is used by a system and thus implements that system's interface.

type ComponentContainer

type ComponentContainer interface {
	// Components returns a list of components for the object.
	Components() []Component
}

ComponentContainer is an interface for an object which has a list of components.

type Drawer

type Drawer interface {
	Draw(screen *ebiten.Image)
}

Drawer is an interface for the draw system. When a component implements this interface it will have its Draw() method called every frame.

type EmitNotifyFunc

type EmitNotifyFunc func(t EventType, data interface{})

EmitNotifyFunc is a function type which is called when an event is emitted.

type Emitter

type Emitter interface {
	// Emit broadcasts an event to all listeners. The event type must be a single event and
	// cannot be EventAll. Panics if the event type is invalid.
	Emit(t EventType, data interface{})
}

Emitter is an interface for the Emit method which is used for emitting an event type and payload.

type Engine

type Engine interface {
	ebiten.Game
	Draw(screen *ebiten.Image)

	Entities() EntityManager
	Events() EventManager
	Scenes() SceneManager
	Systems() SystemManager

	SetInitialScene(scene Scene)
	SetLayout(fn LayoutFunc)
}

Engine is the primary interface used both by ebiten and accessing various managers.

func NewEngine

func NewEngine() Engine

NewEngine returns a ready-to-use engine.

type Entity

type Entity interface {
	ComponentContainer

	// AddComponent adds a component to the entity.
	AddComponent(c ...Component)

	// ID returns the entity's ID
	ID() int64
}

Entity is an interface for an entity in the game (i.e. game object). Every entity has a unique ID and a list of components. The components contain all of the logic and graphics for the entity.

func NewEntity

func NewEntity(id int64) Entity

NewEntity returns a new Entity with the given ID and no components.

type EntityManager

type EntityManager interface {
	// Add adds an entity to the manager.
	Add(e Entity)

	// Create creates a new entity using the components from the given container, adds
	// the entity to the manager, then returns it.
	Create(container ComponentContainer) Entity

	// Get returns an entity with the given ID, or nil if it doesn't exist.
	Get(id int64) Entity

	// NextID returns the next available entity ID.
	NextID() int64

	// Pop removes and returns an entity that was previously added. If no entity with the
	// given ID exists, returns nil.
	Pop(id int64) Entity

	// Reset removes all entities from the manager and resets the entity ID counter.
	Reset()
}

EntityManager stores a collection of entities and keeps track of an incrementing ID which is used for creating new entities.

func NewEntityManager

func NewEntityManager(events Emitter) EntityManager

NewEntityManager creates a new EntityManager. This function accepts an optional Emitter argument which when provided will create an EntityManager which emits entity added/removed events.

type EventListener

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

EventListener describes an observer which has subscribed to the event emitter.

type EventManager

type EventManager interface {
	Emitter

	// AddListener takes a notifier function and a list of event types. When an event is emitted
	// that matches any of the provided types the notifier will be called. To listen for all events,
	// use EventAll. Returns the event listener ID which can be passed to RemoveListener().
	AddListener(fn EmitNotifyFunc, types ...EventType) int

	// RemoveListener takes a listener previously created by AddListener() and removes it. Panics
	// if the listener is nil.
	RemoveListener(id int)
}

EventManager is an interface for an event emitter that notifies listeners when events are emitted.

func NewEventManager

func NewEventManager() EventManager

NewEventManager creates a new blank EventManager.

type EventType

type EventType int

EventType describes what kind of event is being emitted.

const (
	EventAll EventType = iota

	// EventEntityAdded is fired when a new entity is added to the entity manager.
	// The event data is the entity that was added.
	EventEntityAdded

	// EventEntityAdded is fired when an entity is removed from the entity manager.
	// The event data is the entity that was removed.
	EventEntityRemoved

	// EventSceneChanged is fired when switching to a new scene.
	// The event data is a SceneChangedArgs object.
	EventSceneChanged
)

Various types of events accepted by an Emitter.

type LayoutFunc

type LayoutFunc func(w, h int) (int, int)

LayoutFunc is used by ebiten to determine how the rendering should be scaled. See ebiten.Game.Layout for more info.

func FixedLayout

func FixedLayout(fixedWidth, fixedHeight int) LayoutFunc

FixedLayout returns a new LayoutFunc which always returns the given width and height.

type PostUpdateFunc

type PostUpdateFunc func()

PostUpdateFunc is an optional function that is returned by a component's Update() method. If a component returns a non-nil value, the function is deferred until all other components have completed updating.

The purpose of this is to defer all state changes until after update processing has finished. This avoids the problem of components behaving differently when they are processed in different orders.

Thus, if a component needs to perform a state change, it should do so only in the PostUpdateFunc return.

type ProcessArgs

type ProcessArgs struct {
	Clock Clock
}

ProcessArgs is used to give information to systems during processing.

type ProcessTrigger

type ProcessTrigger int

ProcessTrigger represents when a system should run processing.

const (
	// OnUpdate triggers systems to run during the update phase.
	OnUpdate ProcessTrigger = iota

	// OnDraw triggers systems to run during the draw phase.
	OnDraw
)

type Scene

type Scene interface {
	// Setup is called immediately after switching to a new scene.
	Setup(engine Engine)

	// Teardown is called immediately after switching from one scene to another.
	Teardown(engine Engine)
}

Scene is an interface for displaying a scene.

type SceneChangedArgs

type SceneChangedArgs struct {
	Previous Scene
	Current  Scene
}

type SceneManager

type SceneManager interface {
	Current() Scene
	SwitchTo(s Scene)
}

func NewSceneManager

func NewSceneManager(engine Engine) SceneManager

type System

type System interface {
	// CanProcess returns true if the system supports this component.
	CanProcess(c Component) bool

	// Process runs the system logic for the component.
	Process(c Component, args ProcessArgs)

	// PreProcess is called before any processing begins.
	PreProcess()

	// PostProcess is called after all processing has finished.
	PostProcess()
}

System is an interface for handling components that have logic or behavior.

func NewDrawSystem

func NewDrawSystem(screen *ebiten.Image) System

NewDrawSystem returns a new draw system.

func NewUpdateSystem

func NewUpdateSystem() System

NewUpdateSystem returns a new update system.

type SystemManager

type SystemManager interface {
	// AddComponent adds a component to the SystemManager.
	AddComponent(c Component)

	// AddSystem adds a new system to the SystemManager.
	AddSystem(s System, on ProcessTrigger)

	// ProcessAll iterates through each system that matches the given trigger and processes
	// all of its components.
	ProcessAll(args ProcessArgs, on ProcessTrigger)

	// RemoveComponent removes the given component from all systems.
	RemoveComponent(c Component)

	// Reset clears all components from all systems.
	Reset()
}

SystemManager is an interface for managing systems and their components.

func NewSystemManager

func NewSystemManager() SystemManager

NewSystemManager creates a new SystemManager.

type Updater

type Updater interface {
	Update(clock Clock) PostUpdateFunc
}

Updater is an interface for the update system. When a component implements this interface it will have its Update() method called every tick.

Jump to

Keyboard shortcuts

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