Permalink
Browse files

Preliminary object rendering

  • Loading branch information...
1 parent fad7b47 commit d97025da4651a6fb6c34ce662adf2bb27c9a6437 Blixt committed Mar 2, 2014
Showing with 80 additions and 4 deletions.
  1. +80 −4 lib/regionrenderer.js
View
84 lib/regionrenderer.js
@@ -21,6 +21,50 @@ function getInt16(region, offset) {
if (region && region.view) return region.view.getInt16(offset);
}
+function getOrientation(orientations, index) {
+ var curIndex = 0, image, direction;
+
+ // This is a tremendous amount of logic for deciding which image to use...
+ for (var i = 0; i < orientations.length; i++) {
+ var o = orientations[i];
+ if (curIndex == index) {
+ if (o.imageLayers) {
+ // TODO: Support multiple layers.
+ image = o.imageLayers[0].image;
+ } else {
+ image = o.image || o.leftImage || o.dualImage;
+ }
+ direction = o.direction || 'left';
+ if (!image) throw new Error('Could not get image for orientation');
+ break;
+ }
+
+ curIndex++;
+
+ if (o.dualImage || o.rightImage) {
+ if (curIndex == index) {
+ image = o.rightImage || o.dualImage;
+ direction = 'right';
+ break;
+ }
+
+ curIndex++;
+ }
+ }
+
+ if (!image) {
+ console.error(index, orientations);
+ throw new Error('Could not get orientation');
+ }
+
+ return {
+ image: image,
+ direction: direction,
+ flip: o.flipImages || !!(o.dualImage && direction == 'left'),
+ info: o
+ };
+}
+
function getUint8(region, offset) {
if (region && region.view) return region.view.getUint8(offset);
}
@@ -143,12 +187,44 @@ RegionRenderer.prototype._renderNPC = function (renderer, entity) {
RegionRenderer.prototype._renderObject = function (renderer, entity) {
var objects = renderer.objects.index;
- if (!objects) {
- this.dirty = true;
- return;
+ if (!objects) return;
+
+ var assets = renderer.assets;
+ var def = objects[entity.name];
+
+ if (def.animation) {
+ var animationPath = assets.getResourcePath(def, def.animation);
+ // TODO: assets.getAnimation(animationPath);
}
- // TODO: Render object.
+ var orientation = getOrientation(def.orientations, entity.orientationIndex);
+
+ var pathAndFrame = orientation.image.split(':');
+ var imagePath = assets.getResourcePath(def, pathAndFrame[0]);
+ var frames = assets.getFrames(imagePath);
+
+ // Flip all the frames horizontally if the sprite is using a dual image.
+ if (orientation.flip) {
+ if (!frames) return;
+ imagePath += '?flipgridx=' + frames.frameGrid.size[0];
+ }
+
+ var image = assets.getImage(imagePath);
+ if (!frames || !image) return;
+
+ // TODO: Get the correct frame in the frame grid.
+
+ var sprite = {
+ image: image,
+ x: entity.tilePosition[0] + orientation.info.imagePosition[0] / TILE_WIDTH,
+ y: entity.tilePosition[1] + orientation.info.imagePosition[1] / TILE_HEIGHT,
+ sx: 0,
+ sy: 0,
+ width: frames.frameGrid.size[0],
+ height: frames.frameGrid.size[1]
+ };
+
+ return [sprite];
};
RegionRenderer.prototype._renderPlant = function (renderer, entity) {

0 comments on commit d97025d

Please sign in to comment.