-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspells.go
195 lines (179 loc) · 4.48 KB
/
spells.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package rpg
import (
"log"
"math/rand"
"time"
"golang.org/x/image/colornames"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
)
type Animation struct {
Name string
Type ActionType
loc pixel.Vec
rect pixel.Rect
radius float64
step float64
counter float64
cols []pixel.RGBA
until time.Time
start time.Time
damage float64
direction Direction
ticker <-chan time.Time
level uint
}
type ActionType int
const (
Talk ActionType = iota
Slash
ManaStorm
MagicBullet
Arrow
)
func (w *World) Action(char *Character, loc pixel.Vec, t ActionType, dir Direction) {
switch t {
case Talk:
log.Println("nothing to say yet")
case Slash:
log.Println("no weapon yet")
case ManaStorm:
cost := uint(2.5 * float64(char.Level))
if char.Mana < cost {
w.Message("not enough mana")
return
}
char.Mana -= cost
w.NewAnimation(char.Rect.Center(), "manastorm", OUT)
case MagicBullet:
cost := uint(1)
if char.Mana < cost {
w.Message("not enough mana")
return
}
char.Mana -= cost
w.NewAnimation(char.Rect.Center(), "magicbullet", dir)
default: //
}
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func (a *Animation) update(dt float64) {
if time.Since(a.until) > time.Millisecond {
a = nil
return
}
switch a.Type {
default:
log.Println("nil animation?")
return
case MagicBullet:
a.counter += dt
for a.counter > a.step {
if len(a.cols) == 0 {
a.cols = []pixel.RGBA{RandomColor(), RandomColor(), RandomColor(), RandomColor(), RandomColor()}
}
a.counter -= a.step
for i := len(a.cols) - 2; i >= 0; i-- {
a.cols[i+1] = a.cols[i]
}
a.cols[0] = RandomColor().Scaled(0.3)
a.cols[1] = RandomColor().Scaled(0.3)
}
if a.direction != OUT && a.direction != IN {
a.loc = a.loc.Add((a.direction.V().Scaled(100 * dt)))
a.rect = pixel.R(-a.radius, -a.radius, a.radius, a.radius).Moved(a.loc)
}
case ManaStorm:
a.counter += dt
for a.counter > a.step {
if len(a.cols) == 0 {
a.cols = []pixel.RGBA{RandomColor(), RandomColor(), RandomColor(), RandomColor(), RandomColor()}
}
a.counter -= a.step
for i := len(a.cols) - 2; i >= 0; i-- {
a.cols[i+1] = a.cols[i]
}
a.cols[0] = RandomColor().Scaled(0.3)
a.cols[1] = RandomColor().Scaled(0.3)
}
case Arrow:
a.counter += dt
for a.counter > a.step {
a.counter -= a.step
a.direction = Direction(rand.Intn(5))
}
a.loc = a.loc.Add((a.direction.V().Scaled(100 * dt)))
a.rect = pixel.R(-100, -5, 100, 5).Moved(a.loc)
}
}
func (a *Animation) draw(imd *imdraw.IMDraw) {
if a == nil || time.Since(a.start) < time.Millisecond {
return
}
switch a.Type {
case MagicBullet, ManaStorm:
for i := len(a.cols) - 1; i >= 0; i-- {
imd.Color = a.cols[i]
imd.Push(a.loc)
imd.Circle(float64(i+2)*a.radius/float64(len(a.cols)), 0)
}
case Arrow:
imd.Color = RandomColor()
b := a.loc.Add(a.direction.V().Scaled(10))
imd.Push(a.loc, b)
imd.Line(float64(a.level))
default:
log.Println("bad animation?")
}
}
func (w *World) NewAnimation(loc pixel.Vec, kind string, direction Direction) {
switch kind {
default: //
log.Println("invalid animation type")
return
case "magicbullet":
a := new(Animation)
a.Type = MagicBullet
a.loc = loc
a.radius = 10
a.step = 1.0 / 7
a.rect = pixel.R(-a.radius, -a.radius, a.radius, a.radius).Moved(a.loc)
a.cols = []pixel.RGBA{}
a.start = time.Now()
a.until = time.Now().Add(time.Second * 2)
a.direction = direction
a.damage = w.Char.Stats.Intelligence * 0.5
w.Animations = append(w.Animations, a)
case "manastorm":
a := new(Animation)
a.Type = ManaStorm
a.loc = loc
a.radius = (w.Char.Stats.Intelligence / 20) * 4
a.step = 1.0 / 7
a.damage = w.Char.Stats.Intelligence * 0.2
a.rect = pixel.R(-a.radius, -a.radius, a.radius, a.radius).Moved(a.loc)
a.cols = []pixel.RGBA{}
a.start = time.Now()
a.direction = direction
dur := time.Duration(w.Char.Stats.Intelligence * float64(time.Millisecond) * 18)
// log.Println(dur)
a.until = time.Now().Add(dur)
a.ticker = time.Tick(time.Millisecond * 200)
w.Animations = append(w.Animations, a)
case "arrow":
a := new(Animation)
a.Type = Arrow
a.loc = loc
a.damage = w.Char.Stats.Strength * 10
a.rect = pixel.R(-100, -5, 100, 5)
a.cols = []pixel.RGBA{pixel.ToRGBA(colornames.Red)}
a.direction = direction
dur := time.Second * 3
a.until = time.Now().Add(dur)
a.ticker = time.Tick(time.Millisecond * 500)
a.level = w.Char.Level
w.Animations = append(w.Animations, a)
}
}