From 39b86eddf0404d9fd39a32724e47fd54629189a5 Mon Sep 17 00:00:00 2001 From: davidovski Date: Sun, 21 Apr 2024 22:46:25 +0100 Subject: do not let items place ontop of each other --- Makefile | 13 ++++++++ assets/character.png | Bin 0 -> 1351 bytes assets/tiles.png | Bin 0 -> 1416 bytes character.png | Bin 1351 -> 0 bytes html/index.html | 16 +++++++++ main.go | 36 +++++++++++++-------- objects.go | 89 +++++++++++++++++++++++++++++++++++---------------- tiles.png | Bin 1416 -> 0 bytes 8 files changed, 112 insertions(+), 42 deletions(-) create mode 100644 Makefile create mode 100644 assets/character.png create mode 100644 assets/tiles.png delete mode 100644 character.png create mode 100644 html/index.html delete mode 100644 tiles.png diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..52cc8cc --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +wasm-serve: + go run github.com/hajimehoshi/wasmserve@latest . + +target-wasm: html/index.html + mkdir -p out + env GOOS=js GOARCH=wasm go build -o out/main.wasm . + cp html/index.html out/ + cp /usr/lib/go/misc/wasm/wasm_exec.js out/ + cp -r assets out/ + +clean: + rm -rf out + rm -rf go.sum diff --git a/assets/character.png b/assets/character.png new file mode 100644 index 0000000..4cbc070 Binary files /dev/null and b/assets/character.png differ diff --git a/assets/tiles.png b/assets/tiles.png new file mode 100644 index 0000000..5c3ce91 Binary files /dev/null and b/assets/tiles.png differ diff --git a/character.png b/character.png deleted file mode 100644 index 4cbc070..0000000 Binary files a/character.png and /dev/null differ diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..9e9ac4c --- /dev/null +++ b/html/index.html @@ -0,0 +1,16 @@ + + + diff --git a/main.go b/main.go index fb86e10..6438fa4 100644 --- a/main.go +++ b/main.go @@ -98,22 +98,25 @@ func (g * Game)ReplayPoint() { } } -func (g * Game)ResetAll() { +func (g * Game) ResetAll() { for _, obj := range g.objects { obj.x = obj.startx obj.y = obj.starty } + g.recording = g.recording[:0]; } func (g *Game) Init() { g.state = PLACING - g.toPlace = append(g.toPlace, NewBox(0, 0)) - g.toPlace = append(g.toPlace, NewBox(0, 0)) - g.toPlace = append(g.toPlace, NewBox(0, 0)) - g.toPlace = append(g.toPlace, NewSpring(0, 0)) - g.toPlace = append(g.toPlace, NewSpike(0, 0)) - g.toPlace = append(g.toPlace, NewLeftSpike(0, 0)) + g.toPlace = append(g.toPlace, NewSpike(g, 0, 0)) + g.toPlace = append(g.toPlace, NewSpring(g, 0, 0)) + g.toPlace = append(g.toPlace, NewBox(g, 0, 0)) + g.toPlace = append(g.toPlace, NewBox(g, 0, 0)) + g.toPlace = append(g.toPlace, NewBox(g, 0, 0)) + g.toPlace = append(g.toPlace, NewSpike(g, 0, 0)) + g.toPlace = append(g.toPlace, NewLeftSpike(g, 0, 0)) + g.toPlace = append(g.toPlace, NewSpring(g, 0, 0)) g.surface = ebiten.NewImage(screenWidth, screenHeight) g.shaderName = "none" @@ -142,9 +145,9 @@ func (g *Game) Init() { g.tilemap.UpdateSurface() - g.player = NewPlayer(4 * tileSize, 8 * tileSize) + g.player = NewPlayer(g, 4 * tileSize, 8 * tileSize) g.objects = append(g.objects, g.player) - g.exit = NewExit(21 * tileSize, 8 * tileSize) + g.exit = NewExit(g, 21 * tileSize, 8 * tileSize) g.objects = append(g.objects, g.exit) g.ResetAll() @@ -221,11 +224,16 @@ func (g *Game) Update() error { return nil } -func (g *Game)PlaceObject(cx, cy int) { - g.toPlace[0].startx = float32(cx) - g.toPlace[0].starty = float32(cy) +func (g *Game) PlaceObject(cx, cy int) { + placeable := g.toPlace[0] + if placeable.HasCollision(*g.tilemap, g.objects, NONE) { + return + } + + placeable.startx = float32(cx) + placeable.starty = float32(cy) - g.objects = append(g.objects, g.toPlace[0]) + g.objects = append(g.objects, placeable) g.toPlace = g.toPlace[1:len(g.toPlace)] @@ -299,7 +307,7 @@ func main() { LoadShaders() var err error - tilesImage, _, err = ebitenutil.NewImageFromFile("tiles.png") + tilesImage, _, err = ebitenutil.NewImageFromFile("assets/tiles.png") if err != nil { log.Fatal(err) } diff --git a/objects.go b/objects.go index 7793c36..0bc8b62 100644 --- a/objects.go +++ b/objects.go @@ -11,7 +11,18 @@ const ( SPRING_FORCE = 8 ) +type Direction int + +const ( + NONE Direction = iota + LEFT + RIGHT + UP + DOWN +) + type GameObject struct { + game *Game startx, starty float32 x, y float32 vx, vy float32 @@ -22,8 +33,10 @@ type GameObject struct { resistance float32 image *ebiten.Image onGround bool - onCollideX func(this, other *GameObject) bool - onCollideY func(this, other *GameObject) bool + onCollideUp func(this, other *GameObject) bool + onCollideDown func(this, other *GameObject) bool + onCollideLeft func(this, other *GameObject) bool + onCollideRight func(this, other *GameObject) bool } @@ -32,19 +45,34 @@ type Player struct { } func (o * GameObject) Update(tilemap Tilemap, others []*GameObject) { + var direction Direction o.vy += gravity o.vx *= o.resistance o.vy *= o.resistance o.x += o.vx - if o.HasCollision(tilemap, others, true) { + + if o.vx > 0 { + direction = RIGHT + } else { + direction = LEFT + } + + if o.HasCollision(tilemap, others, direction) { o.x -= o.vx o.vx = 0 } o.y += o.vy - if o.HasCollision(tilemap, others, false) { + + if o.vy > 0 { + direction = UP + } else { + direction = DOWN + } + + if o.HasCollision(tilemap, others, direction) { o.onGround = true; o.vx *= o.friction @@ -55,17 +83,22 @@ func (o * GameObject) Update(tilemap Tilemap, others []*GameObject) { } } -func (o * GameObject) HasCollision(tilemap Tilemap, others []*GameObject, x bool) bool { +func (o * GameObject) HasCollision(tilemap Tilemap, others []*GameObject, dir Direction) bool { if tilemap.CollideObject(o) { return true } for _, obj := range others { if obj.Collide(o) { var f func(this, other *GameObject) bool - if x { - f = obj.onCollideX - } else { - f = obj.onCollideY + switch dir { + case UP: + f = obj.onCollideUp + case DOWN: + f = obj.onCollideDown + case LEFT: + f = obj.onCollideLeft + case RIGHT: + f = obj.onCollideRight } if f == nil{ @@ -113,8 +146,9 @@ func OnCollideGeneric(this, other *GameObject) bool { return true } -func NewObject(x, y float32) *GameObject{ +func NewObject(game *Game, x, y float32) *GameObject{ obj := &GameObject{ + game: game, startx: x, starty: y, } @@ -126,10 +160,10 @@ func NewObject(x, y float32) *GameObject{ return obj } -func NewPlayer(x, y float32) *GameObject{ - player := NewObject(x, y) +func NewPlayer(game *Game, x, y float32) *GameObject{ + player := NewObject(game, x, y) - playerImage, _, err := ebitenutil.NewImageFromFile("character.png") + playerImage, _, err := ebitenutil.NewImageFromFile("assets/character.png") if err != nil { log.Fatal(err) } @@ -139,47 +173,47 @@ func NewPlayer(x, y float32) *GameObject{ return player } -func NewExit(x, y float32) *GameObject{ - exit := NewObject(x, y) +func NewExit(game *Game, x, y float32) *GameObject{ + exit := NewObject(game, x, y) exit.image = tilesImage.SubImage(image.Rect(0, 16, 32, 48)).(*ebiten.Image) return exit } -func NewBox(x, y float32) *GameObject{ - box := NewObject(x, y) +func NewBox(game *Game, x, y float32) *GameObject{ + box := NewObject(game, x, y) box.image = tilesImage.SubImage(image.Rect(160, 0, 176, 16)).(*ebiten.Image) return box } -func NewSpring(x, y float32) *GameObject{ - spring := NewObject(x, y) +func NewSpring(game *Game, x, y float32) *GameObject{ + spring := NewObject(game, x, y) spring.image = tilesImage.SubImage(image.Rect(176, 0, 192, 16)).(*ebiten.Image) - spring.onCollideY = OnCollideSpring + spring.onCollideUp = OnCollideSpring return spring } -func NewSpike(x, y float32) *GameObject{ - spike := NewObject(x, y) +func NewSpike(game *Game, x, y float32) *GameObject{ + spike := NewObject(game, x, y) spike.offsetY = 3 spike.image = tilesImage.SubImage(image.Rect(192, 3, 208, 16)).(*ebiten.Image) - spike.onCollideY = OnCollideSpike + spike.onCollideUp = OnCollideSpike return spike } -func NewLeftSpike(x, y float32) *GameObject{ - spike := NewObject(x, y) +func NewLeftSpike(game *Game, x, y float32) *GameObject{ + spike := NewObject(game, x, y) spike.offsetX = 3 spike.image = tilesImage.SubImage(image.Rect(195, 16, 208, 32)).(*ebiten.Image) - spike.onCollideX = OnCollideSpike + spike.onCollideLeft = OnCollideSpike return spike } @@ -191,8 +225,7 @@ func OnCollideSpring(this, other *GameObject) bool { } func OnCollideSpike(this, other *GameObject) bool { - other.x = other.startx - other.y = other.starty + other.game.ResetAll() return true } diff --git a/tiles.png b/tiles.png deleted file mode 100644 index 5c3ce91..0000000 Binary files a/tiles.png and /dev/null differ -- cgit v1.2.1