Update examples per the corrected algorithm

This commit is contained in:
Christian Rocha 2021-07-15 10:26:42 -04:00
parent 62c83bf96a
commit 3250234a99
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
2 changed files with 36 additions and 32 deletions

View File

@ -16,8 +16,8 @@ const (
fps = 60 fps = 60
spriteWidth = 12 spriteWidth = 12
spriteHeight = 5 spriteHeight = 5
frequency = 0.95 frequency = 7.8
damping = 0.98 damping = 0.15
) )
var ( var (
@ -27,8 +27,8 @@ var (
MarginLeft(2) MarginLeft(2)
spriteStyle = lipgloss.NewStyle(). spriteStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("229")). Foreground(lipgloss.Color("#FFFDF5")).
Background(lipgloss.Color("69")) Background(lipgloss.Color("#575BD8"))
) )
type frameMsg time.Time type frameMsg time.Time
@ -39,6 +39,11 @@ func animate() tea.Cmd {
}) })
} }
func waitASec() tea.Msg {
time.Sleep(time.Millisecond * 750)
return nil
}
type model struct { type model struct {
x float64 x float64
xVel float64 xVel float64
@ -63,7 +68,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Quit when we're basically at the target position. // Quit when we're basically at the target position.
if math.Abs(m.x-targetX) < 0.01 { if math.Abs(m.x-targetX) < 0.01 {
return m, tea.Quit return m, tea.Sequentially(waitASec, tea.Quit)
} }
// Request next frame // Request next frame

View File

@ -17,6 +17,9 @@ import (
const ( const (
width = 1024 width = 1024
height = 768 height = 768
bgColor = "#575BD8"
textColor = "#827EFF"
spriteColor = "#FFFDF5"
fontFile = "JetBrainsMono-Regular.ttf" fontFile = "JetBrainsMono-Regular.ttf"
idleTimeout = time.Second * 4 idleTimeout = time.Second * 4
) )
@ -27,19 +30,16 @@ func main() {
} }
type Game struct { type Game struct {
deltaTime float64 deltaTime float64
frequency float64 frequency float64
damping float64 damping float64
win *pixelgl.Window win *pixelgl.Window
bgColor string shift bool
textColor string font font.Face
spriteColor string sprite *Sprite
shift bool lastClick time.Time
font font.Face dirty bool
sprite *Sprite showHelp bool
lastClick time.Time
dirty bool
showHelp bool
} }
func (g Game) Size() (width, height float64) { func (g Game) Size() (width, height float64) {
@ -51,11 +51,8 @@ func (g Game) MousePosition() (x, y float64) {
} }
func (g Game) Run() { func (g Game) Run() {
g.frequency = 2.0 g.frequency = 10.0
g.damping = 2.0 g.damping = 0.2
g.bgColor = "#575BD8"
g.textColor = "#827EFF"
g.spriteColor = "#FFFDF5"
g.showHelp = true g.showHelp = true
var err error var err error
@ -116,9 +113,9 @@ func (g *Game) Update() {
case released(pixelgl.KeyRight): case released(pixelgl.KeyRight):
adjustFreq = 0.1 adjustFreq = 0.1
case released(pixelgl.KeyUp): case released(pixelgl.KeyUp):
adjustDamp = 0.1 adjustDamp = 0.01
case released(pixelgl.KeyDown): case released(pixelgl.KeyDown):
adjustDamp = -0.1 adjustDamp = -0.01
case released(pixelgl.KeySpace): case released(pixelgl.KeySpace):
g.showHelp = !g.showHelp g.showHelp = !g.showHelp
} }
@ -168,7 +165,7 @@ func (g Game) Draw(ctx *gg.Context) {
w, h := g.Size() w, h := g.Size()
// BG // BG
ctx.SetHexColor(g.bgColor) ctx.SetHexColor(bgColor)
ctx.DrawRectangle(0, 0, w, h) ctx.DrawRectangle(0, 0, w, h)
ctx.Fill() ctx.Fill()
@ -181,7 +178,7 @@ func (g Game) Draw(ctx *gg.Context) {
// Instructional text // Instructional text
ctx.Push() ctx.Push()
ctx.SetHexColor(g.textColor) ctx.SetHexColor(textColor)
ctx.SetFontFace(g.font) ctx.SetFontFace(g.font)
ctx.DrawStringAnchored("Click!", w/2, h/2, 0.5, 0.5) ctx.DrawStringAnchored("Click!", w/2, h/2, 0.5, 0.5)
ctx.Fill() ctx.Fill()
@ -189,11 +186,11 @@ func (g Game) Draw(ctx *gg.Context) {
// Status // Status
str := fmt.Sprintf( str := fmt.Sprintf(
"Frequency: %.1f (←/→: adjust) • Damping: %.1f (↑/↓: adjust)", "Frequency: %.1f (←/→: adjust) • Damping: %.2f (↑/↓: adjust)",
g.frequency, g.damping, g.frequency, g.damping,
) )
ctx.Push() ctx.Push()
ctx.SetHexColor(g.textColor) ctx.SetHexColor(textColor)
ctx.SetFontFace(g.font) ctx.SetFontFace(g.font)
ctx.DrawStringAnchored(str, w/2, h-34, 0.5, 0) ctx.DrawStringAnchored(str, w/2, h-34, 0.5, 0)
ctx.Fill() ctx.Fill()
@ -237,7 +234,7 @@ func NewSprite(g *Game) *Sprite {
TargetX: mouseX, TargetX: mouseX,
TargetY: mouseY, TargetY: mouseY,
radius: 0.1, radius: 0.1,
color: g.spriteColor, color: spriteColor,
game: g, game: g,
} }
s.computeSpring() s.computeSpring()
@ -306,7 +303,9 @@ func (s *Sprite) randomRadius() {
} }
func (s *Sprite) Show() { func (s *Sprite) Show() {
s.X, s.Y = s.game.MousePosition() if s.State() == gone {
s.X, s.Y = s.game.MousePosition()
}
if s.radius < 0.1 { if s.radius < 0.1 {
s.radius = 0.1 s.radius = 0.1
} }
@ -316,7 +315,7 @@ func (s *Sprite) Show() {
func (s *Sprite) Hide() { func (s *Sprite) Hide() {
// Fixed frequency and damping when hiding // Fixed frequency and damping when hiding
s.spring = harmonica.NewSpring(s.game.deltaTime, 3.0, 8.0) s.spring = harmonica.NewSpring(s.game.deltaTime, 32.0, 1.0)
s.TargetRadius = 0 s.TargetRadius = 0
} }