diff options
-rw-r--r-- | main.go | 50 | ||||
-rw-r--r-- | objects.go | 26 |
2 files changed, 68 insertions, 8 deletions
@@ -11,8 +11,11 @@ import ( "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/ebitenutil" "github.com/hajimehoshi/ebiten/v2/inpututil" + ) + + const ( screenWidth = 400 screenHeight = 240 @@ -125,7 +128,6 @@ func (g * Game)ReplayPlayerAi() { g.KillPlayer() } - fmt.Printf("pframe %d/%d\n", g.playerAiIdx, len(g.playerAi)) } func (g * Game)ReplayPoint() { @@ -261,24 +263,24 @@ func (g *Game) Update() error { for _, obj := range g.objects { obj.Update(*g.tilemap, g.objects) } + g.tilemap.Update() g.ReplayPlayerAi() + cx, cy := ebiten.CursorPosition() + if len(g.toPlace) > 0 { placeable := g.toPlace[0] - cx, cy := ebiten.CursorPosition() cx = int(math.Floor(float64(cx)/float64(g.tilemap.tileSize)))*g.tilemap.tileSize cy = int(math.Floor(float64(cy)/float64(g.tilemap.tileSize)))*g.tilemap.tileSize cx += placeable.offsetX cy += placeable.offsetY - placeable.x = float32(cx) placeable.y = float32(cy) - - if inpututil.IsMouseButtonJustPressed(ebiten.MouseButton0) { - g.PlaceObject(cx, cy) - } + } + if inpututil.IsMouseButtonJustPressed(ebiten.MouseButton0) { + g.PlaceObject(cx, cy) } } if g.player.y > screenHeight { @@ -290,6 +292,17 @@ func (g *Game) Update() error { } func (g *Game) PlaceObject(cx, cy int) { + if len(g.toPlace) == 0 { + object := GetObjectAt(g.objects, float32(cx), float32(cy)) + if object != nil { + if object.movable { + g.toPlace = append([]*GameObject{object}, g.toPlace...) + g.RemoveObject(object) + } + } + return + } + placeable := g.toPlace[0] if placeable.HasCollision(*g.tilemap, g.objects, NONE) { return @@ -330,7 +343,6 @@ func (g *Game) Draw(screen *ebiten.Image) { // AFTER THE END if g.time > g.animStart + 60 { - fmt.Printf("end of end state transition\n") g.TransitionState() } } @@ -387,6 +399,10 @@ func (g *Game) KillPlayer() { g.ResetAll() g.playerAi = g.playerAi[:0] g.state = IN_GAME + + for _, o := range g.objects { + o.movable = false + } } } } @@ -400,6 +416,24 @@ func (g *Game) EndLevel() { } } +func (g *Game)RemoveObject(obj *GameObject) { + + i := 0 // output index + for _, x := range g.objects { + if x != obj { + // copy and increment index + g.objects[i] = x + i++ + } + } + // Prevent memory leak by erasing truncated values + // (not needed if values don't contain pointers, directly or indirectly) + for j := i; j < len(g.objects); j++ { + g.objects[j] = nil + } + g.objects = g.objects[:i] +} + func main() { LoadShaders() @@ -3,7 +3,9 @@ package main import ( "log" "image" + "image/color" "github.com/hajimehoshi/ebiten/v2" + "github.com/hajimehoshi/ebiten/v2/vector" "github.com/hajimehoshi/ebiten/v2/ebitenutil" ) @@ -37,6 +39,7 @@ type GameObject struct { onCollideDown func(this, other *GameObject) bool onCollideLeft func(this, other *GameObject) bool onCollideRight func(this, other *GameObject) bool + movable bool } @@ -82,6 +85,19 @@ func (o * GameObject) Update(tilemap Tilemap, others []*GameObject) { o.onGround = false; } } +func GetObjectAt(objects []*GameObject, x, y float32) *GameObject { + + for _, object := range objects { + maxX := object.x + float32(object.image.Bounds().Dx()) + maxY := object.y + float32(object.image.Bounds().Dy()) + minX := object.x + minY := object.y + if x >= minX && x < maxX && y >= minY && y < maxY { + return object + } + } + return nil +} func (o * GameObject) HasCollision(tilemap Tilemap, others []*GameObject, dir Direction) bool { if tilemap.CollideObject(o) { @@ -114,6 +130,13 @@ func (o * GameObject) Draw(screen *ebiten.Image, tilemap Tilemap) { op := &ebiten.DrawImageOptions{} op.GeoM.Translate(float64(o.x), float64(o.y)) screen.DrawImage(o.image, op) + + + if o.movable { + vector.StrokeRect(screen, o.x, o.y, float32(o.image.Bounds().Dx()), float32(o.image.Bounds().Dy()), 2, color.RGBA{255, 100, 100, 255}, false) + } + + } func (object * GameObject) Collide(other *GameObject) bool { @@ -171,6 +194,7 @@ func NewObject(game *Game, x, y float32) *GameObject{ obj.resistance = airResistance obj.x = obj.startx obj.y = obj.starty + obj.movable = true return obj } @@ -184,6 +208,7 @@ func NewPlayer(game *Game, x, y float32) *GameObject{ player.image = playerImage.SubImage(image.Rect(4, 8, 27, 32)).(*ebiten.Image) + player.movable = false return player } @@ -196,6 +221,7 @@ func NewExit(game *Game, x, y float32) *GameObject{ exit.onCollideLeft = OnCollideExit exit.onCollideRight = OnCollideExit + exit.movable = false return exit } |