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 0000000..820cfef Binary files /dev/null and b/backend_collider_visualizer/excel.ttf differ diff --git a/backend_collider_visualizer/go.mod b/backend_collider_visualizer/go.mod new file mode 100644 index 0000000..02cb122 --- /dev/null +++ b/backend_collider_visualizer/go.mod @@ -0,0 +1,21 @@ +module viscol + +go 1.19 + +require ( + github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 + github.com/hajimehoshi/ebiten/v2 v2.4.7 + github.com/solarlune/resolv v0.5.1 + golang.org/x/image v0.0.0-20220902085622-e7cb96979f69 +) + +require ( + github.com/ebitengine/purego v0.0.0-20220905075623-aeed57cda744 // indirect + github.com/go-gl/glfw/v3.3/glfw v0.0.0-20220806181222-55e207c401ad // indirect + github.com/hajimehoshi/file2byteslice v0.0.0-20210813153925-5340248a8f41 // indirect + github.com/jezek/xgb v1.0.1 // indirect + github.com/kvartborg/vector v0.0.0-20200419093813-2cba0cabb4f0 // indirect + golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect + golang.org/x/mobile v0.0.0-20220722155234-aaac322e2105 // indirect + golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect +) diff --git a/backend_collider_visualizer/go.sum b/backend_collider_visualizer/go.sum new file mode 100644 index 0000000..22154ea --- /dev/null +++ b/backend_collider_visualizer/go.sum @@ -0,0 +1,85 @@ +github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/ebitengine/purego v0.0.0-20220905075623-aeed57cda744 h1:A8UnJ/5OKzki4HBDwoRQz7I6sxKsokpMXcGh+fUxpfc= +github.com/ebitengine/purego v0.0.0-20220905075623-aeed57cda744/go.mod h1:Eh8I3yvknDYZeCuXH9kRNaPuHEwvXDCk378o9xszmHg= +github.com/go-gl/glfw/v3.3/glfw v0.0.0-20220806181222-55e207c401ad h1:kX51IjbsJPCvzV9jUoVQG9GEUqIq5hjfYzXTqQ52Rh8= +github.com/go-gl/glfw/v3.3/glfw v0.0.0-20220806181222-55e207c401ad/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= +github.com/hajimehoshi/bitmapfont/v2 v2.2.1 h1:y7zcy02/UgO24IL3COqYtrRZzhRucNBtmCo/SNU648k= +github.com/hajimehoshi/bitmapfont/v2 v2.2.1/go.mod h1:wjrYAy8vKgj9JsFgnYAOK346/uvE22TlmqouzdnYIs0= +github.com/hajimehoshi/ebiten/v2 v2.4.7 h1:XuvB7R0Rbw/O7g6vNU8gqr5b9e7MNhhAONMSsyreLDI= +github.com/hajimehoshi/ebiten/v2 v2.4.7/go.mod h1:Ofk1EfQZZ8tL0TlEPF5wPrnN+8Oa/ywuQOYh+uYsqLQ= +github.com/hajimehoshi/file2byteslice v0.0.0-20210813153925-5340248a8f41 h1:s01qIIRG7vN/5ndLwkDktjx44ulFk6apvAjVBYR50Yo= +github.com/hajimehoshi/file2byteslice v0.0.0-20210813153925-5340248a8f41/go.mod h1:CqqAHp7Dk/AqQiwuhV1yT2334qbA/tFWQW0MD2dGqUE= +github.com/hajimehoshi/go-mp3 v0.3.3/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM= +github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI= +github.com/hajimehoshi/oto/v2 v2.3.1/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo= +github.com/jakecoffman/cp v1.2.1/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg= +github.com/jezek/xgb v1.0.1 h1:YUGhxps0aR7J2Xplbs23OHnV1mWaxFVcOl9b+1RQkt8= +github.com/jezek/xgb v1.0.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk= +github.com/jfreymuth/oggvorbis v1.0.4/go.mod h1:1U4pqWmghcoVsCJJ4fRBKv9peUJMBHixthRlBeD6uII= +github.com/jfreymuth/vorbis v1.0.2/go.mod h1:DoftRo4AznKnShRl1GxiTFCseHr4zR9BN3TWXyuzrqQ= +github.com/kvartborg/vector v0.0.0-20200419093813-2cba0cabb4f0 h1:v8lWpj5957KtDMKu+xQtlu6G3ZoZR6Tn9bsfZCRG5Xw= +github.com/kvartborg/vector v0.0.0-20200419093813-2cba0cabb4f0/go.mod h1:GAX7tMJqXx9fB1BrsTWPOXy6IBRX+J461BffVPAdpwo= +github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI= +github.com/solarlune/resolv v0.5.1 h1:Ul6PAs/zaxiMUOEYz1Z6VeUj5k3CDcWMvSh+kivybDY= +github.com/solarlune/resolv v0.5.1/go.mod h1:HjM2f/0NoVjVdZsi26GtugX5aFbA62COEFEXkOhveRw= +github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 h1:estk1glOnSVeJ9tdEZZc5mAMDZk5lNJNyJ6DvrBkTEU= +golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4= +golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= +golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/image v0.0.0-20220722155232-062f8c9fd539/go.mod h1:doUCurBvlfPMKfmIpRIywoHmhN3VyhnoFDbvIEWF4hY= +golang.org/x/image v0.0.0-20220902085622-e7cb96979f69 h1:Lj6HJGCSn5AjxRAH2+r35Mir4icalbqku+CLUtjnvXY= +golang.org/x/image v0.0.0-20220902085622-e7cb96979f69/go.mod h1:doUCurBvlfPMKfmIpRIywoHmhN3VyhnoFDbvIEWF4hY= +golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= +golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= +golang.org/x/mobile v0.0.0-20220722155234-aaac322e2105 h1:3vUV5x5+3LfQbgk7paCM6INOaJG9xXQbn79xoNkwfIk= +golang.org/x/mobile v0.0.0-20220722155234-aaac322e2105/go.mod h1:pe2sM7Uk+2Su1y7u/6Z8KJ24D7lepUjFZbhFOrmDfuQ= +golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 h1:Sx/u41w+OwrInGdEckYmEuU5gHoGSL4QbDz3S9s6j4U= +golang.org/x/sys v0.0.0-20220818161305-2296e01440c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.8-0.20211022200916-316ba0b74098/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/backend_collider_visualizer/main.go b/backend_collider_visualizer/main.go new file mode 100644 index 0000000..84b810f --- /dev/null +++ b/backend_collider_visualizer/main.go @@ -0,0 +1,176 @@ +package main + +import ( + _ "embed" + "errors" + "fmt" + "image/color" + "time" + + "github.com/golang/freetype/truetype" + "github.com/hajimehoshi/ebiten/v2" + "github.com/hajimehoshi/ebiten/v2/ebitenutil" + "github.com/hajimehoshi/ebiten/v2/inpututil" + "github.com/hajimehoshi/ebiten/v2/text" + "github.com/solarlune/resolv" + "golang.org/x/image/font" +) + +//go:embed excel.ttf +var excelFont []byte + +type Game struct { + Worlds []WorldInterface + CurrentWorld int + Width, Height int + Debug bool + ShowHelpText bool + Screen *ebiten.Image + FontFace font.Face +} + +func NewGame() *Game { + + ebiten.SetWindowResizable(true) + ebiten.SetWindowTitle("resolv test") + + g := &Game{ + Width: 640, + Height: 360, + ShowHelpText: true, + } + + g.Worlds = []WorldInterface{ + NewWorldBouncer(g), + NewWorldLineTest(g), + } + + fontData, _ := truetype.Parse(excelFont) + + g.FontFace = truetype.NewFace(fontData, &truetype.Options{Size: 10}) + + // Debug FPS rendering + + go func() { + + for { + + fmt.Println("FPS: ", ebiten.CurrentFPS()) + fmt.Println("Ticks: ", ebiten.CurrentTPS()) + time.Sleep(time.Second) + + } + + }() + + return g + +} + +func (g *Game) Update() error { + + var quit error + + if inpututil.IsKeyJustPressed(ebiten.KeyP) { + if ebiten.CurrentTPS() >= 60 { + ebiten.SetMaxTPS(6) + } else { + ebiten.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")