Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added saving to JSON in editor

  • Loading branch information...
commit 9e29e06f7d4dd1b0b0cfa4ce804639d471f282f9 1 parent 6444b12
Evan Wallace authored
View
3  editor/src/core/editor.js
@@ -278,7 +278,8 @@ Editor.prototype.redo = function() {
};
Editor.prototype.save = function() {
- // TODO
+ // TODO: this will be ajax
+ console.log(saveWorldToJSON(this.doc.world));
};
Editor.prototype.deleteSeleciton = function() {
View
120 editor/src/core/levelformat.js
@@ -24,6 +24,10 @@ function jsonToVec(json) {
return new Vector(json[0], json[1]);
}
+function vecToJSON(vec) {
+ return [vec.x, vec.y];
+}
+
function loadWorldFromJSON(json) {
var world = new World();
@@ -84,7 +88,121 @@ function loadWorldFromJSON(json) {
return world;
}
+function indicesOfLinkedDoors(button, world) {
+ // find all links linking to button
+ var links = [];
+ for (var i = 0; i < world.placeables.length; i++) {
+ var link = world.placeables[i];
+ if ((link instanceof Link) && link.button === button) {
+ links.push(link);
+ }
+ }
+
+ // find the indices of the door in each link
+ var indices = [];
+ for (var i = 0; i < links.length; i++) {
+ var link = links[i];
+ var index = 0;
+ for (var j = 0; j < world.placeables.length; j++) {
+ var door = world.placeables[j];
+ if (door instanceof Door) {
+ if (door === link.door) {
+ indices.push(index);
+ break;
+ }
+ index++;
+ }
+ }
+ }
+
+ return indices.sort();
+}
+
+function spriteTypeFromId(id) {
+ for (var key in enemyToSpriteMap) {
+ if (enemyToSpriteMap[key] == id) {
+ return key;
+ }
+ }
+}
+
function saveWorldToJSON(world) {
- var json = { todo: true };
+ var json = {};
+
+ // fit a bounding box around all non-blank cells
+ var min = new Vector(Number.MAX_VALUE, Number.MAX_VALUE);
+ var max = new Vector(-Number.MAX_VALUE, -Number.MAX_VALUE);
+ for (var i = 0; i < world.sectors.length; i++) {
+ var sector = world.sectors[i];
+ for (var y = 0; y < SECTOR_SIZE; y++) {
+ var sy = sector.offset.y * SECTOR_SIZE + y;
+ for (var x = 0; x < SECTOR_SIZE; x++) {
+ var sx = sector.offset.x * SECTOR_SIZE + x;
+ if (sector.cells[x + y * SECTOR_SIZE].type != CELL_SOLID) {
+ min = min.minComponents(new Vector(sx, sy));
+ max = max.maxComponents(new Vector(sx + 1, sy + 1));
+ }
+ }
+ }
+ }
+
+ // center empty levels at the origin
+ if (min.x == Number.MAX_VALUE) {
+ min.x = min.y = max.x = max.y = 0;
+ }
+
+ // copy the bounding box
+ json.cells = [];
+ json.width = max.x - min.x;
+ json.height = max.y - min.y;
+ for (var y = min.y; y < max.y; y++) {
+ var row = [];
+ for (var x = min.x; x < max.x; x++) {
+ row.push(world.getCell(x, y));
+ }
+ json.cells.push(row);
+ }
+
+ // save entities
+ json.entities = [];
+ for (var i = 0; i < world.placeables.length; i++) {
+ var p = world.placeables[i];
+ if (p instanceof Button) {
+ json.entities.push({
+ 'class': 'button',
+ 'type': p.type,
+ 'pos': vecToJSON(p.anchor.sub(min)),
+ 'walls': indicesOfLinkedDoors(p, world)
+ });
+ } else if (p instanceof Door) {
+ json.entities.push({
+ 'class': 'wall',
+ 'oneway': !!p.isOneWay,
+ 'open': !!p.isInitiallyOpen,
+ 'start': vecToJSON(p.edge.start.sub(min)),
+ 'end': vecToJSON(p.edge.end.sub(min)),
+ 'color': p.color
+ });
+ } else if ((p instanceof Sprite) && p.id == SPRITE_COG) {
+ json.entities.push({
+ 'class': 'cog',
+ 'pos': vecToJSON(p.anchor.sub(min))
+ });
+ } else if (p instanceof Sprite) {
+ json.entities.push({
+ 'class': 'enemy',
+ 'type': spriteTypeFromId(p.id),
+ 'pos': vecToJSON(p.anchor.sub(min)),
+ 'color': p.color,
+ 'angle': 0 // TODO
+ });
+ }
+ }
+
+ // save per-level stuff
+ json.unique_id = Math.round(Math.random() * 0xFFFFFFFF);
+ json.start = vecToJSON(world.playerStart.sub(min));
+ json.end = vecToJSON(world.playerGoal.sub(min));
+
return JSON.stringify(json);
}
View
41 editor/src/placeables/sprite.js
@@ -17,7 +17,8 @@ var SPRITE_STALACBAT = 15;
var SPRITE_WALL_AVOIDER = 16;
var SPRITE_COG = 17;
-function Sprite(radius, drawFunc, anchor, color) {
+function Sprite(id, radius, drawFunc, anchor, color) {
+ this.id = id;
this.radius = radius;
this.drawFunc = drawFunc;
this.anchor = anchor || new Vector(0, 0);
@@ -54,7 +55,7 @@ Sprite.prototype.resetAnchor = function() {
};
Sprite.prototype.clone = function(newAnchor, newColor) {
- return new Sprite(this.radius, this.drawFunc, newAnchor, newColor);
+ return new Sprite(this.id, this.radius, this.drawFunc, newAnchor, newColor);
};
Sprite.prototype.getCenter = function() {
@@ -67,26 +68,26 @@ function todo(c, alpha) {
var spriteTemplates = [
// color-neutral enemies
- { name: 'Bomber', sprite: new Sprite(0.3, function(c, alpha) { Sprites.drawBomber(c, alpha, 0.7); }) },
- { name: 'Doom Magnet', sprite: new Sprite(0.35, function(c, alpha) { Sprites.drawDoomMagnet(c, alpha); }) },
- { name: 'Hunter', sprite: new Sprite(0.3, function(c, alpha) { Sprites.drawHunter(c, alpha); }) },
- { name: 'Multi-Gun', sprite: new Sprite(0.45, function(c, alpha) { Sprites.drawMultiGun(c, alpha); }) },
- { name: 'Popper', sprite: new Sprite(0.5, function(c, alpha) { Sprites.drawPopper(c, alpha); }) },
- { name: 'Jet Stream', sprite: new Sprite(0.45, function(c, alpha) { Sprites.drawRiotGun(c, alpha, 0.75, Math.PI / 2); }) },
- { name: 'Rocket Spider', sprite: new Sprite(0.5, function(c, alpha) { Sprites.drawSpider(c, alpha); }) },
- { name: 'Spike Ball', sprite: new Sprite(0.3, function(c, alpha) { Sprites.drawSpikeBall(c, alpha); }) },
- { name: 'Wall Crawler', sprite: new Sprite(0.25, function(c, alpha) { Sprites.drawWallCrawler(c, alpha); }) },
- { name: 'Wheeligator', sprite: new Sprite(0.3, function(c, alpha) { Sprites.drawWheeligator(c, alpha); }) },
+ { name: 'Bomber', sprite: new Sprite(SPRITE_BOMBER, 0.3, function(c, alpha) { Sprites.drawBomber(c, alpha, 0.7); }) },
+ { name: 'Doom Magnet', sprite: new Sprite(SPRITE_DOOM_MAGNET, 0.35, function(c, alpha) { Sprites.drawDoomMagnet(c, alpha); }) },
+ { name: 'Hunter', sprite: new Sprite(SPRITE_HUNTER, 0.3, function(c, alpha) { Sprites.drawHunter(c, alpha); }) },
+ { name: 'Multi-Gun', sprite: new Sprite(SPRITE_MULTI_GUN, 0.45, function(c, alpha) { Sprites.drawMultiGun(c, alpha); }) },
+ { name: 'Popper', sprite: new Sprite(SPRITE_POPPER, 0.5, function(c, alpha) { Sprites.drawPopper(c, alpha); }) },
+ { name: 'Jet Stream', sprite: new Sprite(SPRITE_JET_STREAM, 0.45, function(c, alpha) { Sprites.drawRiotGun(c, alpha, 0.75, Math.PI / 2); }) },
+ { name: 'Rocket Spider', sprite: new Sprite(SPRITE_ROCKET_SPIDER, 0.5, function(c, alpha) { Sprites.drawSpider(c, alpha); }) },
+ { name: 'Spike Ball', sprite: new Sprite(SPRITE_SPIKE_BALL, 0.3, function(c, alpha) { Sprites.drawSpikeBall(c, alpha); }) },
+ { name: 'Wall Crawler', sprite: new Sprite(SPRITE_WALL_CRAWLER, 0.25, function(c, alpha) { Sprites.drawWallCrawler(c, alpha); }) },
+ { name: 'Wheeligator', sprite: new Sprite(SPRITE_WHEELIGATOR, 0.3, function(c, alpha) { Sprites.drawWheeligator(c, alpha); }) },
// color-specific enemies
- { name: 'Bouncy Rockets', sprite: new Sprite(0.3, function(c, alpha, color) { Sprites.drawBouncyRocketLauncher(c, alpha, color == 1); }) },
- { name: 'Corrosion Cloud', sprite: new Sprite(0.5, function(c, alpha, color) { Sprites.drawCloud(c, alpha, color == 1); }) },
- { name: 'Grenadier', sprite: new Sprite(0.35, function(c, alpha, color) { Sprites.drawGrenadier(c, alpha, color == 1); }) },
- { name: 'Headache', sprite: new Sprite(0.5, todo) },
- { name: 'Shock Hawk', sprite: new Sprite(0.3, function(c, alpha, color) { Sprites.drawShockHawk(c, alpha, color == 1); }) },
- { name: 'Stalacbat', sprite: new Sprite(0.2, function(c, alpha, color) { Sprites.drawStalacbat(c, alpha, color == 1); }) },
- { name: 'Wall Avoider', sprite: new Sprite(0.3, function(c, alpha, color) { Sprites.drawWallAvoider(c, alpha, color == 1); }) },
+ { name: 'Bouncy Rockets', sprite: new Sprite(SPRITE_BOUNCY_ROCKET_LAUNCHER, 0.3, function(c, alpha, color) { Sprites.drawBouncyRocketLauncher(c, alpha, color == 1); }) },
+ { name: 'Corrosion Cloud', sprite: new Sprite(SPRITE_CORROSION_CLOUD, 0.5, function(c, alpha, color) { Sprites.drawCloud(c, alpha, color == 1); }) },
+ { name: 'Grenadier', sprite: new Sprite(SPRITE_GRENADIER, 0.35, function(c, alpha, color) { Sprites.drawGrenadier(c, alpha, color == 1); }) },
+ { name: 'Headache', sprite: new Sprite(SPRITE_HEADACHE, 0.5, todo) },
+ { name: 'Shock Hawk', sprite: new Sprite(SPRITE_SHOCK_HAWK, 0.3, function(c, alpha, color) { Sprites.drawShockHawk(c, alpha, color == 1); }) },
+ { name: 'Stalacbat', sprite: new Sprite(SPRITE_STALACBAT, 0.2, function(c, alpha, color) { Sprites.drawStalacbat(c, alpha, color == 1); }) },
+ { name: 'Wall Avoider', sprite: new Sprite(SPRITE_WALL_AVOIDER, 0.3, function(c, alpha, color) { Sprites.drawWallAvoider(c, alpha, color == 1); }) },
// game objects
- { name: 'Cog', sprite: new Sprite(0.25, function(c, alpha) { Sprites.drawCog(c, alpha, 0.25); }) }
+ { name: 'Cog', sprite: new Sprite(SPRITE_COG, 0.25, function(c, alpha) { Sprites.drawCog(c, alpha, 0.25); }) }
];
View
11 editor/www/index.html
@@ -6,7 +6,7 @@
<script src="editor.js" type="text/javascript"></script>
<link type="text/css" rel="stylesheet" href="style.css" />
- <!-- this is just for testing -->
+ <!-- this is just for testing, TODO: remove this -->
<script src="level.js" type="text/javascript"></script>
</head><body>
<div id="toolbar">
@@ -27,14 +27,15 @@
<a id="mode_enemies" class="first">Enemies
</a><a id="mode_walls_buttons">Walls / Buttons
</a><a id="mode_cog">Cog
- </a><a id="mode_sign">Sign
- </a><a id="mode_select" class="last">Select
+ </a><a id="mode_sign" class="last">Sign
</a>
<div class="label">Objects</div>
</div>
<div class="section">
- <a id="mode_help" class="first last">Help</a>
- <div class="label">Help</div>
+ <a id="mode_select" class="first">Select
+ </a><a id="mode_help" class="last">Help
+ </a>
+ <div class="label">Other</div>
</div>
</div>
<div id="loading">Loading...</div>
View
10 level_converter/src/main.cpp
@@ -77,13 +77,7 @@ class JSON
template <typename X, typename Y> JSON &value(X x, Y y)
{
- if (in_array()) newline_indent();
- *o << '[';
- val(o, x);
- *o << "," << (pack ? "" : " ");
- val(o, y);
- *o << ']';
- return *this;
+ return array(true).value(x).value(y).end();
}
JSON &array(bool packed)
@@ -178,7 +172,7 @@ void convert(const string &input, const string &output, bool pack)
json.name("class").value("button");
json.name("type").value(button.behavior());
json.name("pos").value(button.position_x(), button.position_y());
- json.name("walls").array(false);
+ json.name("walls").array(true);
for (int j = 0; j < button.door_index_size(); j++)
json.value(button.door_index(j));
json.end();
View
9 rails/app/views/levels/edit.html.erb
@@ -18,14 +18,15 @@
<a id="mode_enemies" class="first">Enemies
</a><a id="mode_walls_buttons">Walls / Buttons
</a><a id="mode_cog">Cog
- </a><a id="mode_sign">Sign
- </a><a id="mode_select" class="last">Select
+ </a><a id="mode_sign" class="last">Sign
</a>
<div class="label">Objects</div>
</div>
<div class="section">
- <a id="mode_help" class="first last">Help</a>
- <div class="label">Help</div>
+ <a id="mode_select" class="first">Select
+ </a><a id="mode_help" class="last">Help
+ </a>
+ <div class="label">Other</div>
</div>
</div>
<div id="loading">Loading...</div>

0 comments on commit 9e29e06

Please sign in to comment.
Something went wrong with that request. Please try again.