Repos / shark / e68a0b0f0d
commit e68a0b0f0d78769791598174e469d7da3d62c3a5
Author: Nhân <hi@imnhan.com>
Date: Sat Jul 30 18:11:06 2022 +0700
random walking
Cloned and flipped the assets manually so that I don't have to implement
in-engine flipping logic.
diff --git a/README.md b/README.md
index 9089cd7..35d909b 100644
--- a/README.md
+++ b/README.md
@@ -3,11 +3,12 @@ # What
![](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
diff --git a/main.go b/main.go
index ec39238..21c3fc9 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@
"image"
_ "image/png"
"log"
+ "math/rand"
"time"
"github.com/hajimehoshi/ebiten/v2"
@@ -32,6 +33,12 @@
//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
@@ -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 }
@@ -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 {
@@ -182,7 +219,7 @@ 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")
@@ -190,10 +227,14 @@ func init() {
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",
@@ -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!")
diff --git a/sprites/walk-left/00.png b/sprites/walk-left/00.png
new file mode 100644
index 0000000..d98ec76
Binary files /dev/null and b/sprites/walk-left/00.png differ
diff --git a/sprites/walk-left/01.png b/sprites/walk-left/01.png
new file mode 100644
index 0000000..aa1a14c
Binary files /dev/null and b/sprites/walk-left/01.png differ
diff --git a/sprites/walk-left/02.png b/sprites/walk-left/02.png
new file mode 100644
index 0000000..f985607
Binary files /dev/null and b/sprites/walk-left/02.png differ
diff --git a/sprites/walk-left/03.png b/sprites/walk-left/03.png
new file mode 100644
index 0000000..225f090
Binary files /dev/null and b/sprites/walk-left/03.png differ
diff --git a/sprites/walk-left/04.png b/sprites/walk-left/04.png
new file mode 100644
index 0000000..7ffcc66
Binary files /dev/null and b/sprites/walk-left/04.png differ
diff --git a/sprites/walk-left/05.png b/sprites/walk-left/05.png
new file mode 100644
index 0000000..aa1a14c
Binary files /dev/null and b/sprites/walk-left/05.png differ
diff --git a/sprites/walk-left/06.png b/sprites/walk-left/06.png
new file mode 100644
index 0000000..ffd43b6
Binary files /dev/null and b/sprites/walk-left/06.png differ
diff --git a/sprites/walk-left/07.png b/sprites/walk-left/07.png
new file mode 100644
index 0000000..225f090
Binary files /dev/null and b/sprites/walk-left/07.png differ
diff --git a/sprites/walk-left/08.png b/sprites/walk-left/08.png
new file mode 100644
index 0000000..7ffcc66
Binary files /dev/null and b/sprites/walk-left/08.png differ
diff --git a/sprites/walk-right/00.png b/sprites/walk-right/00.png
new file mode 100644
index 0000000..56af561
Binary files /dev/null and b/sprites/walk-right/00.png differ
diff --git a/sprites/walk-right/01.png b/sprites/walk-right/01.png
new file mode 100644
index 0000000..f256802
Binary files /dev/null and b/sprites/walk-right/01.png differ
diff --git a/sprites/walk-right/02.png b/sprites/walk-right/02.png
new file mode 100644
index 0000000..5ce97b8
Binary files /dev/null and b/sprites/walk-right/02.png differ
diff --git a/sprites/walk-right/03.png b/sprites/walk-right/03.png
new file mode 100644
index 0000000..f49ed1f
Binary files /dev/null and b/sprites/walk-right/03.png differ
diff --git a/sprites/walk-right/04.png b/sprites/walk-right/04.png
new file mode 100644
index 0000000..ed8d3aa
Binary files /dev/null and b/sprites/walk-right/04.png differ
diff --git a/sprites/walk-right/05.png b/sprites/walk-right/05.png
new file mode 100644
index 0000000..f256802
Binary files /dev/null and b/sprites/walk-right/05.png differ
diff --git a/sprites/walk-right/06.png b/sprites/walk-right/06.png
new file mode 100644
index 0000000..9990a61
Binary files /dev/null and b/sprites/walk-right/06.png differ
diff --git a/sprites/walk-right/07.png b/sprites/walk-right/07.png
new file mode 100644
index 0000000..f49ed1f
Binary files /dev/null and b/sprites/walk-right/07.png differ
diff --git a/sprites/walk-right/08.png b/sprites/walk-right/08.png
new file mode 100644
index 0000000..ed8d3aa
Binary files /dev/null and b/sprites/walk-right/08.png differ