From 005cd2ba53a70c639b73f8299e72d9c2871d3420 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Mon, 26 Jan 2015 20:44:47 -0800 Subject: [PATCH] Add block building toolset, first version --- examples/blocks.js | 114 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 examples/blocks.js diff --git a/examples/blocks.js b/examples/blocks.js new file mode 100644 index 0000000000..69d4dbc4d4 --- /dev/null +++ b/examples/blocks.js @@ -0,0 +1,114 @@ +// +// Blocks.js +// +// Created by Philip Rosedale on January 26, 2015 +// Copyright 2015 High Fidelity, Inc. +// +// Create a bunch of building blocks and drop them onto a playing surface in front of you. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +var FLOOR_SIZE = 5.0; +var FLOOR_THICKNESS = 0.10; +var EDGE_THICKESS = 0.25; +var SCALE = 0.25; + +var GRAVITY = -1.0; +var LIFETIME = 6000; +var DAMPING = 0.50; + +var blockTypes = []; +blockTypes.push({ x: 1, y: 1, z: 1, red: 255, green: 0, blue: 0 }); +blockTypes.push({ x: 1, y: 1, z: 2, red: 0, green: 255, blue: 0 }); +blockTypes.push({ x: 1, y: 2, z: 5, red: 0, green: 0, blue: 255 }); +blockTypes.push({ x: 1, y: 2, z: 2, red: 255, green: 255, blue: 0 }); + + +var center = Vec3.sum(MyAvatar.position, Vec3.multiply(FLOOR_SIZE * 2.0, Quat.getFront(Camera.getOrientation()))); + +var floor = Entities.addEntity( + { type: "Box", + position: Vec3.subtract(center, { x: 0, y: SCALE / 2.0, z: 0 }), + dimensions: { x: FLOOR_SIZE, y: FLOOR_THICKNESS, z: FLOOR_SIZE }, + color: { red: 128, green: 128, blue: 128 }, + gravity: { x: 0, y: 0, z: 0 }, + ignoreCollisions: false, + locked: true, + lifetime: LIFETIME }); + +var edge1 = Entities.addEntity( + { type: "Box", + position: Vec3.sum(center, { x: FLOOR_SIZE / 2.0, y: FLOOR_THICKNESS / 2.0, z: 0 }), + dimensions: { x: EDGE_THICKESS, y: EDGE_THICKESS, z: FLOOR_SIZE }, + color: { red: 128, green: 128, blue: 128 }, + gravity: { x: 0, y: 0, z: 0 }, + ignoreCollisions: false, + visible: true, + lifetime: LIFETIME }); + +var edge2 = Entities.addEntity( + { type: "Box", + position: Vec3.sum(center, { x: -FLOOR_SIZE / 2.0, y: FLOOR_THICKNESS / 2.0, z: 0 }), + dimensions: { x: EDGE_THICKESS, y: EDGE_THICKESS, z: FLOOR_SIZE }, + color: { red: 128, green: 128, blue: 128 }, + gravity: { x: 0, y: 0, z: 0 }, + ignoreCollisions: false, + visible: true, + lifetime: LIFETIME }); + +var edge3 = Entities.addEntity( + { type: "Box", + position: Vec3.sum(center, { x: 0, y: FLOOR_THICKNESS / 2.0, z: -FLOOR_SIZE / 2.0 }), + dimensions: { x: FLOOR_SIZE, y: EDGE_THICKESS, z: EDGE_THICKESS }, + color: { red: 128, green: 128, blue: 128 }, + gravity: { x: 0, y: 0, z: 0 }, + ignoreCollisions: false, + visible: true, + lifetime: LIFETIME }); + +var edge4 = Entities.addEntity( + { type: "Box", + position: Vec3.sum(center, { x: 0, y: FLOOR_THICKNESS / 2.0, z: FLOOR_SIZE / 2.0 }), + dimensions: { x: FLOOR_SIZE, y: EDGE_THICKESS, z: EDGE_THICKESS }, + color: { red: 128, green: 128, blue: 128 }, + gravity: { x: 0, y: 0, z: 0 }, + ignoreCollisions: false, + visible: true, + lifetime: LIFETIME }); + +var NUM_BLOCKS = 20; +var DROP_HEIGHT = FLOOR_SIZE / 3; + +blocks = []; + +for (var i = 0; i < NUM_BLOCKS; i++) { + var which = Math.floor(Math.random() * blockTypes.length); + var type = blockTypes[which]; + blocks.push(Entities.addEntity( + { type: "Box", + position: { x: center.x + (Math.random() - 0.5) * (FLOOR_SIZE * 0.75), + y: center.y + DROP_HEIGHT, + z: center.z + (Math.random() - 0.5) * (FLOOR_SIZE * 0.75) }, + dimensions: { x: type.x * SCALE, y: type.y * SCALE, z: type.z * SCALE }, + color: { red: type.red, green: type.green, blue: type.blue }, + gravity: { x: 0, y: GRAVITY, z: 0 }, + ignoreCollisions: false, + damping: DAMPING, + lifetime: LIFETIME, + collisionsWillMove: true })); +} + +function scriptEnding() { + Entities.deleteEntity(edge1); + Entities.deleteEntity(edge2); + Entities.deleteEntity(edge3); + Entities.deleteEntity(edge4); + Entities.deleteEntity(floor); + + for (var i = 0; i < NUM_BLOCKS; i++) { + Entities.deleteEntity(blocks[i]); + } +} + +Script.scriptEnding.connect(scriptEnding); \ No newline at end of file