Skip to content

Commit

Permalink
hungry & feeding animation; also app icon
Browse files Browse the repository at this point in the history
Since the new feeding anim extends 12px upwards, other sprites needed to
be extended as well.

It may have been more proper to implement some sort of per-sprite coords
system but then I decided to just go with the simplest alternative.

Also added app icon and configurable x,y flags.
  • Loading branch information
nhanb committed Jul 9, 2022
1 parent 4b682a2 commit ca5d22a
Show file tree
Hide file tree
Showing 61 changed files with 103 additions and 27 deletions.
31 changes: 24 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
![](https://user-images.githubusercontent.com/1446315/177188223-ad9759c9-4ef4-44e0-84d8-03cfd46129b8.png)

This is a PoC "desktop pet" à la [shimeji][1] using [ebitengine][2] that runs
on Windows, Linux, and macOS. It currently has only 3 animations: idle
(default), left-click dragging, and right-click.
on Windows, Linux, and macOS. It currently has only 5 animations:

Here's a demo video:
- `Idle`
- `Dragging`
- `Right-click`
- After some time has passed (1 hour by default), a `Hungry` animation will be
activated, during which dragging is disabled.
- When `Hungry`, right-click to start `Feeding` animation and reset to the
normal idle state.

https://user-images.githubusercontent.com/1446315/176439983-091dec3d-bc36-4ae3-8b78-2a2a7f11e90d.mp4
Here's a [demo video](https://user-images.githubusercontent.com/1446315/178103169-006c2bc0-ebb9-4014-aba5-8a1fbc3d0733.mp4).

Fair warning: I'm a Go noob who mostly has no idea what he's doing.
Read the source code at your own peril.
Expand All @@ -32,9 +37,20 @@ Simply run the provided binary for your OS. Mac & Linux users may need to first
make the file executable with `chmod +x <file-name>`.

If run from a terminal, use the `-h` argument to see available options.
Currently there's only a `-size` argument which changes how big your shark will
be rendered. Windows users can create a shortcut which lets you specify your
desired arguments.
Windows users can [create a shortcut][7] to save their desired options.

Here are the currently supported options:

```
-hungry int
The number of seconds it takes for Gura to go hungry (default 3600)
-size int
Size multiplier: make Gura as big as you want (default 1)
-x int
X position on screen (default 9999)
-y int
Y position on screen (default 9999)
```

# Compile from source

Expand Down Expand Up @@ -70,6 +86,7 @@ this program. If not, see <https://www.gnu.org/licenses/>.
[4]: https://www.facebook.com/meexway
[5]: https://github.com/nhanb/shark/releases/latest
[6]: https://ebiten.org/documents/install.html
[7]: https://superuser.com/questions/29569/how-to-add-command-line-options-to-shortcut

[srht]: https://builds.sr.ht/~nhanb/shark/commits/master
[gh]: https://github.com/nhanb/shark/actions/workflows/main.yml
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 79 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"bytes"
"embed"
"flag"
"image"
_ "image/png"
"log"
"time"

"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)

const SPRITE_X = 100
const SPRITE_Y = 123
const SPRITE_Y = 135

//go:embed sprites/idle/*
var IdleSprites embed.FS
Expand All @@ -24,6 +26,15 @@ var RightClickSprites embed.FS
//go:embed sprites/drag/*
var DragSprites embed.FS

//go:embed sprites/hungry/*
var HungrySprites embed.FS

//go:embed sprites/feeding/*
var FeedingSprites embed.FS

//go:embed icon.png
var IconFile []byte

type Anim struct {
Frames []*ebiten.Image
}
Expand All @@ -38,6 +49,9 @@ type Game struct {
PreviousMousePos Vector
WinStartPos Vector
MouseStartPos Vector

LastFed time.Time
NanosecondsUntilHungry time.Duration
}

type Vector struct{ x, y int }
Expand All @@ -60,6 +74,46 @@ func GlobalCursorPosition() Vector {
}

func (g *Game) Update() error {
isHungry := false

if time.Now().Sub(g.LastFed) >= g.NanosecondsUntilHungry {
// The only allowed interaction when hungry is right-click to feed.
isHungry = true
g.IsDragging = false
if g.CurrentAnim != Hungry {
g.CurrentAnim = Hungry
g.Ticks = 0
g.CurrentFrame = 0
return nil
} else if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
g.CurrentAnim = Feeding
g.Ticks = 0
g.CurrentFrame = 0
g.LastFed = time.Now()
return nil
}
}

if !isHungry && g.CurrentAnim != Feeding {
handleNonHungryInputs(g)
}

g.Ticks++
if g.Ticks < 10 {
return nil
}
g.Ticks = 0
g.CurrentFrame++
if g.CurrentFrame >= len(g.CurrentAnim.Frames) {
g.CurrentFrame = 0
if g.CurrentAnim == RightClick || g.CurrentAnim == Feeding {
g.CurrentAnim = Idle
}
}
return nil
}

func handleNonHungryInputs(g *Game) {
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
if g.CurrentAnim == Idle {
g.CurrentAnim = RightClick
Expand Down Expand Up @@ -89,28 +143,16 @@ func (g *Game) Update() error {
newWinPos := g.WinStartPos.Add(mousePos.Subtract(g.MouseStartPos))
ebiten.SetWindowPosition(newWinPos.x, newWinPos.y)
}
g.PreviousMousePos = mousePos

g.Ticks++
if g.Ticks < 10 {
return nil
}

g.Ticks = 0
g.CurrentFrame++
if g.CurrentFrame >= len(g.CurrentAnim.Frames) {
g.CurrentFrame = 0
if g.CurrentAnim == RightClick {
g.CurrentAnim = Idle
}
}
return nil
g.PreviousMousePos = mousePos
}

func (g *Game) Draw(screen *ebiten.Image) {
screen.DrawImage(g.CurrentAnim.Frames[g.CurrentFrame], nil)
/*
debugStr := ""
debugStr += fmt.Sprintf("%v\n", g.Ticks)
debugStr += fmt.Sprintf("%v\n", g.LastFed)
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
debugStr += "Dragging\n"
}
Expand Down Expand Up @@ -140,31 +182,48 @@ func NewAnim(sprites embed.FS, subdir string) *Anim {
return &Anim{frames}
}

var Idle, RightClick, Drag *Anim
var Idle, RightClick, Drag, Hungry, Feeding *Anim

func init() {
Idle = NewAnim(IdleSprites, "idle")
Drag = NewAnim(DragSprites, "drag")
RightClick = NewAnim(RightClickSprites, "right-click")
Hungry = NewAnim(HungrySprites, "hungry")
Feeding = NewAnim(FeedingSprites, "feeding")
}

func main() {
var sizeFlag int
var sizeFlag, xFlag, yFlag int
var secondsUntilHungryFlag int64
flag.IntVar(
&sizeFlag, "size", 2, "Size multiplier: make Gura as big as you want.",
&sizeFlag, "size", 1, "Size multiplier: make Gura as big as you want",
)
flag.Int64Var(
&secondsUntilHungryFlag,
"hungry",
3600,
"The number of seconds it takes for Gura to go hungry",
)
flag.IntVar(&xFlag, "x", 9999, "X position on screen")
flag.IntVar(&yFlag, "y", 9999, "Y position on screen")
flag.Parse()

var game Game
game.CurrentAnim = Idle
game.LastFed = time.Now()
game.NanosecondsUntilHungry = time.Duration(secondsUntilHungryFlag) * 1_000_000_000

ebiten.SetWindowSize(SPRITE_X*sizeFlag, SPRITE_Y*sizeFlag)
ebiten.SetWindowTitle("Shark!")
ebiten.SetWindowDecorated(false)
ebiten.SetScreenTransparent(true)
ebiten.SetWindowPosition(9999, 9999)
ebiten.SetWindowPosition(xFlag, yFlag)
ebiten.SetWindowFloating(true)

AppIcon, _, iconerr := image.Decode(bytes.NewReader(IconFile))
PanicIfErr(iconerr)
ebiten.SetWindowIcon([]image.Image{AppIcon})

err := ebiten.RunGame(&game)
PanicIfErr(err)
}
Expand Down
Binary file modified sprites/drag/00.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sprites/drag/01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sprites/drag/02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sprites/drag/03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/00.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/09.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sprites/feeding/20.png
Binary file added sprites/feeding/21.png
Binary file added sprites/feeding/22.png
Binary file added sprites/feeding/23.png
Binary file added sprites/hungry/00.png
Binary file added sprites/hungry/01.png
Binary file added sprites/hungry/02.png
Binary file added sprites/hungry/03.png
Binary file added sprites/hungry/04.png
Binary file added sprites/hungry/05.png
Binary file added sprites/hungry/06.png
Binary file added sprites/hungry/07.png
Binary file added sprites/hungry/08.png
Binary file added sprites/hungry/09.png
Binary file added sprites/hungry/10.png
Binary file added sprites/hungry/11.png
Binary file modified sprites/idle/00.png
Binary file modified sprites/idle/01.png
Binary file modified sprites/idle/02.png
Binary file modified sprites/idle/03.png
Binary file modified sprites/right-click/02.png
Binary file modified sprites/right-click/03.png
Binary file modified sprites/right-click/04.png
Binary file modified sprites/right-click/05.png
Binary file modified sprites/right-click/06.png
Binary file modified sprites/right-click/07.png
Binary file modified sprites/right-click/08.png
Binary file modified sprites/right-click/09.png
Binary file modified sprites/right-click/10.png
Binary file modified sprites/right-click/11.png
Binary file modified sprites/right-click/12.png
Binary file modified sprites/right-click/13.png
Binary file modified sprites/right-click/14.png
Binary file modified sprites/right-click/15.png

0 comments on commit ca5d22a

Please sign in to comment.