ichigo/engine/interface.go

119 lines
3.3 KiB
Go
Raw Normal View History

2021-08-05 12:26:41 +10:00
package engine
2021-08-05 12:33:23 +10:00
import (
"image"
2021-08-23 10:09:49 +10:00
"io/fs"
2021-08-27 15:39:10 +10:00
"reflect"
2021-08-05 12:33:23 +10:00
"github.com/hajimehoshi/ebiten/v2"
)
2021-08-27 15:39:10 +10:00
// Reflection types used for queries... Is there a better way?
var (
// TypeOf(pointer to interface).Elem() is "idiomatic" -
// see https://pkg.go.dev/reflect#example-TypeOf
2021-09-01 10:27:13 +10:00
BounderType = reflect.TypeOf((*Bounder)(nil)).Elem()
ColliderType = reflect.TypeOf((*Collider)(nil)).Elem()
DisablerType = reflect.TypeOf((*Disabler)(nil)).Elem()
DrawerType = reflect.TypeOf((*Drawer)(nil)).Elem()
HiderType = reflect.TypeOf((*Hider)(nil)).Elem()
IdentifierType = reflect.TypeOf((*Identifier)(nil)).Elem()
LoaderType = reflect.TypeOf((*Loader)(nil)).Elem()
PrepperType = reflect.TypeOf((*Prepper)(nil)).Elem()
ScannerType = reflect.TypeOf((*Scanner)(nil)).Elem()
SaverType = reflect.TypeOf((*Saver)(nil)).Elem()
TransformerType = reflect.TypeOf((*Transformer)(nil)).Elem()
UpdaterType = reflect.TypeOf((*Updater)(nil)).Elem()
2021-08-27 15:39:10 +10:00
// Behaviours lists all the behaviours that can be queried with Game.Query.
Behaviours = []reflect.Type{
BounderType,
ColliderType,
DisablerType,
DrawerType,
HiderType,
IdentifierType,
LoaderType,
PrepperType,
ScannerType,
SaverType,
2021-08-31 18:19:14 +10:00
TransformerType,
2021-08-27 15:39:10 +10:00
UpdaterType,
}
)
2021-08-23 10:34:56 +10:00
// Bounder components have a bounding rectangle.
type Bounder interface {
BoundingRect() image.Rectangle
}
2021-08-05 12:33:23 +10:00
// Collider components have tangible form.
type Collider interface {
CollidesWith(image.Rectangle) bool
}
2021-08-05 12:26:41 +10:00
2021-08-23 11:10:46 +10:00
// Disabler components can be disabled.
type Disabler interface {
IsDisabled() bool
Disable()
Enable()
}
2021-09-01 09:32:36 +10:00
// Drawer components can draw themselves. Draw is called often. Each component
// must call Draw on any internal components not known to the engine (i.e. not
// passed to Game.Register or returned from Scan).
2021-08-05 12:26:41 +10:00
type Drawer interface {
Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions)
2021-08-18 14:25:51 +10:00
DrawOrder() float64
}
2021-08-23 10:34:56 +10:00
// Hider components can be hidden.
type Hider interface {
IsHidden() bool
Hide()
Show()
}
2021-08-05 12:26:41 +10:00
// Identifier components have a sense of self. This makes it easier for
// components to find and interact with one another.
type Identifier interface {
Ident() string
}
2021-08-20 15:01:31 +10:00
// Loader components get the chance to load themselves. This happens
// before preparation.
type Loader interface {
2021-08-23 10:09:49 +10:00
Load(fs.FS) error
2021-08-20 15:01:31 +10:00
}
2021-08-05 12:26:41 +10:00
// Prepper components can be prepared. It is called after the component
// database has been populated but before the game is run. The component can
// store the reference to game, if needed, and also query the component database.
type Prepper interface {
2021-08-27 14:52:24 +10:00
Prepare(game *Game) error
2021-08-05 12:26:41 +10:00
}
// Scanner components can be scanned. It is called when the game tree is walked
// (such as when the game component database is constructed).
// Scan should return a slice containing all immediate subcomponents.
type Scanner interface {
Scan() []interface{}
}
2021-08-25 16:46:30 +10:00
// Saver components can be saved to disk.
type Saver interface {
Save() error
}
2021-08-31 18:19:14 +10:00
// Transformer components can transform their child components.
type Transformer interface {
Transform() ebiten.DrawImageOptions
}
2021-09-01 09:32:36 +10:00
// Updater components can update themselves. Update is called repeatedly. Each
// component must call Update on any internal components not known to the engine
// (i.e. not passed to Game.Register or returned from Scan).
2021-08-05 12:26:41 +10:00
type Updater interface {
Update() error
}