Skip to content

Commit

Permalink
random walking
Browse files Browse the repository at this point in the history
Cloned and flipped the assets manually so that I don't have to implement
in-engine flipping logic.
  • Loading branch information
nhanb committed Jul 30, 2022
1 parent 1d11c87 commit e68a0b0
Show file tree
Hide file tree
Showing 20 changed files with 58 additions and 11 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
![](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 5 animations:
on Windows, Linux, and macOS. It currently has these animations:

- `Idle`
- `Dragging`
- `Right-click`
- Randomly `Walk` horizontally
- 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
Expand Down
66 changes: 56 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"image"
_ "image/png"
"log"
"math/rand"
"time"

"github.com/hajimehoshi/ebiten/v2"
Expand All @@ -32,6 +33,12 @@ var HungrySprites embed.FS
//go:embed sprites/feeding/*
var FeedingSprites embed.FS

//go:embed sprites/walk-left/*
var WalkLeftSprites embed.FS

//go:embed sprites/walk-right/*
var WalkRightSprites embed.FS

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

Expand All @@ -42,16 +49,18 @@ type Anim struct {
type Position struct{ x, y int }

type Game struct {
CurrentAnim *Anim
CurrentFrame int
Ticks int
IsDragging bool
PreviousMousePos Vector
WinStartPos Vector
MouseStartPos Vector

CurrentAnim *Anim
CurrentFrame int
Ticks int
IsDragging bool
PreviousMousePos Vector
WinStartPos Vector
MouseStartPos Vector
Size int
LastFed time.Time
NanosecondsUntilHungry time.Duration
WalkChance int
StopChance int
}

type Vector struct{ x, y int }
Expand Down Expand Up @@ -98,21 +107,49 @@ func (g *Game) Update() error {
handleNonHungryInputs(g)
}

switch g.CurrentAnim {
case WalkLeft:
x, y := ebiten.WindowPosition()
ebiten.SetWindowPosition(x-g.Size, y)
case WalkRight:
x, y := ebiten.WindowPosition()
ebiten.SetWindowPosition(x+g.Size, y)
}

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
}

if g.CurrentAnim == Idle {
if randBool(g.WalkChance) {
if randBool(50) {
g.CurrentAnim = WalkLeft
} else {
g.CurrentAnim = WalkRight
}
}
} else if g.CurrentAnim == WalkLeft || g.CurrentAnim == WalkRight {
if randBool(g.StopChance) {
g.CurrentAnim = Idle
}
}
}
return nil
}

func randBool(chance int) bool {
return rand.Intn(100) < chance
}

func handleNonHungryInputs(g *Game) {
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
if g.CurrentAnim == Idle {
Expand Down Expand Up @@ -182,18 +219,22 @@ func NewAnim(sprites embed.FS, subdir string) *Anim {
return &Anim{frames}
}

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

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

func main() {
var sizeFlag, xFlag, yFlag int
rand.Seed(time.Now().UnixNano())

var sizeFlag, xFlag, yFlag, walkChanceFlag, stopChanceFlag int
var secondsUntilHungryFlag int64
flag.IntVar(
&sizeFlag, "size", 1, "Size multiplier: make Gura as big as you want",
Expand All @@ -206,12 +247,17 @@ func main() {
)
flag.IntVar(&xFlag, "x", 9999, "X position on screen")
flag.IntVar(&yFlag, "y", 9999, "Y position on screen")
flag.IntVar(&walkChanceFlag, "walk", 5, "chance to start walking, in %")
flag.IntVar(&stopChanceFlag, "stop", 40, "chance to stop walking, in %")
flag.Parse()

var game Game
game.CurrentAnim = Idle
game.LastFed = time.Now()
game.NanosecondsUntilHungry = time.Duration(secondsUntilHungryFlag) * 1_000_000_000
game.Size = sizeFlag
game.WalkChance = walkChanceFlag
game.StopChance = stopChanceFlag

ebiten.SetWindowSize(SPRITE_X*sizeFlag, SPRITE_Y*sizeFlag)
ebiten.SetWindowTitle("Shark!")
Expand Down
Binary file added sprites/walk-left/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/walk-left/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/walk-left/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/walk-left/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/walk-left/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/walk-left/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/walk-left/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/walk-left/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/walk-left/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/walk-right/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/walk-right/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/walk-right/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/walk-right/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/walk-right/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/walk-right/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/walk-right/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/walk-right/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/walk-right/08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e68a0b0

Please sign in to comment.