Browse Source

Split lib into multiple files

trunk
Shanti Chellaram 10 months ago
parent
commit
c24c00592f
  1. 1
      lib/event.go
  2. 46
      lib/game.go
  3. 51
      lib/scene.go

1
lib/event.go

@ -0,0 +1 @@
package lib

46
lib/lib.go → lib/game.go

@ -2,59 +2,15 @@ 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
// The game has
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{

51
lib/scene.go

@ -0,0 +1,51 @@
package lib
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
// Scenes: Main Menu
// Game Mode
type GameEvent struct {
Type int
}
type HandleResult int
const (
EventNoOp int = iota
EventKeyRelease
)
const (
DoNothing HandleResult = iota
EndProgram
)
// Scene is the highest level organization unit of a Game. It handles events.
// A game has a currently active scene, and can transition
type Scene interface {
// Handles a game event and returns a result.
HandleEvent(*GameEvent) HandleResult
Draw(screen *ebiten.Image)
}
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])
}
Loading…
Cancel
Save