bcvery1 / tilepix

Library for combining tiled maps with pixel
MIT License
46 stars 14 forks source link

Clean way to not DrawAll layers ? #97

Closed steelx closed 4 years ago

steelx commented 4 years ago

I have collision on Layer 3 in Tiled app.

Anyway I can draw them 1 after another. E.g. tilepix.Map.DrawLayer(0) which would allow us to draw hero character after layer 1. Then later we can draw other layers tilepix.Map.DrawLayer(1) to create an illusion of Hero going behind objects

Screen Shot 2020-01-01 at 12 59 51 PM

When I called m.DrawAll it renders everything, all layers. So Instead of painting the layer 3 with RED tile, Im using a transparent PX to paint it, so even if it renders it will render transparent.

So the, important part I still could not figure was, how do I render the Game (hero) Character before rendering layer 2. So that I can create an illusion he can go behind walls and table.

Thank you.

steelx commented 4 years ago

you can close this. I found a hacky solution. please add any tips, Im noob in 2D game development.

you can see below this line if index == layer the callback is getting called inside Update() func

CastleRoomMap.DrawAfter(2, func(canvas *pixelgl.Canvas) {
    gHero.mEntity.TeleportAndDraw(*CastleRoomMap, canvas)
})

Inside my GameMap method, just coped original DrawAll method

func (m GameMap) DrawAfter(layer int, callback func(canvas *pixelgl.Canvas)) error {
    // Draw tiles
    target, mat := global.gWin, pixel.IM

    if m.canvas == nil {
        m.canvas = pixelgl.NewCanvas(m.mTilemap.Bounds())
    }
    m.canvas.Clear(color.Transparent)

    for index, l := range m.mTilemap.TileLayers {
        if index == layer {
            callback(m.canvas)
        }
        if err := l.Draw(m.canvas); err != nil {
            log.WithError(err).Error("Map.DrawAll: could not draw layer")
            return err
        }
    }

    for _, il := range m.mTilemap.ImageLayers {
        // The matrix shift is because images are drawn from the top-left in Tiled.
        if err := il.Draw(m.canvas, pixel.IM.Moved(pixel.V(0, m.pixelHeight()))); err != nil {
            log.WithError(err).Error("Map.DrawAll: could not draw image layer")
            return err
        }
    }

    m.canvas.Draw(target, mat.Moved(m.mTilemap.Bounds().Center()))

    return nil
}