public override void PhysicsUpdate() { // handle attaching #if UNITY_IPHONE || UNITY_ANDROID attaching = Mobile.engage; accelMult = 0; if (Mobile.left) accelMult -= 1; if (Mobile.right) accelMult += 1; #else attaching = Input.GetKey(KeyCode.Space); accelMult = CrossPlatformInputManager.GetAxisRaw("Horizontal"); #endif if(deathgrab) { attaching = true; } trig.enabled = attaching; if (attaching==false && attachedTo!=null) { // do this to detach but remember neighbors CollidingObject temp = attachedTo; OnTriggerExit2D(temp.trig); OnTriggerEnter2D(temp.trig); } if (attaching && attachedTo==null && !wasAttaching) attachingSystem.SetTrigger("RunForwards"); attachingSystemRenderer.enabled = !attachingSystem.GetCurrentAnimatorStateInfo(0).IsName("Off"); if (attachedTo!=wasAttached && attachedTo!=null) attachedSystem.SetTrigger("RunForwards"); if (wasAttached!=null && attachedTo==null) attachedSystem.SetTrigger("RunBackwards"); // handle sounds if (!wasAttached && attaching && attachedTo!=null && !stickToGear.isPlaying) stickToGear.Play(); if (!wasAttaching && attaching && attachedTo==null && !stickToGearEmpty.isPlaying) stickToGearEmpty.Play(); if (!attaching && wasAttaching && wasAttached==null && !letGoOfGear.isPlaying) letGoOfGear.Play(); if (!attaching && wasAttaching && wasAttached!=null && !letGoOfGearEmpty.isPlaying) letGoOfGearEmpty.Play(); wasAttached = attachedTo; wasAttaching = attaching; base.PhysicsUpdate(); }
void Start() { SoundLayerNodes.Clear(); CollisionData.Clear(); foreach (var configNode in GameDatabase.Instance.GetConfigNodes("SHIPEFFECTS_SOUNDLAYERS")) { SoundLayerNodes.AddRange(configNode.GetNodes("SOUNDLAYER")); } foreach (var configNode in GameDatabase.Instance.GetConfigNodes("RSE_SETTINGS")) { if (configNode.HasValue("nextStageClip")) { StageManager.Instance.nextStageClip = GameDatabase.Instance.GetAudioClip(configNode.GetValue("nextStageClip")); } if (configNode.HasValue("cannotSeparateClip")) { StageManager.Instance.cannotSeparateClip = GameDatabase.Instance.GetAudioClip(configNode.GetValue("cannotSeparateClip")); } if (configNode.HasNode("Colliders")) { var colNode = configNode.GetNode("Colliders"); foreach (ConfigNode.Value node in colNode.values) { CollidingObject colDataType = (CollidingObject)Enum.Parse(typeof(CollidingObject), node.value, true); if (!CollisionData.ContainsKey(node.name)) { CollisionData.Add(node.name, colDataType); } else { CollisionData[node.name] = colDataType; } } } } foreach (var source in GameObject.FindObjectsOfType <AudioSource>()) { if (source.name.Contains("Music") || source.name.Contains("PartActionController")) { source.bypassListenerEffects = true; } if (source.name.Contains("airspeedNoise")) { source.bypassListenerEffects = false; } } var stageSource = StageManager.Instance.GetComponent <AudioSource>(); if (stageSource) { stageSource.bypassListenerEffects = true; if (HighLogic.CurrentGame.Parameters.CustomParams <Settings>().DisableStagingSound) { GameObject.Destroy(stageSource); } } //Find Chatterer Players var chattererObjects = GameObject.FindObjectsOfType <GameObject>().Where(x => x.name.Contains("_player")); if (chattererObjects.Count() > 0) { foreach (var chatterer in chattererObjects) { if (ChattererPlayerNames.Contains(Regex.Replace(chatterer.name, @"\d", string.Empty))) { foreach (var source in chatterer.GetComponents <AudioSource>()) { if (source == null) { continue; } source.bypassListenerEffects = !HighLogic.CurrentGame.Parameters.CustomParams <LowpassFilterSettings>().MuffleChatterer; ChattererSources.Add(source); } } } } //This is the easiest way to deal with multiple listeners, instead of chasing which listener is active. //Lowpass filter reads from whatever listener is active. audioListener = gameObject.AddOrGetComponent <AudioListener>(); audioListener.enabled = false; lowpassFilter = gameObject.AddOrGetComponent <LowpassFilter>(); lowpassFilter.enabled = HighLogic.CurrentGame.Parameters.CustomParams <LowpassFilterSettings>().EnableMuffling; lowpassFilter.lowpassResonanceQ = 3; lowpassCurveExt = AnimationCurve.Linear(1, 22200, 0, VacuumFreq); lowpassCurveInt = AnimationCurve.Linear(1, InterFreqAtm, 0, InterFreqVac); audioLimiter = gameObject.AddOrGetComponent <AudioLimiter>(); audioLimiter.enabled = HighLogic.CurrentGame.Parameters.CustomParams <AudioLimiterSettings>().EnableLimiter; GameEvents.onGamePause.Add(onGamePause); GameEvents.onGameUnpause.Add(onGameUnPause); }
public virtual void OnTriggerExit2D(Collider2D coll) { // add neighboring CollidingObjects CollidingObject collObj = coll.GetComponent<CollidingObject>(); if (collObj!=null && neighbors.Contains(collObj)) neighbors.Remove(collObj); // handle exiting water Water water = coll.gameObject.GetComponent<Water>(); if (water!=null) inwaters.Remove(water); // handle detaching if (collObj!=null && collObj==attachedTo) { //print("detatching from "+attachedTo.name); isMovable = true; Vector3 velocity = attachedTo.GetVelAtPoint(transform.position); Vector3 velocity2 = -accelMult*curSpeed*GetVelAtPoint(attachedTo.transform.position).normalized; this.velocity = velocity + velocity2; attachedTo = null; if (this is PlayerGear && collObj is Gear) (collObj as Gear).hasPlayerGear = false; } }
public virtual void OnTriggerEnter2D(Collider2D coll) { // add neighboring CollidingObjects CollidingObject collObj = coll.GetComponent<CollidingObject>(); if (collObj!=null && !neighbors.Contains(collObj)) { neighbors.Add(collObj); // align pegs this.GetPegOffset(collObj); collObj.GetPegOffset(this); } // handle entering water Water water = coll.gameObject.GetComponent<Water>(); if (water!=null) { if (!inwaters.Contains(water)) { water.splash.Play(); inwaters.Add(water); } } // handle attaching if (collObj!=null && (attachedTo==null || coll!=attachedTo) && attaching) { PhysicsUpdate(); attachedTo = collObj; if (this is PlayerGear && collObj is Gear) (collObj as Gear).hasPlayerGear = true; isMovable = false; } }
public float GetPegOffset(CollidingObject obj) { Vector3 diff = obj.transform.position - transform.position; Vector3 v1 = transform.TransformDirection(Vector3.right); float diffAngle = Mathf.Atan2(diff.y, diff.x)*180/Mathf.PI; float angle = Mathf.Atan2(v1.y, v1.x)*180/Mathf.PI; float cumAngle = angle+diffAngle - this.pegOffset - origAngle + 720; float pegAngle = 360f/numPegs; float pegOffset = (cumAngle%pegAngle)/pegAngle - 0.5f; if (name=="Player Gear" || name=="gear04") print(name+": dangle="+diffAngle+", angle="+angle+", cumAngle="+cumAngle+", final="+pegOffset); return pegOffset; /*Vector3 diff = coll.transform.position - transform.position; Vector3 v1 = transform.TransformDirection(Vector3.right); Vector3 v2 = coll.transform.TransformDirection(Vector3.right); float diffAngle = Mathf.Atan2(diff.y, diff.x)*180/Mathf.PI; float angle1 = Mathf.Atan2(v1.y, v1.x)*180/Mathf.PI + diffAngle; float angle2 = Mathf.Atan2(v2.y, v2.x)*180/Mathf.PI - diffAngle; float pegAngle1 = angle1%(360/numPegs); float pegAngle2 = angle2%(360/collObj.numPegs); if (pegAngle1<0) pegAngle1 += 360f/numPegs; if (pegAngle2<0) pegAngle2 += 360f/collObj.numPegs; float test = angle1%15; print(name+": "+angle1+", "+angle2+", "+diffAngle); print("self peg angle: "+ (pegAngle1)); print("other peg angle: "+(pegAngle2));*/ }
public override void OnUpdate() { if (!initialized || !moduleWheel || !moduleWheel.Wheel || !audioParent || gamePaused) { return; } bool running = false; bool motorEnabled = false; float driveOutput = 0; float wheelSpeed = moduleWheel.Wheel.WheelRadius * moduleWheel.Wheel.wheelCollider.angularVelocity; float slipDisplacement = Mathf.Abs(GetSlipDisplacement(wheelSpeed)); WheelHit hit; //ISSUES: it wont detect Vessels :( CollidingObject colObjectType = CollidingObject.Dirt; if (moduleWheel.Wheel.wheelCollider.GetGroundHit(out hit)) { colObjectType = AudioUtility.GetCollidingType(hit.collider.gameObject); } if (moduleMotor) { running = moduleMotor.state == ModuleWheelMotor.MotorState.Running; motorEnabled = moduleMotor.motorEnabled; driveOutput = moduleMotor.driveOutput; } bool isRetracted = false; if (moduleDeploy) { isRetracted = moduleDeploy.stateString == "Retracted"; } foreach (var soundLayerGroup in SoundLayerGroups) { string soundLayerKey = soundLayerGroup.Key; float control = 0; float masterVolume = HighLogic.CurrentGame.Parameters.CustomParams <Settings>().ShipVolume; if (!isRetracted) { switch (soundLayerGroup.Key) { case "Torque": control = running ? driveOutput / 100 : 0; break; case "Speed": control = motorEnabled ? Mathf.Abs(wheelSpeed) : 0; break; case "Ground": control = moduleWheel.isGrounded ? Mathf.Abs(wheelSpeed) : 0; masterVolume = HighLogic.CurrentGame.Parameters.CustomParams <Settings>().EffectsVolume; break; case "Slip": control = moduleWheel.isGrounded ? slipDisplacement : 0; masterVolume = HighLogic.CurrentGame.Parameters.CustomParams <Settings>().EffectsVolume; break; default: continue; } } foreach (var soundLayer in soundLayerGroup.Value) { float finalControl = control; if (soundLayerKey == "Ground" || soundLayerKey == "Slip") { string layerMaskName = soundLayer.data; if (layerMaskName != "") { switch (colObjectType) { case CollidingObject.Vessel: if (!layerMaskName.Contains("vessel")) { finalControl = 0; } break; case CollidingObject.Concrete: if (!layerMaskName.Contains("concrete")) { finalControl = 0; } break; case CollidingObject.Dirt: if (!layerMaskName.Contains("dirt")) { finalControl = 0; } break; } } } if (!spools.ContainsKey(soundLayer.name)) { spools.Add(soundLayer.name, 0); } if (soundLayer.spool) { spools[soundLayer.name] = Mathf.MoveTowards(spools[soundLayer.name], finalControl, soundLayer.spoolSpeed * TimeWarp.deltaTime); finalControl = spools[soundLayer.name]; } else { //fix for audiosource clicks spools[soundLayer.name] = Mathf.MoveTowards(spools[soundLayer.name], control, Mathf.Max(0.1f, control)); finalControl = spools[soundLayer.name]; } if (finalControl < 0.01f) { if (Sources.ContainsKey(soundLayer.name)) { UnityEngine.Object.Destroy(Sources[soundLayer.name]); Sources.Remove(soundLayer.name); } continue; } AudioSource source; if (Sources.ContainsKey(soundLayer.name)) { source = Sources[soundLayer.name]; } else { source = AudioUtility.CreateSource(audioParent, soundLayer); Sources.Add(soundLayer.name, source); } source.volume = soundLayer.volume.Value(finalControl) * volume * masterVolume; source.pitch = soundLayer.pitch.Value(finalControl); AudioUtility.PlayAtChannel(source, soundLayer.channel, soundLayer.loop, soundLayer.loopAtRandom); } } if (Sources.Count > 0) { var sourceKeys = Sources.Keys.ToList(); foreach (var source in sourceKeys) { if (!Sources[source].isPlaying) { UnityEngine.Object.Destroy(Sources[source]); Sources.Remove(source); } } } }
public GearSetRelation(CollidingObject obj, CollidingObject other, bool isAttached) { this.obj = obj; this.other = other; this.isAttached = isAttached; }
// adds obj to this GearSet if possible, returns true if possible public bool TryAddObj(CollidingObject obj, CollidingObject oldObj) { // add relations if both objects have gearSets if (IsVisited(obj) && IsVisited(oldObj)) { if (obj.attachedTo==oldObj) oldObj.gearSet.relations.Add(new GearSetRelation(oldObj, obj, true)); if (oldObj.attachedTo==obj) obj.gearSet.relations.Add(new GearSetRelation(obj, oldObj, true)); if (obj.isMovable) oldObj.gearSet.relations.Add(new GearSetRelation(oldObj, obj, false)); if (oldObj.isMovable) obj.gearSet.relations.Add(new GearSetRelation(obj, oldObj, false)); } // add if not visited and appropriate if (!IsVisited(obj) && ((!obj.isMovable&&obj.attachedTo==null)||gears.Count==0) && !isMovable) { // adjust data obj.visited = NEGATIVE; if (oldObj==null || oldObj.visited==NEGATIVE) obj.visited = POSITIVE; obj.gearSet = this; gears.Add(obj); totalAngularMomentum += Mult(obj)*obj.angularMomentum; totalMomentOfInertia += obj.momentOfInertia; isMovable = obj.isMovable || obj.attachedTo!=null; // try adding neighbors for (int i=0; i<obj.neighbors.Count; ++i) TryAddObj(obj.neighbors[i], obj); return true; } return false; }
private static float Mult(CollidingObject obj) { return (float)(obj.visited*2.0-POSITIVE-NEGATIVE); }
// helper functions private static bool IsVisited(CollidingObject obj) { return obj!=null && obj.visited>visitedNum; }
void PlaySounds(CollisionType collisionType, float control, CollidingObject collidingObjectType = CollidingObject.Dirt, bool oneshot = false) { foreach (var soundLayer in SoundLayerGroups[collisionType]) { float finalVolume = soundLayer.volume.Value(control) * soundLayer.massToVolume.Value((float)part.physicsMass); float finalPitch = soundLayer.pitch.Value(control) * soundLayer.massToPitch.Value((float)part.physicsMass); var layerMaskName = soundLayer.data.ToLower(); if (layerMaskName != "") { switch (collidingObjectType) { case CollidingObject.Vessel: if (!layerMaskName.Contains("vessel")) { finalVolume = 0; } break; case CollidingObject.Concrete: if (!layerMaskName.Contains("concrete")) { finalVolume = 0; } break; case CollidingObject.Dirt: if (!layerMaskName.Contains("dirt")) { finalVolume = 0; } break; } } if (finalVolume > float.Epsilon) { if (!Sources.ContainsKey(soundLayer.name)) { if (oneshot) { Sources.Add(soundLayer.name, AudioUtility.CreateOneShotSource(part.gameObject, 1, 1, soundLayer.maxDistance, soundLayer.spread)); } else { Sources.Add(soundLayer.name, AudioUtility.CreateSource(part.gameObject, soundLayer)); } } var source = Sources[soundLayer.name]; if (source == null) { return; } finalVolume *= HighLogic.CurrentGame.Parameters.CustomParams <Settings>().EffectsVolume; source.pitch = finalPitch; if (oneshot) { var audioClips = soundLayer.audioClips; if (audioClips == null) { continue; } int index = 0; if (audioClips.Length > 1) { index = UnityEngine.Random.Range(0, audioClips.Length); } var clip = GameDatabase.Instance.GetAudioClip(audioClips[index]); source.volume = 1; finalVolume *= UnityEngine.Random.Range(0.9f, 1.0f); AudioUtility.PlayAtChannel(source, soundLayer.channel, false, false, true, finalVolume, clip); } else { source.volume = finalVolume; AudioUtility.PlayAtChannel(source, soundLayer.channel, soundLayer.loop, soundLayer.loopAtRandom); } } else { if (Sources.ContainsKey(soundLayer.name) && Sources[soundLayer.name].isPlaying) { Sources[soundLayer.name].Stop(); } } } }