Added basic backend collider visualizer.

This commit is contained in:
genxium 2022-10-14 11:49:04 +08:00
parent 05dc593d2c
commit 286944b88c
13 changed files with 734 additions and 107 deletions

View File

@ -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

View File

@ -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)
}

Binary file not shown.

View File

@ -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
)

View File

@ -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=

View File

@ -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())
}

View File

@ -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",
)
}
}

View File

@ -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())),
)
}
}

View File

@ -0,0 +1,9 @@
package main
import "github.com/hajimehoshi/ebiten/v2"
type WorldInterface interface {
Init()
Update()
Draw(*ebiten.Image)
}

View File

@ -3,7 +3,6 @@ module server
go 1.19 go 1.19
require ( require (
github.com/ByteArena/box2d v1.0.2
github.com/Masterminds/squirrel v0.0.0-20180815162352-8a7e65843414 github.com/Masterminds/squirrel v0.0.0-20180815162352-8a7e65843414
github.com/davecgh/go-spew v1.1.1 github.com/davecgh/go-spew v1.1.1
github.com/gin-contrib/cors v0.0.0-20180514151808-6f0a820f94be github.com/gin-contrib/cors v0.0.0-20180514151808-6f0a820f94be

View File

@ -10,8 +10,6 @@ import (
"server/api" "server/api"
"server/api/v1" "server/api/v1"
. "server/common" . "server/common"
"server/common/utils"
"server/configs"
"server/env_tools" "server/env_tools"
"server/models" "server/models"
"server/storage" "server/storage"
@ -30,8 +28,6 @@ func main() {
MustParseConstants() MustParseConstants()
storage.Init() storage.Init()
env_tools.LoadPreConf() env_tools.LoadPreConf()
utils.InitWechat(configs.WechatConfigIns)
utils.InitWechatGame(configs.WechatGameConfigIns)
if Conf.General.ServerEnv == SERVER_ENV_TEST { if Conf.General.ServerEnv == SERVER_ENV_TEST {
env_tools.MergeTestPlayerAccounts() env_tools.MergeTestPlayerAccounts()
} }

View File

@ -1,8 +1,6 @@
package models package models
import ( import (
"fmt"
"github.com/ByteArena/box2d"
"math" "math"
) )
@ -17,17 +15,6 @@ type Vec2D struct {
Y float64 `json:"y,omitempty"` 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 { 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. 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:"-"` Points []*Vec2D `json:"-"`
@ -53,78 +40,6 @@ type Polygon2D struct {
TmxObjectHeight float64 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 { func Distance(pt1 *Vec2D, pt2 *Vec2D) float64 {
dx := pt1.X - pt2.X dx := pt1.X - pt2.X
dy := pt1.Y - pt2.Y dy := pt1.Y - pt2.Y

View File

@ -1186,11 +1186,11 @@ func (pR *Room) inputFrameIdDebuggable(inputFrameId int32) bool {
func (pR *Room) refreshColliders() { 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" // 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 spaceW := pR.StageDiscreteW * pR.StageTileW
spaceH := pR.StageDiscreteH*pR.StageTileH spaceH := pR.StageDiscreteH * pR.StageTileH
spaceOffsetX := float64(spaceW)*0.5 spaceOffsetX := float64(spaceW) * 0.5
spaceOffsetY := float64(spaceH)*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 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 { for _, player := range pR.Players {