ctobjectfollower.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. class CTObjectFollower extends ScriptedWidgetEventHandler
  2. {
  3. protected Widget m_FollowerRoot;
  4. protected Widget m_FollowerButton;
  5. protected vector m_Position;
  6. protected vector m_Orientation;
  7. protected EntityAI m_FollowedObject;
  8. protected float m_MaxFade;
  9. protected float m_MinFade;
  10. protected CameraToolsMenu m_Menu;
  11. void ~CTObjectFollower()
  12. {
  13. DestroyFollowedObject();
  14. delete m_FollowerRoot;
  15. }
  16. void CreateFollowedObject( string type )
  17. {
  18. m_FollowedObject = EntityAI.Cast( GetGame().CreateObject( type, m_Position, true ) );
  19. SetPosition( m_Position );
  20. SetRotation( m_Orientation );
  21. }
  22. void DestroyFollowedObject()
  23. {
  24. if( m_FollowedObject )
  25. {
  26. GetGame().ObjectDelete( m_FollowedObject );
  27. }
  28. }
  29. void Update( float timeslice )
  30. {
  31. UpdatePos();
  32. }
  33. EntityAI GetObj()
  34. {
  35. return m_FollowedObject;
  36. }
  37. void SetPosition( vector position )
  38. {
  39. m_Position = position;
  40. if( m_FollowedObject )
  41. {
  42. m_FollowedObject.SetPosition( position );
  43. m_Position = m_FollowedObject.GetPosition();
  44. }
  45. UpdatePos();
  46. }
  47. void SetRotation( vector dir )
  48. {
  49. m_Orientation = dir;
  50. PlayerBase player = PlayerBase.Cast( m_FollowedObject );
  51. if( player )
  52. {
  53. player.SetOrientation( m_Orientation );
  54. }
  55. UpdatePos();
  56. }
  57. vector GetPosition()
  58. {
  59. if( m_FollowedObject && m_FollowedObject.GetPosition() != m_Position )
  60. {
  61. SetPosition( m_FollowedObject.GetPosition() );
  62. }
  63. return m_Position;
  64. }
  65. vector GetRotation()
  66. {
  67. if( m_FollowedObject )
  68. {
  69. return m_FollowedObject.GetOrientation();
  70. }
  71. return "0 0 0";
  72. }
  73. /*! \brief Function updating the position of the tracker widget.
  74. *
  75. * Currently tracks using GetScreenPos() on the position of the player.
  76. */
  77. void UpdatePos()
  78. {
  79. vector relativePos;
  80. relativePos = GetGame().GetScreenPosRelative( GetPosition() );
  81. if( relativePos[0] >= 1 || relativePos[0] == 0 || relativePos[1] >= 1 || relativePos[1] == 0 )
  82. {
  83. m_FollowerRoot.Show( false );
  84. return;
  85. }
  86. else if( relativePos[2] < 0 )
  87. {
  88. m_FollowerRoot.Show( false );
  89. return;
  90. }
  91. else
  92. {
  93. m_FollowerRoot.Show( true );
  94. }
  95. float x, y;
  96. m_FollowerRoot.GetSize( x, y );
  97. m_FollowerRoot.SetPos( relativePos[0], relativePos[1] );
  98. }
  99. void Show()
  100. {
  101. m_FollowerRoot.Show( true );
  102. }
  103. void Hide()
  104. {
  105. m_FollowerRoot.Show( false );
  106. }
  107. void Fade( bool fade )
  108. {
  109. if( fade )
  110. {
  111. m_FollowerRoot.SetAlpha( m_MinFade );
  112. }
  113. else
  114. {
  115. m_FollowerRoot.SetAlpha( m_MaxFade );
  116. }
  117. }
  118. override bool OnClick( Widget w, int x, int y, int button )
  119. {
  120. return false;
  121. }
  122. override bool OnDoubleClick( Widget w, int x, int y, int button )
  123. {
  124. return false;
  125. }
  126. override bool OnMouseButtonDown( Widget w, int x, int y, int button )
  127. {
  128. if( w == m_FollowerButton && button == MouseState.LEFT )
  129. {
  130. if( m_Menu )
  131. {
  132. m_Menu.SelectActor( CTActor.Cast( this ) );
  133. }
  134. return true;
  135. }
  136. return false;
  137. }
  138. override bool OnMouseButtonUp( Widget w, int x, int y, int button )
  139. {
  140. if( w == m_FollowerButton && button == MouseState.LEFT )
  141. {
  142. if( m_Menu )
  143. {
  144. m_Menu.SelectActor( null );
  145. }
  146. return true;
  147. }
  148. return false;
  149. }
  150. }