// // roosterChase.js // A script to make the rooster chase people in hifi://Mexico // // Author: Elisa Lupin-Jimenez // Copyright High Fidelity 2018 // // Licensed under the Apache 2.0 License // See accompanying license file or http://apache.org/ // // All assets are under CC Attribution Non-Commerical // http://creativecommons.org/licenses/ // (function() { var STANDING_URL = Script.resolvePath("./assets/rooster/rooster_standing.fbx"); var RUNNING_URL = Script.resolvePath("./assets/rooster/rooster_running.fbx"); var INTERVAL = 200; var DISTANCE_FROM_START = 0.25; var STANDING_ANIMATION = { url: STANDING_URL, fps: 30, firstFrame: 1, lastFrame: 200 }; var RUNNING_ANIMATION = { url: RUNNING_URL, fps: 24, firstFrame: 1, lastFrame: 90 }; var moveTowardAvatarInterval; var moveBackToStartInterval; var roosterID; var position; var startingPosition; var isAtDestination; this.preload = function(entityID) { var roosterArray = Entities.getChildrenIDs(entityID); roosterArray.forEach(function(roosterEntityID) { roosterID = roosterEntityID; }); startingPosition = Entities.getEntityProperties(roosterID, "position").position; }; function moveTowardAvatar(isAtDestination) { if (isAtDestination === false) { position = Entities.getEntityProperties(roosterID, "position").position; var destination = Vec3.multiply(-2, Vec3.normalize(Vec3.subtract(position, MyAvatar.position))); var newProperties = { "rotation": Quat.multiply(MyAvatar.orientation, {x:0, y:0, z:0, w:0}), "velocity": destination }; Entities.editEntity(roosterID, newProperties); if (Vec3.distance(MyAvatar.position, position) < DISTANCE_FROM_START) { var updateProperties = { velocity: {x:0, y:0, z:0}, animation: STANDING_ANIMATION }; Entities.editEntity(roosterID, updateProperties); isAtDestination = true; } } } function moveBackToStart(isAtDestination) { if (isAtDestination === false) { position = Entities.getEntityProperties(roosterID, "position").position; var destination = Vec3.multiply(-2, Vec3.normalize(Vec3.subtract(position, startingPosition))); var newProperties = { "rotation": Quat.lookAtSimple(startingPosition, position), "velocity": destination }; Entities.editEntity(roosterID, newProperties); if (Vec3.distance(startingPosition, position) < DISTANCE_FROM_START) { var updateProperties = { velocity: {x:0, y:0, z:0}, animation: STANDING_ANIMATION }; Entities.editEntity(roosterID, updateProperties); isAtDestination = true; } } } this.enterEntity = function(entityID) { if (moveBackToStartInterval) { Script.clearInterval(moveBackToStartInterval); moveBackToStartInterval = null; } var updateAnimation = { animation: RUNNING_ANIMATION }; isAtDestination = false; Entities.editEntity(roosterID, updateAnimation); moveTowardAvatarInterval = Script.setInterval(moveTowardAvatar(isAtDestination), INTERVAL); }; this.leaveEntity = function(entityID) { if (moveTowardAvatarInterval) { Script.clearInterval(moveTowardAvatarInterval); moveTowardAvatarInterval = null; } var updateAnimation = { animation: RUNNING_ANIMATION }; isAtDestination = false; Entities.editEntity(roosterID, updateAnimation); moveBackToStartInterval = Script.setInterval(moveBackToStart(isAtDestination), INTERVAL); }; });