File: main.go

package info (click to toggle)
golang-github-charmbracelet-harmonica 0.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 284 kB
  • sloc: makefile: 3
file content (325 lines) | stat: -rw-r--r-- 6,331 bytes parent folder | download
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main

import (
	"fmt"
	"image"
	"math"
	"math/rand"
	"time"

	"github.com/charmbracelet/harmonica"
	"github.com/faiface/pixel"
	"github.com/faiface/pixel/pixelgl"
	"github.com/fogleman/gg"
	"golang.org/x/image/font"
)

const (
	width       = 1024
	height      = 768
	bgColor     = "#575BD8"
	textColor   = "#827EFF"
	spriteColor = "#FFFDF5"
	fontFile    = "JetBrainsMono-Regular.ttf"
	idleTimeout = time.Second * 4
)

func main() {
	rand.Seed(time.Now().UnixNano())
	pixelgl.Run(Game{}.Run)
}

type Game struct {
	deltaTime float64
	frequency float64
	damping   float64
	win       *pixelgl.Window
	shift     bool
	font      font.Face
	sprite    *Sprite
	lastClick time.Time
	dirty     bool
	showHelp  bool
}

func (g Game) Size() (width, height float64) {
	return g.win.Bounds().W(), g.win.Bounds().H()
}

func (g Game) MousePosition() (x, y float64) {
	return g.win.MousePosition().X, g.win.MousePosition().Y
}

func (g Game) Run() {
	g.frequency = 10.0
	g.damping = 0.2
	g.showHelp = true

	var err error
	if g.font, err = gg.LoadFontFace(fontFile, 24); err != nil {
		panic(err)
	}

	cfg := pixelgl.WindowConfig{
		Title:     "Harmonica Example",
		Bounds:    pixel.R(0, 0, width, height),
		VSync:     true,
		Resizable: false,
	}

	if g.win, err = pixelgl.NewWindow(cfg); err != nil {
		panic(err)
	}

	last := time.Now()
	for !g.win.Closed() {
		w, h := g.Size()

		// Get delta time
		g.deltaTime = time.Since(last).Seconds()
		last = time.Now()

		g.Update()

		// Use fogleman/gg to render everything
		ctx := gg.NewContext(int(w), int(h))
		g.Draw(ctx)
		canvas := g.win.Canvas()
		canvas.SetPixels(ctx.Image().(*image.RGBA).Pix)

		// Render
		g.win.Update()
	}
}

func (g *Game) Update() {
	pressed := g.win.JustPressed
	released := g.win.JustReleased

	// Handle shift key
	switch {
	case pressed(pixelgl.KeyLeftShift), pressed(pixelgl.KeyRightShift):
		g.shift = true
	case released(pixelgl.KeyLeftShift), released(pixelgl.KeyRightShift):
		g.shift = false
	}

	adjustFreq, adjustDamp := 0.0, 0.0

	// Handle arrow keys to adjust frequency and damping
	switch {
	case released(pixelgl.KeyLeft):
		adjustFreq = -0.1
	case released(pixelgl.KeyRight):
		adjustFreq = 0.1
	case released(pixelgl.KeyUp):
		adjustDamp = 0.01
	case released(pixelgl.KeyDown):
		adjustDamp = -0.01
	case released(pixelgl.KeySpace):
		g.showHelp = !g.showHelp
	}

	if g.shift {
		adjustDamp *= 10
		adjustFreq *= 10
	}

	if adjustFreq != 0 || adjustDamp != 0 {
		g.frequency = math.Max(0, g.frequency+adjustFreq)
		g.damping = math.Max(0, g.damping+adjustDamp)
		g.dirty = true
	}

	if time.Now().After(g.lastClick.Add(idleTimeout)) {
		if g.sprite != nil {
			g.sprite.Hide()
		}
	}

	if released(pixelgl.MouseButtonLeft) {
		if g.sprite == nil {
			g.sprite = NewSprite(g)
		} else {
			s := g.sprite
			s.TargetX, s.TargetY = g.MousePosition()

			switch s.State() {
			case hiding, gone:
				s.Show()
			}
		}

		g.lastClick = time.Now()
	}

	// Sprite
	if g.sprite != nil {
		g.sprite.Update()
	}

	g.dirty = false
}

func (g Game) Draw(ctx *gg.Context) {
	w, h := g.Size()

	// BG
	ctx.SetHexColor(bgColor)
	ctx.DrawRectangle(0, 0, w, h)
	ctx.Fill()

	// Help text
	if g.showHelp {
		// For some reason our text renders upside down and backwards. Apply a
		// matrix to fix it.
		ctx.ScaleAbout(-1, 1, w/2, h/2)
		ctx.RotateAbout(math.Pi, w/2, h/2)

		// Instructional text
		ctx.Push()
		ctx.SetHexColor(textColor)
		ctx.SetFontFace(g.font)
		ctx.DrawStringAnchored("Click!", w/2, h/2, 0.5, 0.5)
		ctx.Fill()
		ctx.Pop()

		// Status
		str := fmt.Sprintf(
			"Frequency: %.1f (←/→: adjust) • Damping: %.2f (↑/↓: adjust)",
			g.frequency, g.damping,
		)
		ctx.Push()
		ctx.SetHexColor(textColor)
		ctx.SetFontFace(g.font)
		ctx.DrawStringAnchored(str, w/2, h-34, 0.5, 0)
		ctx.Fill()
		ctx.Pop()

		// Reset matrix
		ctx.Identity()
	}

	// Draw sprites
	if g.sprite != nil {
		g.sprite.Draw(ctx)
	}
}

type SpriteState int

const (
	moving SpriteState = iota
	stopped
	hiding
	gone
)

type Sprite struct {
	TargetX, TargetY, TargetRadius float64
	X, xVel                        float64
	Y, yVel                        float64
	radius, radiusVel              float64
	color                          string
	spring                         harmonica.Spring
	game                           *Game
}

func NewSprite(g *Game) *Sprite {
	mouseX, mouseY := g.MousePosition()

	s := &Sprite{
		X:       mouseX,
		Y:       mouseY,
		TargetX: mouseX,
		TargetY: mouseY,
		radius:  0.1,
		color:   spriteColor,
		game:    g,
	}
	s.computeSpring()
	s.randomRadius()

	return s
}

func (s *Sprite) Update() {
	if s.State() == gone {
		return
	}

	if s.game.dirty {
		// Recompute spring coefficients since our frequency or damping has
		// changed.
		s.computeSpring()
	}

	// Calculate positions based on our spring
	s.X, s.xVel = s.spring.Update(s.X, s.xVel, s.TargetX)
	s.Y, s.yVel = s.spring.Update(s.Y, s.yVel, s.TargetY)
	s.radius, s.radiusVel = s.spring.Update(s.radius, s.radiusVel, s.TargetRadius)
}

func (s Sprite) Draw(ctx *gg.Context) {
	if s.State() == gone {
		return
	}

	ctx.Push()
	ctx.DrawCircle(s.X, s.Y, s.radius)
	ctx.SetHexColor(s.color)
	ctx.Fill()
	ctx.Pop()
}

func (s Sprite) State() SpriteState {
	const precision = 2
	x := roundFloat(s.X, precision)
	tX := roundFloat(s.TargetX, precision)
	y := roundFloat(s.Y, precision)
	tY := roundFloat(s.TargetY, precision)
	r := roundFloat(s.radius, precision)
	tR := roundFloat(s.TargetRadius, precision)

	if r == 0.0 {
		return gone
	}
	if s.TargetRadius == 0 {
		return hiding
	}
	if x == tX && y == tY && r == tR {
		return stopped
	}
	return moving
}

func (s *Sprite) computeSpring() {
	// Calculate spring coefficients
	s.spring = harmonica.NewSpring(s.game.deltaTime, s.game.frequency, s.game.damping)
}

func (s *Sprite) randomRadius() {
	s.TargetRadius = rand.Float64()*100.0 + 100.0
}

func (s *Sprite) Show() {
	if s.State() == gone {
		s.X, s.Y = s.game.MousePosition()
	}
	if s.radius < 0.1 {
		s.radius = 0.1
	}
	s.computeSpring()
	s.randomRadius()
}

func (s *Sprite) Hide() {
	// Fixed frequency and damping when hiding
	s.spring = harmonica.NewSpring(s.game.deltaTime, 32.0, 1.0)
	s.TargetRadius = 0
}

func roundFloat(input float64, decimalPlaces int) float64 {
	pow := math.Pow(10, float64(decimalPlaces))
	return math.Round(pow*input) / pow
}