Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add non-player bodies to world.

  • Loading branch information...
commit 95408633ac047aec726f751a0d31ec0bd4e3ccd3 1 parent c0e5429
@kpreid authored
View
4 body.js
@@ -24,6 +24,7 @@
this.yaw = Math.PI/4 * 5;
this.flying = false;
this.noclip = false;
+ this.isPlayerBody = false;
// non-persisted properties
this.world = world;
@@ -43,6 +44,8 @@
json.flying = this.flying;
if (this.noclip !== false)
json.noclip = this.noclip;
+ if (this.isPlayerBody !== false)
+ json.isPlayerBody = this.isPlayerBody;
return json;
};
@@ -54,6 +57,7 @@
if ("yaw" in json) body.yaw = +json.yaw;
if ("flying" in json) body.flying = !!json.flying;
if ("noclip" in json) body.noclip = !!json.noclip;
+ if ("isPlayerBody" in json) body.isPlayerBody = !!json.isPlayerBody;
return body;
};
View
5 player.js
@@ -47,7 +47,7 @@
this.selection = null;
// find or make body
- var body = world.playerBody;
+ var body = world.getPlayerBody();
if (body) {
this.bodyIsWorldly = true;
//console.log("Found existing body at " + vec3.str(body.pos));
@@ -127,6 +127,9 @@
currentPlace.body.debugHitAABBs.forEach(function (aabb) {
draw(ZEROVEC, aabb, [0,1,0]);
});
+ currentPlace.body.world.forEachBody(function (body) {
+ draw(body.pos, body.aabb, [1,0,0]);
+ });
}
});
View
9 test/test-world.js
@@ -38,12 +38,13 @@ describe("World", function() {
// TODO test that it dirties
});
- it("should step the playerBody", function () {
+ it("should step the bodies", function () {
var world = new World([1, 1, 1], new Blockset([]));
- world.playerBody = new Body(world, Player.aabb);
+ var body = new Body(world, Player.aabb);
+ world.addBody(body); // TODO proper add/remove interface
world.step(1/60);
- expect(world.playerBody.vel[1]).toBeLessThan(0);
- expect(world.playerBody.pos[1]).toBeLessThan(0);
+ expect(body.vel[1]).toBeLessThan(0);
+ expect(body.pos[1]).toBeLessThan(0);
});
});
View
12 world-gen.js
@@ -4,6 +4,7 @@
(function () {
"use strict";
+ var AAB = cubes.util.AAB;
var abs = Math.abs;
var Blockset = cubes.Blockset;
var BlockType = cubes.BlockType;
@@ -1117,10 +1118,19 @@
}());
var playerBody = new Body(topWorld, Player.aabb, null);
+ playerBody.isPlayerBody = true;
playerBody.pos[0] = topWorld.wx/2;
playerBody.pos[1] = topWorld.wy;
playerBody.pos[2] = topWorld.wz/2;
- topWorld.playerBody = playerBody;
+ topWorld.addBody(playerBody);
+
+ var testBodyAABB = new AAB(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5);
+ for (var tbi = 0; tbi < 10; tbi++) {
+ var body = new Body(topWorld, testBodyAABB);
+ vec3.set([wx/2, wy, wz/2], body.pos);
+ vec3.set([Math.random(), Math.random(), Math.random()], body.vel);
+ topWorld.addBody(body);
+ }
return topWorld;
}
View
83 world.js
@@ -99,7 +99,7 @@
// Blocks which a body is touching. Values are of the form {facevector: true}.
var contacts = new IntVectorMap();
- var playerBody = null;
+ var bodies = [];
var numToDisturbPerSec = cubeCount * spontaneousBaseRate;
@@ -539,10 +539,14 @@
evaluateLightsInQueue();
- if (playerBody) {
- playerBody.step(timestep, function () {
- self.persistence.dirty();
- });
+ var someBodyChanged = false;
+ function signal() { someBodyChanged = true; }
+ for (var bi = bodies.length - 1; bi >= 0; bi--) {
+ var body = bodies[bi];
+ body.step(timestep, signal);
+ }
+ if (someBodyChanged) {
+ self.persistence.dirty();
}
}
@@ -711,6 +715,31 @@
notifier.notify("transientEvent", cube, gt(cube[0],cube[1],cube[2]), mode);
}
+ function addBody(body) {
+ if (bodies.indexOf(body) !== -1) return;
+ if (body.world !== self && body.world !== null) {
+ throw new Error("the provided body already belongs to another world");
+ }
+ body.world = self;
+ bodies.push(body);
+ self.persistence.dirty();
+ }
+ function forEachBody(f) {
+ bodies.forEach(function (body) {
+ f(body);
+ });
+ }
+ function getPlayerBody() {
+ // TODO optimize?
+ var playerBody = null;
+ bodies.forEach(function (candidate) {
+ if (candidate.isPlayerBody && !playerBody) {
+ playerBody = candidate;
+ }
+ });
+ return playerBody;
+ }
+
var RLE_BASE = 0xA1;
function rleBytes(bytes) {
var ser = [];
@@ -744,7 +773,7 @@
blocks: rleBytes(blocks),
subData: rleBytes(subData),
lightCache: rleBytes(lighting),
- playerBody: playerBody ? subSerialize(playerBody) : null
+ bodies: bodies.map(subSerialize)
};
subSerialize.setUnserializer(json, World);
return json;
@@ -783,14 +812,18 @@
this.getCircuits = function () { return circuits; }; // TODO should be read-only interface
this.getCircuit = function (block) { return blockCircuits.get(block) || null; };
this.edit = edit;
-
+
+ this.addBody = addBody;
+ this.forEachBody = forEachBody;
+ this.getPlayerBody = getPlayerBody;
+
this.step = step;
this.polishLightInVicinity = polishLightInVicinity;
-
+
this.setContacts = setContacts;
this.getContacts = getContacts;
this.transientEvent = transientEvent;
-
+
this.listen = notifier.listen;
this.serialize = serialize;
@@ -814,26 +847,6 @@
self.persistence.dirty();
}
}
- },
- playerBody: {
- enumerable: true,
- get: function () {
- return playerBody;
- },
- set: function (body) {
- if (body === playerBody) return;
- if (playerBody) {
- playerBody.world = null;
- }
- if (body !== null) {
- if (body.world !== self && body.world !== null) {
- throw new Error("the provided body already belongs to another world");
- }
- body.world = self;
- }
- playerBody = body;
- self.persistence.dirty();
- }
}
});
@@ -858,11 +871,21 @@
}
var world = new World([json.wx, json.wy, json.wz], unserialize(json.blockset || json.blockSet, Blockset));
+
unrleBytes(json.blocks, world.raw);
unrleBytes(json.subData, world.rawSubData);
unrleBytes(json.lightCache, world.rawLighting);
world.notifyRawEdit();
- world.playerBody = json.playerBody ? unserialize(json.playerBody) : null;
+
+ (json.bodies || []).forEach(function (bodyJson) {
+ world.addBody(unserialize(bodyJson));
+ });
+ if (json.playerBody) { // obsolete serialization
+ var body = unserialize(json.playerBody);
+ body.isPlayerBody = true;
+ world.addBody(body);
+ }
+
return world;
};
Please sign in to comment.
Something went wrong with that request. Please try again.