Skip to content

Commit

Permalink
feat: zooming
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Oct 20, 2024
1 parent 78ebed5 commit b0e196a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 33 deletions.
38 changes: 12 additions & 26 deletions pkg/anim/slime.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package anim

import (
"math"
"math/rand"
"time"

"github.com/cfoust/cy/pkg/anim/slime"
"github.com/cfoust/cy/pkg/geom/image"
)

type Slime struct {
frame int
last time.Duration
sim *slime.Simulator
start image.Image
Expand All @@ -22,29 +22,7 @@ func (f *Slime) Init(start image.Image) {
f.start = start

size := start.Size()

var agents []slime.Agent
for row := 0; row < size.R; row++ {
for col := 0; col < size.C; col++ {
if start[row][col].IsEmpty() {
continue
}
agents = append(agents, slime.Agent{
Pos: slime.Vec2{
X: float64(col),
Y: float64(row),
},
Dir: slime.Vec2{X: 1, Y: 0}.
Rot(rand.Float64() * 2 * math.Pi),
})
}
}

f.sim = slime.New(
size.R,
size.C,
agents,
)
f.sim = slime.New(size.R, size.C)
}

func (f *Slime) Update(delta time.Duration) image.Image {
Expand All @@ -54,11 +32,19 @@ func (f *Slime) Update(delta time.Duration) image.Image {

f.last = delta

f.sim.Step(slime.Cursor{})

size := f.start.Size()
cursor := slime.Cursor{}
if math.Sin(float64(f.frame)/50) > 0.6 {
cursor.Pressed = true
cursor.X = float64(size.C) / 2
cursor.Y = float64(size.R) / 2
}

f.sim.Step(cursor)

f.current = image.New(size)
f.sim.Render(f.current)
f.frame++
return f.current
}

Expand Down
14 changes: 7 additions & 7 deletions pkg/anim/slime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ func randCircle() Vec2 {
}

type Cursor struct {
pressed bool
x, y float64
Pressed bool
X, Y float64
}

type Simulator struct {
Expand All @@ -151,7 +151,7 @@ type Simulator struct {

func (s *Simulator) updateView(cursor Cursor) {
var targetScale Vec2
if cursor.pressed {
if cursor.Pressed {
// Zoom in for detailed view
targetScale = Vec2{Y: 1 / ASPECT, X: 1}
} else if float64(s.rows)/ASPECT < float64(s.cols) {
Expand All @@ -173,8 +173,8 @@ func (s *Simulator) updateView(cursor Cursor) {
s.viewScale.X += 0.1 * (targetScale.X - s.viewScale.X)

var targetFocus Vec2
if cursor.pressed {
targetFocus = Vec2{Y: cursor.y / float64(s.rows), X: cursor.x / float64(s.cols)}
if cursor.Pressed {
targetFocus = Vec2{Y: cursor.Y / float64(s.rows), X: cursor.X / float64(s.cols)}
} else {
targetFocus = Vec2{Y: 0.5, X: 0.5}
}
Expand Down Expand Up @@ -278,11 +278,11 @@ func (s *Simulator) Render(out image.Image) {
}
}

func New(rows, cols int, agents []Agent) *Simulator {
func New(rows, cols int) *Simulator {
chem := make([]float64, HEIGHT*WIDTH)
wip := make([]float64, HEIGHT*WIDTH)

agents = make([]Agent, NUM_AGENTS)
agents := make([]Agent, NUM_AGENTS)
for i := 0; i < NUM_AGENTS; i++ {
agents[i] = Agent{
Pos: randCircle().
Expand Down

0 comments on commit b0e196a

Please sign in to comment.