Skip to content

Commit 8ff35c8

Browse files
authored
Use ffmath.Vec (#17)
1 parent 8e27ff2 commit 8ff35c8

10 files changed

Lines changed: 97 additions & 180 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.25.5
44

55
require (
66
github.com/aperturerobotics/protobuf-go-lite v0.11.0
7-
github.com/applejag/firefly-go-math v0.1.0
7+
github.com/applejag/firefly-go-math v0.2.0
88
github.com/firefly-zero/firefly-go v0.10.0
99
github.com/orsinium-labs/tinymath v1.1.0
1010
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
github.com/aperturerobotics/protobuf-go-lite v0.11.0 h1:IAaZISqrEpodqECYxk0yKWgROEbZtMhs7bErP+Zma9o=
22
github.com/aperturerobotics/protobuf-go-lite v0.11.0/go.mod h1:c4kGy7Dkfz6B1m0t4QBIMQoNeQ7m+nYj3Qxxnlwhygo=
3-
github.com/applejag/firefly-go-math v0.1.0 h1:fPhG2sfNAHg11xdLLteMr11LXsKbIKWVtRZHAyU9n+E=
4-
github.com/applejag/firefly-go-math v0.1.0/go.mod h1:XbmMPAO9vDIYvwAJIPlY6HE0m1RZ3tXgn3GP9agFXos=
3+
github.com/applejag/firefly-go-math v0.2.0 h1:IiDXTbHpLDU8LR8LP3RKGpyUA59mQp2HwAdkDyLLask=
4+
github.com/applejag/firefly-go-math v0.2.0/go.mod h1:XbmMPAO9vDIYvwAJIPlY6HE0m1RZ3tXgn3GP9agFXos=
55
github.com/firefly-zero/firefly-go v0.10.0 h1:CFEVPrXdLCZYgMna2DMncZ+lR01xg3danlXtIw+dJkE=
66
github.com/firefly-zero/firefly-go v0.10.0/go.mod h1:+X/XGyPdES51OESkV8NSf1mszEBZionoROM7x2pBofw=
77
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=

pkg/scenes/field/firefly.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package field
33
import (
44
"github.com/applejag/epic-wizard-firefly-gladiators/assets"
55
"github.com/applejag/epic-wizard-firefly-gladiators/pkg/util"
6+
"github.com/applejag/firefly-go-math/ffmath"
67
"github.com/applejag/firefly-go-math/ffrand"
78

89
"github.com/firefly-zero/firefly-go/firefly"
@@ -21,8 +22,8 @@ type Firefly struct {
2122
spritesHat util.AnimatedSheet
2223
spritesHatRev util.AnimatedSheet
2324

24-
pos util.Vec2
25-
nextPos util.Vec2
25+
pos ffmath.Vec
26+
nextPos ffmath.Vec
2627
sleepTicks int
2728
}
2829

@@ -40,11 +41,11 @@ func NewFirefly(id int) Firefly {
4041
spritesHatRev.SetFrame(frame)
4142
return Firefly{
4243
id: id,
43-
pos: util.V(
44-
float32(40+ffrand.Intn(firefly.Width-40-40)),
45-
float32(40+ffrand.Intn(firefly.Height-30-30)),
44+
pos: ffmath.V(
45+
float32(ffrand.IntRange(40, firefly.Width-40)),
46+
float32(ffrand.IntRange(40, firefly.Height-30)),
4647
),
47-
sleepTicks: FPS + ffrand.Intn(FPS*3-FPS),
48+
sleepTicks: ffrand.IntRange(FPS, FPS*3),
4849
sprites: sprites,
4950
spritesRev: spritesRev,
5051
spritesHat: spritesHat,
@@ -63,17 +64,17 @@ func (f *Firefly) Update() {
6364
f.sleepTicks--
6465
if f.sleepTicks <= 0 {
6566
f.nextPos = f.pos
66-
f.nextPos = util.V(
67-
float32(40+ffrand.Intn(firefly.Width-40-40)),
68-
float32(30+ffrand.Intn(firefly.Height-30-30)),
67+
f.nextPos = ffmath.V(
68+
float32(ffrand.IntRange(40, firefly.Width-40)),
69+
float32(ffrand.IntRange(30, firefly.Height-30)),
6970
)
7071
}
7172
} else {
7273
// move to nextPos
7374
f.pos = f.pos.MoveTowards(f.nextPos, FireflySpeedPixelsPerTick)
7475

7576
if f.nextPos.Sub(f.pos).RadiusSquared() < 10 {
76-
f.sleepTicks = FPS + ffrand.Intn(FPS*3-FPS)
77+
f.sleepTicks = ffrand.IntRange(FPS, FPS*3)
7778
}
7879
}
7980
}

pkg/scenes/racebattle/camera.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package racebattle
33
import (
44
"github.com/applejag/epic-wizard-firefly-gladiators/assets"
55
"github.com/applejag/epic-wizard-firefly-gladiators/pkg/state"
6-
"github.com/applejag/epic-wizard-firefly-gladiators/pkg/util"
76
"github.com/applejag/firefly-go-math/ffmath"
87

98
"github.com/firefly-zero/firefly-go/firefly"
@@ -15,10 +14,10 @@ const (
1514
)
1615

1716
type Camera struct {
18-
pos util.Vec2
17+
pos ffmath.Vec
1918
}
2019

21-
func (c Camera) WorldVec2ToCameraSpace(pos util.Vec2) firefly.Point {
20+
func (c Camera) WorldVec2ToCameraSpace(pos ffmath.Vec) firefly.Point {
2221
return (pos.Sub(c.pos)).Round().Point()
2322
}
2423

@@ -30,7 +29,7 @@ func (c Camera) WorldPointToCameraSpace(pos firefly.Point) firefly.Point {
3029
func (c *Camera) Update(scene *Scene) {
3130
for _, player := range scene.Players {
3231
if player.IsPlayer && player.Peer == state.Input.Me {
33-
c.pos = player.Pos.Sub(util.V(ScreenWidthHalf, ScreenHeightHalf))
32+
c.pos = player.Pos.Sub(ffmath.V(ScreenWidthHalf, ScreenHeightHalf))
3433

3534
c.pos.X = ffmath.Clamp(c.pos.X, 0, float32(assets.RacingMap.Width()-firefly.Width))
3635
c.pos.Y = ffmath.Clamp(c.pos.Y, 0, float32(assets.RacingMap.Height()-firefly.Height))

pkg/scenes/racebattle/path.go

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,67 @@
11
package racebattle
22

33
import (
4-
"github.com/applejag/epic-wizard-firefly-gladiators/pkg/util"
54
"github.com/applejag/firefly-go-math/ffmath"
65
)
76

87
var path = Path{
9-
util.V(40, 414),
10-
util.V(42, 434),
11-
util.V(56, 451),
12-
util.V(125, 504),
13-
util.V(194, 573),
14-
util.V(257, 588),
15-
util.V(337, 583),
16-
util.V(428, 575),
17-
util.V(438, 541),
18-
util.V(434, 513),
19-
util.V(405, 456),
20-
util.V(420, 415),
21-
util.V(465, 410),
22-
util.V(532, 365),
23-
util.V(566, 294),
24-
util.V(547, 247),
25-
util.V(512, 212),
26-
util.V(470, 195),
27-
util.V(438, 110),
28-
util.V(395, 90),
29-
util.V(349, 114),
30-
util.V(321, 192),
31-
util.V(288, 195),
32-
util.V(284, 241),
33-
util.V(245, 268),
34-
util.V(207, 261),
35-
util.V(198, 230),
36-
util.V(215, 183),
37-
util.V(243, 150),
38-
util.V(245, 91),
39-
util.V(234, 69),
40-
util.V(188, 62),
41-
util.V(143, 68),
42-
util.V(115, 84),
43-
util.V(101, 122),
44-
util.V(69, 138),
45-
util.V(57, 165),
46-
util.V(56, 199),
47-
util.V(85, 232),
48-
util.V(82, 271),
49-
util.V(67, 292),
50-
util.V(74, 312),
51-
util.V(82, 331),
52-
util.V(75, 350),
53-
util.V(41, 361),
54-
util.V(36, 380),
55-
util.V(40, 405),
8+
ffmath.V(40, 414),
9+
ffmath.V(42, 434),
10+
ffmath.V(56, 451),
11+
ffmath.V(125, 504),
12+
ffmath.V(194, 573),
13+
ffmath.V(257, 588),
14+
ffmath.V(337, 583),
15+
ffmath.V(428, 575),
16+
ffmath.V(438, 541),
17+
ffmath.V(434, 513),
18+
ffmath.V(405, 456),
19+
ffmath.V(420, 415),
20+
ffmath.V(465, 410),
21+
ffmath.V(532, 365),
22+
ffmath.V(566, 294),
23+
ffmath.V(547, 247),
24+
ffmath.V(512, 212),
25+
ffmath.V(470, 195),
26+
ffmath.V(438, 110),
27+
ffmath.V(395, 90),
28+
ffmath.V(349, 114),
29+
ffmath.V(321, 192),
30+
ffmath.V(288, 195),
31+
ffmath.V(284, 241),
32+
ffmath.V(245, 268),
33+
ffmath.V(207, 261),
34+
ffmath.V(198, 230),
35+
ffmath.V(215, 183),
36+
ffmath.V(243, 150),
37+
ffmath.V(245, 91),
38+
ffmath.V(234, 69),
39+
ffmath.V(188, 62),
40+
ffmath.V(143, 68),
41+
ffmath.V(115, 84),
42+
ffmath.V(101, 122),
43+
ffmath.V(69, 138),
44+
ffmath.V(57, 165),
45+
ffmath.V(56, 199),
46+
ffmath.V(85, 232),
47+
ffmath.V(82, 271),
48+
ffmath.V(67, 292),
49+
ffmath.V(74, 312),
50+
ffmath.V(82, 331),
51+
ffmath.V(75, 350),
52+
ffmath.V(41, 361),
53+
ffmath.V(36, 380),
54+
ffmath.V(40, 405),
5655
}
5756

58-
type Path []util.Vec2
57+
type Path []ffmath.Vec
5958

6059
type PathTracker struct {
6160
path Path
6261
index int
63-
previous util.Vec2
64-
current util.Vec2
65-
next util.Vec2
62+
previous ffmath.Vec
63+
current ffmath.Vec
64+
next ffmath.Vec
6665
}
6766

6867
func NewPathTracker(path Path) PathTracker {
@@ -74,15 +73,15 @@ func NewPathTracker(path Path) PathTracker {
7473
}
7574
}
7675

77-
func (p PathTracker) PeekPrevious() util.Vec2 {
76+
func (p PathTracker) PeekPrevious() ffmath.Vec {
7877
return p.previous
7978
}
8079

81-
func (p PathTracker) PeekCurrent() util.Vec2 {
80+
func (p PathTracker) PeekCurrent() ffmath.Vec {
8281
return p.current
8382
}
8483

85-
func (p PathTracker) PeekNext() util.Vec2 {
84+
func (p PathTracker) PeekNext() ffmath.Vec {
8685
return p.next
8786
}
8887

@@ -93,7 +92,7 @@ func (p *PathTracker) goNext() {
9392
p.next = p.path[(p.index+1)%len(p.path)]
9493
}
9594

96-
func (p *PathTracker) PeekSoftNext(currentPos util.Vec2) util.Vec2 {
95+
func (p *PathTracker) PeekSoftNext(currentPos ffmath.Vec) ffmath.Vec {
9796
currentTarget := p.PeekCurrent()
9897
nextTarget := p.PeekNext()
9998
prevTarget := p.PeekPrevious()
@@ -106,15 +105,15 @@ func (p *PathTracker) PeekSoftNext(currentPos util.Vec2) util.Vec2 {
106105
distSqFromPrev := currentTarget.Sub(prevTarget).RadiusSquared()
107106
distWeight := 1 - min(distSqToCurrent/distSqFromPrev, 1)
108107

109-
return util.V(
108+
return ffmath.V(
110109
ffmath.Lerp(currentTarget.X, nextTarget.X, distWeight),
111110
ffmath.Lerp(currentTarget.Y, nextTarget.Y, distWeight),
112111
)
113112
}
114113

115114
// Progress returns the percentage (0.0-1.0) of progress made throughout the
116115
// path. The "pos" is used to calculate fractional progress between checkpoints.
117-
func (p *PathTracker) Progress(pos util.Vec2) float32 {
116+
func (p *PathTracker) Progress(pos ffmath.Vec) float32 {
118117
prev := p.PeekPrevious()
119118
current := p.PeekCurrent()
120119

@@ -125,7 +124,7 @@ func (p *PathTracker) Progress(pos util.Vec2) float32 {
125124
return float32(p.index+1)/float32(len(p.path)) + distWeight/float32(len(p.path))
126125
}
127126

128-
func (p *PathTracker) Update(pos util.Vec2) PathTrackerResult {
127+
func (p *PathTracker) Update(pos ffmath.Vec) PathTrackerResult {
129128
curr := p.PeekCurrent()
130129
prev := p.PeekPrevious()
131130
distSqToCurr := curr.Sub(pos).RadiusSquared()

pkg/scenes/racebattle/player.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ type Firefly struct {
5353
SpriteSheet util.AnimatedSheet
5454
SpriteSheetRev util.AnimatedSheet
5555

56-
Pos util.Vec2
56+
Pos ffmath.Vec
5757
Angle firefly.Angle
5858
SpeedFactor float32
5959

6060
MoveSpeed float32
6161
Nimbleness float32
6262
}
6363

64-
func NewFireflyPlayer(peer firefly.Peer, stats state.Firefly, pos util.Vec2, angle firefly.Angle) Firefly {
64+
func NewFireflyPlayer(peer firefly.Peer, stats state.Firefly, pos ffmath.Vec, angle firefly.Angle) Firefly {
6565
return Firefly{
6666
IsPlayer: true,
6767
Peer: peer,
@@ -75,10 +75,10 @@ func NewFireflyPlayer(peer firefly.Peer, stats state.Firefly, pos util.Vec2, ang
7575
}
7676
}
7777

78-
func NewFireflyBot(pos util.Vec2, angle firefly.Angle) Firefly {
78+
func NewFireflyBot(pos ffmath.Vec, angle firefly.Angle) Firefly {
7979
// Randomize skills
8080
// These skills must be better than the starting score when buying a basic firefly
81-
speed := 12 + ffrand.Intn(18-12)
81+
speed := ffrand.IntRange(12, 18)
8282
nimbleness := 12 + (18 - speed)
8383
return Firefly{
8484
IsPlayer: false,
@@ -100,7 +100,7 @@ func (f *Firefly) Update() PathTrackerResult {
100100
} else {
101101
f.updateBotInput()
102102
}
103-
dir := util.AngleToVec2(f.Angle)
103+
dir := ffmath.VAngle(f.Angle)
104104
newPos := f.Pos.Add(dir.Scale(f.MoveSpeed * f.SpeedFactor * StatsMoveSpeedFactor))
105105
f.Move(newPos)
106106
trackerResult := f.PathTracker.Update(f.Pos)
@@ -111,7 +111,7 @@ func (f *Firefly) Update() PathTrackerResult {
111111
return trackerResult
112112
}
113113

114-
func (f *Firefly) Move(to util.Vec2) {
114+
func (f *Firefly) Move(to ffmath.Vec) {
115115
delta := to.Sub(f.Pos)
116116
if delta.RadiusSquared() <= 0.01 {
117117
// no need to move
@@ -194,7 +194,7 @@ func (f *Firefly) Render(scene *Scene) {
194194
// Draw arrow to direction you should move in
195195
targetPoint := f.PathTracker.PeekSoftNext(f.Pos)
196196
targetAngle := targetPoint.Sub(f.Pos).Azimuth()
197-
targetDir := util.AngleToVec2(targetAngle)
197+
targetDir := ffmath.VAngle(targetAngle)
198198
drawArrow(
199199
point.Add(targetDir.Scale(10).Point()),
200200
targetAngle,
@@ -212,20 +212,20 @@ func (f *Firefly) Render(scene *Scene) {
212212
}
213213

214214
func drawArrow(from firefly.Point, angle firefly.Angle, length int, lineStyle firefly.LineStyle) {
215-
fromV := util.PointToVec2(from)
216-
toV := fromV.Add(util.AngleToVec2(angle).Scale(float32(length)))
215+
fromV := ffmath.VPoint(from)
216+
toV := fromV.Add(ffmath.VAngle(angle).Scale(float32(length)))
217217
to := toV.Point()
218218
firefly.DrawLine(
219219
from,
220220
to,
221221
lineStyle)
222222
firefly.DrawLine(
223223
to,
224-
toV.Add(util.AngleToVec2(angle.Add(firefly.Degrees(145))).Scale(3)).Point(),
224+
toV.Add(ffmath.VAngle(angle.Add(firefly.Degrees(145))).Scale(3)).Point(),
225225
lineStyle)
226226
firefly.DrawLine(
227227
to,
228-
toV.Add(util.AngleToVec2(angle.Add(firefly.Degrees(-145))).Scale(3)).Point(),
228+
toV.Add(ffmath.VAngle(angle.Add(firefly.Degrees(-145))).Scale(3)).Point(),
229229
lineStyle)
230230
}
231231

0 commit comments

Comments
 (0)