Skip to content

Commit

Permalink
translating spriteGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
Jbudone committed Feb 19, 2019
1 parent 8ca1eec commit b86e831
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 14 deletions.
58 changes: 57 additions & 1 deletion resourceBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,13 @@ const packageRoutines = {
delete sheet.newDependencies;
delete sheet.oldSprites;

// NOTE: This part may not be necessary because we create spriteGroup again
//sheet.spriteGroups.forEach((spriteGroup) => {
// if (spriteGroup.oldSpriteGroup) {
// delete spriteGroup.oldSpriteGroup;
// }
//});

if (sheet.sprites && sheet.sprites.length === 0) {
delete sheet.sprites;
}
Expand Down Expand Up @@ -730,15 +737,20 @@ readPackage('resources', 'resources/data/resources.json').then((details) => {
fs.copyFile(Resources.file, output, (err) => {

if (err) {
console.error("Error copying package to output");
console.error(err);
return;
process.exit(1);
}

console.log("Saved resources to " + output);
});
}).catch((err) => {
console.error("Error saving packages");
process.exit(1);
});
}, () => {
console.error("There was an error reading from Resources");
process.exit(1);
});
});

Expand Down Expand Up @@ -901,6 +913,8 @@ let processResources = (package) => {
let bothPromises = new Promise((success, fail) => {
processResources(asset).then(processAsset).then(() => {
success();
}, (err) => {
fail(err);
});
});
processingAssetsPromises.push(bothPromises);
Expand Down Expand Up @@ -937,6 +951,9 @@ let processResources = (package) => {
console.log("Saved to " + package.file);
success();
});
}).catch((err) => {
console.error("Fail in processing assets");
fail(err);
});
} else {
success();
Expand Down Expand Up @@ -1222,6 +1239,8 @@ const processGeneratedTilesheet = (package) => {
});
});

const spriteGroupsToTranslate = [];

// Append to package.sprites for image based deps
imageDeps.forEach((dependency) => {
package.dependencies.push(dependency);
Expand Down Expand Up @@ -1259,6 +1278,13 @@ const processGeneratedTilesheet = (package) => {
width: spriteGroup.width,
height: spriteGroup.height
});


// If the spriteGroup has been modified (moved? scaled?) we need to translate individual sprites in the
// spriteGroup (in the map and such)
if (spriteGroup.oldSpriteGroup) {
spriteGroupsToTranslate.push(spriteGroup);
}
});


Expand Down Expand Up @@ -1346,6 +1372,33 @@ const processGeneratedTilesheet = (package) => {
});
}

spriteGroupsToTranslate.forEach((spriteGroup) => {

// FIXME: If package.tilesize changes then we need to take that into consideration
if (spriteGroup.oldSpriteGroup) {

const tw = Math.ceil(spriteGroup.width / package.tilesize),
th = Math.ceil(spriteGroup.height / package.tilesize),
twOld = Math.ceil(spriteGroup.oldSpriteGroup.width / package.tilesize),
thOld = Math.ceil(spriteGroup.oldSpriteGroup.height / package.tilesize),
dstX = spriteGroup.dstX / package.tilesize,
dstY = spriteGroup.dstX / package.tilesize,
dstXOld = spriteGroup.oldSpriteGroup.dstX / package.tilesize,
dstYOld = spriteGroup.oldSpriteGroup.dstY / package.tilesize;

spriteTranslations[oldSprite] = 0;

// FIXME:
// - Add to spriteTranslations (each individual sprite oldPos -> newPos)
// - Add deleted sprites to spriteTranslations (eg. scaling) (oldPos -> null)
// - Add newSprites as neighbours to oldSprites (oldSprite[north] -> newSprite) -- map finds sprite in
// this list, then looks 1 up and if the sprite is empty use this new sprite. We MAY need to include
// the entire spriteGroup in case the sprite above is NOT empty, then see if the sprite is apart of a
// different spriteGroup, and if so then either delete that spriteGroup instance off the map, or shift
// it slightly until it fits
}
});

if (oldSprites) {
oldSprites.forEach((sprite) => {
const newSpriteInfo = package.sprites.find((s) => s.sprite === sprite.sprite && s.source === sprite.source),
Expand All @@ -1364,6 +1417,9 @@ const processGeneratedTilesheet = (package) => {
spriteTranslations[oldSprite] = null; // Deleted
}
});
}

if (boundsHaveChanged) {

// Translate data: collisions/floating/shootable
for (let i = 0; i < package.data.collisions.length; ++i) {
Expand Down
44 changes: 31 additions & 13 deletions tools/toolbelt/js/modTilesheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1620,8 +1620,8 @@ const ModTilesheet = (function(containerEl){
gridAlpha = 0.1,
x = spriteGroup.newDstX,
y = spriteGroup.newDstY,
width = spriteGroup.width,
height = spriteGroup.height;
width = tilesize * Math.ceil(spriteGroup.width / tilesize),
height = tilesize * Math.ceil(spriteGroup.height / tilesize);
canvasCtx.globalAlpha = 1.0;
canvasCtx.strokeStyle = '#FF0000';
canvasCtx.strokeRect(x - (gridLineWidth/2), y - (gridLineWidth/2), width + (gridLineWidth/2), height + (gridLineWidth/2));
Expand All @@ -1634,8 +1634,8 @@ const ModTilesheet = (function(containerEl){
gridAlpha = 0.1,
x = borderedIsland.newDstX,
y = borderedIsland.newDstY,
width = borderedIsland.width,
height = borderedIsland.height;
width = tilesize * Math.ceil(borderedIsland.width / tilesize),
height = tilesize * Math.ceil(borderedIsland.height / tilesize);
canvasCtx.globalAlpha = 1.0;
canvasCtx.strokeStyle = '#FFFF00';
canvasCtx.strokeRect(x - (gridLineWidth/2), y - (gridLineWidth/2), width + (gridLineWidth/2), height + (gridLineWidth/2));
Expand Down Expand Up @@ -1723,6 +1723,7 @@ const ModTilesheet = (function(containerEl){
resource.sprites.push(_sprite);
});

const oldSpriteGroups = loadedResource.spriteGroups;
resource.spriteGroups = [];
spriteGroups.forEach((spriteGroup) => {

Expand All @@ -1735,6 +1736,32 @@ const ModTilesheet = (function(containerEl){
newSpriteGroup.dstY = spriteGroup.newDstY;
newSpriteGroup.width = spriteGroup.width;
newSpriteGroup.height = spriteGroup.height;

// Find old spriteGroup, compare against this one
const oldSpriteGroup = oldSpriteGroups.find((oldSpriteGroup) => oldSpriteGroup.imageSrc === spriteGroup.imageSrc);
if (oldSpriteGroup) {

if
(
oldSpriteGroup.dstX !== newSpriteGroup.dstX ||
oldSpriteGroup.dstY !== newSpriteGroup.dstY ||
oldSpriteGroup.width !== newSpriteGroup.width ||
oldSpriteGroup.height !== newSpriteGroup.height
)
{
newSpriteGroup.oldSpriteGroup = {
dstX: oldSpriteGroup.dstX,
dstY: oldSpriteGroup.dstY,
width: oldSpriteGroup.width,
height: oldSpriteGroup.height
};

resource.dirty = true;
}
} else {
resource.dirty = true;
}

} else {

newSpriteGroup.assetId = spriteGroup.assetId;
Expand All @@ -1755,8 +1782,6 @@ const ModTilesheet = (function(containerEl){
resource.spriteGroups.push(newSpriteGroup);
});

// FIXME: Do we need to compare loadedResource.spriteGroups === resource.spriteGroups to flag resource.dirty?

// Have any of the dependencies been modified?
resource.dependencies = [];
dependencies.forEach((dep) => {
Expand All @@ -1783,8 +1808,6 @@ const ModTilesheet = (function(containerEl){
resource.dirty = true;
return;
}
const spriteGroup = resource.spriteGroups.find((spriteGroup) => spriteGroup.imageSrc === dep.imageSrc),
oldSpriteGroup = loadedResource.spriteGroups.find((oldSpriteGroup) => oldSpriteGroup.imageSrc === spriteGroup.imageSrc);

// Image based dep?
if (dep.imageSrc) {
Expand All @@ -1794,11 +1817,6 @@ const ModTilesheet = (function(containerEl){
resource.dirty = true;
}
}

// Sprite Group moved?
if (spriteGroup.dstX !== oldSpriteGroup.dstX || spriteGroup.dstY !== oldSpriteGroup.dstY) {
resource.dirty = true;
}
});

console.log(sprites);
Expand Down

0 comments on commit b86e831

Please sign in to comment.