History

Added enity script for balloon spawner and sample json entity with userData to attach script to

Johnathan Franck committed 9 years ago

Commit bc7ea111 parent 8507fff2 files · +201 −0Browse files

Showing the changes to scripts/tutorials/entity_scripts (2 of 2 files). Show all

Unified · Split

2 files
  1. scripts/tutorials/entity_scripts/balloonSpawner/balloonSpawner.json +20 −0
  2. scripts/tutorials/entity_scripts/balloonSpawner/spawnBalloons.js +181 −0
scripts/tutorials/entity_scripts/balloonSpawner/balloonSpawner.json+20−0
@@ -0,0 +1,20 @@
1+{
2+ "Entities": [
3+ {
4+ "description": "Spawns balloons",
5+ "dimensions": {
6+ "x": 2,
7+ "y": 0.1,
8+ "z": 2
9+ },
10+ "name": "Balloon Spawner",
11+ "serverScripts": "https://s3.amazonaws.com/hifi-rabelaiis/spawnBalloons.js",
12+ "type": "Shape",
13+ "shape": "Cube",
14+ "visible": "false",
15+ "collisionless": "true",
16+ "userData": "{\"spawnRate\":1000,\"xRangeMax\":1,\"zRangeMax\":1,\"gravityCoefficient\":0.25,\"balloonLifetime\":10,\"spawnTime\":300}"
17+ }
18+ ],
19+ "Version": 63
20+}
scripts/tutorials/entity_scripts/balloonSpawner/spawnBalloons.js+181−0
@@ -0,0 +1,181 @@
1+"use strict";
2+
3+//
4+// spawnBalloons.js
5+//
6+// Created by Johnathan Franck on 3 May 2017.
7+// Copyright 2017 High Fidelity, Inc.
8+//
9+// Distributed under the Apache License, Version 2.0.
10+// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11+//
12+
13+(function() {
14+ var RED_BALLOON_URL = "https://s3.amazonaws.com/hifi-rabelaiis/redBalloon.fbx";
15+ var BLUE_BALLOON_URL = "https://s3.amazonaws.com/hifi-rabelaiis/blueBalloon.fbx";
16+ var GREEN_BALLOON_URL = "https://s3.amazonaws.com/hifi-rabelaiis/greenBalloon.fbx";
17+ var YELLOW_BALLOON_URL = "https://s3.amazonaws.com/hifi-rabelaiis/yellowBalloon.fbx";
18+ var ORANGE_BALLOON_URL = "https://s3.amazonaws.com/hifi-rabelaiis/orangeBalloon.fbx";
19+ var CYAN_BALLOON_URL = "https://s3.amazonaws.com/hifi-rabelaiis/cyanBalloon.fbx";
20+ var PURPLE_BALLOON_URL = "https://s3.amazonaws.com/hifi-rabelaiis/purpleBalloon.fbx";
21+
22+ var BALLOON_COLORS = ["red", "blue", "green", "yellow", "orange", "cyan", "purple"];
23+ var BALLOON_URLS = [RED_BALLOON_URL, BLUE_BALLOON_URL, GREEN_BALLOON_URL,
24+ YELLOW_BALLOON_URL, ORANGE_BALLOON_URL, CYAN_BALLOON_URL, PURPLE_BALLOON_URL];
25+
26+ var NUM_COLORS = 7;
27+ var COUNTDOWN_SECONDS = 9;
28+
29+ var spawnRate = 2000;
30+ var xRangeMax = 1;
31+ var zRangeMax = 1;
32+ var gravityCoefficient = 0.25;
33+ var balloonLifetime = 10;
34+ var spawnTime = 300;
35+ var spawnIntervalID;
36+ var countdownIntervalID;
37+ var countdownEntityID;
38+
39+ this.preload = function(pEntityID) {
40+ startCountdown(pEntityID);
41+ };
42+
43+ function startCountdown(pEntityID) {
44+ var countdownSeconds = COUNTDOWN_SECONDS,
45+ parentProperties = Entities.getEntityProperties(pEntityID, ["position"]),
46+ countdownEntityProperties;
47+
48+ countdownEntityProperties = {
49+ type: "Text",
50+ text: countdownSeconds,
51+ lineHeight: 0.71,
52+ dimensions: {
53+ x: 0.5,
54+ y: 1,
55+ z: 0.01
56+ },
57+ textColor: {
58+ red: 0,
59+ blue: 255,
60+ green: 0
61+ },
62+ backgroundColor: {
63+ red: 255,
64+ blue: 255,
65+ green: 255
66+ },
67+ parentID: pEntityID,
68+ position: parentProperties.position
69+ };
70+
71+ countdownEntityID = Entities.addEntity(countdownEntityProperties);
72+ countdownIntervalID = Script.setInterval(function () {
73+ countdownSeconds -= 1;
74+ if (countdownSeconds < 0) {
75+ Script.clearInterval(countdownIntervalID);
76+ Entities.deleteEntity(countdownEntityID);
77+ } else {
78+ Entities.editEntity(countdownEntityID, {"text": countdownSeconds});
79+ if (countdownSeconds === 0) {
80+ spawnBalloons(pEntityID);
81+ }
82+ }
83+ }, 1000);
84+ }
85+
86+ function spawnBalloons(pEntityID) {
87+ var parentProperties = Entities.getEntityProperties(pEntityID, ["position", "userData"]),
88+ spawnerSettings,
89+ spawnCount = 0;
90+
91+ if (parentProperties.userData){
92+ spawnerSettings = JSON.parse(parentProperties.userData);
93+ }
94+
95+ xRangeMax = !isNaN(spawnerSettings.xRangeMax) ? spawnerSettings.xRangeMax : xRangeMax;
96+ zRangeMax = !isNaN(spawnerSettings.zRangeMax) ? spawnerSettings.zRangeMax : zRangeMax;
97+ gravityCoefficient = !isNaN(spawnerSettings.gravityCoefficient) ? spawnerSettings.gravityCoefficient : gravityCoefficient;
98+ spawnTime = !isNaN(spawnerSettings.spawnTime) ? spawnerSettings.spawnTime : spawnTime;
99+ balloonLifetime = !isNaN(spawnerSettings.balloonLifetime) ? spawnerSettings.balloonLifetime : balloonLifetime;
100+ spawnRate = !isNaN(spawnerSettings.spawnRate) ? spawnerSettings.spawnRate : spawnRate;
101+
102+ spawnIntervalID = Script.setInterval(function() {
103+ var colorID = Math.floor(Math.random() * NUM_COLORS),
104+ color = BALLOON_COLORS[colorID],
105+ balloonURL = BALLOON_URLS[colorID],
106+ balloonPosition = {},
107+ balloonProperties;
108+
109+ spawnCount ++;
110+ //Randomize balloon spawn position
111+ balloonPosition.y = parentProperties.position.y + 0.5;
112+ balloonPosition.x = parentProperties.position.x + (Math.random() - 0.5) * 2 * xRangeMax;
113+ balloonPosition.z = parentProperties.position.z + (Math.random() - 0.5) * 2 * zRangeMax;
114+
115+ balloonProperties = {
116+ position: balloonPosition,
117+ lifetime: balloonLifetime,
118+ angularVelocity: {
119+ x: -0.03654664754867554,
120+ y: -0.4030083637684583664 + Math.random(),
121+ z: 0.02576472796499729
122+ },
123+ collisionsWillMove: 1,
124+ density: 100,
125+ description: "A happy " + color + " balloon",
126+ dimensions: {
127+ x: 0.3074322044849396,
128+ y: 0.40930506587028503,
129+ z: 0.30704551935195923
130+ },
131+ dynamic: 1,
132+ gravity: {
133+ x: 0,
134+ y: gravityCoefficient,
135+ z: 0
136+ },
137+ modelURL: balloonURL,
138+ name: color + " balloon",
139+ queryAACube: {
140+ scale: 0.596927285194397,
141+ x: -0.2984636425971985,
142+ y: -0.2984636425971985,
143+ z: -0.2984636425971985
144+ },
145+ restitution: 0.9900000095367432,
146+ rotation: {
147+ w: -0.10368101298809052,
148+ x: 0.5171623826026917,
149+ y: 0.1211432576179504,
150+ z: -0.670971691608429 + Math.random()
151+ },
152+ shapeType: "sphere",
153+ type : "Model",
154+ velocity: {
155+ x: 0.003623033408075571,
156+ y: 0.0005839366931468248,
157+ z: -0.01512028019875288
158+ }
159+ };
160+ Entities.addEntity(balloonProperties);
161+
162+ //Stop spawning after spawnTime
163+ if (spawnCount * spawnRate / 1000 > spawnTime){
164+ Script.clearInterval(spawnIntervalID);
165+ }
166+ }, spawnRate);
167+ }
168+
169+ this.unload = function(){
170+ if (spawnIntervalID){
171+ Script.clearInterval(spawnIntervalID);
172+ }
173+ if (countdownIntervalID){
174+ Script.clearInterval(countdownIntervalID);
175+ }
176+ if (countdownEntityID){
177+ Entities.deleteEntity(countdownEntityID);
178+ }
179+ };
180+
181+});