everything I got
This commit is contained in:
57
states/gameOverState.go
Normal file
57
states/gameOverState.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package states
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image/color"
|
||||
"math/rand"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||
"github.com/hajimehoshi/ebiten/v2/text"
|
||||
"golang.org/x/image/font"
|
||||
"src.robn.tv/MrDonuts/RGB/assets"
|
||||
geom "src.robn.tv/MrDonuts/RGB/geometry"
|
||||
)
|
||||
|
||||
type GameOverState struct {
|
||||
}
|
||||
|
||||
func (st *GameOverState) Enter() {
|
||||
fmt.Println("Entering Game Over State")
|
||||
assets.SilenceSongTracks()
|
||||
assets.SFXLibrary["gameOver"].Rewind()
|
||||
assets.SFXLibrary["gameOver"].Play()
|
||||
|
||||
}
|
||||
|
||||
func (st *GameOverState) Exit() {
|
||||
fmt.Println("Exiting Game OverState")
|
||||
|
||||
}
|
||||
|
||||
func (st *GameOverState) Update() {
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyN) {
|
||||
geom.ResetPlayerValues()
|
||||
geom.BuildGrid(rand.Intn(5+geom.CurrentLevel)+2, rand.Intn(5+geom.CurrentLevel)+2)
|
||||
CurrTrack = 1
|
||||
assets.SongTracks[CurrTrack].SetVolume(0.7) //turn up track 1
|
||||
SM.Transition("load")
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (st *GameOverState) Draw(screen *ebiten.Image) {
|
||||
w, h := ebiten.WindowSize()
|
||||
|
||||
gameOverStr := "Game Over"
|
||||
face := assets.LoadFontFace("fonts/robot.otf", 30)
|
||||
stringWidth := font.MeasureString(face, gameOverStr).Ceil()
|
||||
text.Draw(screen, gameOverStr, face, w/2-stringWidth/2, h/2, color.White)
|
||||
|
||||
newGameStr := "(press N for new game)"
|
||||
face = assets.LoadFontFace("fonts/robot.otf", 12)
|
||||
stringWidth = font.MeasureString(face, newGameStr).Ceil()
|
||||
text.Draw(screen, newGameStr, face, w/2-stringWidth/2, h-int(0.1*float64(h)), color.White)
|
||||
|
||||
}
|
||||
52
states/loadLevelState.go
Normal file
52
states/loadLevelState.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package states
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
geom "src.robn.tv/MrDonuts/RGB/geometry"
|
||||
score "src.robn.tv/MrDonuts/RGB/tools"
|
||||
)
|
||||
|
||||
var currentEnd = 1 //used for animating squares
|
||||
var secondsPerUpdate = 0.02
|
||||
var ticksPerSecond = 60.0
|
||||
var deltaSeconds = 1.0 / ticksPerSecond
|
||||
var secondsCounter = 0.0
|
||||
|
||||
type LoadLevelState struct {
|
||||
}
|
||||
|
||||
func (s *LoadLevelState) Enter() {
|
||||
fmt.Println("Entering Loading State")
|
||||
currentEnd = 1
|
||||
}
|
||||
|
||||
func (s *LoadLevelState) Exit() {
|
||||
fmt.Println("Exiting Loading State")
|
||||
}
|
||||
|
||||
func (s *LoadLevelState) Update() {
|
||||
secondsCounter += deltaSeconds
|
||||
|
||||
//determine how much to jump by on each draw
|
||||
totalSquares := geom.GameGrid.NumSquares()
|
||||
jumpAmount := math.Ceil(float64(totalSquares) / ticksPerSecond)
|
||||
if secondsCounter >= float64(secondsPerUpdate) {
|
||||
secondsCounter = 0
|
||||
currentEnd += int(jumpAmount)
|
||||
}
|
||||
|
||||
if currentEnd > geom.GameGrid.NumSquares() {
|
||||
SM.Transition("play")
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *LoadLevelState) Draw(screen *ebiten.Image) {
|
||||
|
||||
geom.GameGrid.DrawUpTo(currentEnd, screen)
|
||||
score.ScoreMngr.Draw(screen)
|
||||
}
|
||||
71
states/playLevelState.go
Normal file
71
states/playLevelState.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package states
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||
"src.robn.tv/MrDonuts/RGB/assets"
|
||||
geom "src.robn.tv/MrDonuts/RGB/geometry"
|
||||
score "src.robn.tv/MrDonuts/RGB/tools"
|
||||
)
|
||||
|
||||
var CurrTrack = 1
|
||||
|
||||
type PlayLevelState struct {
|
||||
}
|
||||
|
||||
func (s *PlayLevelState) Enter() {
|
||||
fmt.Println("Entering Play State")
|
||||
geom.SetupPlayer()
|
||||
fmt.Println(geom.GameGrid.ColouredSquaresRemaining())
|
||||
|
||||
}
|
||||
|
||||
func (s *PlayLevelState) Exit() {
|
||||
fmt.Println("Exiting Play State")
|
||||
|
||||
geom.PrepareForNextLevel()
|
||||
|
||||
if CurrTrack < len(assets.SongTracks) {
|
||||
if geom.CurrentLevel%3 == 0 {
|
||||
CurrTrack++
|
||||
assets.SongTracks[CurrTrack].SetVolume(0.7)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *PlayLevelState) Update() {
|
||||
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyN) {
|
||||
newRowSize := min(rand.Intn(5+geom.CurrentLevel)+2, geom.MaxGridDimension)
|
||||
newColSize := min(rand.Intn(5+geom.CurrentLevel)+2, geom.MaxGridDimension)
|
||||
geom.BuildGrid(newRowSize, newColSize)
|
||||
SM.Transition("load")
|
||||
assets.SFXLibrary["newLevel"].Rewind()
|
||||
assets.SFXLibrary["newLevel"].Play()
|
||||
}
|
||||
|
||||
geom.MainPlayer.Update()
|
||||
if geom.CurrentEnergy == 0 {
|
||||
SM.Transition("game over")
|
||||
}
|
||||
if geom.GameGrid.ColouredSquaresRemaining() == 0 {
|
||||
//go to next level
|
||||
newRowSize := min(rand.Intn(5+geom.CurrentLevel)+2, geom.MaxGridDimension)
|
||||
newColSize := min(rand.Intn(5+geom.CurrentLevel)+2, geom.MaxGridDimension)
|
||||
geom.BuildGrid(newRowSize, newColSize)
|
||||
SM.Transition("load")
|
||||
assets.SFXLibrary["newLevel"].Rewind()
|
||||
assets.SFXLibrary["newLevel"].Play()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *PlayLevelState) Draw(screen *ebiten.Image) {
|
||||
geom.GameGrid.Draw(screen)
|
||||
geom.MainPlayer.Draw(screen)
|
||||
score.ScoreMngr.Draw(screen)
|
||||
}
|
||||
54
states/statemachine.go
Normal file
54
states/statemachine.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package states
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
var SM StateMachine
|
||||
var StateMap map[string]State
|
||||
|
||||
type State interface {
|
||||
Enter()
|
||||
Exit()
|
||||
Update()
|
||||
Draw(screen *ebiten.Image)
|
||||
}
|
||||
|
||||
type StateMachine struct {
|
||||
CurrentState State
|
||||
}
|
||||
|
||||
func NewStateMachine(initialState State) *StateMachine {
|
||||
sm := &StateMachine{CurrentState: initialState}
|
||||
sm.CurrentState.Enter()
|
||||
return sm
|
||||
}
|
||||
|
||||
func (sm *StateMachine) Transition(newState string) {
|
||||
sm.CurrentState.Exit()
|
||||
sm.CurrentState = StateMap[newState]
|
||||
sm.CurrentState.Enter()
|
||||
}
|
||||
|
||||
func (sm *StateMachine) Update() {
|
||||
sm.CurrentState.Update()
|
||||
}
|
||||
|
||||
func (sm *StateMachine) Draw(screen *ebiten.Image) {
|
||||
sm.CurrentState.Draw(screen)
|
||||
}
|
||||
|
||||
func SetupStateMachine() {
|
||||
|
||||
loadState := &LoadLevelState{}
|
||||
playState := &PlayLevelState{}
|
||||
gameOverState := &GameOverState{}
|
||||
|
||||
StateMap = make(map[string]State)
|
||||
StateMap["load"] = loadState
|
||||
StateMap["play"] = playState
|
||||
StateMap["game over"] = gameOverState
|
||||
|
||||
SM = *NewStateMachine(loadState)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user