light.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //***************************************************************************
  2. //
  3. // Light.cpp -- File contains the Light Object code
  4. //
  5. // MechCommander 2
  6. //
  7. //---------------------------------------------------------------------------//
  8. // Copyright (C) Microsoft Corporation. All rights reserved. //
  9. //===========================================================================//
  10. #ifndef MCLIB_H
  11. #include "mclib.h"
  12. #endif
  13. #ifndef GAMEOBJ_H
  14. #include "gameobj.h"
  15. #endif
  16. #ifndef LIGHT_H
  17. #include "light.h"
  18. #endif
  19. #include <stdio.h>
  20. #define LIGHT_DEPTH_FIXUP -500
  21. //***************************************************************************
  22. // LIGHT TYPE class
  23. //***************************************************************************
  24. GameObjectPtr LightType::createInstance (void) {
  25. LightPtr newLight = new Light;
  26. if (!newLight)
  27. return(NULL);
  28. newLight->init(true, this);
  29. return(newLight);
  30. }
  31. //---------------------------------------------------------------------------
  32. void LightType::destroy (void)
  33. {
  34. ObjectType::destroy();
  35. }
  36. //---------------------------------------------------------------------------
  37. long LightType::init (FilePtr objFile, unsigned long fileSize) {
  38. long result = 0;
  39. FitIniFile explFile;
  40. result = explFile.open(objFile,fileSize);
  41. if (result != NO_ERR)
  42. return(result);
  43. result = explFile.seekBlock("LightData");
  44. if (result != NO_ERR)
  45. return result;
  46. result = explFile.readIdBoolean("OneShotFlag",oneShotFlag);
  47. if (result != NO_ERR)
  48. return result;
  49. result = explFile.readIdFloat("AltitudeOffset",altitudeOffset);
  50. if (result != NO_ERR)
  51. return result;
  52. //------------------------------------------------------------------
  53. // Initialize the base object Type from the current file.
  54. result = ObjectType::init(&explFile);
  55. return(result);
  56. }
  57. //---------------------------------------------------------------------------
  58. bool LightType::handleCollision (GameObjectPtr collidee, GameObjectPtr collider) {
  59. //-----------------------------
  60. // Nothing collides with light!
  61. return(false);
  62. }
  63. //---------------------------------------------------------------------------
  64. bool LightType::handleDestruction (GameObjectPtr collidee, GameObjectPtr collider) {
  65. //----------------------------
  66. // No destroy'n light, either!
  67. return(false);
  68. }
  69. //***************************************************************************
  70. // LIGHT class
  71. //***************************************************************************
  72. void Light::init (bool create) {
  73. setFlag(OBJECT_FLAG_JUSTCREATED, true);
  74. }
  75. //---------------------------------------------------------------------------
  76. long Light::update (void) {
  77. if (!getFlag(OBJECT_FLAG_DONE)) {
  78. //----------------------------------
  79. //-- Always force position altitude 'cause set every frame.
  80. position.z += ((LightTypePtr)getObjectType())->altitudeOffset;
  81. setPosition(position);
  82. if (getFlag(OBJECT_FLAG_JUSTCREATED)) {
  83. setFlag(OBJECT_FLAG_JUSTCREATED, false);
  84. setFlag(OBJECT_FLAG_TANGIBLE, false);
  85. }
  86. #ifdef USE_LIGHT_APPEARANCE
  87. //-----------------------------------------
  88. // Light is NEVER Done. Unless its done!
  89. bool inView = onScreen();
  90. lightAppearance->setInView(inView);
  91. long result = lightAppearance->update();
  92. if (!result && ((LightTypePtr)getObjectType())->oneShotFlag)
  93. setFlag(OBJECT_FLAG_DONE, true);
  94. #endif
  95. }
  96. return (true);
  97. }
  98. //---------------------------------------------------------------------------
  99. void Light::render (void) {
  100. if (gamePaused)
  101. onScreen();
  102. if (!getFlag(OBJECT_FLAG_JUSTCREATED) && (windowsVisible == turn) && !getFlag(OBJECT_FLAG_DONE)) {
  103. #ifdef USE_LIGHT_APPEARANCE
  104. lightAppearance->render(LIGHT_DEPTH_FIXUP);
  105. #endif
  106. }
  107. }
  108. //---------------------------------------------------------------------------
  109. void Light::destroy (void)
  110. {
  111. }
  112. //---------------------------------------------------------------------------
  113. void Light::init (bool create, ObjectTypePtr _type) {
  114. //-------------------------------------------
  115. // Initialize the Light Appearance here.
  116. GameObject::init(create, _type);
  117. setFlag(OBJECT_FLAG_JUSTCREATED, true);
  118. setFlag(OBJECT_FLAG_TANGIBLE, false);
  119. //-------------------------------------------------------------
  120. // The appearance is initialized here using data from the type
  121. #ifdef USE_LIGHT_APPEARANCE
  122. unsigned long appearanceType = _type->getAppearanceTypeNum();
  123. AppearanceTypePtr lightAppearanceType = appearanceTypeList->getAppearance(appearanceType);
  124. if (!lightAppearanceType)
  125. return(NO_APPEARANCE_TYPE_FOR_EXPL);
  126. //--------------------------------------------------------------
  127. // The only kind of appearanceType the explosions currently know how
  128. // to work with is a spriteTree. Anything else is wrong.
  129. if (lightAppearanceType->getAppearanceClass() == VFX_APPEAR)
  130. {
  131. lightAppearance = new VFXAppearance;
  132. if (!lightAppearance)
  133. return(NO_APPEARANCE_FOR_EXPL);
  134. result = lightAppearance->init((VFXAppearanceType *)lightAppearanceType, (GameObjectPtr)this);
  135. if (result != NO_ERR)
  136. return(result);
  137. }
  138. else
  139. {
  140. return(APPEARANCE_NOT_VFX_XPL);
  141. }
  142. #endif
  143. objectClass = KLIEG_LIGHT;
  144. setFlag(OBJECT_FLAG_DONE, false);
  145. }
  146. //***************************************************************************