Add foul and strike audio to baseball game

This commit is contained in:
Ryan Huffman 2015-10-27 13:25:43 -07:00
parent b75ad7f254
commit b46d42889d

View file

@ -14,6 +14,15 @@ var AUDIO = {
SoundCache.getSound("atp:e68661374e2145c480809c26134782aad11e0de456c7802170c7abccc4028873.wav", false),
SoundCache.getSound("atp:787e3c9af17dd3929527787176ede83d6806260e63ddd5a4cef48cd22e32c6f7.wav", false),
SoundCache.getSound("atp:fc65383431a6238c7a4749f0f6f061f75a604ed5e17d775ab1b2955609e67ebb.wav", false),
],
strike: [
SoundCache.getSound("atp:2a258076a85fffde4ba04b5ddc1de9034c7ae7d2af8c5d93d4fed0bcdef3472a.wav", false),
SoundCache.getSound("atp:518363524af3ed9b9ae4ca2ceee61f01aecd37e266a51c5a5f5487efe2520fd5.wav", false),
SoundCache.getSound("atp:d51d38b089574acbdfdf53ef733bfb3ab41d848fb8c0b6659c7790a785240009.wav", false),
],
foul: [
SoundCache.getSound("atp:316fa18ff9eef457f670452b449a8dc5a41ccabd4e948781c50aaafaae63b0ab.wav", false),
SoundCache.getSound("atp:c84d88352d38437edd7414b26dc74e567618712caeb59fec70822398b0c5a279.wav", false),
]
}
@ -105,6 +114,12 @@ var BASEBALL_PROPERTIES = {
z: 0
}
};
var BASEBALL_STATE = {
PITCHING: 0,
HIT: 1,
STRIKE: 2,
FOUL: 3
};
var pitchingMachineID = Entities.addEntity(PITCHING_MACHINE_PROPERTIES);
@ -199,6 +214,8 @@ function setupTrail(entityID, position) {
function Baseball(position, velocity, ballScale) {
var self = this;
this.state = BASEBALL_STATE.PITCHING;
// Setup entity properties
var properties = shallowCopy(BASEBALL_PROPERTIES);
properties.position = position;
@ -251,35 +268,57 @@ Baseball.prototype = {
var name = Entities.getEntityProperties(entityB, ["name"]).name;
print("Hit: " + name);
if (name == "Bat") {
print("HIT");
if (this.state == BASEBALL_STATE.PITCHING) {
print("HIT");
// Update ball velocity
Entities.editEntity(self.entityID, {
velocity: Vec3.multiply(2, myVelocity),
});
// Setup line update interval
setupTrail(self.entityID, myPosition);
// Setup bat hit sound
playRandomSound(AUDIO.batHit, {
position: myPosition,
volume: 2.0
});
// Setup crowd reaction sound
var speed = Vec3.length(myVelocity);
Script.setTimeout(function() {
playRandomSound((speed < 5.0) ? AUDIO.crowdBoos : AUDIO.crowdCheers, {
position: { x: 0 ,y: 0, z: 0 },
volume: 1.0
// Update ball velocity
Entities.editEntity(self.entityID, {
velocity: Vec3.multiply(2, myVelocity),
});
}, 500);
// Setup line update interval
setupTrail(self.entityID, myPosition);
// Setup bat hit sound
playRandomSound(AUDIO.batHit, {
position: myPosition,
volume: 2.0
});
// Setup crowd reaction sound
var speed = Vec3.length(myVelocity);
Script.setTimeout(function() {
playRandomSound((speed < 5.0) ? AUDIO.crowdBoos : AUDIO.crowdCheers, {
position: { x: 0 ,y: 0, z: 0 },
volume: 1.0
});
}, 500);
var yaw = Math.atan2(myVelocity.x, myVelocity.z) * 180 / Math.PI;
var foul = yaw > -135 && yaw < 135;
if (foul) {
print("FOUL ", yaw)
this.state = BASEBALL_STATE.FOUL;
playRandomSound(AUDIO.foul, {
position: myPosition,
volume: 2.0
});
} else {
print("HIT ", yaw);
this.state = BASEBALL_STATE.HIT;
}
}
} else if (name == "stadium") {
print("PARTICLES");
entityCollisionWithGround(entityB, this.entityID, collision);
} else if (name == "backstop") {
print("STRIKE");
if (this.state == BASEBALL_STATE.PITCHING) {
this.state = BASEBALL_STATE.STRIKE;
print("STRIKE");
playRandomSound(AUDIO.strike, {
position: myPosition,
volume: 2.0
});
}
}
}
}
@ -349,4 +388,3 @@ Script.scriptEnding.connect(function() {
});
Script.setInterval(pitchBall, PITCH_RATE);