doctorlectro/game/game.go

91 lines
1.8 KiB
Go
Raw Normal View History

2022-06-17 21:17:03 +00:00
package game
import (
"os"
2022-06-18 21:08:53 +00:00
"github.com/jakecoffman/cp"
2022-06-17 21:59:23 +00:00
"code.rocketnine.space/tslocum/doctorlectro/entity"
2022-06-17 21:17:03 +00:00
"code.rocketnine.space/tslocum/doctorlectro/level"
"code.rocketnine.space/tslocum/doctorlectro/system"
"code.rocketnine.space/tslocum/doctorlectro/world"
"code.rocketnine.space/tslocum/gohan"
"github.com/hajimehoshi/ebiten/v2"
)
type Game struct {
}
func NewGame() (*Game, error) {
g := &Game{}
2022-06-18 21:08:53 +00:00
2022-06-17 21:59:23 +00:00
g.reset()
2022-06-17 21:17:03 +00:00
g.addSystems()
return g, nil
}
2022-06-28 00:42:56 +00:00
// addSystems adds all systems to the game. Systems run in the order they are
// added in. Systems do not run in parallel.
2022-06-17 21:17:03 +00:00
func (g *Game) addSystems() {
2022-06-18 21:08:53 +00:00
gohan.AddSystem(system.NewPhysicsSystem())
2022-06-22 02:52:48 +00:00
gohan.AddSystem(system.NewPlayerMoveSystem())
2022-06-18 21:08:53 +00:00
gohan.AddSystem(system.NewMovementSystem())
2022-06-28 00:42:56 +00:00
for layer := world.LayerDefault; layer >= world.LayerPlatform; layer-- {
gohan.AddSystem(system.NewRenderSystem(layer))
}
2022-06-17 21:59:23 +00:00
gohan.AddSystem(system.NewRenderMessageSystem())
2022-06-17 21:17:03 +00:00
gohan.AddSystem(system.NewRenderDebugTextSystem())
}
2022-06-17 21:59:23 +00:00
func (g *Game) reset() {
for _, e := range gohan.AllEntities() {
e.Remove()
}
world.GameOver = false
world.MetalRects = nil
world.HazardRects = nil
world.Clinging = false
2022-06-18 21:08:53 +00:00
world.Space = cp.NewSpace()
world.Space.Iterations = 1
world.Space.SetGravity(cp.Vector{0, world.Gravity})
2022-06-17 21:59:23 +00:00
world.Player = entity.NewPlayer()
2022-06-18 21:08:53 +00:00
2022-06-17 21:59:23 +00:00
level.LoadMap()
}
2022-06-17 21:17:03 +00:00
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return world.ScreenWidth, world.ScreenHeight
}
func (g *Game) Update() error {
if ebiten.IsWindowBeingClosed() {
g.Exit()
return nil
}
2022-06-17 21:59:23 +00:00
if world.ResetGame {
g.reset()
world.ResetGame = false
}
2022-06-17 21:17:03 +00:00
return gohan.Update()
}
func (g *Game) Draw(screen *ebiten.Image) {
err := gohan.Draw(screen)
if err != nil {
panic(err)
}
}
func (g *Game) Exit() {
os.Exit(0)
}