import Game from '@/core/game'

class ScenesManager {
    constructor () {
        this._zoom = 1.2
        this._left = 0
        this._top = 0
        this._scene = null
    }

    init () {


        Game.on('draw', ev => {
            const ctx = ev.ctx

            ctx.save()
            ctx.translate(this._left, this._top)
            ctx.scale(this._zoom, this._zoom)

            this._scene.draw(ev)

            ctx.restore()
        })

        Game.on('pointermove', ev => {
            Game.emit('scenepointermove', this.coords(ev.x, ev.y))
        })

        Game.on('pointerdown', ev => {
            Game.emit('scenepointerdown', this.coords(ev.x, ev.y))
        })

        Game.on('pointerup', ev => {
            Game.emit('scenepointerup', this.coords(ev.x, ev.y))
        })
    }

    coords (x, y) {
        return {
            x: (x - Game.rect.left - this._left) / this._zoom,
            y: (y - Game.rect.top - this._top) / this._zoom
        }
    }

    setScene (scene) {
        this._scene = scene
        this._scene.init(this)
    }
}


export default new ScenesManager
