1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2025-02-04 07:37:48 -05:00
OpenDiablo2/d2core/d2components/camera.go

70 lines
1.8 KiB
Go
Raw Normal View History

//nolint:dupl,golint,stylecheck // component declarations are supposed to look the same
2020-10-28 18:49:49 -04:00
package d2components
import (
"github.com/gravestench/akara"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
2020-10-28 18:49:49 -04:00
)
// static check that CameraComponent implements Component
var _ akara.Component = &CameraComponent{}
// static check that CameraMap implements ComponentMap
var _ akara.ComponentMap = &CameraMap{}
// CameraComponent represents a camera that can be rendered to
type CameraComponent struct {
*akara.BaseComponent
*d2vector.Vector
Width int
Height int
2020-10-28 18:49:49 -04:00
Zoom float64
}
// CameraMap is a map of entity ID's to Camera
type CameraMap struct {
*akara.BaseComponentMap
2020-10-28 18:49:49 -04:00
}
// AddCamera adds a new CameraComponent for the given entity id and returns it.
// this is a convenience method for the generic Add method, as it returns a
// *CameraComponent instead of an akara.Component
func (cm *CameraMap) AddCamera(id akara.EID) *CameraComponent {
camera := cm.Add(id).(*CameraComponent)
2020-10-28 18:49:49 -04:00
camera.Vector = d2vector.NewVector(0, 0)
2020-10-28 18:49:49 -04:00
return camera
2020-10-28 18:49:49 -04:00
}
// GetCamera returns the CameraComponent associated with the given entity id
func (cm *CameraMap) GetCamera(id akara.EID) (*CameraComponent, bool) {
entry, found := cm.Get(id)
if entry == nil {
return nil, false
2020-10-28 18:49:49 -04:00
}
return entry.(*CameraComponent), found
2020-10-28 18:49:49 -04:00
}
// Camera is a convenient reference to be used as a component identifier
var Camera = newCamera() // nolint:gochecknoglobals // global by design
2020-10-28 18:49:49 -04:00
func newCamera() akara.Component {
return &CameraComponent{
BaseComponent: akara.NewBaseComponent(CameraCID, newCamera, newCameraMap),
}
2020-10-28 18:49:49 -04:00
}
func newCameraMap() akara.ComponentMap {
baseComponent := akara.NewBaseComponent(CameraCID, newCamera, newCameraMap)
baseMap := akara.NewBaseComponentMap(baseComponent)
2020-10-28 18:49:49 -04:00
cm := &CameraMap{
BaseComponentMap: baseMap,
}
return cm
2020-10-28 18:49:49 -04:00
}