diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2022-12-27 14:29:39 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-01-27 16:43:43 +0100 |
| commit | 7f7389422834f628c9e45278e90fab6911173b25 (patch) | |
| tree | 0bd8c0f4daaed7619d467cf82e7f7add09ef1f32 /go/ui/map.go | |
| parent | 3c2ee4ee48480a28dc606878503f314ec8862d45 (diff) | |
| download | muhqs-game-7f7389422834f628c9e45278e90fab6911173b25.tar.gz muhqs-game-7f7389422834f628c9e45278e90fab6911173b25.zip | |
intermediate commit
Diffstat (limited to 'go/ui/map.go')
| -rw-r--r-- | go/ui/map.go | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/go/ui/map.go b/go/ui/map.go new file mode 100644 index 00000000..2e83ad2d --- /dev/null +++ b/go/ui/map.go @@ -0,0 +1,109 @@ +package ui + +import ( + "log" + "math" + + "github.com/hajimehoshi/ebiten/v2" + "muhq.space/muhqs-game/go/assets" + "muhq.space/muhqs-game/go/game" +) + +const ( + TILE_WIDTH int = 50 + TILE_HEIGHT int = 50 +) + +type MapLayer struct { + m *game.Map + img *ebiten.Image +} + +func NewMapLayerFromMap(m *game.Map) *MapLayer { + mv := MapLayer{m, nil} + return &mv +} + +func rotateStreetImg(x, y int, radian float64, op *ebiten.DrawImageOptions) { + // Move the image's center to the screen's upper-left corner. + // This is a preparation for rotating. When geometry matrices are applied, + // the origin point is the upper-left corner. + op.GeoM.Translate(-float64(TILE_WIDTH)/2, -float64(TILE_HEIGHT)/2) + + // Rotate the image. As a result, the anchor point of this rotate is + // the center of the image. + op.GeoM.Rotate(radian) + + // Reset translation + op.GeoM.Translate(float64(TILE_WIDTH)/2, float64(TILE_HEIGHT)/2) +} + +func (m *MapLayer) rotateStreet(x, y int, op *ebiten.DrawImageOptions) { + connections, left, right, above, below := m.m.FindStreetConnections(x, y) + + if connections == 0 { + // This street is not connected to another street -> + // check any other non neutral tiles + connections, left, right, above, below = m.m.FindAnyConnections(x, y) + } + + // This street is not connected to anything. Seams odd! + if connections == 0 { + log.Printf("Street at (%d, %d) is not connected", x, y) + } else if (connections == 1 || + (connections == 2 && ((left && right) || (above && below)))) && + (above || below) { + rotateStreetImg(x, y, math.Pi/2, op) + } else if connections == 2 { + // normal orientation above and right + if right && below { + rotateStreetImg(x, y, 3*math.Pi/2, op) + } else if left && below { + rotateStreetImg(x, y, math.Pi, op) + } else if above && left { + rotateStreetImg(x, y, math.Pi/2, op) + } + } else if connections == 3 { + // normal orientation left above right + if above && right && below { + rotateStreetImg(x, y, 3*math.Pi/2, op) + } else if left && below && right { + rotateStreetImg(x, y, math.Pi, op) + } else if below && left && above { + rotateStreetImg(x, y, math.Pi/2, op) + } + } +} + +func (m *MapLayer) Draw(screen *ebiten.Image) { + if m.img == nil { + // TODO: support non symetric maps + maxWidth := len(m.m.Tiles[0]) * TILE_WIDTH + maxHeight := len(m.m.Tiles) * TILE_HEIGHT + m.img = ebiten.NewImage(maxWidth, maxHeight) + + x_px, y_px := 0.0, 0.0 + for y := 0; y < len(m.m.Tiles); y++ { + for x := 0; x < len(m.m.Tiles[y]); x++ { + tile := &m.m.Tiles[y][x] + tileImg := assets.GetTile(tile.Raw) + + op := &ebiten.DrawImageOptions{} + // Rotate Street + if tile.Type == game.TileTypes.Street { + m.rotateStreet(x, y, op) + } + + op.GeoM.Translate(x_px, y_px) + + m.img.DrawImage(tileImg, op) + + x_px += float64(TILE_WIDTH) + } + x_px = 0 + y_px += float64(TILE_HEIGHT) + } + } + + screen.DrawImage(m.img, &ebiten.DrawImageOptions{}) +} |
