History

Deprecate globalCollisionsExample.js, and add entityCollisionExample.js

Howard Stearns committed 11 years ago

Commit 09e9d351 parent c9932a52 files · +56 −14Browse files

Showing the changes to examples/example/entityCollisionExample.js (1 of 2 files). Show all

Unified · Split

examples/example/entityCollisionExample.js+53−0
@@ -0,0 +1,53 @@
1//
2// globalCollisionsExample.js
3// examples
4//
5// Created by Brad Hefta-Gaub on 1/29/14.
6// Copyright 2014 High Fidelity, Inc.
7//
8// This is an example script that demonstrates use of the Controller class
9//
10// Distributed under the Apache License, Version 2.0.
11// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
12//
13
14function someCollisionFunction(entityA, entityB, collision) {
15 print("collision: " + JSON.stringify({a: entityA, b: entityB, c: collision}));
16}
17
18var position = Vec3.sum(MyAvatar.position, Quat.getFront(MyAvatar.orientation));
19var properties = {
20 type: "Box",
21 position: position,
22 collisionsWillMove: true,
23 color: { red: 200, green: 0, blue: 0 }
24};
25var collider = Entities.addEntity(properties);
26var armed = false;
27function togglePrinting() {
28 print('togglePrinting from ' + armed + ' on ' + collider);
29 if (armed) {
30 Script.removeEventHandler(collider, "collisionWithEntity", someCollisionFunction);
31 } else {
32 Script.addEventHandler(collider, "collisionWithEntity", someCollisionFunction);
33 }
34 armed = !armed;
35 print("Red box " + (armed ? "will" : "will not") + " print on collision.");
36}
37togglePrinting();
38
39properties.position.y += 0.2;
40properties.color.blue += 200;
41// A handy target for the collider to hit.
42var target = Entities.addEntity(properties);
43
44properties.position.y += 0.2;
45properties.color.green += 200;
46var button = Entities.addEntity(properties);
47Script.addEventHandler(button, "clickReleaseOnEntity", togglePrinting);
48
49Script.scriptEnding.connect(function () {
50 Entities.deleteEntity(collider);
51 Entities.deleteEntity(target);
52 Entities.deleteEntity(button);
53});