Skip to content

Commit

Permalink
feat: add collapse animation
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Sep 25, 2023
1 parent e6e81b6 commit f0ddd9f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pkg/anim/collapse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package anim

import (
"time"

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

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

var _ Animation = (*Collapse)(nil)

func (c *Collapse) Init(start image.Image) {
c.current = start
}

func (c *Collapse) Update(delta time.Duration) image.Image {
if (delta - c.last) < (time.Second / TICKS_PER_SECOND) {
return c.current
}

c.last = delta

size := c.current.Size()

for row := size.R - 1; row >= 0; row-- {
for col := size.C - 1; col >= 0; col-- {
// can't go past bottom
if row+1 == size.R {
continue
}

next := c.current[row+1][col]
if !next.IsEmpty() {
continue
}
c.current[row+1][col] = c.current[row][col]
c.current[row][col] = emu.EmptyGlyph()
}
}

return c.current
}
1 change: 1 addition & 0 deletions pkg/anim/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func NewAnimator(

func Random() Animation {
anims := []Animation{
&Collapse{},
&Midjo{},
&Cyform{},
&Conway{},
Expand Down
4 changes: 4 additions & 0 deletions pkg/emu/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ type Glyph struct {
FG, BG Color
}

func (g Glyph) IsEmpty() bool {
return g.Char == ' '
}

func EmptyGlyph() Glyph {
return Glyph{
Char: ' ',
Expand Down

0 comments on commit f0ddd9f

Please sign in to comment.