diff options
author | davidovski <david@davidovski.xyz> | 2024-04-21 22:46:25 +0100 |
---|---|---|
committer | davidovski <david@davidovski.xyz> | 2024-04-21 22:46:25 +0100 |
commit | 39b86eddf0404d9fd39a32724e47fd54629189a5 (patch) | |
tree | efda9cea5f44231aab18b346193c2f78a4283e30 | |
parent | bc09e485e6981e5b4943161b5a9d410ac179eb4a (diff) |
do not let items place ontop of each other
-rw-r--r-- | Makefile | 13 | ||||
-rw-r--r-- | assets/character.png (renamed from character.png) | bin | 1351 -> 1351 bytes | |||
-rw-r--r-- | assets/tiles.png (renamed from tiles.png) | bin | 1416 -> 1416 bytes | |||
-rw-r--r-- | html/index.html | 16 | ||||
-rw-r--r-- | main.go | 36 | ||||
-rw-r--r-- | objects.go | 89 |
6 files changed, 112 insertions, 42 deletions
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/character.png b/assets/character.png Binary files differindex 4cbc070..4cbc070 100644 --- a/character.png +++ b/assets/character.png diff --git a/tiles.png b/assets/tiles.png Binary files differindex 5c3ce91..5c3ce91 100644 --- a/tiles.png +++ b/assets/tiles.png 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 @@ +<!DOCTYPE html> +<script src="wasm_exec.js"></script> +<script> +// Polyfill +if (!WebAssembly.instantiateStreaming) { + WebAssembly.instantiateStreaming = async (resp, importObject) => { + const source = await (await resp).arrayBuffer(); + return await WebAssembly.instantiate(source, importObject); + }; +} + +const go = new Go(); +WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then(result => { + go.run(result.instance); +}); +</script> @@ -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) } @@ -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 } |