Skip to content

Commit

Permalink
feat: break out all animations into packages
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Nov 11, 2024
1 parent 6cb663e commit 67c02d1
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 83 deletions.
15 changes: 7 additions & 8 deletions pkg/anim/collapse.go → pkg/anim/collapse/module.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package anim
package collapse

import (
"time"

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

const (
TICKS_PER_SECOND = 10
)

type Collapse struct {
last time.Duration
current image.Image
}

var _ Animation = (*Collapse)(nil)
var _ meta.Animation = (*Collapse)(nil)

func (c *Collapse) Init(start image.Image) {
c.current = start
Expand Down Expand Up @@ -45,9 +50,3 @@ func (c *Collapse) Update(delta time.Duration) image.Image {

return c.current
}

func init() {
registerAnimation("collapse", func() Animation {
return &Collapse{}
})
}
11 changes: 3 additions & 8 deletions pkg/anim/conway.go → pkg/anim/conway/module.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package anim
package conway

import (
"time"

"github.com/cfoust/cy/pkg/anim/meta"
"github.com/cfoust/cy/pkg/emu"
"github.com/cfoust/cy/pkg/geom/image"
)
Expand All @@ -12,7 +13,7 @@ type Conway struct {
image image.Image
}

var _ Animation = (*Conway)(nil)
var _ meta.Animation = (*Conway)(nil)

func (c *Conway) Init(start image.Image) {
c.image = start.Clone()
Expand Down Expand Up @@ -94,9 +95,3 @@ func (c *Conway) Update(delta time.Duration) image.Image {
c.image = next
return c.image
}

func init() {
registerAnimation("conway", func() Animation {
return &Conway{}
})
}
8 changes: 3 additions & 5 deletions pkg/anim/cos.go → pkg/anim/cos/module.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package anim
package cos

import (
"math"
"time"

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

Expand All @@ -12,7 +13,7 @@ type Cos struct {
out image.Image
}

var _ Animation = (*Cos)(nil)
var _ meta.Animation = (*Cos)(nil)

func (cos *Cos) Init(start image.Image) {
cos.in = start.Clone()
Expand All @@ -37,7 +38,4 @@ func (cos *Cos) Update(delta time.Duration) image.Image {
}

func init() {
registerAnimation("cos", func() Animation {
return &Cos{}
})
}
11 changes: 3 additions & 8 deletions pkg/anim/form.go → pkg/anim/cy/module.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package anim
package cy

import (
"time"
"unicode"

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

type Cyform struct {
start image.Image
}

var _ Animation = (*Cyform)(nil)
var _ meta.Animation = (*Cyform)(nil)

func (c *Cyform) Init(start image.Image) {
c.start = start
Expand Down Expand Up @@ -51,9 +52,3 @@ func (c *Cyform) Update(delta time.Duration) image.Image {

return c.start
}

func init() {
registerAnimation("cy", func() Animation {
return &Cyform{}
})
}
22 changes: 8 additions & 14 deletions pkg/anim/fluid.go → pkg/anim/fluid/anim.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package anim
package fluid

import (
"time"

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

type Fluid struct {
last time.Duration
sim *fluid.Simulator
sim *Simulator
start image.Image
current image.Image
}

var _ Animation = (*Fluid)(nil)
var _ meta.Animation = (*Fluid)(nil)

const (
POSITION_FACTOR = 10
Expand All @@ -26,13 +26,13 @@ func (f *Fluid) Init(start image.Image) {

size := start.Size()

var particles []fluid.Particle
var particles []Particle
for row := 0; row < size.R; row++ {
for col := 0; col < size.C; col++ {
if start[row][col].IsEmpty() {
continue
}
particles = append(particles, fluid.NewParticle(
particles = append(particles, NewParticle(
float64(col)*POSITION_FACTOR,
float64(row)*POSITION_FACTOR,
0,
Expand All @@ -41,7 +41,7 @@ func (f *Fluid) Init(start image.Image) {
}
}

f.sim = fluid.New(
f.sim = New(
float64(size.C)*POSITION_FACTOR,
float64(size.R)*POSITION_FACTOR,
particles,
Expand All @@ -66,7 +66,7 @@ func (f *Fluid) Update(delta time.Duration) image.Image {
i := f.current
particles := f.sim.Particles()

var particle fluid.Particle
var particle Particle
var index, destRow, destCol int
for row := 0; row < size.R; row++ {
for col := 0; col < size.C; col++ {
Expand Down Expand Up @@ -96,9 +96,3 @@ func (f *Fluid) Update(delta time.Duration) image.Image {

return i
}

func init() {
registerAnimation("fluid", func() Animation {
return &Fluid{}
})
}
9 changes: 1 addition & 8 deletions pkg/anim/animation.go → pkg/anim/meta/module.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
package anim
package meta

import (
"time"

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

const DEFAULT_FPS = 30

type Animation interface {
Init(image.Image)
Update(delta time.Duration) image.Image
}

type Creator func() Animation

var Animations = map[string]Creator{}

func registerAnimation(name string, animation Creator) {
Animations[name] = animation
}
11 changes: 3 additions & 8 deletions pkg/anim/midjo.go → pkg/anim/midjo/module.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package anim
package midjo

import (
"math"
"time"

"github.com/cfoust/cy/pkg/anim/meta"
"github.com/cfoust/cy/pkg/emu"
"github.com/cfoust/cy/pkg/geom/image"
)
Expand All @@ -13,7 +14,7 @@ type Midjo struct {
out image.Image
}

var _ Animation = (*Midjo)(nil)
var _ meta.Animation = (*Midjo)(nil)

func (m *Midjo) Init(start image.Image) {
m.in = start.Clone()
Expand Down Expand Up @@ -44,9 +45,3 @@ func (mid *Midjo) Update(delta time.Duration) image.Image {
}
return mid.out
}

func init() {
registerAnimation("midjo", func() Animation {
return &Midjo{}
})
}
51 changes: 51 additions & 0 deletions pkg/anim/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package anim

import (
"github.com/cfoust/cy/pkg/anim/collapse"
"github.com/cfoust/cy/pkg/anim/conway"
"github.com/cfoust/cy/pkg/anim/cos"
"github.com/cfoust/cy/pkg/anim/cy"
"github.com/cfoust/cy/pkg/anim/fluid"
"github.com/cfoust/cy/pkg/anim/meta"
"github.com/cfoust/cy/pkg/anim/midjo"
"github.com/cfoust/cy/pkg/anim/musicforprogramming"
"github.com/cfoust/cy/pkg/anim/slime"
)

const DEFAULT_FPS = 30

type Creator = meta.Creator
type Animation = meta.Animation

var Animations = map[string]Creator{}

func registerAnimation(name string, animation Creator) {
Animations[name] = animation
}

func init() {
registerAnimation("collapse", func() Animation {
return &collapse.Collapse{}
})
registerAnimation("conway", func() Animation {
return &conway.Conway{}
})
registerAnimation("cos", func() Animation {
return &cos.Cos{}
})
registerAnimation("cy", func() Animation {
return &cy.Cyform{}
})
registerAnimation("fluid", func() Animation {
return &fluid.Fluid{}
})
registerAnimation("midjo", func() Animation {
return &midjo.Midjo{}
})
registerAnimation("musicforprogramming", func() Animation {
return musicforprogramming.New()
})
registerAnimation("slime", func() Animation {
return &slime.Slime{}
})
}
15 changes: 7 additions & 8 deletions pkg/anim/reform.go → pkg/anim/musicforprogramming/module.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package anim
package musicforprogramming

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

"github.com/cfoust/cy/pkg/anim/meta"
"github.com/cfoust/cy/pkg/emu"
"github.com/cfoust/cy/pkg/geom"
"github.com/cfoust/cy/pkg/geom/image"
Expand Down Expand Up @@ -52,7 +53,7 @@ type MFP struct {
lastReverse bool
}

var _ Animation = (*MFP)(nil)
var _ meta.Animation = (*MFP)(nil)

func (r *MFP) Init(start image.Image) {
r.render = taro.NewRenderer()
Expand Down Expand Up @@ -424,10 +425,8 @@ func (r *MFP) Update(delta time.Duration) image.Image {
return r.out
}

func init() {
registerAnimation("musicforprogramming", func() Animation {
return &MFP{
duration: 5 * time.Second,
}
})
func New() *MFP {
return &MFP{
duration: 5 * time.Second,
}
}
18 changes: 6 additions & 12 deletions pkg/anim/slime.go → pkg/anim/slime/anim.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package anim
package slime

import (
"math"
"time"

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

type Slime struct {
frame int
zoomFrame int
last time.Duration
sim *slime.Simulator
sim *Simulator
start image.Image
current image.Image
}

var _ Animation = (*Slime)(nil)
var _ meta.Animation = (*Slime)(nil)

func (f *Slime) Init(start image.Image) {
f.start = start

size := start.Size()
f.sim = slime.New(size.R, size.C)
f.sim = New(size.R, size.C)
}

func (f *Slime) Update(delta time.Duration) image.Image {
Expand All @@ -34,7 +34,7 @@ func (f *Slime) Update(delta time.Duration) image.Image {
f.last = delta

size := f.start.Size()
cursor := slime.Cursor{}
cursor := Cursor{}

// This determines how often we zoom
zoom := math.Sin(float64(f.frame) / 80)
Expand Down Expand Up @@ -62,9 +62,3 @@ func (f *Slime) Update(delta time.Duration) image.Image {
f.frame++
return f.current
}

func init() {
registerAnimation("slime", func() Animation {
return &Slime{}
})
}
Loading

0 comments on commit 67c02d1

Please sign in to comment.