DelayNoMore/collider_visualizer/worldColliderDisplay.go

199 lines
8.7 KiB
Go
Raw Permalink Normal View History

package main
import (
. "dnmshared"
"fmt"
"github.com/hajimehoshi/ebiten/v2"
"go.uber.org/zap"
"image/color"
2022-12-26 12:37:40 +00:00
. "jsexport/battle"
"resolv"
)
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
2022-11-09 06:20:26 +00:00
playerPosList := *(playerPosMap["PlayerStartingPos"])
2022-10-14 10:47:09 +00:00
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))
spaceW := stageDiscreteW * stageTileW
spaceH := stageDiscreteH * stageTileH
spaceOffsetX := float64(spaceW) * 0.5
spaceOffsetY := float64(spaceH) * 0.5
2022-12-14 13:30:01 +00:00
worldToVirtualGridRatio := float64(1000)
2022-12-15 04:28:39 +00:00
virtualGridToWorldRatio := float64(1) / worldToVirtualGridRatio
2022-12-14 13:30:01 +00:00
playerDefaultSpeed := 1 * worldToVirtualGridRatio
minStep := (int(float64(playerDefaultSpeed)*virtualGridToWorldRatio) << 3)
2022-12-14 13:30:01 +00:00
playerColliderRadius := float64(12)
2022-12-26 12:37:40 +00:00
playerColliders := make([]*resolv.Object, len(playerPosList))
2022-12-15 04:28:39 +00:00
snapIntoPlatformOverlap := float64(0.1)
space := resolv.NewSpace(int(spaceW), int(spaceH), minStep, minStep)
topPadding, bottomPadding, leftPadding, rightPadding := snapIntoPlatformOverlap, snapIntoPlatformOverlap, snapIntoPlatformOverlap, snapIntoPlatformOverlap
2022-12-26 12:37:40 +00:00
for i, playerPos := range playerPosList {
2022-12-15 04:28:39 +00:00
colliderWidth, colliderHeight := playerColliderRadius*2, playerColliderRadius*4
2022-12-26 12:37:40 +00:00
playerCollider := GenerateRectCollider(playerPos.X, playerPos.Y, colliderWidth, colliderHeight, topPadding, bottomPadding, leftPadding, rightPadding, spaceOffsetX, spaceOffsetY, nil, fmt.Sprintf("Player%d", i+1)) // [WARNING] Deliberately not using a circle because "resolv v0.5.1" doesn't yet align circle center with space cell center, regardless of the "specified within-object offset"
Logger.Info(fmt.Sprintf("Player Collider#%d: player world pos=(%.2f, %.2f), shape=%v", i, playerPos.X, playerPos.Y, ConvexPolygonStr(playerCollider.Shape.(*resolv.ConvexPolygon))))
playerColliders[i] = playerCollider
space.Add(playerCollider)
}
barrierLocalId := 0
2022-12-26 12:37:40 +00:00
for _, barrierUnaligned := range barrierList {
barrierCollider := GenerateConvexPolygonCollider(barrierUnaligned, spaceOffsetX, spaceOffsetY, nil, "Barrier")
2022-12-14 13:30:01 +00:00
Logger.Debug(fmt.Sprintf("Added barrier: shape=%v", ConvexPolygonStr(barrierCollider.Shape.(*resolv.ConvexPolygon))))
space.Add(barrierCollider)
barrierLocalId++
}
2022-10-14 10:47:09 +00:00
world.Space = space
moveToCollide := true
if moveToCollide {
2022-11-12 03:20:16 +00:00
effPushback := Vec2D{X: float64(0), Y: float64(0)}
2022-12-22 13:27:14 +00:00
colliderWidth, colliderHeight := playerColliderRadius*2, playerColliderRadius*4
playerColliders[0].X, playerColliders[0].Y = VirtualGridToPolygonColliderBLPos(int32(-139000), int32(-474500), colliderWidth, colliderHeight, topPadding, bottomPadding, leftPadding, rightPadding, spaceOffsetX, spaceOffsetY, virtualGridToWorldRatio)
2022-12-26 12:37:40 +00:00
playerColliders[0].Update()
2022-11-12 14:53:35 +00:00
2022-12-26 12:37:40 +00:00
playerColliders[1].X, playerColliders[1].Y = VirtualGridToPolygonColliderBLPos(int32(-163000), int32(-520000), colliderWidth, colliderHeight, topPadding, bottomPadding, leftPadding, rightPadding, spaceOffsetX, spaceOffsetY, virtualGridToWorldRatio)
playerColliders[1].Update()
2022-11-12 14:53:35 +00:00
2022-12-26 12:37:40 +00:00
toTestPlayerCollider := playerColliders[1]
if collision := toTestPlayerCollider.Check(0, 0); collision != nil {
playerShape := toTestPlayerCollider.Shape.(*resolv.ConvexPolygon)
2022-11-12 03:20:16 +00:00
for _, obj := range collision.Objects {
2022-12-14 13:30:01 +00:00
bShape := obj.Shape.(*resolv.ConvexPolygon)
2022-12-15 04:28:39 +00:00
Logger.Warn(fmt.Sprintf("Checking potential: a=%v, b=%v", ConvexPolygonStr(playerShape), ConvexPolygonStr(bShape)))
2022-12-14 13:30:01 +00:00
if overlapped, pushbackX, pushbackY, overlapResult := CalcPushbacks(0, 0, playerShape, bShape); overlapped {
Logger.Warn(fmt.Sprintf("Overlapped: a=%v, b=%v, pushbackX=%v, pushbackY=%v", ConvexPolygonStr(playerShape), ConvexPolygonStr(bShape), pushbackX, pushbackY))
2022-11-12 03:20:16 +00:00
effPushback.X += pushbackX
effPushback.Y += pushbackY
} else {
2022-12-14 13:30:01 +00:00
Logger.Warn(fmt.Sprintf("Collided BUT not overlapped: a=%v, b=%v, overlapResult=%v", ConvexPolygonStr(playerShape), ConvexPolygonStr(bShape), overlapResult))
2022-11-12 03:20:16 +00:00
}
}
//toTestPlayerCollider.X -= effPushback.X
//toTestPlayerCollider.Y -= effPushback.Y
//toTestPlayerCollider.Update()
2022-11-12 03:20:16 +00:00
Logger.Info(fmt.Sprintf("effPushback={%v, %v}", effPushback.X, effPushback.Y))
}
}
meleeBullet := &MeleeBullet{
// for offender
StartupFrames: int32(18),
ActiveFrames: int32(1),
RecoveryFrames: int32(61),
RecoveryFramesOnBlock: int32(61),
RecoveryFramesOnHit: int32(61),
2022-12-26 12:37:40 +00:00
SelfMoveforwardX: 0,
SelfMoveforwardY: 0,
HitboxOffset: float64(24.0),
HitboxSizeX: float64(45.0),
HitboxSizeY: float64(32.0),
// for defender
HitStunFrames: int32(18),
BlockStunFrames: int32(9),
Pushback: float64(22.0),
ReleaseTriggerType: int32(1), // 1: rising-edge, 2: falling-edge
Damage: int32(5),
}
bulletLeftToRight := false
if bulletLeftToRight {
xfac := float64(1.0)
2022-12-26 12:37:40 +00:00
offenderWx, offenderWy := playerPosList[0].X, playerPosList[0].Y
bulletWx, bulletWy := offenderWx+xfac*meleeBullet.HitboxOffset, offenderWy
2022-12-26 12:37:40 +00:00
newBulletCollider := GenerateRectCollider(bulletWx, bulletWy, meleeBullet.HitboxSizeX, meleeBullet.HitboxSizeY, topPadding, bottomPadding, leftPadding, rightPadding, spaceOffsetX, spaceOffsetY, nil, "MeleeBullet")
space.Add(newBulletCollider)
bulletShape := newBulletCollider.Shape.(*resolv.ConvexPolygon)
Logger.Warn(fmt.Sprintf("bullet ->: Added bullet collider to space: a=%v", ConvexPolygonStr(bulletShape)))
if collision := newBulletCollider.Check(0, 0); collision != nil {
for _, obj := range collision.Objects {
objShape := obj.Shape.(*resolv.ConvexPolygon)
if overlapped, pushbackX, pushbackY, overlapResult := CalcPushbacks(0, 0, bulletShape, objShape); overlapped {
Logger.Warn(fmt.Sprintf("bullet ->: Overlapped: a=%v, b=%v, pushbackX=%v, pushbackY=%v", ConvexPolygonStr(bulletShape), ConvexPolygonStr(objShape), pushbackX, pushbackY))
} else {
Logger.Warn(fmt.Sprintf("bullet ->: Collided BUT not overlapped: a=%v, b=%v, overlapResult=%v", ConvexPolygonStr(bulletShape), ConvexPolygonStr(objShape), overlapResult))
}
}
}
}
bulletRightToLeft := false
if bulletRightToLeft {
xfac := float64(-1.0)
2022-12-26 12:37:40 +00:00
offenderWx, offenderWy := playerPosList[1].X, playerPosList[1].Y
bulletWx, bulletWy := offenderWx+xfac*meleeBullet.HitboxOffset, offenderWy
2022-12-26 12:37:40 +00:00
newBulletCollider := GenerateRectCollider(bulletWx, bulletWy, meleeBullet.HitboxSizeX, meleeBullet.HitboxSizeY, topPadding, bottomPadding, leftPadding, rightPadding, spaceOffsetX, spaceOffsetY, nil, "MeleeBullet")
space.Add(newBulletCollider)
bulletShape := newBulletCollider.Shape.(*resolv.ConvexPolygon)
Logger.Warn(fmt.Sprintf("bullet <-: Added bullet collider to space: a=%v", ConvexPolygonStr(bulletShape)))
if collision := newBulletCollider.Check(0, 0); collision != nil {
for _, obj := range collision.Objects {
objShape := obj.Shape.(*resolv.ConvexPolygon)
if overlapped, pushbackX, pushbackY, overlapResult := CalcPushbacks(0, 0, bulletShape, objShape); overlapped {
Logger.Warn(fmt.Sprintf("bullet <-: Overlapped: a=%v, b=%v, pushbackX=%v, pushbackY=%v", ConvexPolygonStr(bulletShape), ConvexPolygonStr(objShape), pushbackX, pushbackY))
} else {
Logger.Warn(fmt.Sprintf("bullet <-: Collided BUT not overlapped: a=%v, b=%v, overlapResult=%v", ConvexPolygonStr(bulletShape), ConvexPolygonStr(objShape), overlapResult))
}
}
}
}
return world
}
func (world *WorldColliderDisplay) Update() {
}
func (world *WorldColliderDisplay) Draw(screen *ebiten.Image) {
for _, o := range world.Space.Objects() {
2022-12-26 12:37:40 +00:00
if o.HasTags("Player1") {
drawColor := color.RGBA{255, 0, 0, 255}
DrawPolygon(screen, o.Shape.(*resolv.ConvexPolygon), drawColor)
2022-12-26 12:37:40 +00:00
} else if o.HasTags("Player2") {
drawColor := color.RGBA{0, 0, 255, 255}
DrawPolygon(screen, o.Shape.(*resolv.ConvexPolygon), drawColor)
2022-12-26 12:37:40 +00:00
} else if o.HasTags("MeleeBullet") {
drawColor := color.RGBA{78, 255, 112, 255}
DrawPolygon(screen, o.Shape.(*resolv.ConvexPolygon), drawColor)
} else {
drawColor := color.RGBA{60, 60, 60, 255}
DrawPolygon(screen, o.Shape.(*resolv.ConvexPolygon), drawColor)
}
}
2022-12-14 13:30:01 +00:00
//world.Game.DebugDraw(screen, world.Space)
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())),
)
}
}