doctorlectro/game/game.go

115 lines
2.3 KiB
Go
Raw Normal View History

2022-06-17 21:17:03 +00:00
package game
import (
2022-06-28 17:48:33 +00:00
"fmt"
2022-06-17 21:17:03 +00:00
"os"
2022-06-28 17:48:33 +00:00
"strings"
2022-06-17 21:17:03 +00:00
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 17:48:33 +00:00
for layer := world.LayerPlatform; layer <= world.LayerDefault; layer++ {
2022-06-28 00:42:56 +00:00
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
2022-06-28 17:48:33 +00:00
world.CanJump = false
world.CanMagnetize = false
world.CanFire = false
world.CanDoubleJump = false
2022-06-17 21:59:23 +00:00
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-28 17:48:33 +00:00
if world.Give != "" {
for _, ability := range strings.Split(world.Give, ",") {
switch strings.ToLower(ability) {
case "jump":
world.CanJump = true
case "magnetize":
world.CanMagnetize = true
case "fire":
world.CanFire = true
case "doublejump":
world.CanDoubleJump = true
default:
panic(fmt.Sprintf("unknown ability to give player: %s", ability))
}
}
}
2022-06-17 21:59:23 +00:00
}
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)
}