doctorlectro/game/game.go

156 lines
3.1 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-28 21:30:47 +00:00
"github.com/hajimehoshi/ebiten/v2/inpututil"
2022-06-28 19:53:04 +00:00
"code.rocketnine.space/tslocum/doctorlectro/asset"
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"
2022-06-28 18:56:02 +00:00
"github.com/jakecoffman/cp"
2022-06-17 21:17:03 +00:00
)
2022-06-28 18:56:02 +00:00
const introMessage = `
DOCTOR LECTRO, THE MAD SCIENTIST, HAS TAKEN
CHARGE OF EARTH'S MAGNETIC POLES!
ANIMALS, ELECTRONICS, AND HAIRSTYLES ACROSS
THE GLOBE ARE IN GRAVE DANGER!
STOP DOCTOR LECTRO BEFORE IT IS TOO LATE!
`
2022-06-17 21:17:03 +00:00
type Game struct {
2022-06-28 18:56:02 +00:00
shownIntro bool
2022-06-17 21:17:03 +00:00
}
func NewGame() (*Game, error) {
g := &Game{}
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 18:56:02 +00:00
world.Tick = 0
2022-06-17 21:59:23 +00:00
2022-06-28 17:48:33 +00:00
world.CanJump = false
world.CanMagnetize = false
world.CanFire = false
world.CanDoubleJump = false
2022-06-28 22:54:58 +00:00
world.LastFire = 0
2022-06-17 21:59:23 +00:00
world.MetalRects = nil
world.HazardRects = nil
world.Clinging = false
2022-06-28 21:30:47 +00:00
world.LastWalkDirL = true
world.LastWalkDirU = true
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-28 21:30:47 +00:00
if inpututil.IsKeyJustPressed(ebiten.KeyM) {
if asset.SoundMusic.IsPlaying() {
asset.SoundMusic.Pause()
} else {
asset.SoundMusic.Play()
}
}
2022-06-28 18:14:30 +00:00
if world.MessageText != "" {
if ebiten.IsKeyPressed(ebiten.KeyEnter) {
world.MessageText = ""
}
return nil
}
2022-06-28 18:56:02 +00:00
if !g.shownIntro && world.Tick == 0 {
world.SetMessage(strings.TrimSpace(introMessage))
g.shownIntro = true
2022-06-28 19:53:04 +00:00
if !world.StartMuted {
asset.SoundMusic.Play()
}
2022-06-28 18:56:02 +00:00
}
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)
}