1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2025-02-13 20:16:41 -05:00
OpenDiablo2/d2core/d2components/scene_main_viewport.go
gravestench 8b1b6b9adc more work on ecs implementation
* removed d2common/d2scene, was not the right way to go.
* added components for animation, scale, main viewport, viewport filter
* added interface for scenes, which are extensions of akara.System
* BootStrap is now AppBootstrap, common to game clients and headless
server
* added generic BasicScene struct for common scene functionality
* added game object factory as a system, with single sprite factory
* added update counter system, shows how many times the world updates
per second
* integration test is now the game client test
2020-12-07 12:44:11 -08:00

98 lines
3.0 KiB
Go

package d2components
import (
"github.com/gravestench/akara"
)
// static check that MainViewportComponent implements Component
var _ akara.Component = &MainViewportComponent{}
// static check that MainViewportMap implements ComponentMap
var _ akara.ComponentMap = &MainViewportMap{}
// MainViewportComponent is a component that is used to tag the main viewport of a scene
type MainViewportComponent struct {}
// ID returns a unique identifier for the component type
func (*MainViewportComponent) ID() akara.ComponentID {
return MainViewportCID
}
// NewMap returns a new component map for the component type
func (*MainViewportComponent) NewMap() akara.ComponentMap {
return NewMainViewportMap()
}
// MainViewport is a convenient reference to be used as a component identifier
var MainViewport = (*MainViewportComponent)(nil) // nolint:gochecknoglobals // global by design
// NewMainViewportMap creates a new map of entity ID's to MainViewport
func NewMainViewportMap() *MainViewportMap {
cm := &MainViewportMap{
components: make(map[akara.EID]*MainViewportComponent),
}
return cm
}
// MainViewportMap is a map of entity ID's to MainViewport
type MainViewportMap struct {
world *akara.World
components map[akara.EID]*MainViewportComponent
}
// Init initializes the component map with the given world
func (cm *MainViewportMap) Init(world *akara.World) {
cm.world = world
}
// ID returns a unique identifier for the component type
func (*MainViewportMap) ID() akara.ComponentID {
return MainViewportCID
}
// NewMap returns a new component map for the component type
func (*MainViewportMap) NewMap() akara.ComponentMap {
return NewMainViewportMap()
}
// Add a new MainViewportComponent for the given entity id, return that component.
// If the entity already has a component, just return that one.
func (cm *MainViewportMap) Add(id akara.EID) akara.Component {
if com, has := cm.components[id]; has {
return com
}
cm.components[id] = &MainViewportComponent{}
cm.world.UpdateEntity(id)
return cm.components[id]
}
// AddMainViewport adds a new MainViewportComponent for the given entity id and returns it.
// If the entity already has a file type component, just return that one.
// this is a convenience method for the generic Add method, as it returns a
// *MainViewportComponent instead of an akara.Component
func (cm *MainViewportMap) AddMainViewport(id akara.EID) *MainViewportComponent {
return cm.Add(id).(*MainViewportComponent)
}
// Get returns the component associated with the given entity id
func (cm *MainViewportMap) Get(id akara.EID) (akara.Component, bool) {
entry, found := cm.components[id]
return entry, found
}
// GetMainViewport returns the MainViewportComponent associated with the given entity id
func (cm *MainViewportMap) GetMainViewport(id akara.EID) (*MainViewportComponent, bool) {
entry, found := cm.components[id]
return entry, found
}
// Remove a component for the given entity id, return the component.
func (cm *MainViewportMap) Remove(id akara.EID) {
delete(cm.components, id)
cm.world.UpdateEntity(id)
}