Author: Not specified Language: text
Description: Not specified Timestamp: 2013-06-11 02:04:53 +0000
View raw paste Reply
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Networking;
  6. using UnityEngine;
  7.  
  8. [ServerControlled]
  9. [RequireComponent(typeof(Damageable))]
  10. public class Temperature : MonoBehaviour, ISyncable {
  11.         public bool ShouldSendUpdate { get { return true; } }
  12.         public uint ComponentID { get; set; }
  13.         public uint ObjectID { get; set; }
  14.  
  15.         public float CurrentTemperature { get; set; } // in Kelvins
  16.         public float AmbientTemperature { get; set; } // also in Kelvins
  17.         public float ThermalResistance;
  18.         public float MalfunctionThreshold;
  19.         public float DamageThreshold;
  20.  
  21.         public const float BaseAmbientTemperature = 300;
  22.         public float AmbientAddForLaser { get; set; }
  23.         public float AmbientAddForTurbo { get; set; }
  24.  
  25.         private Damageable damageable;
  26.         private Attachment attachment;
  27.         private Ship ship;
  28.  
  29.         public void Start() {
  30.                 CurrentTemperature = BaseAmbientTemperature;
  31.                 AmbientTemperature = BaseAmbientTemperature;
  32.                 damageable = GetComponent<Damageable>();
  33.                 attachment = GetComponent<Attachment>();
  34.                 ship = GetComponent<Ship>();
  35.         }
  36.  
  37.         public void FixedUpdate() {
  38.                 float chassisTemperature;
  39.                 if (attachment == null) {
  40.                         AmbientTemperature = BaseAmbientTemperature + AmbientAddForLaser; // + additional ambient temperature contributed by other parts
  41.                 }
  42.                 else {
  43.                         chassisTemperature = transform.root.GetComponent<Temperature>().CurrentTemperature;
  44.                         AmbientTemperature = chassisTemperature + (AmbientTemperature - chassisTemperature) * Mathf.Exp(-Time.fixedDeltaTime / ThermalResistance);
  45.                         AmbientTemperature += AmbientAddForLaser + AmbientAddForTurbo;
  46.                 }
  47.                 CurrentTemperature = AmbientTemperature + (CurrentTemperature - AmbientTemperature) * Mathf.Exp(-Time.fixedDeltaTime / ThermalResistance);
  48.  
  49.                 if (CurrentTemperature >= DamageThreshold)
  50.                         damageable.TakeDamage(2 * (CurrentTemperature - DamageThreshold) * Time.fixedDeltaTime / ThermalResistance, DamageType.Heat, null);
  51.  
  52.                 AmbientAddForLaser = 0;
  53.         }
  54.  
  55.         public void Serialize(Lidgren.Network.NetOutgoingMessage m, bool forceFull) {
  56.                 m.Write(CurrentTemperature);
  57.         }
  58.  
  59.         public void Deserialize(Lidgren.Network.NetIncomingMessage m) {
  60.                 CurrentTemperature = m.ReadFloat();
  61.         }
  62.  
  63.         public float MalfunctionProbability {
  64.                 get {
  65.                         return Mathf.Clamp01(Mathf.Atan((CurrentTemperature - MalfunctionThreshold) / 100) / Mathf.PI);
  66.                 }
  67.         }
  68. }
  69.  
View raw paste Reply