From f873e81bf03b7ea5b0f5b1120953ce09ff02a661 Mon Sep 17 00:00:00 2001 From: Sven Eberhardt Date: Sun, 9 Aug 2015 19:28:07 -0400 Subject: [PATCH] Fade airplane sound pitch when motor is turned on and off. --- .../Vehicles.ocd/Airplane.ocd/Script.c | 64 +++++++++++++++---- 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/planet/Objects.ocd/Vehicles.ocd/Airplane.ocd/Script.c b/planet/Objects.ocd/Vehicles.ocd/Airplane.ocd/Script.c index e2b17e129..3b28a860a 100644 --- a/planet/Objects.ocd/Vehicles.ocd/Airplane.ocd/Script.c +++ b/planet/Objects.ocd/Vehicles.ocd/Airplane.ocd/Script.c @@ -143,7 +143,7 @@ public func ContainedStop(object clonk) public func StartFlight(int new_throttle) { - AddEffect("IntSoundDelay",this,1,1,this); + SetPropellerSpeedTarget(100); SetAction("Fly"); throttle = new_throttle; } @@ -151,8 +151,7 @@ public func StartFlight(int new_throttle) public func StartInstantFlight(int angle, int new_throttle) { angle -= 10; - var effect = AddEffect("IntSoundDelay",this,1,1,this); - effect.Immediate = true; + SetPropellerSpeed(100); SetAction("Fly"); throttle = new_throttle; thrust = new_throttle; @@ -164,22 +163,12 @@ public func StartInstantFlight(int angle, int new_throttle) public func CancelFlight() { - RemoveEffect("IntSoundDelay",this); - Sound("PropellerLoop",0,100,nil,-1); + SetPropellerSpeedTarget(0); SetAction("Land"); rdir = 0; throttle = 0; } -private func FxIntSoundDelayTimer(object target, effect, int timer) -{ - if(timer >= 78 || (effect.Immediate && timer >= 5)) - { - Sound("PropellerLoop",0,100,nil,1); - return -1; - } -} - private func FxIntPlaneTimer(object target, effect, int timer) { //Lift @@ -367,6 +356,53 @@ public func PlaneDismount(object clonk) return true; } + +/* Propeller sound */ + +local prop_speed, prop_speed_target, prop_speed_timer; // current and target propeller speed [0, 100] + +// Instantly set new propeller speed +private func SetPropellerSpeed(int new_speed) +{ + if (prop_speed_timer) + { + RemoveTimer(this.PropellerSpeedTimer); + prop_speed_timer = false; + } + return SetPropellerSound(prop_speed = prop_speed_target = new_speed); +} + +// Schedule fading to new propeller speed +private func SetPropellerSpeedTarget(int new_speed_target) +{ + prop_speed_target = new_speed_target; + if (!prop_speed_timer) prop_speed_timer = AddTimer(this.PropellerSpeedTimer, 10); + return true; +} + +// Execute fading to new propeller speed +private func PropellerSpeedTimer() +{ + prop_speed = BoundBy(prop_speed_target, prop_speed - 10, prop_speed + 4); + if (prop_speed == prop_speed_target) + { + RemoveTimer(this.PropellerSpeedTimer); + prop_speed_timer = false; + } + return SetPropellerSound(prop_speed); +} + +// Set propeller speed sound. 0 = off, 100 = max speed. +private func SetPropellerSound(int speed) +{ + if (speed <= 0) + return Sound("PropellerLoop",0,100,nil,-1); + else + return Sound("PropellerLoop",0,100,nil,1,0,(speed-100)*2/3); +} + +/* Properties */ + func IsShipyardProduct() { return true; } local ActMap = {