wobblyhead hg > minor_monsters
view game/js/assets.js @ 3:cc0c32e71090
added sound sources
| author | ark |
|---|---|
| date | 2026-10-18 20:09 +0200 |
| parents | 5492efd60e2c |
| children |
line wrap: on
line source
// image loading and the sprite table window.MM = window.MM || {}; MM.assets = (function () { "use strict"; var images = {}; var cfg = MM.config; // load every sprite of the config's sprite table and every map image. // calls done(failed names) once all have loaded or failed. function load(done) { var names = Object.keys(cfg.sprites); var jobs = names.map(function (n) { return [n, "assets/" + n + ".png"]; }); MM.maps.forEach(function (m) { jobs.push([m.name, m.image]); }); var left = jobs.length, failed = []; jobs.forEach(function (job) { var img = new Image(); img.onload = finish; img.onerror = function () { failed.push(job[1]); finish(); }; img.src = job[1]; images[job[0]] = img; }); function finish() { if (--left === 0) done(failed); } } function img(name) { return images[name]; } // sprite size in map pixels at the given scale function size(name, scale) { var s = cfg.sprites[name]; return [s[0] * scale, s[1] * scale]; } // draw a sprite with its pivot at x, y. angle is the direction to face // in radians, ignored for sprites that do not turn. sx, sy stretch the // sprite around its pivot. function draw(ctx, name, x, y, scale, angle, sx, sy) { var image = images[name]; if (!image || !image.complete) return; var w = cfg.sprites[name][0] * scale; var h = cfg.sprites[name][1] * scale; var pivot = cfg.art.pivot[name] || [0.5, 0.5]; var drawn = cfg.art.angle[name]; ctx.save(); ctx.translate(x, y); if (drawn !== undefined && angle !== undefined && angle !== null) { ctx.rotate(angle - MM.util.deg(drawn)); } if (sx !== undefined) ctx.scale(sx, sy); ctx.drawImage(image, -pivot[0] * w, -pivot[1] * h, w, h); ctx.restore(); } return { load: load, img: img, size: size, draw: draw }; })();
