Repos / shark / 6a97ac03b8
commit 6a97ac03b88a55cabf304937c0ce1a36caa1dcc4
Author: Bùi Thành Nhân <hi@imnhan.com>
Date: Wed Jun 29 19:35:54 2022 +0700
first "mvp"
- Crop empty space around sprite
- Add github action to build
- Correct dragging algorithm
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
new file mode 100644
index 0000000..f498e6d
--- /dev/null
+++ b/.github/workflows/main.yml
@@ -0,0 +1,39 @@
+name: Go package
+
+on: [push]
+
+jobs:
+ build:
+
+ runs-on: ['ubuntu-latest', 'windows-latest', 'macos-latest']
+ steps:
+ - uses: actions/checkout@v3
+
+ - name: Set up Go
+ uses: actions/setup-go@v3
+ with:
+ go-version: 1.18
+
+ - name: Build
+ run: go build -v ./...
+
+ - name: Upload linux
+ if: runner.os == 'Linux'
+ uses: actions/upload-artifact@v3
+ with:
+ name: shark-linux
+ path: shark
+
+ - name: Upload macOS
+ if: runner.os == 'macOS'
+ uses: actions/upload-artifact@v3
+ with:
+ name: shark-osx
+ path: shark
+
+ - name: Upload Windows
+ if: runner.os == 'Windows'
+ uses: actions/upload-artifact@v3
+ with:
+ name: shark-win.exe
+ path: shark.exe
diff --git a/.gitignore b/.gitignore
index b35de61..1880ce9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
/shark
+/shark.exe
diff --git a/main.go b/main.go
index ced738d..ceca17c 100644
--- a/main.go
+++ b/main.go
@@ -12,6 +12,9 @@
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
+const SPRITE_X = 100
+const SPRITE_Y = 123
+
//go:embed sprites/idle/*
var IdleSprites embed.FS
@@ -33,14 +36,28 @@ type Game struct {
Ticks int
ShouldResetToIdle bool
IsDragging bool
- StartMouseX int
- StartMouseY int
+ PreviousMousePos Vector
+ WinStartPos Vector
+ MouseStartPos Vector
}
-func GlobalCursorPosition() (x, y int) {
+type Vector struct{ x, y int }
+
+func CreateVector(x, y int) Vector {
+ return Vector{x, y}
+}
+
+func (this Vector) Add(that Vector) Vector {
+ return Vector{this.x + that.x, this.y + that.y}
+}
+func (this Vector) Subtract(that Vector) Vector {
+ return Vector{this.x - that.x, this.y - that.y}
+}
+
+func GlobalCursorPosition() Vector {
cx, cy := ebiten.CursorPosition()
wx, wy := ebiten.WindowPosition()
- return cx + wx, cy + wy
+ return Vector{cx + wx, cy + wy}
}
func (g *Game) Update() error {
@@ -57,27 +74,23 @@ func (g *Game) Update() error {
g.CurrentAnim = Drag
g.Ticks = 0
g.CurrentFrame = 0
- g.StartMouseX, g.StartMouseY = GlobalCursorPosition()
- return nil
+ g.PreviousMousePos = GlobalCursorPosition()
+ g.WinStartPos = CreateVector(ebiten.WindowPosition())
+ g.MouseStartPos = GlobalCursorPosition()
}
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
g.IsDragging = false
g.CurrentAnim = Idle
g.Ticks = 0
g.CurrentFrame = 0
- return nil
}
- if g.IsDragging {
- currentX, currentY := GlobalCursorPosition()
- diffX := currentX - g.StartMouseX
- diffY := currentY - g.StartMouseY
-
- wx, wy := ebiten.WindowPosition()
- ebiten.SetWindowPosition(wx+diffX, wy+diffY)
-
- g.StartMouseX, g.StartMouseY = GlobalCursorPosition()
+ mousePos := GlobalCursorPosition()
+ if g.IsDragging && mousePos != g.PreviousMousePos {
+ newWinPos := g.WinStartPos.Add(mousePos.Subtract(g.MouseStartPos))
+ ebiten.SetWindowPosition(newWinPos.x, newWinPos.y)
}
+ g.PreviousMousePos = mousePos
g.Ticks++
if g.Ticks < 10 {
@@ -107,19 +120,21 @@ func (g *Game) Draw(screen *ebiten.Image) {
),
)
*/
- debugStr := ""
- if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
- debugStr += "Dragging\n"
- }
- if ebiten.IsMouseButtonPressed(ebiten.MouseButtonRight) {
- debugStr += "Right click\n"
- }
- ebitenutil.DebugPrint(screen, debugStr)
screen.DrawImage(g.CurrentAnim.Frames[g.CurrentFrame], nil)
+ /*
+ debugStr := ""
+ if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
+ debugStr += "Dragging\n"
+ }
+ if ebiten.IsMouseButtonPressed(ebiten.MouseButtonRight) {
+ debugStr += "Right click\n"
+ }
+ ebitenutil.DebugPrint(screen, debugStr)
+ */
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (w, h int) {
- return outsideWidth / 3, outsideHeight / 3
+ return SPRITE_X, SPRITE_Y
}
func NewAnim(sprites embed.FS, subdir string) *Anim {
@@ -149,7 +164,7 @@ func main() {
var game Game
game.CurrentAnim = Idle
- ebiten.SetWindowSize(500, 540)
+ ebiten.SetWindowSize(SPRITE_X*3, SPRITE_Y*3)
ebiten.SetWindowTitle("Shark!")
ebiten.SetWindowDecorated(false)
ebiten.SetScreenTransparent(true)
diff --git a/sprites/drag/00.png b/sprites/drag/00.png
index 3650dce..aa3d25c 100644
Binary files a/sprites/drag/00.png and b/sprites/drag/00.png differ
diff --git a/sprites/drag/01.png b/sprites/drag/01.png
index edf53cf..903b9b7 100644
Binary files a/sprites/drag/01.png and b/sprites/drag/01.png differ
diff --git a/sprites/drag/02.png b/sprites/drag/02.png
index f41e29b..5dbbbff 100644
Binary files a/sprites/drag/02.png and b/sprites/drag/02.png differ
diff --git a/sprites/drag/03.png b/sprites/drag/03.png
index 9fbf55b..0166ff3 100644
Binary files a/sprites/drag/03.png and b/sprites/drag/03.png differ
diff --git a/sprites/idle/00.png b/sprites/idle/00.png
index 2a91c7d..bf58b9d 100644
Binary files a/sprites/idle/00.png and b/sprites/idle/00.png differ
diff --git a/sprites/idle/01.png b/sprites/idle/01.png
index 27606fe..c415bc6 100644
Binary files a/sprites/idle/01.png and b/sprites/idle/01.png differ
diff --git a/sprites/idle/02.png b/sprites/idle/02.png
index c458c3b..104cd6b 100644
Binary files a/sprites/idle/02.png and b/sprites/idle/02.png differ
diff --git a/sprites/idle/03.png b/sprites/idle/03.png
index 95e0d30..30d9a92 100644
Binary files a/sprites/idle/03.png and b/sprites/idle/03.png differ
diff --git a/sprites/right-click/00.png b/sprites/right-click/00.png
index 638db16..d986f55 100644
Binary files a/sprites/right-click/00.png and b/sprites/right-click/00.png differ
diff --git a/sprites/right-click/01.png b/sprites/right-click/01.png
index 13b56ef..8d6d93f 100644
Binary files a/sprites/right-click/01.png and b/sprites/right-click/01.png differ
diff --git a/sprites/right-click/02.png b/sprites/right-click/02.png
index ee73776..2bb24e9 100644
Binary files a/sprites/right-click/02.png and b/sprites/right-click/02.png differ
diff --git a/sprites/right-click/03.png b/sprites/right-click/03.png
index 16c287b..708d7a0 100644
Binary files a/sprites/right-click/03.png and b/sprites/right-click/03.png differ
diff --git a/sprites/right-click/04.png b/sprites/right-click/04.png
index e64f959..08cad6e 100644
Binary files a/sprites/right-click/04.png and b/sprites/right-click/04.png differ
diff --git a/sprites/right-click/05.png b/sprites/right-click/05.png
index fea1fb9..3529645 100644
Binary files a/sprites/right-click/05.png and b/sprites/right-click/05.png differ
diff --git a/sprites/right-click/06.png b/sprites/right-click/06.png
index e64f959..08cad6e 100644
Binary files a/sprites/right-click/06.png and b/sprites/right-click/06.png differ
diff --git a/sprites/right-click/07.png b/sprites/right-click/07.png
index 16c287b..708d7a0 100644
Binary files a/sprites/right-click/07.png and b/sprites/right-click/07.png differ
diff --git a/sprites/right-click/08.png b/sprites/right-click/08.png
index cdb0a53..732939f 100644
Binary files a/sprites/right-click/08.png and b/sprites/right-click/08.png differ
diff --git a/sprites/right-click/09.png b/sprites/right-click/09.png
index bc59569..0828b1b 100644
Binary files a/sprites/right-click/09.png and b/sprites/right-click/09.png differ
diff --git a/sprites/right-click/10.png b/sprites/right-click/10.png
index cdb0a53..732939f 100644
Binary files a/sprites/right-click/10.png and b/sprites/right-click/10.png differ
diff --git a/sprites/right-click/11.png b/sprites/right-click/11.png
index eafe734..9c65e86 100644
Binary files a/sprites/right-click/11.png and b/sprites/right-click/11.png differ
diff --git a/sprites/right-click/12.png b/sprites/right-click/12.png
index eafe734..9c65e86 100644
Binary files a/sprites/right-click/12.png and b/sprites/right-click/12.png differ
diff --git a/sprites/right-click/13.png b/sprites/right-click/13.png
index ef0a068..56fd593 100644
Binary files a/sprites/right-click/13.png and b/sprites/right-click/13.png differ
diff --git a/sprites/right-click/14.png b/sprites/right-click/14.png
index c6abda5..094a760 100644
Binary files a/sprites/right-click/14.png and b/sprites/right-click/14.png differ
diff --git a/sprites/right-click/15.png b/sprites/right-click/15.png
index ee73776..f3ee2ac 100644
Binary files a/sprites/right-click/15.png and b/sprites/right-click/15.png differ