-
Notifications
You must be signed in to change notification settings - Fork 0
/
animationManager.go
78 lines (68 loc) · 1.74 KB
/
animationManager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"sync"
"time"
)
var globalAnimationManager *AnimationManager //for now, before blueprints
type AnimationManager struct {
queue []*Animation
mutex sync.Mutex
total, empty int64
}
func (receiver *AnimationManager) Add(object *Animation) {
receiver.mutex.Lock()
receiver.queue = append(receiver.queue, object)
object.Manager = receiver
receiver.mutex.Unlock()
}
func (receiver *AnimationManager) Remove(object *Animation) {
receiver.mutex.Lock()
for indx, candidate := range receiver.queue {
if object == candidate {
receiver.queue[indx] = nil
object.Manager = nil
object.Spriteer = ErrorSprite //to visible show unmanaged but rendered animation
}
}
receiver.mutex.Unlock()
}
func (receiver *AnimationManager) Compact() {
i, j := 0, 0
for i < len(receiver.queue) {
if receiver.queue[i] == nil {
//
} else {
receiver.queue[j] = receiver.queue[i]
j++
}
i++
}
receiver.queue = receiver.queue[0:j]
receiver.total = int64(len(receiver.queue))
receiver.empty = 0
}
func (receiver *AnimationManager) NeedCompact() bool {
return receiver.total > 100 && receiver.empty > 0 && receiver.total/receiver.empty < 2
}
func (receiver *AnimationManager) Execute(timeLeft time.Duration) {
receiver.mutex.Lock()
defer receiver.mutex.Unlock()
for _, object := range receiver.queue {
if object == nil {
continue
}
object.Update(timeLeft)
}
}
func NewAnimationManager(queueSize int) (*AnimationManager, error) {
return &AnimationManager{
queue: make([]*Animation, 0, queueSize),
}, nil
}
func getAnimationManager() (*AnimationManager, error) {
var err error
if globalAnimationManager == nil {
globalAnimationManager, err = NewAnimationManager(100)
}
return globalAnimationManager, err
}