Skip to content

Commit

Permalink
Set the animation directory as a subdirectory of sprite
Browse files Browse the repository at this point in the history
  • Loading branch information
JiepengTan committed Aug 13, 2024
1 parent 16bd1db commit 5ff12bf
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 27 deletions.
18 changes: 9 additions & 9 deletions internal/anim/anim.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package anim

import (
"errors"
"math"
"log"

"math"

spxfs "github.com/goplus/spx/fs"
"github.com/goplus/spx/internal/anim/common"
"github.com/goplus/spx/internal/anim/skeleton"
"github.com/goplus/spx/internal/anim/vertex"
"github.com/goplus/spx/internal/math32"
"github.com/hajimehoshi/ebiten/v2"
"github.com/goplus/spx/internal/tools"
"github.com/hajimehoshi/ebiten/v2"
)

type ANIMSTATUS uint8
Expand All @@ -34,6 +34,7 @@ const (
ANIMATOR_TYPE_SKELETON = "skeleton"
ANIMATOR_TYPE_FRAME = "frame"
)

type IAnimatable interface {
GetTarget() IAnimationTarget
Animate() bool
Expand Down Expand Up @@ -92,29 +93,28 @@ type Anim struct {
var globalAnimId int = 1

// loopmodel = -1
func NewAnimator(fs spxfs.Dir, animatorPath string, avatarPath string) IAnimator {
func NewAnimator(fs spxfs.Dir, spriteDir string, animatorPath string, avatarPath string) IAnimator {
var config common.AnimatorConfig
err := common.LoadJson(&config, fs, animatorPath)
err := common.LoadJson(&config, fs, spriteDir, animatorPath)
if err != nil {
log.Panicf("animator config [%s] not exist", animatorPath)
}

var avatarConfig common.AvatarConfig
err = common.LoadJson(&avatarConfig, fs, avatarPath)
err = common.LoadJson(&avatarConfig, fs, spriteDir, avatarPath)
if err != nil {
log.Panicf("avatar config [%s] not exist", animatorPath)
}

var animator IAnimator
if config.Type == ANIMATOR_TYPE_VERTEX {
animator = vertex.NewAnimator(fs, &config, &avatarConfig)
animator = vertex.NewAnimator(fs, spriteDir, &config, &avatarConfig)
} else {
animator = skeleton.NewAnimator(fs, &config, &avatarConfig)
animator = skeleton.NewAnimator(fs, spriteDir, &config, &avatarConfig)
}
return animator
}


func NewAnim(name string, fps float64, totalframe int, isLoop bool) *Anim {
this := &Anim{}

Expand Down
6 changes: 6 additions & 0 deletions internal/anim/common/animator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import (
"github.com/hajimehoshi/ebiten/v2"
)

type AnimFrameExportData struct {
Indices []uint16 `json:"Indices"`
Vertices []math32.Vector3 `json:"Vertices"`
Uvs []math32.Vector2 `json:"Uvs"`
}

type Avatar struct {
Image *ebiten.Image
// transform
Expand Down
13 changes: 7 additions & 6 deletions internal/anim/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@ import (
"fmt"
"image"
"os"
"path"

spxfs "github.com/goplus/spx/fs"
"github.com/hajimehoshi/ebiten/v2"
)

func LoadJson(ret interface{}, fs spxfs.Dir, file string) (err error) {
f, err := fs.Open(file)
func LoadJson(ret interface{}, fs spxfs.Dir, dir string, fileName string) (err error) {
f, err := fs.Open(path.Join(dir, fileName))
if err != nil {
return
}
defer f.Close()
return json.NewDecoder(f).Decode(ret)
}
func LoadImage(fs spxfs.Dir, path string) (*ebiten.Image, error) {
file, err := fs.Open(path)
func LoadImage(fs spxfs.Dir, dir string, fileName string) (*ebiten.Image, error) {
file, err := fs.Open(path.Join(dir, fileName))
if err != nil {
fmt.Println("Error: File could not be opened ", path)
fmt.Println("Error: File could not be opened ", fileName)
os.Exit(1)
}
defer file.Close()
data, _, err := image.Decode(file)
if err != nil {
fmt.Println("Error: Image could not be decoded ", path)
fmt.Println("Error: Image could not be decoded ", fileName)
os.Exit(1)
}
img := ebiten.NewImageFromImage(data)
Expand Down
8 changes: 4 additions & 4 deletions internal/anim/skeleton/animator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ type Animator struct {
LogicBones []*Bone
}

func NewAnimator(fs spxfs.Dir, config *common.AnimatorConfig, avatarConfig *common.AvatarConfig) *Animator {
func NewAnimator(fs spxfs.Dir, spriteDir string, config *common.AnimatorConfig, avatarConfig *common.AvatarConfig) *Animator {
pself := &Animator{}
pself.Clips = make(map[string]common.IAnimClip)
pself.CurClipName = ""
pself.Scale = avatarConfig.Scale
pself.Offset = *avatarConfig.Offset.Multiply(&avatarConfig.Scale)
pself.Mesh = &AnimMesh{}
err := common.LoadJson(pself.Mesh, fs, avatarConfig.Mesh)
err := common.LoadJson(pself.Mesh, fs, spriteDir, avatarConfig.Mesh)
if err != nil {
log.Panicf("animator Mesh [%s] not exist", avatarConfig.Mesh)
}
pself.Image, err = common.LoadImage(fs, avatarConfig.Image)
pself.Image, err = common.LoadImage(fs, spriteDir, avatarConfig.Image)
if err != nil {
log.Panicf("animator image [%s] not exist", avatarConfig.Mesh)
}
for _, clipConfig := range config.Clips {
clip := &AnimClip{}
clip.Name = clipConfig.Name
clip.Config = clipConfig
err = common.LoadJson(&clip.Data, fs, clipConfig.Path)
err = common.LoadJson(&clip.Data, fs, spriteDir, clipConfig.Path)
if err != nil {
log.Panicf("animator clip [%s] not exist", clipConfig.Path)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/anim/vertex/animator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ type Animator struct {
Mesh *AnimMesh
}

func NewAnimator(fs spxfs.Dir, config *common.AnimatorConfig, avatarConfig *common.AvatarConfig) *Animator {
func NewAnimator(fs spxfs.Dir, spriteDir string, config *common.AnimatorConfig, avatarConfig *common.AvatarConfig) *Animator {
pself := &Animator{}
pself.Clips = make(map[string]common.IAnimClip)
pself.CurClipName = ""
pself.Scale = avatarConfig.Scale
pself.Offset = *avatarConfig.Offset.Multiply(&avatarConfig.Scale)
pself.Mesh = &AnimMesh{}
err := common.LoadJson(pself.Mesh, fs, avatarConfig.Mesh)
err := common.LoadJson(pself.Mesh, fs, spriteDir, avatarConfig.Mesh)
if err != nil {
log.Panicf("animator Mesh [%s] not exist", avatarConfig.Mesh)
}
pself.Image, err = common.LoadImage(fs, avatarConfig.Image)
pself.Image, err = common.LoadImage(fs, spriteDir, avatarConfig.Image)
if err != nil {
log.Panicf("animator image [%s] not exist", avatarConfig.Mesh)
}
for _, clipConfig := range config.Clips {
clip := &AnimClip{}
clip.Name = clipConfig.Name
clip.Config = clipConfig
err = common.LoadJson(&clip.Data, fs, clipConfig.Path)
err = common.LoadJson(&clip.Data, fs, spriteDir, clipConfig.Path)
if err != nil {
log.Panicf("animator clip [%s] not exist", clipConfig.Path)
}
Expand Down
9 changes: 5 additions & 4 deletions sprite.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type Sprite struct {
lastAnim *anim.Anim
isWaitingStopAnim bool
defaultCostumeIndex int

animator anim.IAnimator
}

Expand Down Expand Up @@ -226,7 +226,8 @@ func (p *Sprite) init(
if sprite.Avatar == "" {
log.Panicf("[%s] missing avatar config", name)
}
p.animator = anim.NewAnimator(g.fs, sprite.Animator, sprite.Avatar)

p.animator = anim.NewAnimator(g.fs, base, sprite.Animator, sprite.Avatar)
}
}
func (p *Sprite) awake() {
Expand Down Expand Up @@ -723,7 +724,7 @@ func (p *Sprite) goAnimateInternal(name string, ani *aniConfig, isBlocking bool)
if debugInstr {
log.Printf("New anim [name %s id %d] from:%v to:%v framenum:%d fps:%f", an.Name, an.Id, fromval, toval, framenum, fps)
}

if p.animator != nil && ani.ClipName != "" {
state := p.animator.Play(ani.ClipName)
if state != nil {
Expand Down Expand Up @@ -815,7 +816,7 @@ func (p *Sprite) Animate(name string) {
}
if ani, ok := p.animations[name]; ok {
p.goAnimate(name, ani)
return
return
}
if p.animator != nil {
p.animator.Play(name)
Expand Down
File renamed without changes.

0 comments on commit 5ff12bf

Please sign in to comment.