189 lines
3.0 KiB
Go
189 lines
3.0 KiB
Go
package world
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
|
|
"github.com/jakecoffman/cp"
|
|
|
|
"code.rocketnine.space/tslocum/gohan"
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
"github.com/lafriks/go-tiled"
|
|
)
|
|
|
|
const (
|
|
ScreenWidth = 960
|
|
ScreenHeight = 540
|
|
)
|
|
|
|
const TPS = 144
|
|
|
|
const TileSize = 16
|
|
|
|
const (
|
|
PlayerWidth = 15.0
|
|
PlayerHeight = 23.0
|
|
)
|
|
|
|
const Gravity = 500.0
|
|
|
|
const (
|
|
MagnetizeDown = iota
|
|
MagnetizeDownLeft
|
|
MagnetizeLeft
|
|
MagnetizeUpLeft
|
|
MagnetizeUp
|
|
MagnetizeUpRight
|
|
MagnetizeRight
|
|
MagnetizeDownRight
|
|
)
|
|
|
|
const (
|
|
LayerDefault = 0 - iota
|
|
LayerDecorationAbove
|
|
LayerPlayer
|
|
LayerDecorationBelow
|
|
LayerPlatform
|
|
)
|
|
|
|
type TriggerAction int
|
|
|
|
const (
|
|
TriggerJump = iota + 1
|
|
TriggerMagnetize
|
|
TriggerFire
|
|
TriggerSwitch
|
|
TriggerData
|
|
)
|
|
|
|
const (
|
|
TileScreenOff = 395
|
|
)
|
|
|
|
var (
|
|
Debug int
|
|
|
|
// Give is a list of abilities the player is given at the start of the game.
|
|
// This is only available to users as a commandline flag.
|
|
Give string
|
|
|
|
// DisableEsc is set to true when the escape key should not exit the game.
|
|
// This is used on WASM builds, where users exit the game by closing the tab.
|
|
DisableEsc bool
|
|
|
|
Map *tiled.Map
|
|
|
|
TileImages = make(map[uint32]*ebiten.Image)
|
|
|
|
ObjectGroups []*tiled.ObjectGroup
|
|
|
|
GameOver bool
|
|
|
|
ResetGame bool
|
|
|
|
ShownIntro bool
|
|
|
|
Player gohan.Entity
|
|
|
|
CamX, CamY float64
|
|
|
|
CamTargetX, CamTargetY float64
|
|
|
|
SpawnX, SpawnY float64
|
|
|
|
MetalRects []image.Rectangle
|
|
|
|
PlayerGrounded bool
|
|
|
|
MagnetActive bool
|
|
MagnetDirection int // 0 down, 1 left, 2 up, 3 right
|
|
|
|
PlayerDirection int
|
|
|
|
Clinging bool
|
|
|
|
LastWalkDirL, LastWalkDirU bool
|
|
|
|
HazardRects []image.Rectangle
|
|
|
|
Space *cp.Space
|
|
|
|
PlayerShape *cp.Shape
|
|
PlayerBody *cp.Body
|
|
|
|
PlayerNormal cp.Vector
|
|
|
|
PlayerIdleFrames []*ebiten.Image
|
|
PlayerWalkFrames []*ebiten.Image
|
|
|
|
CollisionShapes []*cp.Shape
|
|
|
|
TriggerRects []image.Rectangle
|
|
TriggerActions []TriggerAction
|
|
TriggerEntities []gohan.Entity
|
|
|
|
DataRect image.Rectangle
|
|
|
|
CanJump bool
|
|
CanMagnetize bool
|
|
CanFire bool
|
|
CanDoubleJump bool
|
|
|
|
LastFire int
|
|
|
|
MessageText string
|
|
MessageUpdated bool
|
|
|
|
RemoveTrigger = -1
|
|
|
|
Tick int
|
|
|
|
StartMuted bool
|
|
|
|
DestructibleRects []image.Rectangle
|
|
DestructibleEntities []gohan.Entity
|
|
DestructibleShapes [][]*cp.Shape
|
|
|
|
BeamRects []image.Rectangle
|
|
BeamShapes [][]*cp.Shape
|
|
BeamEntities []gohan.Entity
|
|
|
|
GameOverTicks int
|
|
)
|
|
|
|
func LevelCoordinatesToScreen(x, y float64) (float64, float64) {
|
|
px, py := CamX, CamY
|
|
py *= -1
|
|
return (x - px) * 1, y + py
|
|
}
|
|
|
|
func BBToScreenRect(bb cp.BB) image.Rectangle {
|
|
x1, y1 := LevelCoordinatesToScreen(bb.L, bb.B)
|
|
x2, y2 := LevelCoordinatesToScreen(bb.R, bb.T)
|
|
return image.Rect(int(x1), int(y1), int(x2), int(y2))
|
|
}
|
|
|
|
func BBToLevelRect(bb cp.BB) image.Rectangle {
|
|
return image.Rect(int(bb.L), int(bb.T), int(bb.R), int(bb.B))
|
|
}
|
|
|
|
func SetMessage(message string) {
|
|
MessageText = fmt.Sprintf("%s\n\n<ENTER> TO CONTINUE.", message)
|
|
MessageUpdated = true
|
|
}
|
|
|
|
func OpenSwitch() {
|
|
for _, beam := range BeamEntities {
|
|
beam.Remove()
|
|
}
|
|
for _, shapes := range BeamShapes {
|
|
for _, shape := range shapes {
|
|
Space.RemoveShape(shape)
|
|
}
|
|
}
|
|
|
|
BeamRects = nil
|
|
BeamEntities = nil
|
|
BeamShapes = nil
|
|
}
|