2022-10-14 10:02:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"image/color"
|
2022-10-14 10:47:09 +00:00
|
|
|
"go.uber.org/zap"
|
2022-10-14 10:02:03 +00:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
|
|
"github.com/solarlune/resolv"
|
|
|
|
. "dnmshared"
|
2022-10-14 10:47:09 +00:00
|
|
|
|
|
|
|
"math"
|
2022-10-14 10:02:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type WorldColliderDisplay struct {
|
|
|
|
Game *Game
|
|
|
|
Space *resolv.Space
|
|
|
|
}
|
|
|
|
|
|
|
|
func (world *WorldColliderDisplay) Init() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWorldColliderDisplay(game *Game, stageDiscreteW, stageDiscreteH, stageTileW, stageTileH int32, playerPosMap StrToVec2DListMap, barrierMap StrToPolygon2DListMap) *WorldColliderDisplay {
|
2022-10-14 10:47:09 +00:00
|
|
|
|
|
|
|
playerList := *(playerPosMap["PlayerStartingPos"])
|
|
|
|
barrierList := *(barrierMap["Barrier"])
|
|
|
|
|
|
|
|
world := &WorldColliderDisplay{Game: game}
|
|
|
|
|
|
|
|
Logger.Info("Parsed variables", zap.Any("stageDiscreteW", stageDiscreteW), zap.Any("stageDiscreteH", stageDiscreteH), zap.Any("stageTileW", stageTileW), zap.Any("stageTileH", stageTileH))
|
2022-10-14 10:02:03 +00:00
|
|
|
|
|
|
|
spaceW := stageDiscreteW * stageTileW
|
|
|
|
spaceH := stageDiscreteH * stageTileH
|
|
|
|
|
|
|
|
spaceOffsetX := float64(spaceW) * 0.5
|
|
|
|
spaceOffsetY := float64(spaceH) * 0.5
|
|
|
|
|
2022-10-14 16:35:32 +00:00
|
|
|
// TODO: Move collider y-axis transformation to a "dnmshared"
|
2022-10-14 10:02:03 +00:00
|
|
|
playerColliderRadius := float64(12) // hardcoded
|
|
|
|
space := resolv.NewSpace(int(spaceW), int(spaceH), int(stageTileW), int(stageTileH))
|
|
|
|
for _, player := range playerList {
|
2022-10-14 16:35:32 +00:00
|
|
|
playerCollider := resolv.NewObject(player.X+spaceOffsetX, -player.Y+spaceOffsetY, playerColliderRadius*2, playerColliderRadius*2, "Player")
|
2022-10-14 10:02:03 +00:00
|
|
|
playerColliderShape := resolv.NewCircle(0, 0, playerColliderRadius*2)
|
|
|
|
playerCollider.SetShape(playerColliderShape)
|
2022-10-14 10:47:09 +00:00
|
|
|
Logger.Info("player shape added:", zap.Any("shape", playerColliderShape))
|
2022-10-14 10:02:03 +00:00
|
|
|
space.Add(playerCollider)
|
|
|
|
}
|
|
|
|
|
2022-10-14 10:47:09 +00:00
|
|
|
barrierLocalId := 0
|
2022-10-14 10:02:03 +00:00
|
|
|
for _, barrier := range barrierList {
|
|
|
|
var w float64 = 0
|
|
|
|
var h float64 = 0
|
2022-10-14 10:47:09 +00:00
|
|
|
|
2022-10-14 10:02:03 +00:00
|
|
|
for i, pi := range barrier.Points {
|
|
|
|
for j, pj := range barrier.Points {
|
|
|
|
if i == j {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if math.Abs(pj.X-pi.X) > w {
|
|
|
|
w = math.Abs(pj.X - pi.X)
|
|
|
|
}
|
|
|
|
if math.Abs(pj.Y-pi.Y) > h {
|
|
|
|
h = math.Abs(pj.Y - pi.Y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
barrierColliderShape := resolv.NewConvexPolygon()
|
2022-10-14 16:35:32 +00:00
|
|
|
for i := 0; i < len(barrier.Points); i++ {
|
2022-10-14 10:47:09 +00:00
|
|
|
p := barrier.Points[i]
|
2022-10-14 10:02:03 +00:00
|
|
|
barrierColliderShape.AddPoints(p.X, p.Y)
|
|
|
|
}
|
|
|
|
|
2022-10-14 16:35:32 +00:00
|
|
|
barrierCollider := resolv.NewObject(barrier.Anchor.X+spaceOffsetX, -barrier.Anchor.Y+spaceOffsetY, w, h, "Barrier")
|
2022-10-14 10:02:03 +00:00
|
|
|
barrierCollider.SetShape(barrierColliderShape)
|
2022-10-14 10:47:09 +00:00
|
|
|
|
|
|
|
Logger.Info("barrier shape added:", zap.Any("barrierLocalId", barrierLocalId), zap.Any("shape", barrierColliderShape))
|
2022-10-14 10:02:03 +00:00
|
|
|
space.Add(barrierCollider)
|
2022-10-14 10:47:09 +00:00
|
|
|
|
|
|
|
barrierLocalId++
|
2022-10-14 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 10:47:09 +00:00
|
|
|
world.Space = space
|
|
|
|
return world
|
2022-10-14 10:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (world *WorldColliderDisplay) Update() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (world *WorldColliderDisplay) Draw(screen *ebiten.Image) {
|
|
|
|
|
|
|
|
for _, o := range world.Space.Objects() {
|
|
|
|
drawColor := color.RGBA{60, 60, 60, 255}
|
|
|
|
if o.HasTags("Player") {
|
|
|
|
drawColor = color.RGBA{0, 255, 0, 255}
|
|
|
|
}
|
|
|
|
ebitenutil.DrawRect(screen, o.X, o.Y, o.W, o.H, drawColor)
|
|
|
|
}
|
|
|
|
|
2022-10-14 16:35:32 +00:00
|
|
|
// world.Game.DebugDraw(screen, world.Space)
|
2022-10-14 10:02:03 +00:00
|
|
|
|
|
|
|
if world.Game.ShowHelpText {
|
|
|
|
|
|
|
|
world.Game.DrawText(screen, 16, 16,
|
|
|
|
"~ Collider Display test ~",
|
|
|
|
"F1: Toggle Debug View",
|
|
|
|
"F2: Show / Hide help text",
|
|
|
|
"R: Restart world",
|
|
|
|
fmt.Sprintf("%d FPS (frames per second)", int(ebiten.CurrentFPS())),
|
|
|
|
fmt.Sprintf("%d TPS (ticks per second)", int(ebiten.CurrentTPS())),
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|