2 Commits
ac61d0d4b5
...
06e44045ac
| Author | SHA1 | Message | Date |
|---|---|---|---|
|
|
06e44045ac |
Add initial "event loop" and scene
Start with the main menu with only one option, and build the abstractions between the event loop and what the scene itself handles. |
10 months ago |
|
|
3f73cef233 |
Figure out go modules
I tried this and a dual-module setup with a cmd/lib structure, but this is what ended up working. |
10 months ago |
5 changed files with 144 additions and 3 deletions
-
1.gitignore
-
13go.mod
-
16go.sum
-
98lib/lib.go
-
19main.go
@ -0,0 +1 @@ |
|||
/gotris |
|||
@ -1,3 +1,14 @@ |
|||
module gotris/main |
|||
module git.shanti.wtf/shanti/gotris |
|||
|
|||
go 1.24.1 |
|||
|
|||
require github.com/hajimehoshi/ebiten/v2 v2.8.6 |
|||
|
|||
require ( |
|||
github.com/ebitengine/gomobile v0.0.0-20240911145611-4856209ac325 // indirect |
|||
github.com/ebitengine/hideconsole v1.0.0 // indirect |
|||
github.com/ebitengine/purego v0.8.0 // indirect |
|||
github.com/jezek/xgb v1.1.1 // indirect |
|||
golang.org/x/sync v0.8.0 // indirect |
|||
golang.org/x/sys v0.25.0 // indirect |
|||
) |
|||
@ -0,0 +1,16 @@ |
|||
github.com/ebitengine/gomobile v0.0.0-20240911145611-4856209ac325 h1:Gk1XUEttOk0/hb6Tq3WkmutWa0ZLhNn/6fc6XZpM7tM= |
|||
github.com/ebitengine/gomobile v0.0.0-20240911145611-4856209ac325/go.mod h1:ulhSQcbPioQrallSuIzF8l1NKQoD7xmMZc5NxzibUMY= |
|||
github.com/ebitengine/hideconsole v1.0.0 h1:5J4U0kXF+pv/DhiXt5/lTz0eO5ogJ1iXb8Yj1yReDqE= |
|||
github.com/ebitengine/hideconsole v1.0.0/go.mod h1:hTTBTvVYWKBuxPr7peweneWdkUwEuHuB3C1R/ielR1A= |
|||
github.com/ebitengine/purego v0.8.0 h1:JbqvnEzRvPpxhCJzJJ2y0RbiZ8nyjccVUrSM3q+GvvE= |
|||
github.com/ebitengine/purego v0.8.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= |
|||
github.com/hajimehoshi/ebiten/v2 v2.8.6 h1:Dkd/sYI0TYyZRCE7GVxV59XC+WCi2BbGAbIBjXeVC1U= |
|||
github.com/hajimehoshi/ebiten/v2 v2.8.6/go.mod h1:cCQ3np7rdmaJa1ZnvslraVlpxNb3wCjEnAP1LHNyXNA= |
|||
github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4= |
|||
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk= |
|||
golang.org/x/image v0.20.0 h1:7cVCUjQwfL18gyBJOmYvptfSHS8Fb3YUDtfLIZ7Nbpw= |
|||
golang.org/x/image v0.20.0/go.mod h1:0a88To4CYVBAHp5FXJm8o7QbUl37Vd85ply1vyD8auM= |
|||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= |
|||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= |
|||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= |
|||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= |
|||
@ -0,0 +1,98 @@ |
|||
package lib |
|||
|
|||
import ( |
|||
"github.com/hajimehoshi/ebiten/v2" |
|||
"github.com/hajimehoshi/ebiten/v2/ebitenutil" |
|||
"github.com/hajimehoshi/ebiten/v2/inpututil" |
|||
) |
|||
|
|||
// Scenes: Main Menu
|
|||
// Game Mode
|
|||
|
|||
//Scene - is the highest-level element of state the game can be in.
|
|||
|
|||
type GameEvent struct { |
|||
Type int |
|||
} |
|||
|
|||
type HandleResult int |
|||
|
|||
const ( |
|||
EventNoOp int = iota |
|||
EventKeyRelease |
|||
) |
|||
|
|||
const ( |
|||
DoNothing HandleResult = iota |
|||
EndProgram |
|||
) |
|||
|
|||
type Scene interface { |
|||
HandleEvent(*GameEvent) HandleResult |
|||
Draw(screen *ebiten.Image) |
|||
} |
|||
|
|||
// Implements ebiten.Game
|
|||
type TetrisGame struct { |
|||
activeScene Scene |
|||
} |
|||
|
|||
type MainMenuScene struct { |
|||
selections [1]string |
|||
currentSelectionIndex int |
|||
} |
|||
|
|||
func (menu *MainMenuScene) HandleEvent(e *GameEvent) HandleResult { |
|||
if e.Type == 1 { |
|||
if menu.selections[menu.currentSelectionIndex] == "Quit" { |
|||
return EndProgram |
|||
} |
|||
} |
|||
return DoNothing |
|||
} |
|||
|
|||
func (menu *MainMenuScene) Draw(screen *ebiten.Image) { |
|||
ebitenutil.DebugPrint(screen, menu.selections[menu.currentSelectionIndex]) |
|||
} |
|||
|
|||
func NewGame() (*TetrisGame, error) { |
|||
return &TetrisGame{ |
|||
activeScene: &MainMenuScene{ |
|||
selections: [1]string{"Quit"}, |
|||
currentSelectionIndex: 0, |
|||
}, |
|||
}, nil |
|||
} |
|||
|
|||
func (g *TetrisGame) Layout(outsideWidth, outsideHeight int) (int, int) { |
|||
//TODO: Make this do whatever it is supposed to do : resizing etc
|
|||
return 640, 480 |
|||
} |
|||
|
|||
func (g *TetrisGame) Update() error { |
|||
//while there are events
|
|||
|
|||
var keys []ebiten.Key |
|||
keys = inpututil.AppendJustReleasedKeys(keys) |
|||
|
|||
var event GameEvent |
|||
if len(keys) > 0 { |
|||
event = GameEvent{Type: EventKeyRelease} |
|||
} else { |
|||
event = GameEvent{Type: EventNoOp} |
|||
} |
|||
|
|||
switch g.activeScene.HandleEvent(&event) { |
|||
case EndProgram: |
|||
return ebiten.Termination |
|||
case DoNothing: |
|||
return nil |
|||
default: |
|||
return nil |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
func (g *TetrisGame) Draw(screen *ebiten.Image) { |
|||
g.activeScene.Draw(screen) |
|||
} |
|||
@ -1,7 +1,22 @@ |
|||
package main |
|||
|
|||
import "fmt" |
|||
import ( |
|||
"log" |
|||
|
|||
"git.shanti.wtf/shanti/gotris/lib" |
|||
|
|||
"github.com/hajimehoshi/ebiten/v2" |
|||
) |
|||
|
|||
// Entry point; initialize global state and begin ebiten game loop
|
|||
func main() { |
|||
fmt.Println("Hello, World!") |
|||
game, err := lib.NewGame() |
|||
if err != nil { |
|||
log.Fatal(err) |
|||
} |
|||
ebiten.SetWindowSize(640, 480) |
|||
ebiten.SetWindowTitle("Gotris") |
|||
if err := ebiten.RunGame(game); err != nil { |
|||
log.Fatal(err) |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue