diff options
author | davidovski <david@davidovski.xyz> | 2024-04-22 17:54:21 +0100 |
---|---|---|
committer | davidovski <david@davidovski.xyz> | 2024-04-22 17:54:21 +0100 |
commit | e4c0edeacb2c5829f864e116b68f6cad175141b3 (patch) | |
tree | 88ebcb9dd06affb1e525f807e3a40b864dd0865b | |
parent | d57ab031de3987ead9c06c48c8a5ad18a9de860c (diff) |
paper-style art
-rw-r--r-- | assets/tiles.png | bin | 4259 -> 4361 bytes | |||
-rw-r--r-- | level.go | 39 | ||||
-rw-r--r-- | main.go | 13 | ||||
-rw-r--r-- | objects.go | 20 | ||||
-rw-r--r-- | tilemap.go | 16 |
5 files changed, 69 insertions, 19 deletions
diff --git a/assets/tiles.png b/assets/tiles.png Binary files differindex 4ee5ff8..ecd1a5d 100644 --- a/assets/tiles.png +++ b/assets/tiles.png @@ -56,6 +56,25 @@ func StartLevel1(g *Game ) { 0, 69, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 0, 0, 69, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 0, }, + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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55, 53, 54, 55, 53, 54, 55, 53, 54, 55, 53, 54, 55, 53, 54, 55, 53, 54, 0, 0, 53, 70, 71, 69, 70, 71, 69, 70, 71, 69, 70, 71, 69, 70, 71, 69, 70, 71, 69, 70, 71, 69, 70, 0, - 0, 69, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, + 0, 69, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 39, 37, 38, 68, + }, + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 84, 82, 83, 84, 82, 83, 84, 82, 115, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0, + 0, 0, 99, 84, 82, 83, 84, 82, 83, 84, 82, 83, 84, 82, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0, + 0, 98, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0, + 0, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0, + 0, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 135, }, }, 25) @@ -36,6 +36,7 @@ const ( audioFadeIn = 0.999 sampleRate = 44100 + shadowOffset = 1 ) var ( @@ -405,10 +406,7 @@ func (g *Game) Draw(screen *ebiten.Image) { g.surface.Fill(color.Alpha16{0x9ccf}) DrawBackground(g.surface, g.time) - op := &ebiten.DrawImageOptions{} - op.GeoM.Translate(float64(g.offsetX), float64(g.offsetY-2)) - - g.surface.DrawImage(g.tilemap.surface, op) + g.tilemap.Draw(g.surface, float32(g.offsetX), float32(g.offsetY-2)) for i := len(g.objects)-1; i >= 0; i-- { obj := g.objects[i] @@ -432,12 +430,11 @@ func (g *Game) Draw(screen *ebiten.Image) { } } + screen.DrawImage(g.surface, &ebiten.DrawImageOptions{}) + screen.DrawImage(g.surface, &ebiten.DrawImageOptions{}) - op = &ebiten.DrawImageOptions{} - PostProcess(g.surface, g.shaderName, g.time) + PostProcess(screen, g.shaderName, g.time) - op = &ebiten.DrawImageOptions{} - screen.DrawImage(g.surface, &ebiten.DrawImageOptions{}) ebitenutil.DebugPrint(screen, fmt.Sprintf("tps: %.4f", ebiten.ActualFPS())) } @@ -133,11 +133,23 @@ func (o * GameObject) HasCollision(tilemap Tilemap, others []*GameObject, dir Di return false } -func DrawObject(o * GameObject, screen *ebiten.Image, tilemap Tilemap) { +func ShadowDraw(screen *ebiten.Image, image *ebiten.Image, x, y float32, alpha float32) { op := &ebiten.DrawImageOptions{} - op.ColorScale.ScaleAlpha(o.alpha) - op.GeoM.Translate(float64(o.x), float64(o.y)) - screen.DrawImage(o.image, op) + if alpha > 0{ + op = &ebiten.DrawImageOptions{} + op.ColorScale.ScaleAlpha(alpha) + op.ColorScale.Scale(0, 0, 0, 1); + op.GeoM.Translate(float64(x+shadowOffset), float64(y + shadowOffset)) + screen.DrawImage(image, op) + } + op = &ebiten.DrawImageOptions{} + op.ColorScale.ScaleAlpha(alpha) + op.GeoM.Translate(float64(x), float64(y)) + screen.DrawImage(image, op) +} + +func DrawObject(o * GameObject, screen *ebiten.Image, tilemap Tilemap) { + ShadowDraw(screen, o.image, o.x, o.y, o.alpha) if o.highlight { vector.StrokeRect(screen, o.x, o.y, float32(o.image.Bounds().Dx()), float32(o.image.Bounds().Dy()), hightlightBorder, color.RGBA{255, 100, 100, 255}, false) @@ -10,7 +10,7 @@ import ( type Tilemap struct { tilesImage *ebiten.Image - surface *ebiten.Image + surfaces []*ebiten.Image mapWidth int tileSize int layers [][]int @@ -27,25 +27,31 @@ func (* Tilemap) Update() error { return nil } -func (tm * Tilemap) Draw(screen *ebiten.Image) { +func (tm * Tilemap) Draw(screen *ebiten.Image, x, y float32) { + for _, surf := range tm.surfaces { + ShadowDraw(screen, surf, x, y, 1.0) + } } func (tm *Tilemap) UpdateSurface() { w := tm.tilesImage.Bounds().Dx() tileXCount := w / tileSize + tm.surfaces = make([]*ebiten.Image, len(tm.layers)) + // Draw each tile with each DrawImage call. // As the source images of all DrawImage calls are always same, // this rendering is done very efficiently. // For more detail, see https://pkg.go.dev/github.com/hajimehoshi/ebiten/v2#Image.DrawImage - for _, l := range tm.layers { + for idx, l := range tm.layers { + tm.surfaces[idx] = ebiten.NewImage(tm.mapWidth*tm.tileSize, len(l)/tm.mapWidth*tm.tileSize) for i, t := range l { op := &ebiten.DrawImageOptions{} op.GeoM.Translate(float64((i%tm.mapWidth)*tm.tileSize), float64((i/tm.mapWidth)*tm.tileSize)) sx := (t % tileXCount) * tileSize sy := (t / tileXCount) * tileSize - tm.surface.DrawImage(tm.tilesImage.SubImage(image.Rect(sx, sy, sx+tileSize, sy+tileSize)).(*ebiten.Image), op) + tm.surfaces[idx].DrawImage(tm.tilesImage.SubImage(image.Rect(sx, sy, sx+tileSize, sy+tileSize)).(*ebiten.Image), op) } } @@ -60,8 +66,6 @@ func NewTilemap(layers [][]int, mapWidth int) Tilemap { tilemap.layers = layers - tilemap.surface = ebiten.NewImage(mapWidth*tilemap.tileSize, len(layers[0])/mapWidth*tilemap.tileSize) - var err error tilemap.tilesImage = tilesImage |