-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrow.go
173 lines (149 loc) · 3.94 KB
/
arrow.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
package main
import (
"math"
"github.com/faiface/pixel"
"golang.org/x/image/colornames"
)
type Arrow struct {
Entity
StuckSprite *pixel.Sprite
baseScale float64
vel pixel.Vec // velocity of the arrow
target pixel.Vec // where the arrow should drop down
halfDistance float64 // half the distance from original spawn point to the target
maxHeight float64
State ArrowState
}
// Arrow starts this far from the center of the hero.
const ArrowStartDistance = 10.0
type ArrowState uint8
const (
ArrowInactive ArrowState = iota
ArrowQuiver
ArrowHands
ArrowFlying
ArrowStuck
)
func NewArrow(normal, stuck *pixel.Sprite) *Arrow {
a := &Arrow{Entity: *NewEntity(normal, pixel.ZV)}
a.StuckSprite = stuck
a.Deactivate()
a.State = ArrowInactive
a.baseScale = 1
a.Color = colornames.Goldenrod
r := pixel.R(-1, -1, 1, 1)
a.Collider = &r
return a
}
func (a *Arrow) DistanceToTarget() float64 {
return a.Pos.Sub(a.target).Len()
}
func (a *Arrow) CanKill() bool {
return a.CurrentHeight() < 8
}
// DistanceFromEnds returns values in range 0 ... 1 ... 0,
// it returns 1 in the middle (the highest point) of trajectory.
func (a *Arrow) DistanceFromEnds() float64 {
return (a.halfDistance - math.Abs(a.DistanceToTarget()-a.halfDistance)) / a.halfDistance
}
func (a *Arrow) CurrentHeight() float64 {
return a.maxHeight * math.Sqrt(a.DistanceFromEnds())
}
func (a *Arrow) ToHands() {
a.Active = true
a.Visible = true
a.State = ArrowHands
}
func (a *Arrow) ToQuiver() {
a.Active = true
a.Visible = true
a.State = ArrowQuiver
}
func (a *Arrow) AttachToHands(from, to pixel.Vec) {
dir := to.Sub(from).Unit()
a.Pos = from.Add(dir.Scaled(ArrowStartDistance))
a.Angle = dir.Angle()
a.ScaleXY.X = 1.0
a.ScaleXY.Y = 1.0
}
func (a *Arrow) AttachToQuiver(pos pixel.Vec, idx int) {
a.Pos = pos.Add(pixel.V(-8+3*float64(idx), 7))
a.Angle = math.Pi / 2
a.ScaleXY.X = 0.5
a.ScaleXY.Y = 0.5
}
func (a *Arrow) Fly(from, to, relational pixel.Vec) {
a.State = ArrowFlying
dir := to.Sub(from).Unit()
a.Pos = from.Add(dir.Scaled(ArrowStartDistance))
a.Angle = dir.Angle()
a.vel = dir.Scaled(150).Add(relational)
a.target = to
a.halfDistance = a.Pos.Sub(a.target).Len() / 2
// height takes values in range [0, 50]
a.maxHeight = pixel.Clamp(a.halfDistance/1.2, 0, 100)
// fmt.Println(a.halfDistance, a.maxHeight)
}
func (a *Arrow) Kills(col pixel.Rect) bool {
return a.State == ArrowFlying && a.CanKill() && collides(col, a.AbsCollider())
}
func (a *Arrow) Stick() {
a.State = ArrowStuck
}
func (a *Arrow) Update() {
if a.State == ArrowStuck {
}
if !a.Active || a.State != ArrowFlying {
return
}
size := math.Sqrt(a.DistanceFromEnds())
oldDist := a.DistanceToTarget()
a.Pos = a.Pos.Add(a.vel.Scaled(engine.dt))
newDist := a.DistanceToTarget()
// Maximum scaling should depend on the a.distance.
// If we shot on short distance then arrow should not rise high to the air.
perspect := a.maxHeight / 150
if newDist < a.halfDistance {
// make size smaller close to the target since arrow drops to the floow
perspect += (a.halfDistance - newDist) / a.halfDistance / 5
}
a.ScaleXY.X = a.baseScale + size*a.maxHeight/100 - perspect
a.ScaleXY.Y = a.baseScale + size*a.maxHeight/100
//fmt.Printf("%4.2f %4.2f\n", size, a.ScaleXY.X)
if newDist > oldDist {
// a.Active = false
// a.Visible = false
a.State = ArrowStuck
return
}
acol := a.AbsCollider()
walls := world.GetColliders(acol)
for _, wall := range walls {
if collides(acol, wall) {
// a.Active = false
// a.Visible = false
a.State = ArrowStuck
return
}
}
}
func (a *Arrow) Draw(t pixel.Target) {
if a.Visible {
m := pixel.IM.ScaledXY(pixel.ZV, a.ScaleXY).Rotated(pixel.ZV, a.Angle).Moved(a.Pos)
if a.State == ArrowStuck {
a.StuckSprite.DrawColorMask(t, m, a.Color)
} else {
a.Sprite.DrawColorMask(t, m, a.Color)
}
}
}
func firstFreeArrow(s []*Arrow) int {
free := -1
for i := range s {
if !s[i].Active {
free = i
break
}
}
return free
}