// renderArena.js : dessin de l'arene a partir de la map Tiled + sprite Soulgate

import { Sprite, Assets } from 'pixi.js';
import { loadTiledMap, drawTiledMap } from './tiledLoader.js';
import { iso, getTw, getTh } from './renderer.js';
import { SOULGATE_X, SOULGATE_Y } from './constants.js';

let _mapData = null;

export async function loadArenaMap() {
    if (_mapData) return;
    _mapData = await loadTiledMap('assets/maps/arrena.tmj');
    await Assets.load('assets/soulgate.png');
}

export function drawStaticArena(layer) {
    if (!_mapData) {
        console.warn('arena map not loaded yet');
        return;
    }
    drawTiledMap(layer, _mapData);
    _drawSoulgate(layer);
}

function _drawSoulgate(layer) {
    const tex = Assets.get('assets/soulgate.png');
    if (!tex) return;
    const tw = getTw();
    const th = getTh();

    const sprite = new Sprite(tex);
    sprite.anchor.set(0.5, 1.0);
    const p = iso(SOULGATE_X, SOULGATE_Y);
    sprite.x = p.x;
    sprite.y = p.y + th / 2;
    // taille : ~1.5 unite monde de large (source 128px), assez present sans ecraser
    sprite.scale.set((tw * 1.5) / 128);
    layer.addChild(sprite);
}
