84 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2016-03-25 12:52:53 -07:00
//
// Created by Eric Levin on 3/25/16
// Copyright 2016 High Fidelity, Inc.
//
// This entity script handles the logic for untipping a cow after it collides with something
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
2016-03-25 10:57:54 -07:00
(function() {
var _this = this;
2016-03-25 11:19:30 -07:00
_this.COLLISION_COOLDOWN_TIME = 5000;
2016-03-25 10:57:54 -07:00
2016-04-26 11:21:56 -07:00
_this.preload = function(entityID) {
2016-03-25 10:57:54 -07:00
2016-04-26 11:21:56 -07:00
//set our id so other methods can get it.
2016-03-25 10:57:54 -07:00
_this.entityID = entityID;
2016-04-26 11:21:56 -07:00
//load the mooing sound
2022-08-21 13:35:28 +02:00
_this.mooSound = SoundCache.getSound("https://content.overte.org/Developer/Tutorials/cow/moo.wav")
2016-04-26 11:21:56 -07:00
_this.mooSoundOptions = {
volume: 0.7,
loop: false
};
//variables we will use to keep track of when to reset the cow
2016-03-25 11:19:30 -07:00
_this.timeSinceLastCollision = 0;
_this.shouldUntipCow = true;
2016-03-25 10:57:54 -07:00
}
2016-04-26 11:21:56 -07:00
_this.collisionWithEntity = function(myID, otherID, collisionInfo) {
//we dont actually use any of the parameters above, since we don't really care what we collided with, or the details of the collision.
//5 seconds after a collision, upright the cow. protect from multiple collisions in a short timespan with the 'shouldUntipCow' variable
if (_this.shouldUntipCow) {
//in Hifi, preface setTimeout with Script.setTimeout
Script.setTimeout(function() {
_this.untipCow();
_this.shouldUntipCow = true;
}, _this.COLLISION_COOLDOWN_TIME);
}
2016-03-25 11:19:30 -07:00
_this.shouldUntipCow = false;
2016-04-26 11:21:56 -07:00
2016-03-25 10:57:54 -07:00
}
2016-04-26 11:21:56 -07:00
_this.untipCow = function() {
2016-03-25 10:57:54 -07:00
// keep yaw but reset pitch and roll
var cowProps = Entities.getEntityProperties(_this.entityID, ["rotation", "position"]);
var eulerRotation = Quat.safeEulerAngles(cowProps.rotation);
eulerRotation.x = 0;
eulerRotation.z = 0;
var newRotation = Quat.fromVec3Degrees(eulerRotation);
2016-04-26 11:21:56 -07:00
//we zero out the velocity and angular velocity so the cow doesn't change position or spin
2016-03-25 11:19:30 -07:00
Entities.editEntity(_this.entityID, {
rotation: newRotation,
2016-04-26 11:21:56 -07:00
velocity: {
x: 0,
y: 0,
z: 0
},
angularVelocity: {
x: 0,
y: 0,
z: 0
}
2016-03-25 10:57:54 -07:00
});
2016-04-26 11:21:56 -07:00
//play the mooing sound when we untip! if it isn't already playing.
2016-03-25 10:57:54 -07:00
_this.mooSoundOptions.position = cowProps.position;
if (!_this.soundInjector) {
_this.soundInjector = Audio.playSound(_this.mooSound, _this.mooSoundOptions);
} else {
2016-04-26 11:21:56 -07:00
//if its already playing, just restart
2016-03-25 10:57:54 -07:00
_this.soundInjector.setOptions(_this.mooSoundOptions);
_this.soundInjector.restart();
}
}
2016-04-26 11:21:56 -07:00
});