From 286944b88c38dece7ed1b4d7f4f5f17b49b3ef82 Mon Sep 17 00:00:00 2001 From: genxium Date: Fri, 14 Oct 2022 11:49:04 +0800 Subject: [PATCH] Added basic backend collider visualizer. --- backend_collider_visualizer/Makefile | 21 +++ backend_collider_visualizer/common.go | 66 +++++++ backend_collider_visualizer/excel.ttf | Bin 0 -> 25144 bytes backend_collider_visualizer/go.mod | 21 +++ backend_collider_visualizer/go.sum | 85 +++++++++ backend_collider_visualizer/main.go | 176 ++++++++++++++++++ backend_collider_visualizer/worldBouncer.go | 169 +++++++++++++++++ .../worldLineOfSight.go | 170 +++++++++++++++++ backend_collider_visualizer/worldinterface.go | 9 + battle_srv/go.mod | 1 - battle_srv/main.go | 4 - battle_srv/models/math.go | 85 --------- battle_srv/models/room.go | 34 ++-- 13 files changed, 734 insertions(+), 107 deletions(-) create mode 100644 backend_collider_visualizer/Makefile create mode 100644 backend_collider_visualizer/common.go create mode 100644 backend_collider_visualizer/excel.ttf create mode 100644 backend_collider_visualizer/go.mod create mode 100644 backend_collider_visualizer/go.sum create mode 100644 backend_collider_visualizer/main.go create mode 100644 backend_collider_visualizer/worldBouncer.go create mode 100644 backend_collider_visualizer/worldLineOfSight.go create mode 100644 backend_collider_visualizer/worldinterface.go diff --git a/backend_collider_visualizer/Makefile b/backend_collider_visualizer/Makefile new file mode 100644 index 0000000..87d47c5 --- /dev/null +++ b/backend_collider_visualizer/Makefile @@ -0,0 +1,21 @@ +PROJECTNAME=viscol.exe +ROOT_DIR=. +all: help +## Available proxies for downloading go modules are listed in "https://github.com/golang/go/wiki/Modules#how-do-i-use-vendoring-with-modules-is-vendoring-going-away". +GOPROXY=https://mirrors.aliyun.com/goproxy + +build: + go build -o $(ROOT_DIR)/$(PROJECTNAME) + +run: build + ./$(PROJECTNAME) + +.PHONY: help + +help: Makefile + @echo + @echo " Choose a command run:" + @echo + @sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /' + @echo + diff --git a/backend_collider_visualizer/common.go b/backend_collider_visualizer/common.go new file mode 100644 index 0000000..f3b7693 --- /dev/null +++ b/backend_collider_visualizer/common.go @@ -0,0 +1,66 @@ +package main + +import ( + "image/color" + "math" + + "github.com/hajimehoshi/ebiten/v2" + "github.com/hajimehoshi/ebiten/v2/ebitenutil" + "github.com/solarlune/resolv" +) + +var circleBuffer map[resolv.Shape]*ebiten.Image = map[resolv.Shape]*ebiten.Image{} + +func DrawPolygon(screen *ebiten.Image, shape *resolv.ConvexPolygon, color color.Color) { + + verts := shape.Transformed() + for i := 0; i < len(verts); i++ { + vert := verts[i] + next := verts[0] + + if i < len(verts)-1 { + next = verts[i+1] + } + ebitenutil.DrawLine(screen, vert.X(), vert.Y(), next.X(), next.Y(), color) + + } + +} + +func DrawCircle(screen *ebiten.Image, circle *resolv.Circle, drawColor color.Color) { + + // Actually drawing the circles live is too inefficient, so we will simply draw them to an image and then draw that instead + // when necessary. + + if _, exists := circleBuffer[circle]; !exists { + newImg := ebiten.NewImage(int(circle.Radius)*2, int(circle.Radius)*2) + + newImg.Set(int(circle.X), int(circle.Y), color.White) + + stepCount := float64(32) + + // Half image width and height. + hw := circle.Radius + hh := circle.Radius + + for i := 0; i < int(stepCount); i++ { + + x := (math.Sin(math.Pi*2*float64(i)/stepCount) * (circle.Radius - 2)) + hw + y := (math.Cos(math.Pi*2*float64(i)/stepCount) * (circle.Radius - 2)) + hh + + x2 := (math.Sin(math.Pi*2*float64(i+1)/stepCount) * (circle.Radius - 2)) + hw + y2 := (math.Cos(math.Pi*2*float64(i+1)/stepCount) * (circle.Radius - 2)) + hh + + ebitenutil.DrawLine(newImg, x, y, x2, y2, color.White) + + } + circleBuffer[circle] = newImg + } + + drawOpt := &ebiten.DrawImageOptions{} + r, g, b, _ := drawColor.RGBA() + drawOpt.ColorM.Scale(float64(r)/65535, float64(g)/65535, float64(b)/65535, 1) + drawOpt.GeoM.Translate(circle.X-circle.Radius, circle.Y-circle.Radius) + screen.DrawImage(circleBuffer[circle], drawOpt) + +} diff --git a/backend_collider_visualizer/excel.ttf b/backend_collider_visualizer/excel.ttf new file mode 100644 index 0000000000000000000000000000000000000000..820cfeffd2aa940d75c6cac10c473dc5318d8461 GIT binary patch literal 25144 zcmeHPZH!#ic|NnVyIz0dWJ9pQ4mW}DQH)G9j_lun| zUL#bcRiz@4N~le%+HI;R;RmFsimF7VQhwwIKN4vjRc%y7QGXzXAN)wANCjiF+uc6T z`=00CJ9lPwW{urn88~~-J@=gVecsRWF?SqFA%sog8=)MM$DSP8|I(2g4}?&<1Fc^h zn;xC}@?(#`976A&5Xvu1Or0J7-p$|sCuWBbh`4b ze|hPt5WYGU!oc_EW^47*--pEzPVC_GK&&X&?fTJQ|Iu}a-V0$n8bbKh&vyKYuFDse zMz$;t4�x^wo*`?tlH(+lgFP3p9@K{d$VB}9PWigr9nO%2%9-JfkvtIb6!Y^ zH2cRx*oZb2_GLTAvkYJapRvC2Tcp?nR6^&g|Wg-rT66wJ{Z_MxEvzzZnzJc;e2p=6lc_W0uEhuOkyoB`e z49br~xE?sJe;w)L5Xy@vzYO7q<0zoF17mlB&Q7%5xE*B<4-Xx4ja=?I?HrB!oL5`|s^Q!Lz$SYtPq0xEnO?hD>|+ zpkUn4QIxks*a!UkFy@}^DByR`n<4B+-{D@AZ-p>&E6QRBpLr2WDbTzZI1X$ zzYl%xe;nlpAshsbgW&nVLI{WMK>1Aw52D{gc>d6DL-_1m2%j53`A!Ij(RTQq5I%n& z3i^B=eUBUp;V5VyeI@# zN5S<`^nLX05Wc(%xNu{nh{O-MI8V z2HYRQ()+0Yx_8t2Z2M;t0j2$Y$3J*I4E7!GJO19&aLQLdc>ROd`zHGSFoX~O@q^=> zvfss$&Sxg9hp@IQbNwZTe8hFt3RbDS>aIM@8Sdf)P-0!ty9h!p$Cb|e2=oD)C9sQE zj4Pe9^;^Zhd3_-o-NPw7j@{LA`lzE5_moa9PfA zi}1e%92Y+94nu&1^Tshw6x3KhH1Rt)^~g^YVdA+HlB+ZH;3yY z&YQz6;r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.SetMaxTPS(60) + } + } + + if inpututil.IsKeyJustPressed(ebiten.KeyF1) { + g.Debug = !g.Debug + } + + if inpututil.IsKeyJustPressed(ebiten.KeyF2) { + g.ShowHelpText = !g.ShowHelpText + } + + if inpututil.IsKeyJustPressed(ebiten.KeyE) { + g.CurrentWorld++ + } + + if inpututil.IsKeyJustPressed(ebiten.KeyQ) { + g.CurrentWorld-- + } + + if g.CurrentWorld >= len(g.Worlds) { + g.CurrentWorld = 0 + } else if g.CurrentWorld < 0 { + g.CurrentWorld = len(g.Worlds) - 1 + } + + world := g.Worlds[g.CurrentWorld] + + if inpututil.IsKeyJustPressed(ebiten.KeyR) { + world.Init() + } + + if ebiten.IsKeyPressed(ebiten.KeyEscape) { + quit = errors.New("quit") + } + + world.Update() + + return quit + +} + +func (g *Game) Draw(screen *ebiten.Image) { + g.Screen = screen + screen.Fill(color.RGBA{20, 20, 40, 255}) + g.Worlds[g.CurrentWorld].Draw(screen) +} + +func (g *Game) DrawText(screen *ebiten.Image, x, y int, textLines ...string) { + rectHeight := 10 + for _, txt := range textLines { + w := float64(font.MeasureString(g.FontFace, txt).Round()) + ebitenutil.DrawRect(screen, float64(x), float64(y-8), w, float64(rectHeight), color.RGBA{0, 0, 0, 192}) + + text.Draw(screen, txt, g.FontFace, x+1, y+1, color.RGBA{0, 0, 150, 255}) + text.Draw(screen, txt, g.FontFace, x, y, color.RGBA{100, 150, 255, 255}) + y += rectHeight + } +} + +func (g *Game) DebugDraw(screen *ebiten.Image, space *resolv.Space) { + + for y := 0; y < space.Height(); y++ { + + for x := 0; x < space.Width(); x++ { + + cell := space.Cell(x, y) + + cw := float64(space.CellWidth) + ch := float64(space.CellHeight) + cx := float64(cell.X) * cw + cy := float64(cell.Y) * ch + + drawColor := color.RGBA{20, 20, 20, 255} + + if cell.Occupied() { + drawColor = color.RGBA{255, 255, 0, 255} + } + + ebitenutil.DrawLine(screen, cx, cy, cx+cw, cy, drawColor) + + ebitenutil.DrawLine(screen, cx+cw, cy, cx+cw, cy+ch, drawColor) + + ebitenutil.DrawLine(screen, cx+cw, cy+ch, cx, cy+ch, drawColor) + + ebitenutil.DrawLine(screen, cx, cy+ch, cx, cy, drawColor) + } + + } + +} + +func (g *Game) Layout(w, h int) (int, int) { + return g.Width, g.Height +} + +func main() { + ebiten.RunGame(NewGame()) +} diff --git a/backend_collider_visualizer/worldBouncer.go b/backend_collider_visualizer/worldBouncer.go new file mode 100644 index 0000000..e06d3b3 --- /dev/null +++ b/backend_collider_visualizer/worldBouncer.go @@ -0,0 +1,169 @@ +package main + +import ( + "fmt" + "image/color" + "math/rand" + + "github.com/hajimehoshi/ebiten/v2" + "github.com/hajimehoshi/ebiten/v2/ebitenutil" + "github.com/solarlune/resolv" +) + +type WorldBouncer struct { + Game *Game + Space *resolv.Space + Geometry []*resolv.Object + Bouncers []*Bouncer + ShowHelpText bool +} + +type Bouncer struct { + Object *resolv.Object + SpeedX, SpeedY float64 +} + +func NewWorldBouncer(game *Game) *WorldBouncer { + + w := &WorldBouncer{ + Game: game, + ShowHelpText: true, + } + + w.Init() + + return w +} + +func (world *WorldBouncer) Init() { + + gw := float64(world.Game.Width) + gh := float64(world.Game.Height) + cellSize := 8 + + world.Space = resolv.NewSpace(int(gw), int(gh), cellSize, cellSize) + + world.Geometry = []*resolv.Object{ + resolv.NewObject(0, 0, 16, gh), + resolv.NewObject(gw-16, 0, 16, gh), + resolv.NewObject(0, 0, gw, 16), + resolv.NewObject(0, gh-24, gw, 32), + } + + world.Space.Add(world.Geometry...) + + world.Bouncers = []*Bouncer{} + + world.SpawnObject() + +} + +func (world *WorldBouncer) SpawnObject() { + + bouncer := &Bouncer{ + Object: resolv.NewObject(0, 0, 2, 2), + SpeedX: (rand.Float64() * 8) - 4, + SpeedY: (rand.Float64() * 8) - 4, + } + + world.Space.Add(bouncer.Object) + + // Choose an unoccupied cell to spawn a bouncing object in + var c *resolv.Cell + for c == nil { + rx := rand.Intn(world.Space.Width()) + ry := rand.Intn(world.Space.Height()) + c = world.Space.Cell(rx, ry) + if c.Occupied() { + c = nil + } else { + bouncer.Object.X, bouncer.Object.Y = world.Space.SpaceToWorld(c.X, c.Y) + } + } + + world.Bouncers = append(world.Bouncers, bouncer) + +} + +func (world *WorldBouncer) Update() { + + if ebiten.IsKeyPressed(ebiten.KeyUp) { + for i := 0; i < 5; i++ { + world.SpawnObject() + } + } else if ebiten.IsKeyPressed(ebiten.KeyDown) { + if len(world.Bouncers) > 0 { + b := world.Bouncers[0] + world.Space.Remove(b.Object) + world.Bouncers = world.Bouncers[1:] + } + } + + for _, b := range world.Bouncers { + + b.SpeedY += 0.1 + + dx := b.SpeedX + dy := b.SpeedY + + if check := b.Object.Check(dx, 0); check != nil { + // We move a bouncer into contact with the owning cell rather than the object because we don't need to be that specific and + // moving into contact with another moving object that bounces away can get them both stuck; it's easier to bounce off of the + // "containing" cells, which are static. + contact := check.ContactWithCell(check.Cells[0]) + dx = contact.X() + b.SpeedX *= -1 + } + + b.Object.X += dx + + if check := b.Object.Check(0, dy); check != nil { + contact := check.ContactWithCell(check.Cells[0]) + dy = contact.Y() + b.SpeedY *= -1 + } + + b.Object.Y += dy + + b.Object.Update() + + } + +} + +func (world *WorldBouncer) Draw(screen *ebiten.Image) { + + for _, o := range world.Geometry { + ebitenutil.DrawRect(screen, o.X, o.Y, o.W, o.H, color.RGBA{60, 60, 60, 255}) + } + + for _, b := range world.Bouncers { + o := b.Object + ebitenutil.DrawRect(screen, o.X, o.Y, o.W, o.H, color.RGBA{0, 80, 255, 255}) + } + + if world.Game.Debug { + world.Game.DebugDraw(screen, world.Space) + } + + if world.Game.ShowHelpText { + + world.Game.DrawText(screen, 16, 16, + "~ Bouncer Demo ~", + "Up Arrow: Add bouncer", + "Down Arrow: Remove bouncer", + "", + fmt.Sprintf("%d Bouncers in the world.", len(world.Bouncers)), + fmt.Sprintf("%d FPS (frames per second)", int(ebiten.CurrentFPS())), + fmt.Sprintf("%d TPS (ticks per second)", int(ebiten.CurrentTPS())), + "", + "F1: Toggle Debug View", + "F2: Show / Hide help text", + "R: Restart world", + "E: Next world", + "Q: Previous world", + ) + + } + +} diff --git a/backend_collider_visualizer/worldLineOfSight.go b/backend_collider_visualizer/worldLineOfSight.go new file mode 100644 index 0000000..4429603 --- /dev/null +++ b/backend_collider_visualizer/worldLineOfSight.go @@ -0,0 +1,170 @@ +package main + +import ( + "fmt" + "image/color" + "strconv" + + "github.com/hajimehoshi/ebiten/v2" + "github.com/hajimehoshi/ebiten/v2/ebitenutil" + "github.com/solarlune/resolv" +) + +type WorldLineTest struct { + Game *Game + Space *resolv.Space + Player *resolv.Object +} + +func NewWorldLineTest(game *Game) *WorldLineTest { + w := &WorldLineTest{Game: game} + w.Init() + return w +} + +func (world *WorldLineTest) Init() { + + gw := float64(world.Game.Width) + gh := float64(world.Game.Height) + + cellSize := 8 + + world.Space = resolv.NewSpace(int(gw), int(gh), cellSize, cellSize) + + // Construct geometry + geometry := []*resolv.Object{ + + resolv.NewObject(0, 0, 16, gh), + resolv.NewObject(gw-16, 0, 16, gh), + resolv.NewObject(0, 0, gw, 16), + resolv.NewObject(0, gh-24, gw, 32), + resolv.NewObject(0, gh-24, gw, 32), + + resolv.NewObject(200, -160, 16, gh), + } + + world.Space.Add(geometry...) + + for _, o := range world.Space.Objects() { + o.AddTags("solid") + } + + world.Player = resolv.NewObject(160, 160, 16, 16) + world.Player.AddTags("player") + world.Space.Add(world.Player) + +} + +func (world *WorldLineTest) Update() { + + dx, dy := 0.0, 0.0 + moveSpd := 2.0 + + if ebiten.IsKeyPressed(ebiten.KeyW) { + dy = -moveSpd + } + + if ebiten.IsKeyPressed(ebiten.KeyS) { + dy += moveSpd + } + + if ebiten.IsKeyPressed(ebiten.KeyA) { + dx = -moveSpd + } + + if ebiten.IsKeyPressed(ebiten.KeyD) { + dx += moveSpd + } + + if col := world.Player.Check(dx, 0, "solid"); col != nil { + dx = col.ContactWithObject(col.Objects[0]).X() + } + + world.Player.X += dx + + if col := world.Player.Check(0, dy, "solid"); col != nil { + dy = col.ContactWithObject(col.Objects[0]).Y() + } + + world.Player.Y += dy + + world.Player.Update() + +} + +func (world *WorldLineTest) 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) + } + + mouseX, mouseY := ebiten.CursorPosition() + + mx, my := world.Space.WorldToSpace(float64(mouseX), float64(mouseY)) + + cx, cy := world.Player.CellPosition() + + sightLine := world.Space.CellsInLine(cx, cy, mx, my) + + interrupted := false + + for i, cell := range sightLine { + + if i == 0 { // Skip the beginning because that's the player + continue + } + + drawColor := color.RGBA{255, 255, 0, 255} + + // if interrupted { + // drawColor = color.RGBA{0, 0, 255, 255} + // } + + if !interrupted && cell.ContainsTags("solid") { + drawColor = color.RGBA{255, 0, 0, 255} + interrupted = true + } + + ebitenutil.DrawRect(screen, + float64(cell.X*world.Space.CellWidth), + float64(cell.Y*world.Space.CellHeight), + float64(world.Space.CellWidth), + float64(world.Space.CellHeight), + drawColor) + + if interrupted { + break + } + + } + + if world.Game.Debug { + world.Game.DebugDraw(screen, world.Space) + } + + if world.Game.ShowHelpText { + + world.Game.DrawText(screen, 16, 16, + "~ Line of sight test ~", + "WASD keys: Move player", + "Mouse: Hover over impassible objects", + "to get the closest wall to the player.", + fmt.Sprintf("Mouse X: %d, Mouse Y: %d", mouseX, mouseY), + "Clear line of sight: "+strconv.FormatBool(!interrupted), + "", + "F1: Toggle Debug View", + "F2: Show / Hide help text", + "R: Restart world", + "E: Next world", + "Q: Previous world", + fmt.Sprintf("%d FPS (frames per second)", int(ebiten.CurrentFPS())), + fmt.Sprintf("%d TPS (ticks per second)", int(ebiten.CurrentTPS())), + ) + + } + +} diff --git a/backend_collider_visualizer/worldinterface.go b/backend_collider_visualizer/worldinterface.go new file mode 100644 index 0000000..325b038 --- /dev/null +++ b/backend_collider_visualizer/worldinterface.go @@ -0,0 +1,9 @@ +package main + +import "github.com/hajimehoshi/ebiten/v2" + +type WorldInterface interface { + Init() + Update() + Draw(*ebiten.Image) +} diff --git a/battle_srv/go.mod b/battle_srv/go.mod index 816c005..0f30654 100644 --- a/battle_srv/go.mod +++ b/battle_srv/go.mod @@ -3,7 +3,6 @@ module server go 1.19 require ( - github.com/ByteArena/box2d v1.0.2 github.com/Masterminds/squirrel v0.0.0-20180815162352-8a7e65843414 github.com/davecgh/go-spew v1.1.1 github.com/gin-contrib/cors v0.0.0-20180514151808-6f0a820f94be diff --git a/battle_srv/main.go b/battle_srv/main.go index ce2aacf..303fad3 100644 --- a/battle_srv/main.go +++ b/battle_srv/main.go @@ -10,8 +10,6 @@ import ( "server/api" "server/api/v1" . "server/common" - "server/common/utils" - "server/configs" "server/env_tools" "server/models" "server/storage" @@ -30,8 +28,6 @@ func main() { MustParseConstants() storage.Init() env_tools.LoadPreConf() - utils.InitWechat(configs.WechatConfigIns) - utils.InitWechatGame(configs.WechatGameConfigIns) if Conf.General.ServerEnv == SERVER_ENV_TEST { env_tools.MergeTestPlayerAccounts() } diff --git a/battle_srv/models/math.go b/battle_srv/models/math.go index 37e8f5b..0e91ae0 100644 --- a/battle_srv/models/math.go +++ b/battle_srv/models/math.go @@ -1,8 +1,6 @@ package models import ( - "fmt" - "github.com/ByteArena/box2d" "math" ) @@ -17,17 +15,6 @@ type Vec2D struct { Y float64 `json:"y,omitempty"` } -func CreateVec2DFromB2Vec2(b2V2 box2d.B2Vec2) *Vec2D { - return &Vec2D{ - X: b2V2.X, - Y: b2V2.Y, - } -} - -func (v2 *Vec2D) ToB2Vec2() box2d.B2Vec2 { - return box2d.MakeB2Vec2(v2.X, v2.Y) -} - type Polygon2D struct { Anchor *Vec2D `json:"-"` // This "Polygon2D.Anchor" is used to be assigned to "B2BodyDef.Position", which in turn is used as the position of the FIRST POINT of the polygon. Points []*Vec2D `json:"-"` @@ -53,78 +40,6 @@ type Polygon2D struct { TmxObjectHeight float64 } -func MoveDynamicBody(body *box2d.B2Body, pToTargetPos *box2d.B2Vec2, inSeconds float64) { - if body.GetType() != box2d.B2BodyType.B2_dynamicBody { - return - } - body.SetTransform(*pToTargetPos, 0.0) - body.SetLinearVelocity(box2d.MakeB2Vec2(0.0, 0.0)) - body.SetAngularVelocity(0.0) -} - -func PrettyPrintFixture(fix *box2d.B2Fixture) { - fmt.Printf("\t\tfriction:\t%v\n", fix.M_friction) - fmt.Printf("\t\trestitution:\t%v\n", fix.M_restitution) - fmt.Printf("\t\tdensity:\t%v\n", fix.M_density) - fmt.Printf("\t\tisSensor:\t%v\n", fix.M_isSensor) - fmt.Printf("\t\tfilter.categoryBits:\t%d\n", fix.M_filter.CategoryBits) - fmt.Printf("\t\tfilter.maskBits:\t%d\n", fix.M_filter.MaskBits) - fmt.Printf("\t\tfilter.groupIndex:\t%d\n", fix.M_filter.GroupIndex) - - switch fix.M_shape.GetType() { - case box2d.B2Shape_Type.E_circle: - { - s := fix.M_shape.(*box2d.B2CircleShape) - fmt.Printf("\t\tb2CircleShape shape: {\n") - fmt.Printf("\t\t\tradius:\t%v\n", s.M_radius) - fmt.Printf("\t\t\toffset:\t%v\n", s.M_p) - fmt.Printf("\t\t}\n") - } - break - - case box2d.B2Shape_Type.E_polygon: - { - s := fix.M_shape.(*box2d.B2PolygonShape) - fmt.Printf("\t\tb2PolygonShape shape: {\n") - for i := 0; i < s.M_count; i++ { - fmt.Printf("\t\t\t%v\n", s.M_vertices[i]) - } - fmt.Printf("\t\t}\n") - } - break - - default: - break - } -} - -func PrettyPrintBody(body *box2d.B2Body) { - bodyIndex := body.M_islandIndex - - fmt.Printf("{\n") - fmt.Printf("\tHeapRAM addr:\t%p\n", body) - fmt.Printf("\ttype:\t%d\n", body.M_type) - fmt.Printf("\tposition:\t%v\n", body.GetPosition()) - fmt.Printf("\tangle:\t%v\n", body.M_sweep.A) - fmt.Printf("\tlinearVelocity:\t%v\n", body.GetLinearVelocity()) - fmt.Printf("\tangularVelocity:\t%v\n", body.GetAngularVelocity()) - fmt.Printf("\tlinearDamping:\t%v\n", body.M_linearDamping) - fmt.Printf("\tangularDamping:\t%v\n", body.M_angularDamping) - fmt.Printf("\tallowSleep:\t%d\n", body.M_flags&box2d.B2Body_Flags.E_autoSleepFlag) - fmt.Printf("\tawake:\t%d\n", body.M_flags&box2d.B2Body_Flags.E_awakeFlag) - fmt.Printf("\tfixedRotation:\t%d\n", body.M_flags&box2d.B2Body_Flags.E_fixedRotationFlag) - fmt.Printf("\tbullet:\t%d\n", body.M_flags&box2d.B2Body_Flags.E_bulletFlag) - fmt.Printf("\tactive:\t%d\n", body.M_flags&box2d.B2Body_Flags.E_activeFlag) - fmt.Printf("\tgravityScale:\t%v\n", body.M_gravityScale) - fmt.Printf("\tislandIndex:\t%v\n", bodyIndex) - fmt.Printf("\tfixtures: {\n") - for f := body.M_fixtureList; f != nil; f = f.M_next { - PrettyPrintFixture(f) - } - fmt.Printf("\t}\n") - fmt.Printf("}\n") -} - func Distance(pt1 *Vec2D, pt2 *Vec2D) float64 { dx := pt1.X - pt2.X dy := pt1.Y - pt2.Y diff --git a/battle_srv/models/room.go b/battle_srv/models/room.go index 9e9eff6..8538042 100644 --- a/battle_srv/models/room.go +++ b/battle_srv/models/room.go @@ -435,13 +435,13 @@ func (pR *Room) StartBattle() { elapsedNanosSinceLastFrameIdTriggered := stCalculation - pR.LastRenderFrameIdTriggeredAt if elapsedNanosSinceLastFrameIdTriggered < pR.RollbackEstimatedDtNanos { Logger.Debug(fmt.Sprintf("Avoiding too fast frame@roomId=%v, renderFrameId=%v: elapsedNanosSinceLastFrameIdTriggered=%v", pR.Id, pR.RenderFrameId, elapsedNanosSinceLastFrameIdTriggered)) - continue + continue } if pR.RenderFrameId > pR.BattleDurationFrames { Logger.Info(fmt.Sprintf("The `battleMainLoop` for roomId=%v is stopped@renderFrameId=%v, with battleDurationFrames=%v:\n%v", pR.Id, pR.RenderFrameId, pR.BattleDurationFrames, pR.InputsBufferString(true))) pR.StopBattleForSettlement() - return + return } if swapped := atomic.CompareAndSwapInt32(&pR.State, RoomBattleStateIns.IN_BATTLE, RoomBattleStateIns.IN_BATTLE); !swapped { @@ -789,8 +789,8 @@ func (pR *Room) OnDismissed() { pR.NstDelayFrames = 8 pR.InputScaleFrames = uint32(2) pR.ServerFps = 60 - pR.RollbackEstimatedDt = 0.016667 // Use fixed-and-low-precision to mitigate the inconsistent floating-point-number issue between Golang and JavaScript - pR.RollbackEstimatedDtMillis = 16.667 // Use fixed-and-low-precision to mitigate the inconsistent floating-point-number issue between Golang and JavaScript + pR.RollbackEstimatedDt = 0.016667 // Use fixed-and-low-precision to mitigate the inconsistent floating-point-number issue between Golang and JavaScript + pR.RollbackEstimatedDtMillis = 16.667 // Use fixed-and-low-precision to mitigate the inconsistent floating-point-number issue between Golang and JavaScript pR.RollbackEstimatedDtNanos = 16666666 // A little smaller than the actual per frame time, just for preventing FAST FRAME pR.BattleDurationFrames = 30 * pR.ServerFps pR.BattleDurationNanos = int64(pR.BattleDurationFrames) * (pR.RollbackEstimatedDtNanos + 1) @@ -1142,9 +1142,9 @@ func (pR *Room) applyInputFrameDownsyncDynamics(fromRenderFrameId int32, toRende encodedInput := inputList[joinIndex-1] decodedInput := DIRECTION_DECODER[encodedInput] decodedInputSpeedFactor := DIRECTION_DECODER_INVERSE_LENGTH[encodedInput] - if 0.0 == decodedInputSpeedFactor { - continue - } + if 0.0 == decodedInputSpeedFactor { + continue + } baseChange := player.Speed * pR.RollbackEstimatedDt * decodedInputSpeedFactor dx := baseChange * float64(decodedInput[0]) dy := baseChange * float64(decodedInput[1]) @@ -1153,7 +1153,7 @@ func (pR *Room) applyInputFrameDownsyncDynamics(fromRenderFrameId int32, toRende playerCollider := pR.CollisionSysMap[collisionPlayerIndex] if collision := playerCollider.Check(dx, dy, "Barrier"); collision != nil { changeWithCollision := collision.ContactWithObject(collision.Objects[0]) - Logger.Info(fmt.Sprintf("Collided: roomId=%v, playerId=%v, orig dx=%v, orig dy=%v, new dx =%v, new dy=%v", pR.Id, player.Id, dx, dy, changeWithCollision.X(), changeWithCollision.Y())) + Logger.Info(fmt.Sprintf("Collided: roomId=%v, playerId=%v, orig dx=%v, orig dy=%v, new dx =%v, new dy=%v", pR.Id, player.Id, dx, dy, changeWithCollision.X(), changeWithCollision.Y())) dx = changeWithCollision.X() dy = changeWithCollision.Y() } @@ -1172,7 +1172,7 @@ func (pR *Room) applyInputFrameDownsyncDynamics(fromRenderFrameId int32, toRende newRenderFrame := pb.RoomDownsyncFrame{ Id: collisionSysRenderFrameId + 1, Players: toPbPlayers(pR.Players), - CountdownNanos: (pR.BattleDurationNanos - int64(collisionSysRenderFrameId)*pR.RollbackEstimatedDtNanos), + CountdownNanos: (pR.BattleDurationNanos - int64(collisionSysRenderFrameId)*pR.RollbackEstimatedDtNanos), } pR.RenderFrameBuffer.Put(&newRenderFrame) pR.CurDynamicsRenderFrameId++ @@ -1184,18 +1184,18 @@ func (pR *Room) inputFrameIdDebuggable(inputFrameId int32) bool { } func (pR *Room) refreshColliders() { - playerColliderRadius := float64(12) // hardcoded + playerColliderRadius := float64(12) // hardcoded // Kindly note that by now, we've already got all the shapes in the tmx file into "pR.(Players | Barriers)" from "ParseTmxLayersAndGroups" - spaceW := pR.StageDiscreteW*pR.StageTileW - spaceH := pR.StageDiscreteH*pR.StageTileH + spaceW := pR.StageDiscreteW * pR.StageTileW + spaceH := pR.StageDiscreteH * pR.StageTileH - spaceOffsetX := float64(spaceW)*0.5 - spaceOffsetY := float64(spaceH)*0.5 + spaceOffsetX := float64(spaceW) * 0.5 + spaceOffsetY := float64(spaceH) * 0.5 space := resolv.NewSpace(int(spaceW), int(spaceH), int(pR.StageTileW), int(pR.StageTileH)) // allocate a new collision space everytime after a battle is settled for _, player := range pR.Players { - playerCollider := resolv.NewObject(player.X+spaceOffsetX, player.Y+spaceOffsetY, playerColliderRadius*2, playerColliderRadius*2) - playerColliderShape := resolv.NewCircle(0, 0, playerColliderRadius*2) + playerCollider := resolv.NewObject(player.X+spaceOffsetX, player.Y+spaceOffsetY, playerColliderRadius*2, playerColliderRadius*2) + playerColliderShape := resolv.NewCircle(0, 0, playerColliderRadius*2) playerCollider.SetShape(playerColliderShape) space.Add(playerCollider) // Keep track of the collider in "pR.CollisionSysMap" @@ -1224,7 +1224,7 @@ func (pR *Room) refreshColliders() { barrierColliderShape := resolv.NewConvexPolygon() for _, p := range barrier.Boundary.Points { - barrierColliderShape.AddPoints(p.X, p.Y) + barrierColliderShape.AddPoints(p.X, p.Y) } barrierCollider := resolv.NewObject(barrier.Boundary.Anchor.X+spaceOffsetX, barrier.Boundary.Anchor.Y+spaceOffsetY, w, h, "Barrier")