2019-11-10 03:36:53 -05:00
|
|
|
package d2render
|
2019-10-24 09:31:59 -04:00
|
|
|
|
|
|
|
import (
|
2019-10-24 19:13:30 -04:00
|
|
|
"image/color"
|
2019-10-24 09:31:59 -04:00
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2helper"
|
2019-12-21 20:53:18 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2asset"
|
2019-12-28 16:46:08 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2render/d2surface"
|
2019-10-24 09:31:59 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Sprite struct {
|
2019-12-28 23:32:24 -05:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
animation *d2asset.Animation
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
2019-11-24 17:58:23 -05:00
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func LoadSprite(animationPath, palettePath string) (*Sprite, error) {
|
|
|
|
animation, err := d2asset.LoadAnimation(animationPath, palettePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-15 14:07:57 -05:00
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
return &Sprite{animation: animation}, nil
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
2019-12-15 14:07:57 -05:00
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func MustLoadSprite(animationPath, palettePath string) *Sprite {
|
|
|
|
sprite, err := LoadSprite(animationPath, palettePath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
return sprite
|
2019-11-10 10:44:13 -05:00
|
|
|
}
|
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
func (s *Sprite) Render(target *d2surface.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()
|
|
|
|
return s.animation.Render(target)
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
func (s *Sprite) RenderSegmented(target *d2surface.Surface, segmentsX, segmentsY, frameOffset int) error {
|
2019-12-21 20:53:18 -05:00
|
|
|
var currentY int
|
|
|
|
for y := 0; y < segmentsY; y++ {
|
|
|
|
var currentX int
|
|
|
|
var maxFrameHeight int
|
|
|
|
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()
|
|
|
|
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()
|
|
|
|
maxFrameHeight = d2helper.MaxInt(maxFrameHeight, frameHeight)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) GetCurrentFrame() int {
|
|
|
|
return s.animation.GetCurrentFrame()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) GetFrameCount() int {
|
|
|
|
return s.animation.GetFrameCount()
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) IsOnLastFrame() bool {
|
|
|
|
return s.animation.IsOnLastFrame()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) GetDirectionCount() int {
|
|
|
|
return s.animation.GetDirectionCount()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) SetDirection(directionIndex int) error {
|
|
|
|
return s.animation.SetDirection(directionIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) GetDirection() int {
|
|
|
|
return s.animation.GetDirection()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) SetCurrentFrame(frameIndex int) error {
|
|
|
|
return s.animation.SetCurrentFrame(frameIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) Rewind() {
|
|
|
|
s.animation.SetCurrentFrame(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) PlayForward() {
|
|
|
|
s.animation.PlayForward()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) PlayBackward() {
|
|
|
|
s.animation.PlayBackward()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) Pause() {
|
|
|
|
s.animation.Pause()
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) SetBlend(blend bool) {
|
|
|
|
s.animation.SetBlend(blend)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|