Author: Not specified Language: text
Description: Not specified Timestamp: 2013-06-11 01:53:14 +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.                 damageable = GetComponent<Damageable>();
  32.                 attachment = GetComponent<Attachment>();
  33.                 ship = GetComponent<Ship>();
  34.         }
  35.  
  36.         public void FixedUpdate() {
  37.                 float chassisTemperature = ship.GetComponent<Temperature>().CurrentTemperature;
  38.                 if (attachment == null) {
  39.                         AmbientTemperature = BaseAmbientTemperature + AmbientAddForLaser; // + additional ambient temperature contributed by other parts
  40.                 }
  41.                 else {
  42.                         AmbientTemperature = chassisTemperature + (AmbientTemperature - chassisTemperature) * Mathf.Exp(-Time.fixedDeltaTime / ThermalResistance);
  43.                         AmbientTemperature += AmbientAddForLaser + AmbientAddForTurbo;
  44.                 }
  45.                 CurrentTemperature = AmbientTemperature + (CurrentTemperature - AmbientTemperature) * Mathf.Exp(-Time.fixedDeltaTime / ThermalResistance);
  46.  
  47.                 if (CurrentTemperature >= DamageThreshold)
  48.                         damageable.TakeDamage(2 * (CurrentTemperature - DamageThreshold) * Time.fixedDeltaTime / ThermalResistance, DamageType.Heat, null);
  49.  
  50.                 AmbientAddForLaser = 0;
  51.         }
  52.  
  53.         public void Serialize(Lidgren.Network.NetOutgoingMessage m, bool forceFull) {
  54.                 m.Write(CurrentTemperature);
  55.         }
  56.  
  57.         public void Deserialize(Lidgren.Network.NetIncomingMessage m) {
  58.                 CurrentTemperature = m.ReadFloat();
  59.         }
  60.  
  61.         public float MalfunctionProbability {
  62.                 get {
  63.                         return Mathf.Clamp01(Mathf.Atan((CurrentTemperature - MalfunctionThreshold) / 100) / Mathf.PI);
  64.                 }
  65.         }
  66. }
  67.  
View raw paste Reply