mirror of
https://github.com/stevenhowes/GoRetro.git
synced 2026-05-27 00:03:29 +01:00
Move all engine stuff to GoRetro. Still many interactions between game and engine code that there should not be.
This commit is contained in:
+95
@@ -0,0 +1,95 @@
|
||||
package GoRetro
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
)
|
||||
|
||||
type component interface {
|
||||
onUpdate() error
|
||||
onDraw() error
|
||||
onCollision(other *Element) error
|
||||
}
|
||||
|
||||
type Element struct {
|
||||
Renderer *sdl.Renderer
|
||||
Position Vector
|
||||
Rotation float64
|
||||
Active bool
|
||||
Delete bool
|
||||
Collisions []Circle
|
||||
components []component
|
||||
parentElement *Element
|
||||
ZIndex int
|
||||
CollisionLayer int
|
||||
}
|
||||
|
||||
var Elements []*Element
|
||||
|
||||
func (elem *Element) Draw() error {
|
||||
for _, comp := range elem.components {
|
||||
err := comp.onDraw()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (elem *Element) Update() error {
|
||||
for _, comp := range elem.components {
|
||||
err := comp.onUpdate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (elem *Element) collision(other *Element) error {
|
||||
for _, comp := range elem.components {
|
||||
err := comp.onCollision(other)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (elem *Element) AddComponent(new component) {
|
||||
for _, existing := range elem.components {
|
||||
if reflect.TypeOf(new) == reflect.TypeOf(existing) {
|
||||
panic(fmt.Sprintf(
|
||||
"attempt to add new component with existing type %v",
|
||||
reflect.TypeOf(new)))
|
||||
}
|
||||
}
|
||||
elem.components = append(elem.components, new)
|
||||
}
|
||||
|
||||
func (elem *Element) getComponent(withType component) component {
|
||||
typ := reflect.TypeOf(withType)
|
||||
for _, comp := range elem.components {
|
||||
if reflect.TypeOf(comp) == typ {
|
||||
return comp
|
||||
}
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("no component with type %v", reflect.TypeOf(withType)))
|
||||
}
|
||||
|
||||
func (elem *Element) checkComponentIsPresent(withType component) bool {
|
||||
typ := reflect.TypeOf(withType)
|
||||
for _, comp := range elem.components {
|
||||
if reflect.TypeOf(comp) == typ {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user