phaser/src/textures/parsers/SpriteSheet.js

82 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-11-03 10:27:02 +00:00
var GetFastValue = require('../../utils/object/GetFastValue');
var SpriteSheet = function (texture, sourceIndex, x, y, width, height, config)
{
2017-11-03 10:27:02 +00:00
var frameWidth = GetFastValue(config, 'frameWidth', null);
var frameHeight = GetFastValue(config, 'frameHeight', frameWidth);
// If missing we can't proceed
if (frameWidth === null)
{
throw new Error('TextureManager.SpriteSheet: Invalid frameWidth given.');
}
2017-03-27 22:38:34 +00:00
// Add in a __BASE entry (for the entire atlas)
var source = texture.source[sourceIndex];
texture.add('__BASE', sourceIndex, 0, 0, source.width, source.height);
2017-11-03 10:27:02 +00:00
var startFrame = GetFastValue(config, 'startFrame', 0);
var endFrame = GetFastValue(config, 'endFrame', -1);
var margin = GetFastValue(config, 'margin', 0);
var spacing = GetFastValue(config, 'spacing', 0);
var row = Math.floor((width - margin) / (frameWidth + spacing));
var column = Math.floor((height - margin) / (frameHeight + spacing));
var total = row * column;
if (startFrame > total || startFrame < -total)
{
startFrame = 0;
}
if (startFrame < 0)
{
// Allow negative skipframes.
startFrame = total + startFrame;
}
if (endFrame !== -1)
{
total = startFrame + (endFrame + 1);
}
var fx = margin;
var fy = margin;
var ax = 0;
var ay = 0;
for (var i = 0; i < total; i++)
{
ax = 0;
ay = 0;
var w = fx + frameWidth;
var h = fy + frameHeight;
if (w > width)
{
ax = w - width;
}
if (h > height)
{
ay = h - height;
}
texture.add(i, sourceIndex, x + fx, y + fy, frameWidth - ax, frameHeight - ay);
fx += frameWidth + spacing;
if (fx + frameWidth > width)
{
fx = margin;
fy += frameHeight + spacing;
}
}
return texture;
};
module.exports = SpriteSheet;