-
Notifications
You must be signed in to change notification settings - Fork 9
/
give.go
100 lines (86 loc) · 2.56 KB
/
give.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
// 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 trigger
import (
"fmt"
"github.com/divVerent/aaaaxy/internal/animation"
"github.com/divVerent/aaaaxy/internal/engine"
"github.com/divVerent/aaaaxy/internal/fun"
"github.com/divVerent/aaaaxy/internal/game/interfaces"
"github.com/divVerent/aaaaxy/internal/game/mixins"
"github.com/divVerent/aaaaxy/internal/level"
"github.com/divVerent/aaaaxy/internal/propmap"
)
const (
giveFadeFrames = 60
)
// Give grants the player an ability when touched.
type Give struct {
mixins.NonSolidTouchable
Ability string
Text string
AnimFrame int
Anim animation.State
}
func (g *Give) Spawn(w *engine.World, sp *level.SpawnableProps, e *engine.Entity) error {
g.NonSolidTouchable.Init(w, e)
var parseErr error
g.Ability = propmap.ValueP(sp.Properties, "ability", "", &parseErr)
g.Text = propmap.ValueP(sp.Properties, "text", "", &parseErr)
err := g.Anim.Init("can_"+g.Ability, map[string]*animation.Group{
"default": {
Frames: 30,
Symmetric: true,
FrameInterval: 4,
NextInterval: 4 * 30,
NextAnim: "default",
}}, "default")
if err != nil {
return fmt.Errorf("could not initialize give animation: %w", err)
}
if g.World.Player.Impl.(interfaces.Abilityer).HasAbility(g.Ability) {
g.AnimFrame = 0
} else {
g.AnimFrame = giveFadeFrames
}
return parseErr
}
func (g *Give) Despawn() {}
func (g *Give) Update() {
g.NonSolidTouchable.Update()
if g.World.Player.Impl.(interfaces.Abilityer).HasAbility(g.Ability) {
g.AnimFrame--
} else {
g.AnimFrame++
}
if g.AnimFrame <= 0 {
g.Entity.Alpha = 0
g.AnimFrame = 0
} else if g.AnimFrame >= giveFadeFrames {
g.Entity.Alpha = 1
g.AnimFrame = giveFadeFrames
} else {
g.Entity.Alpha = float64(g.AnimFrame) / giveFadeFrames
}
g.Anim.Update(g.Entity)
}
func (g *Give) Touch(other *engine.Entity) {
if other != g.World.Player {
return
}
g.World.Player.Impl.(interfaces.Abilityer).GiveAbility(g.Ability, fun.FormatText(&g.World.PlayerState, g.Text))
}
func init() {
engine.RegisterEntityType(&Give{})
}