Line data Source code
1 : package systems
2 :
3 : import (
4 : gc "github.com/kijimaD/ruins/internal/components"
5 : w "github.com/kijimaD/ruins/internal/world"
6 : "github.com/kijimaD/ruins/internal/world/query"
7 : )
8 :
9 : const (
10 : // TotalAnimationDuration はアニメーション全体の時間(フレーム数)
11 : // 120フレーム = 60FPSで2秒
12 : TotalAnimationDuration = 120
13 : )
14 :
15 : // AnimationSystem は全エンティティのスプライトアニメーションを更新する
16 : type AnimationSystem struct {
17 : animationCounter int64
18 : }
19 :
20 : // NewAnimationSystem はAnimationSystemを初期化する
21 : func NewAnimationSystem() *AnimationSystem {
22 1 : return &AnimationSystem{}
23 1 : }
24 :
25 : // String はシステム名を返す
26 : // w.Updater interfaceを実装
27 : func (sys AnimationSystem) String() string {
28 1 : return "AnimationSystem"
29 1 : }
30 :
31 : // Update は全エンティティのスプライトアニメーションを更新する
32 : // w.Updater interfaceを実装
33 : func (sys *AnimationSystem) Update(world w.World) error {
34 0 : if world.Resources.Config.DisableAnimation {
35 0 : return nil
36 0 : }
37 :
38 0 : sys.animationCounter++
39 0 :
40 0 : spriteQuery := query.ActiveFilter1[gc.SpriteRender](world).Query()
41 0 : for spriteQuery.Next() {
42 0 : entity := spriteQuery.Entity()
43 0 : spriteRender := world.Components.SpriteRender.Get(entity)
44 0 :
45 : // AnimKeysが空ならアニメーションなし
46 0 : if len(spriteRender.AnimKeys) == 0 {
47 0 : continue
48 : }
49 :
50 : // アニメーション速度を計算(総時間を固定してフレーム数で割る)
51 0 : numFrames := int64(len(spriteRender.AnimKeys))
52 0 : frameInterval := TotalAnimationDuration / numFrames
53 0 :
54 : // フレームインデックスを計算
55 0 : frameIndex := (sys.animationCounter / frameInterval) % numFrames
56 0 :
57 : // SpriteKeyを更新
58 0 : spriteRender.SpriteKey = spriteRender.AnimKeys[frameIndex]
59 : }
60 0 : return nil
61 : }
|