2020-02-01 18:55:56 -05:00
|
|
|
package d2ui
|
2019-10-24 09:31:59 -04:00
|
|
|
|
|
|
|
import (
|
2020-01-31 23:18:11 -05:00
|
|
|
"errors"
|
2020-06-30 12:43:13 -04:00
|
|
|
"image"
|
2019-10-24 19:13:30 -04:00
|
|
|
"image/color"
|
2019-10-24 09:31:59 -04:00
|
|
|
|
2020-07-08 21:57:35 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-06-29 00:41:58 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
|
|
|
2020-02-01 21:06:22 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
2019-10-24 09:31:59 -04:00
|
|
|
)
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// Sprite is a positioned visual object.
|
2019-10-24 09:31:59 -04:00
|
|
|
type Sprite struct {
|
2019-12-28 23:32:24 -05:00
|
|
|
x int
|
|
|
|
y int
|
2020-07-05 13:01:44 -04:00
|
|
|
animation d2interface.Animation
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
2019-11-24 17:58:23 -05:00
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
var (
|
2020-02-01 21:51:49 -05:00
|
|
|
ErrNoAnimation = errors.New("no animation was specified")
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
2019-12-15 14:07:57 -05:00
|
|
|
|
2020-07-05 13:01:44 -04:00
|
|
|
func LoadSprite(animation d2interface.Animation) (*Sprite, error) {
|
2020-01-31 23:18:11 -05:00
|
|
|
if animation == nil {
|
|
|
|
return nil, ErrNoAnimation
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
2020-01-31 23:18:11 -05:00
|
|
|
return &Sprite{animation: animation}, nil
|
2019-11-10 10:44:13 -05:00
|
|
|
}
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
func (s *Sprite) Render(target d2interface.Surface) error {
|
2019-12-21 20:53:18 -05:00
|
|
|
_, frameHeight := s.animation.GetCurrentFrameSize()
|
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
target.PushTranslation(s.x, s.y-frameHeight)
|
|
|
|
defer target.Pop()
|
2020-06-30 12:43:13 -04:00
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
return s.animation.Render(target)
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// RenderSection renders the section of the sprite enclosed by bounds
|
|
|
|
func (s *Sprite) RenderSection(sfc d2interface.Surface, bound image.Rectangle) error {
|
|
|
|
sfc.PushTranslation(s.x, s.y-bound.Dy())
|
|
|
|
defer sfc.Pop()
|
|
|
|
|
|
|
|
return s.animation.RenderSection(sfc, bound)
|
|
|
|
}
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
func (s *Sprite) RenderSegmented(target d2interface.Surface, segmentsX, segmentsY, frameOffset int) error {
|
2019-12-21 20:53:18 -05:00
|
|
|
var currentY int
|
2020-06-30 12:43:13 -04:00
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
for y := 0; y < segmentsY; y++ {
|
|
|
|
var currentX int
|
|
|
|
var maxFrameHeight int
|
2020-06-30 12:43:13 -04:00
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
for x := 0; x < segmentsX; x++ {
|
|
|
|
if err := s.animation.SetCurrentFrame(x + y*segmentsX + frameOffset*segmentsX*segmentsY); err != nil {
|
|
|
|
return err
|
2019-11-13 00:31:52 -05:00
|
|
|
}
|
2019-12-21 20:53:18 -05:00
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
target.PushTranslation(s.x+currentX, s.y+currentY)
|
|
|
|
err := s.animation.Render(target)
|
|
|
|
target.Pop()
|
2020-06-30 12:43:13 -04:00
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
if err != nil {
|
2019-12-21 20:53:18 -05:00
|
|
|
return err
|
2019-10-27 02:58:37 -04:00
|
|
|
}
|
2019-12-21 20:53:18 -05:00
|
|
|
|
|
|
|
frameWidth, frameHeight := s.GetCurrentFrameSize()
|
2020-02-01 21:06:22 -05:00
|
|
|
maxFrameHeight = d2common.MaxInt(maxFrameHeight, frameHeight)
|
2019-12-21 20:53:18 -05:00
|
|
|
currentX += frameWidth
|
2019-10-27 02:58:37 -04:00
|
|
|
}
|
2019-12-21 20:53:18 -05:00
|
|
|
|
|
|
|
currentY += maxFrameHeight
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
2019-12-21 20:53:18 -05:00
|
|
|
|
|
|
|
return nil
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// SetPosition places the sprite in 2D
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) SetPosition(x, y int) {
|
|
|
|
s.x = x
|
|
|
|
s.y = y
|
2019-10-27 02:58:37 -04:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// GetPosition retrieves the 2D position of the sprite
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) GetPosition() (int, int) {
|
|
|
|
return s.x, s.y
|
2019-10-27 02:58:37 -04:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// GetFrameSize gets the Size(width, height) of a indexed frame.
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) GetFrameSize(frameIndex int) (int, int, error) {
|
|
|
|
return s.animation.GetFrameSize(frameIndex)
|
2019-10-25 23:41:54 -04:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// GetCurrentFrameSize gets the Size(width, height) of the current frame.
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) GetCurrentFrameSize() (int, int) {
|
|
|
|
return s.animation.GetCurrentFrameSize()
|
2019-10-25 23:41:54 -04:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// GetFrameBounds gets maximum Size(width, height) of all frame.
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) GetFrameBounds() (int, int) {
|
|
|
|
return s.animation.GetFrameBounds()
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// GetCurrentFrame gets index of current frame in animation
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) GetCurrentFrame() int {
|
|
|
|
return s.animation.GetCurrentFrame()
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// GetFrameCount gets number of frames in animation
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) GetFrameCount() int {
|
|
|
|
return s.animation.GetFrameCount()
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// IsOnFirstFrame gets if the animation on its first frame
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) IsOnFirstFrame() bool {
|
|
|
|
return s.animation.IsOnFirstFrame()
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
2019-10-25 18:40:27 -04:00
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// IsOnLastFrame gets if the animation on its last frame
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) IsOnLastFrame() bool {
|
|
|
|
return s.animation.IsOnLastFrame()
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// GetDirectionCount gets the number of animation direction
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) GetDirectionCount() int {
|
|
|
|
return s.animation.GetDirectionCount()
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// SetDirection places the animation in the direction of an animation
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) SetDirection(directionIndex int) error {
|
|
|
|
return s.animation.SetDirection(directionIndex)
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// GetDirection get the current animation direction
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) GetDirection() int {
|
|
|
|
return s.animation.GetDirection()
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// SetCurrentFrame sets animation at a specific frame
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) SetCurrentFrame(frameIndex int) error {
|
|
|
|
return s.animation.SetCurrentFrame(frameIndex)
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// Rewind sprite to beginning
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) Rewind() {
|
|
|
|
s.animation.SetCurrentFrame(0)
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// PlayForward plays sprite forward
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) PlayForward() {
|
|
|
|
s.animation.PlayForward()
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// PlayBackward play sprites backward
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) PlayBackward() {
|
|
|
|
s.animation.PlayBackward()
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// Pause animation
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) Pause() {
|
|
|
|
s.animation.Pause()
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// SetPlayLoop sets whether to loop the animation
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) SetPlayLoop(loop bool) {
|
|
|
|
s.animation.SetPlayLoop(loop)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) SetPlayLength(playLength float64) {
|
|
|
|
s.animation.SetPlayLength(playLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) SetPlayLengthMs(playLengthMs int) {
|
|
|
|
s.animation.SetPlayLengthMs(playLengthMs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) SetColorMod(color color.Color) {
|
|
|
|
s.animation.SetColorMod(color)
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (s *Sprite) Advance(elapsed float64) error {
|
|
|
|
return s.animation.Advance(elapsed)
|
2019-10-25 18:40:27 -04:00
|
|
|
}
|
2020-07-08 21:57:35 -04:00
|
|
|
|
|
|
|
func (s *Sprite) SetEffect(e d2enum.DrawEffect) {
|
|
|
|
s.animation.SetEffect(e)
|
|
|
|
}
|