-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmovement.go
More file actions
115 lines (105 loc) · 3.24 KB
/
movement.go
File metadata and controls
115 lines (105 loc) · 3.24 KB
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
package main
import (
"math"
"math/rand"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
// enableAutoWalk schedules scripted movement for a limited duration.
func (g *Game) enableAutoWalk(duration time.Duration) {
g.autoWalk = true
g.autoWalkDeadline = time.Now().Add(duration)
if g.autoWalkRand == nil {
g.autoWalkRand = rand.New(rand.NewSource(time.Now().UnixNano() + 3))
}
g.autoWalkFrameCount = 0
}
// movementVector selects either manual or automatic movement direction.
func (g *Game) movementVector() (float64, float64) {
if g.autoWalk {
if time.Now().After(g.autoWalkDeadline) {
g.autoWalk = false
return 0, 0
}
return g.autoWalkVector()
}
return g.manualMovementVector()
}
// manualMovementVector returns WASD-based input movement scaled by moveSpeed.
func (g *Game) manualMovementVector() (float64, float64) {
dx, dy := 0.0, 0.0
if ebiten.IsKeyPressed(ebiten.KeyW) {
dy -= moveSpeed
}
if ebiten.IsKeyPressed(ebiten.KeyS) {
dy += moveSpeed
}
if ebiten.IsKeyPressed(ebiten.KeyA) {
dx -= moveSpeed
}
if ebiten.IsKeyPressed(ebiten.KeyD) {
dx += moveSpeed
}
if dx != 0 && dy != 0 {
dx *= 0.7071
dy *= 0.7071
}
return dx, dy
}
// autoWalkVector returns a pseudo-random, collision-aware movement vector.
func (g *Game) autoWalkVector() (float64, float64) {
if g.autoWalkRand == nil {
g.autoWalkRand = rand.New(rand.NewSource(time.Now().UnixNano() + 4))
}
for attempts := 0; attempts < 5; attempts++ {
if g.autoWalkFrameCount <= 0 {
g.randomizeAutoWalkDirection()
}
nextX := g.ex + g.autoWalkDirX*moveSpeed
nextY := g.ey + g.autoWalkDirY*moveSpeed
if nextX > float64(emitterRad) && nextX < float64(w-emitterRad-1) &&
nextY > float64(emitterRad) && nextY < float64(h-emitterRad-1) &&
!g.isWall(int(nextX), int(nextY)) {
g.autoWalkFrameCount--
return g.autoWalkDirX * moveSpeed, g.autoWalkDirY * moveSpeed
}
g.autoWalkFrameCount = 0
}
return 0, 0
}
// randomizeAutoWalkDirection chooses a new heading for automatic walking.
func (g *Game) randomizeAutoWalkDirection() {
if g.autoWalkRand == nil {
g.autoWalkRand = rand.New(rand.NewSource(time.Now().UnixNano() + 5))
}
angle := g.autoWalkRand.Float64() * 2 * math.Pi
g.autoWalkDirX = math.Cos(angle)
g.autoWalkDirY = math.Sin(angle)
g.autoWalkFrameCount = 20 + g.autoWalkRand.Intn(50)
}
// handleDebugControls processes debug overlay hotkeys.
func (g *Game) handleDebugControls() {
if !*debugFlag {
return
}
if inpututil.IsKeyJustPressed(ebiten.KeyMinus) || inpututil.IsKeyJustPressed(ebiten.KeyKPSubtract) {
g.adjustSimMultiplier(-simMultiplierStep)
}
if inpututil.IsKeyJustPressed(ebiten.KeyEqual) || inpututil.IsKeyJustPressed(ebiten.KeyKPAdd) {
g.adjustSimMultiplier(simMultiplierStep)
}
}
// adjustSimMultiplier clamps the simulation batch size delta within bounds.
func (g *Game) adjustSimMultiplier(delta int) {
g.simStepMultiplier += delta
if g.simStepMultiplier < minSimMultiplier {
g.simStepMultiplier = minSimMultiplier
} else if g.simStepMultiplier > maxSimMultiplier {
g.simStepMultiplier = maxSimMultiplier
}
}
// simStepsPerSecond returns the nominal simulation steps executed each second.
func (g *Game) simStepsPerSecond() float64 {
return defaultTPS * float64(g.simStepMultiplier)
}