Skip to content

Commit

Permalink
first "mvp"
Browse files Browse the repository at this point in the history
- Crop empty space around sprite
- Add github action to build
- Correct dragging algorithm
  • Loading branch information
nhanb committed Jun 29, 2022
1 parent 24ea7bb commit 6a97ac0
Show file tree
Hide file tree
Showing 27 changed files with 81 additions and 26 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/shark
/shark.exe
67 changes: 41 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"github.com/hajimehoshi/ebiten/v2/inpututil"
)

const SPRITE_X = 100
const SPRITE_Y = 123

//go:embed sprites/idle/*
var IdleSprites embed.FS

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
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 modified sprites/idle/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/idle/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/idle/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/idle/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 modified sprites/right-click/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/right-click/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/right-click/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/right-click/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 modified sprites/right-click/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 modified sprites/right-click/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 modified sprites/right-click/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 modified sprites/right-click/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 modified sprites/right-click/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 modified sprites/right-click/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 modified sprites/right-click/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 modified sprites/right-click/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 modified sprites/right-click/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 modified sprites/right-click/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 modified sprites/right-click/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 modified sprites/right-click/15.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 6a97ac0

Please sign in to comment.