2020-06-21 18:40:37 -04:00
|
|
|
package d2maprenderer
|
2019-12-08 22:18:42 -05:00
|
|
|
|
2020-07-18 23:37:35 -04:00
|
|
|
import "github.com/OpenDiablo2/OpenDiablo2/d2common/d2math/d2vector"
|
|
|
|
|
|
|
|
// Camera is the position of the Camera perspective in orthogonal world space. See viewport.go.
|
2019-12-08 22:18:42 -05:00
|
|
|
type Camera struct {
|
2020-07-18 23:37:35 -04:00
|
|
|
position *d2vector.Position
|
|
|
|
target *d2vector.Position
|
|
|
|
}
|
|
|
|
|
|
|
|
// MoveTo sets the position of the Camera to the given position
|
|
|
|
func (c *Camera) MoveTo(position *d2vector.Position) {
|
|
|
|
c.position = position
|
|
|
|
}
|
|
|
|
|
|
|
|
// MoveBy adds the given vector to the current position of the Camera.
|
|
|
|
func (c *Camera) MoveBy(vector *d2vector.Vector) {
|
|
|
|
c.position.Add(vector)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTarget sets the target position
|
|
|
|
func (c *Camera) SetTarget(target *d2vector.Position) {
|
|
|
|
c.target = target
|
|
|
|
}
|
|
|
|
|
|
|
|
// MoveBy adds the given vector to the current position of the Camera.
|
|
|
|
func (c *Camera) MoveTargetBy(vector *d2vector.Vector) {
|
|
|
|
if c.target == nil {
|
|
|
|
v := c.position.Clone()
|
|
|
|
c.target = &d2vector.Position{v}
|
|
|
|
}
|
|
|
|
c.target.Add(vector)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClearTarget sets the target position
|
|
|
|
func (c *Camera) ClearTarget() {
|
|
|
|
c.target = nil
|
2019-12-08 22:18:42 -05:00
|
|
|
}
|
|
|
|
|
2020-07-18 23:37:35 -04:00
|
|
|
// GetPosition returns the Camera position
|
|
|
|
func (c *Camera) GetPosition() *d2vector.Position {
|
|
|
|
return c.position
|
2019-12-08 22:18:42 -05:00
|
|
|
}
|
|
|
|
|
2020-07-18 23:37:35 -04:00
|
|
|
// Advance returns the Camera position
|
|
|
|
func (c *Camera) Advance(elapsed float64) {
|
|
|
|
c.advanceToTarget(elapsed)
|
2019-12-08 22:18:42 -05:00
|
|
|
}
|
|
|
|
|
2020-07-18 23:37:35 -04:00
|
|
|
func (c *Camera) advanceToTarget(_ float64) {
|
|
|
|
if c.target != nil {
|
|
|
|
diff := c.position.World().Subtract(c.target.World())
|
|
|
|
diff.Scale(-0.85)
|
|
|
|
c.MoveBy(diff)
|
|
|
|
}
|
2019-12-08 22:18:42 -05:00
|
|
|
}
|