battery9v.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. class Battery9V : ItemBase
  2. {
  3. private int m_Efficiency0To10; // Synchronized variable
  4. static private float m_EfficiencyDecayStart = 0.1; // At this % of maximum energy the output of the battery starts to weaken.
  5. void Battery9V()
  6. {
  7. m_Efficiency0To10 = 10;
  8. RegisterNetSyncVariableInt("m_Efficiency0To10");
  9. }
  10. //! Returns efficiency of this battery. The value is synchronized from server to all clients and is accurate down to 0.1 units.
  11. float GetEfficiency0To1()
  12. {
  13. return m_Efficiency0To10 / 10;
  14. }
  15. //! Returns efficiency of this battery. The value is synchronized from server to all clients and is accurate down to 0.1 unit.
  16. float GetEfficiencyDecayStart()
  17. {
  18. return m_EfficiencyDecayStart;
  19. }
  20. override void OnEnergyConsumed()
  21. {
  22. super.OnEnergyConsumed();
  23. if (GetGame().IsServer())
  24. {
  25. float energyCoef = GetCompEM().GetEnergy0To1();
  26. if (energyCoef < m_EfficiencyDecayStart && m_EfficiencyDecayStart > 0)
  27. {
  28. m_Efficiency0To10 = Math.Round((energyCoef / m_EfficiencyDecayStart) * 10);
  29. SetSynchDirty();
  30. }
  31. }
  32. }
  33. // Not needed right now, but it will be useful if we add rechargable batteries.
  34. override void OnEnergyAdded()
  35. {
  36. super.OnEnergyAdded();
  37. if (GetGame().IsServer())
  38. {
  39. float energyCoef = GetCompEM().GetEnergy0To1();
  40. if (energyCoef < m_EfficiencyDecayStart && m_EfficiencyDecayStart > 0)
  41. {
  42. m_Efficiency0To10 = Math.Round((energyCoef / m_EfficiencyDecayStart) * 10);
  43. SetSynchDirty();
  44. }
  45. else
  46. {
  47. m_Efficiency0To10 = 10;
  48. SetSynchDirty();
  49. }
  50. }
  51. }
  52. override void SetActions()
  53. {
  54. super.SetActions();
  55. AddAction(ActionMeasureBattery);
  56. }
  57. }