1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2025-02-04 07:37:48 -05:00
OpenDiablo2/d2core/d2components/renderable.go
gravestench b86ac4df84 akara update: BaseComponent and BaseComponentMap
* common component methods have been put into BaseComponent and
BaseComponentMap
* boilerplate code for components has been significantly reduced
* all lint errors fixed in d2components
2020-12-07 12:44:11 -08:00

63 lines
1.9 KiB
Go

//nolint:dupl,golint,stylecheck // component declarations are supposed to look the same
package d2components
import (
"github.com/gravestench/akara"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
)
// static check that RenderableComponent implements Component
var _ akara.Component = &RenderableComponent{}
// static check that RenderableMap implements ComponentMap
var _ akara.ComponentMap = &RenderableMap{}
// RenderableComponent is a component that contains an embedded surface interface, which is used for rendering
type RenderableComponent struct {
*akara.BaseComponent
d2interface.Surface
}
// RenderableMap is a map of entity ID's to Surface
type RenderableMap struct {
*akara.BaseComponentMap
}
// AddRenderable adds a new RenderableComponent for the given entity id and returns it.
// this is a convenience method for the generic Add method, as it returns a
// *RenderableComponent instead of an akara.Component
func (cm *RenderableMap) AddRenderable(id akara.EID) *RenderableComponent {
return cm.Add(id).(*RenderableComponent)
}
// GetRenderable returns the RenderableComponent associated with the given entity id
func (cm *RenderableMap) GetRenderable(id akara.EID) (*RenderableComponent, bool) {
entry, found := cm.Get(id)
if entry == nil {
return nil, false
}
return entry.(*RenderableComponent), found
}
// Surface is a convenient reference to be used as a component identifier
var Surface = newRenderable() // nolint:gochecknoglobals // global by design
func newRenderable() akara.Component {
return &RenderableComponent{
BaseComponent: akara.NewBaseComponent(RenderableCID, newRenderable, newRenderableMap),
}
}
func newRenderableMap() akara.ComponentMap {
baseComponent := akara.NewBaseComponent(RenderableCID, newRenderable, newRenderableMap)
baseMap := akara.NewBaseComponentMap(baseComponent)
cm := &RenderableMap{
BaseComponentMap: baseMap,
}
return cm
}