Merge branch 'baseball' of github.com:huffman/hifi into baseball

This commit is contained in:
Atlante45 2015-11-03 17:45:43 -08:00
commit 04f4e49158
4 changed files with 197 additions and 9 deletions

View file

@ -2,11 +2,11 @@
Script.include("pitching.js");
var pitchingMachine = null;
this.startNearGrab = function() {
print("Started near grab!");
if (!pitchingMachine) {
getPitchingMachine();
pitchingMachine = getPitchingMachine();
Script.update.connect(function(dt) { pitchingMachine.update(dt); });
}
print("Started near grab!");
pitchingMachine.start();
MyAvatar.shouldRenderLocally = false;
};

View file

@ -0,0 +1,144 @@
Script.include(Script.resolvePath("utils.js"));
var emitters = [];
var smokeTrailSettings = {
"name":"ParticlesTest Emitter",
"type": "ParticleEffect",
"color":{"red":205,"green":84.41176470588235,"blue":84.41176470588235},
"maxParticles":1000,
"velocity": { x: 0, y: 18.0, z: 0 },
"lifetime": 20,
"lifespan":3,
"emitRate":1000,
"emitSpeed":0.5,
"speedSpread":1,
"emitOrientation":{"x":-0.2,"y":0,"z":0,"w":0.7000000000000001},
"emitDimensions":{"x":0,"y":0,"z":0},
"emitRadiusStart":0.5,
"polarStart":1,
"polarFinish":1,
"azimuthStart":-Math.PI,
"azimuthFinish":Math.PI,
"emitAcceleration":{"x":0,"y":-0.70000001192092896,"z":0},
"accelerationSpread":{"x":0,"y":0,"z":0},
"particleRadius":0.03999999910593033,
"radiusSpread":0,
"radiusStart":0.13999999910593033,
"radiusFinish":0.14,
"colorSpread":{"red":0,"green":0,"blue":0},
"colorStart":{"red":255,"green":255,"blue":255},
"colorFinish":{"red":255,"green":255,"blue":255},
"alpha":1,
"alphaSpread":0,
"alphaStart":0,
"alphaFinish":1,
"textures":"https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png"
};
var fireworkSettings = {
"name":"ParticlesTest Emitter",
"type": "ParticleEffect",
"color":{"red":205,"green":84.41176470588235,"blue":84.41176470588235},
"maxParticles":1000,
"lifetime": 20,
"lifespan":6,
"emitRate":1000,
"emitSpeed":1.5,
"speedSpread":1.0,
"emitOrientation":{"x":-0.2,"y":0,"z":0,"w":0.7000000000000001},
"emitDimensions":{"x":0,"y":0,"z":0},
"emitRadiusStart":0.5,
"polarStart":1,
"polarFinish":1.2,
"azimuthStart":-Math.PI,
"azimuthFinish":Math.PI,
"emitAcceleration":{"x":0,"y":-0.70000001192092896,"z":0},
"accelerationSpread":{"x":0,"y":0,"z":0},
"particleRadius":0.03999999910593033,
"radiusSpread":0,
"radiusStart":0.13999999910593033,
"radiusFinish":0.14,
"colorSpread":{"red":0,"green":0,"blue":0},
"colorStart":{"red":255,"green":255,"blue":255},
"colorFinish":{"red":255,"green":255,"blue":255},
"alpha":1,
"alphaSpread":0,
"alphaStart":0,
"alphaFinish":1,
"textures":"https://hifi-public.s3.amazonaws.com/alan/Particles/spark_2.png",
//"textures": "https://hifi-public.s3.amazonaws.com/alan/Particles/spark.png"
};
var popSounds = getSounds([
"atp:a2bf79c95fe74c2c6c9188acc7230f7cd1b0f6008f2c81954ecd93eca0497ec6.wav",
"atp:901067ebc2cda4c0d86ec02fcca2ed901e85f9097ad68bbde78b4cad8eaf2ed7.wav",
"atp:830312930577cb1ea36ba2d743e957debbacceb441b20addead5a6faa05a3771.wav",
"atp:62e80d0a9f084cf731bcc66ca6e9020ee88587417071a281eee3167307b53560.wav"
]);
var fireSounds = getSounds([
"atp:ee6afe565576c4546c6d6cd89c1af532484c9b60ab30574d6b40c2df022f7260.wav",
"atp:91ef19ba1c78be82d3fd06530cd05ceb90d1e75f4204c66819c208c55da049ef.wav",
"atp:ee56993daf775012cf49293bfd5971eec7e5c396642f8bfbea902ba8f47b56cd.wav",
"atp:37775d267f00f82242a7e7f61f3f3d7bf64a54c5a3799e7f2540fa5f6b79bd02.wav"
])
function playRandomSound(sounds, options) {
if (options === undefined) {
options = {
volume: 1.0,
position: MyAvatar.position,
}
}
Audio.playSound(sounds[randomInt(sounds.length)], options);
}
function shootFirework(position, color, options) {
smokeTrailSettings.position = position;
smokeTrailSettings.velocity = randomVec3(-5, 5, 10, 20, 10, 15);
smokeTrailSettings.gravity = randomVec3(-5, 5, -9.8, -9.8, 20, 40);
playRandomSound(fireSounds, { position: {x: 0, y: 0 , z: 0}, volume: 3.0 });
var smokeID = Entities.addEntity(smokeTrailSettings);
emitters.push(smokeID);
Script.scriptEnding.connect(function() {
Entities.deleteEntity(smokeID);
});
Script.setTimeout(function() {
Entities.editEntity(smokeID, { emitRate: 0 });
var position = Entities.getEntityProperties(smokeID, ['position']).position;
Vec3.print("pos", position);
options.position = position;
options.colorStart = color;
options.colorFinish = color;
var id = Entities.addEntity(options);
emitters.push(id);
playRandomSound(popSounds, { position: {x: 0, y: 0 , z: 0}, volume: 3.0 });
Script.setTimeout(function() {
Entities.editEntity(id, { emitRate: 0 });
Entities.deleteEntity(smokeID);
}, 500);
}, 2000);
}
Script.scriptEnding.connect(function() {
for (var i = 0; i < emitters.lengths; ++i) {
Entities.deleteEntity(emitters[i]);
}
});
playFireworkShow = function(numberOfFireworks, duration) {
var position = { x: 0, y: 0, z: -78.0 };
for (var i = 0; i < numberOfFireworks; i++) {
var randomOffset = randomVec3(-15, 15, -3, 3, -1, 1);
var randomPosition = Vec3.sum(position, randomOffset);
Script.setTimeout(function(position) {
return function() {
var color = randomColor(128, 255, 128, 255, 128, 255);
shootFirework(position, color, fireworkSettings);
}
}(randomPosition), Math.random() * duration)
}
}
//playFireworkShow(50, 10000);

View file

@ -1,6 +1,7 @@
print("Loading pitching");
//Script.include("../libraries/line.js");
Script.include("https://raw.githubusercontent.com/huffman/hifi/line-js/examples/libraries/line.js");
Script.include(Script.include("firework.js"));
// Return all entities with properties `properties` within radius `searchRadius`
function findEntities(properties, searchRadius) {
@ -357,7 +358,6 @@ function setupTrail(entityID, position) {
trailInterval = Script.setInterval(function() {
var properties = Entities.getEntityProperties(entityID, ['position']);
if (Vec3.distance(properties.position, lastPosition)) {
Vec3.print("Adding trail", properties.position);
var strokeWidth = Math.log(1 + trail.size) * 0.05;
trail.enqueuePoint(properties.position, strokeWidth);
lastPosition = properties.position;
@ -430,6 +430,8 @@ function updateBillboard(distance) {
}
}
var FIREWORK_SHOW_DISTANCE_FEET = 200;
Baseball.prototype = {
finished: function() {
return this.state == BASEBALL_STATE.FOUL
@ -442,12 +444,10 @@ Baseball.prototype = {
this.timeSinceHit += dt;
var myProperties = Entities.getEntityProperties(this.entityID, ['position', 'velocity']);
var speed = Vec3.length(myProperties.velocity);
this.distanceTravelled = Vec3.distance(this.hitBallAtPosition, myProperties.position);
updateBillboard(Math.ceil(this.distanceTravelled * METERS_TO_FEET));
this.distanceTravelled = Vec3.distance(this.hitBallAtPosition, myProperties.position) * METERS_TO_FEET;
updateBillboard(Math.ceil(this.distanceTravelled));
if (this.timeSinceHit > 10 || speed < 1) {
this.state = BASEBALL_STATE.HIT_LANDED;
print("Ball took " + this.timeSinceHit.toFixed(3) + " seconds to land");
print("Ball travelled " + this.distanceTravelled + " meters")
this.ballLanded();
}
} else if (this.state == BASEBALL_STATE.PITCHING) {
if (this.timeSincePitched > 10) {
@ -456,6 +456,15 @@ Baseball.prototype = {
}
}
},
ballLanded: function() {
this.state = BASEBALL_STATE.HIT_LANDED;
if (this.distanceTravelled > FIREWORK_SHOW_DISTANCE_FEET) {
print("PLAYING SHOW")
playFireworkShow(50, 10000);
}
print("Ball took " + this.timeSinceHit.toFixed(3) + " seconds to land");
print("Ball travelled " + this.distanceTravelled + " feet")
},
collisionCallback: function(entityA, entityB, collision) {
var self = this;
var myProperties = Entities.getEntityProperties(this.entityID, ['position', 'velocity']);
@ -525,7 +534,7 @@ Baseball.prototype = {
print("PARTICLES");
entityCollisionWithGround(entityB, this.entityID, collision);
if (this.state == BASEBALL_STATE.HIT) {
this.state = BASEBALL_STATE.HIT_LANDED;
this.ballLanded();
}
} else if (name == "backstop") {
if (this.state == BASEBALL_STATE.PITCHING) {

View file

@ -0,0 +1,35 @@
randomInt = function(low, high) {
return Math.floor(randomFloat(low, high));
};
randomFloat = function(low, high) {
if (high === undefined) {
high = low;
low = 0;
}
return low + Math.random() * (high - low);
};
randomColor = function(redMin, redMax, greenMin, greenMax, blueMin, blueMax) {
return {
red: Math.ceil(randomFloat(redMin, redMax)),
green: Math.ceil(randomFloat(greenMin, greenMax)),
blue: Math.ceil(randomFloat(blueMin, blueMax)),
}
};
randomVec3 = function(xMin, xMax, yMin, yMax, zMin, zMax) {
return {
x: randomFloat(xMin, xMax),
y: randomFloat(yMin, yMax),
z: randomFloat(zMin, zMax),
}
};
getSounds = function(soundURLs) {
var sounds = [];
for (var i = 0; i < soundURLs.length; ++i) {
sounds.push(SoundCache.getSound(soundURLs[i], false));
}
return sounds;
};