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+
14+function someCollisionFunction(entityA, entityB, collision) {
15+ print("collision: " + JSON.stringify({a: entityA, b: entityB, c: collision}));
16+}
17+
18+var position = Vec3.sum(MyAvatar.position, Quat.getFront(MyAvatar.orientation));
19+var properties = {
20+ type: "Box",
21+ position: position,
22+ collisionsWillMove: true,
23+ color: { red: 200, green: 0, blue: 0 }
24+};
25+var collider = Entities.addEntity(properties);
26+var armed = false;
27+function 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+}
37+togglePrinting();
38+
39+properties.position.y += 0.2;
40+properties.color.blue += 200;
41+// A handy target for the collider to hit.
42+var target = Entities.addEntity(properties);
43+
44+properties.position.y += 0.2;
45+properties.color.green += 200;
46+var button = Entities.addEntity(properties);
47+Script.addEventHandler(button, "clickReleaseOnEntity", togglePrinting);
48+
49+Script.scriptEnding.connect(function () {
50+ Entities.deleteEntity(collider);
51+ Entities.deleteEntity(target);
52+ Entities.deleteEntity(button);
53+});