-
Notifications
You must be signed in to change notification settings - Fork 11
/
fadable.go
114 lines (97 loc) · 2.82 KB
/
fadable.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mixins
import (
"fmt"
"time"
"github.com/divVerent/aaaaxy/internal/engine"
"github.com/divVerent/aaaaxy/internal/level"
)
const (
defaultFadeFrames = 16
solidThreshold = 8
opaqueThreshold = 16
)
// Fadable a mixin to make an object fade in/out when toggled.
// Must be initialized _after_ alpha and contents are set by the entity.
type Fadable struct {
Settable
World *engine.World
Entity *engine.Entity
Alpha float64
Contents level.Contents
FadeFrames int
FadeDespawn bool
AnimDir int
AnimFrame int
}
func (f *Fadable) Init(w *engine.World, sp *level.SpawnableProps, e *engine.Entity) error {
f.Settable.Init(sp)
f.World = w
f.Entity = e
// Collect the sprite info.
f.Alpha = f.Entity.Alpha
f.Contents = f.Entity.Contents()
fadeString := sp.Properties["fade_time"]
if fadeString != "" {
animTime, err := time.ParseDuration(fadeString)
if err != nil {
return fmt.Errorf("could not parse fade time: %v", fadeString)
}
f.FadeFrames = int((animTime*engine.GameTPS + (time.Second / 2)) / time.Second)
if f.FadeFrames < 1 {
f.FadeFrames = 1
}
} else {
f.FadeFrames = defaultFadeFrames
}
f.FadeDespawn = sp.Properties["fade_despawn"] == "true" // default false
// Skip the animation on initial load.
if f.Settable.State && sp.Properties["fade_skip_animation"] != "false" { // default true
f.AnimFrame = f.FadeFrames
} else {
f.AnimFrame = 0
}
f.Update()
return nil
}
func (f *Fadable) SetState(originator, predecessor *engine.Entity, state bool) {
f.Settable.SetState(originator, predecessor, state)
if f.FadeDespawn {
f.World.Detach(f.Entity)
}
}
func (f *Fadable) Update() {
if f.Settable.State {
f.AnimFrame++
} else {
f.AnimFrame--
if f.AnimFrame <= 0 && f.FadeDespawn {
f.World.Despawn(f.Entity)
return
}
}
if f.AnimFrame <= 0 {
f.Entity.Alpha = 0
f.AnimFrame = 0
} else if f.AnimFrame >= f.FadeFrames {
f.Entity.Alpha = f.Alpha
f.AnimFrame = f.FadeFrames
} else {
alpha := float64(f.AnimFrame) / float64(f.FadeFrames)
f.Entity.Alpha = alpha * f.Alpha
}
f.World.MutateContentsBool(f.Entity, f.Contents&level.SolidContents, f.AnimFrame >= solidThreshold)
f.World.MutateContentsBool(f.Entity, f.Contents&level.OpaqueContents, f.AnimFrame >= opaqueThreshold)
}