1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2025-02-10 10:36:42 -05:00
OpenDiablo2/d2core/d2components/viewport.go
gravestench 3f5d2c0938 major refactor of akara ecs
* component ID's are dynamically allocated now
* removed `akara.BaseComponent` member from components
* component declarations drastically reduced
2020-12-07 12:44:11 -08:00

56 lines
1.6 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/d2geom"
)
// static check that Viewport implements Component
var _ akara.Component = &Viewport{}
// Viewport represents the size and position of a scene viewport. This is used
// to control where on screen a viewport is rendered.
type Viewport struct {
*d2geom.Rectangle
}
// New creates a new Viewport. By default, the viewport size is 800x600,
// and is positioned at the top-left of the screen.
func (*Viewport) New() akara.Component {
c := &Viewport{}
const defaultWidth, defaultHeight = 800, 600
c.Rectangle = &d2geom.Rectangle{
Left: 0,
Top: 0,
Width: defaultWidth,
Height: defaultHeight,
}
return c
}
// ViewportFactory is a wrapper for the generic component factory that returns Viewport component instances.
// This can be embedded inside of a system to give them the methods for adding, retrieving, and removing a Viewport.
type ViewportFactory struct {
Viewport *akara.ComponentFactory
}
// AddViewport adds a Viewport component to the given entity and returns it
func (m *ViewportFactory) AddViewport(id akara.EID) *Viewport {
return m.Viewport.Add(id).(*Viewport)
}
// GetViewport returns the Viewport component for the given entity, and a bool for whether or not it exists
func (m *ViewportFactory) GetViewport(id akara.EID) (*Viewport, bool) {
component, found := m.Viewport.Get(id)
if !found {
return nil, found
}
return component.(*Viewport), found
}