-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.go
188 lines (166 loc) · 2.77 KB
/
util.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
// aerth game
// copyright 2017 aerth <[email protected]>
package rpg
import (
"fmt"
"io/ioutil"
"math"
"math/rand"
"os"
"time"
"golang.org/x/image/font"
_ "image/png"
"github.com/faiface/pixel"
"github.com/golang/freetype/truetype"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// Direction LEFT RIGHT DOWN UP
type Direction int
type animState int
const (
LEFT Direction = iota
RIGHT
DOWN
UP
IN
OUT
UPLEFT
UPRIGHT
DOWNLEFT
DOWNRIGHT
)
const (
WEST = LEFT
EAST = RIGHT
NORTH = UP
SOUTH = DOWN
)
const (
Idle animState = iota
Running
)
func (d Direction) String() string {
switch d {
case LEFT:
return "left"
case RIGHT:
return "right"
case UP:
return "up"
case DOWN:
return "down"
case DOWNLEFT:
return "down-left"
case DOWNRIGHT:
return "down-right"
case UPLEFT:
return "up-left"
case UPRIGHT:
return "up-right"
case IN:
return "within"
case OUT:
return "without"
default:
return fmt.Sprintf("invalid direction: %v", int(d))
}
}
func UnitToDirection(v pixel.Vec) Direction {
v.X = math.Floor(v.X + 0.5)
v.Y = math.Floor(v.Y + 0.5)
switch v {
default:
return OUT
case LEFT.V():
return LEFT
case UPLEFT.V():
return UPLEFT
case DOWNLEFT.V():
return DOWNLEFT
case RIGHT.V():
return RIGHT
case UPRIGHT.V():
return UPRIGHT
case DOWNRIGHT.V():
return DOWNRIGHT
case DOWN.V():
return DOWN
case UP.V():
return UP
}
}
func (d Direction) V() pixel.Vec {
switch d {
case LEFT:
return pixel.V(-1, 0)
case RIGHT:
return pixel.V(1, 0)
case UP:
return pixel.V(0, 1)
case DOWN:
return pixel.V(0, -1)
case UPRIGHT:
return pixel.V(1, 1)
case UPLEFT:
return pixel.V(-1, 1)
case DOWNLEFT:
return pixel.V(-1, -1)
case DOWNRIGHT:
return pixel.V(1, -1)
default:
return pixel.V(0, 0)
}
}
func RandomColor() pixel.RGBA {
r := rand.Float64()
g := rand.Float64()
b := rand.Float64()
len := math.Sqrt(r*r + g*g + b*b)
//if len == 0 {
// goto again
//}
return pixel.RGB(r/len, g/len, b/len)
}
func LoadTTF(path string, size float64) (font.Face, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
font, err := truetype.Parse(data)
if err != nil {
return nil, err
}
return truetype.NewFace(font, &truetype.Options{
Size: size,
GlyphCacheEntries: 1,
}), nil
}
// Distance between two vectors
func Distance(v1, v2 pixel.Vec) float64 {
r := pixel.Rect{v1, v2}.Norm()
v1 = r.Min
v2 = r.Max
h := (v1.X - v2.X) * (v1.X - v2.X)
v := (v1.Y - v2.Y) * (v1.Y - v2.Y)
return Sqrt(h + v)
}
// ?
func Sqrt(x float64) float64 {
z := float64(2.)
s := float64(0)
for i := 0; i < 10; i++ {
z = z - (z*z-x)/(2*z)
if math.Abs(z-s) < 1e-10 {
break
}
s = z
}
return z
}