PlayerKam.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "PlayerKam.h"
  2. #include "Globals.h"
  3. PlayerKam::PlayerKam( Framework::Bildschirm3D* zScreen )
  4. : Kam3D()
  5. {
  6. kameraControll = 0;
  7. setBildschirmPosition( 0, 0 );
  8. setBildschirmSize( zScreen->getBackBufferSize() );
  9. setStyle( Kam3D::Style::Tick | Kam3D::Style::Movable | Kam3D::Style::Rotatable );
  10. setRotation( { (float)PI / 2.f, 0, 0 } );
  11. }
  12. void PlayerKam::setDirection( Framework::Vec3<float> direction )
  13. {
  14. if( direction.getLengthSq() > 0 )
  15. {
  16. float rotZ = std::atan2( direction.y, direction.x ) + (float)PI / 2;
  17. setRotation( { getRotation().x, getRotation().y, rotZ } );
  18. }
  19. }
  20. void PlayerKam::doTastaturEreignis( Framework::TastaturEreignis& te )
  21. {
  22. char action[ 2 ];
  23. if( te.id == TE_Press )
  24. {
  25. action[ 0 ] = 1;
  26. if( te.taste == 'w' || te.taste == 'W' )
  27. {
  28. action[ 1 ] = 0;
  29. network->zFactoryClient()->sendPlayerAction( action, 2 );
  30. }
  31. if( te.taste == 'a' || te.taste == 'A' )
  32. {
  33. action[ 1 ] = 1;
  34. network->zFactoryClient()->sendPlayerAction( action, 2 );
  35. }
  36. if( te.taste == 's' || te.taste == 'S' )
  37. {
  38. action[ 1 ] = 2;
  39. network->zFactoryClient()->sendPlayerAction( action, 2 );
  40. }
  41. if( te.taste == 'd' || te.taste == 'D' )
  42. {
  43. action[ 1 ] = 3;
  44. network->zFactoryClient()->sendPlayerAction( action, 2 );
  45. }
  46. if( te.taste == T_Shift )
  47. {
  48. action[ 1 ] = 4;
  49. network->zFactoryClient()->sendPlayerAction( action, 2 );
  50. }
  51. if( te.taste == 'q' || te.taste == 'Q' )
  52. {
  53. action[ 1 ] = 5;
  54. network->zFactoryClient()->sendPlayerAction( action, 2 );
  55. }
  56. if( te.taste == 'e' || te.taste == 'E' )
  57. {
  58. action[ 1 ] = 6;
  59. network->zFactoryClient()->sendPlayerAction( action, 2 );
  60. }
  61. if( te.taste == T_Space )
  62. {
  63. action[ 1 ] = 7;
  64. network->zFactoryClient()->sendPlayerAction( action, 2 );
  65. }
  66. }
  67. if( te.id == TE_Release )
  68. {
  69. action[ 0 ] = 0;
  70. if( te.taste == 'w' || te.taste == 'W' )
  71. {
  72. action[ 1 ] = 0;
  73. network->zFactoryClient()->sendPlayerAction( action, 2 );
  74. }
  75. if( te.taste == 'a' || te.taste == 'A' )
  76. {
  77. action[ 1 ] = 1;
  78. network->zFactoryClient()->sendPlayerAction( action, 2 );
  79. }
  80. if( te.taste == 's' || te.taste == 'S' )
  81. {
  82. action[ 1 ] = 2;
  83. network->zFactoryClient()->sendPlayerAction( action, 2 );
  84. }
  85. if( te.taste == 'd' || te.taste == 'D' )
  86. {
  87. action[ 1 ] = 3;
  88. network->zFactoryClient()->sendPlayerAction( action, 2 );
  89. }
  90. if( te.taste == T_Shift )
  91. {
  92. action[ 1 ] = 4;
  93. network->zFactoryClient()->sendPlayerAction( action, 2 );
  94. }
  95. if( te.taste == 'q' || te.taste == 'Q' )
  96. {
  97. action[ 1 ] = 5;
  98. network->zFactoryClient()->sendPlayerAction( action, 2 );
  99. }
  100. if( te.taste == 'e' || te.taste == 'E' )
  101. {
  102. action[ 1 ] = 6;
  103. network->zFactoryClient()->sendPlayerAction( action, 2 );
  104. }
  105. if( te.taste == T_Space )
  106. {
  107. action[ 1 ] = 7;
  108. network->zFactoryClient()->sendPlayerAction( action, 2 );
  109. }
  110. if( te.taste == T_Esc )
  111. {
  112. kameraControll = 0;
  113. ShowCursor( true );
  114. }
  115. }
  116. }
  117. void PlayerKam::doMausEreignis( Framework::MausEreignis& me )
  118. {
  119. if( me.verarbeitet )
  120. {
  121. kameraControll = 0;
  122. ShowCursor( true );
  123. }
  124. else
  125. {
  126. if( !kameraControll )
  127. {
  128. if( me.id == ME_PLinks )
  129. {
  130. lastMousePos = { me.originalX, me.originalY };
  131. kameraControll = 1;
  132. }
  133. }
  134. else
  135. {
  136. if( kameraControll )
  137. {
  138. if( me.id == ME_Bewegung )
  139. {
  140. int yDir = lastMousePos.y - me.originalY;
  141. int xDir = lastMousePos.x - me.originalX;
  142. setRotation( { min( max( getRotation().x - yDir * 0.005f, 0.1f ), 2.5f ), getRotation().y, getRotation().z - xDir * 0.005f } );
  143. if( getRotation().z > 2 * PI )
  144. setRotation( { getRotation().x, getRotation().y, getRotation().z - 2.f * (float)PI } );
  145. if( getRotation().z < -2 * PI )
  146. setRotation( { getRotation().x, getRotation().y, getRotation().z + 2.f * (float)PI } );
  147. Vec3<float> direction = getWorldDirection( getScreenPos() + getScreenSize() / 2 );
  148. char action[ 13 ];
  149. action[ 0 ] = 2;
  150. *(float*)(action + 1) = direction.x;
  151. *(float*)(action + 5) = direction.y;
  152. *(float*)(action + 9) = direction.z;
  153. network->zFactoryClient()->sendPlayerAction( action, 13 );
  154. SetCursorPos( lastMousePos.x + window->getPosition().x, lastMousePos.y + window->getPosition().y );
  155. ShowCursor( false );
  156. }
  157. if( me.id == ME_PLinks )
  158. {
  159. char action[ 2 ] = { 1, 8 };
  160. network->zFactoryClient()->sendPlayerAction( action, 2 );
  161. }
  162. if( me.id == ME_RLinks )
  163. {
  164. char action[ 2 ] = { 0, 8 };
  165. network->zFactoryClient()->sendPlayerAction( action, 2 );
  166. }
  167. if( me.id == ME_PRechts )
  168. {
  169. char action[ 2 ] = { 1, 9 };
  170. network->zFactoryClient()->sendPlayerAction( action, 2 );
  171. }
  172. if( me.id == ME_RRechts )
  173. {
  174. char action[ 2 ] = { 0, 9 };
  175. network->zFactoryClient()->sendPlayerAction( action, 2 );
  176. }
  177. }
  178. }
  179. me.verarbeitet = 1;
  180. }
  181. }
  182. bool PlayerKam::tick( double time )
  183. {
  184. __int64 style = 0;
  185. if( hatStyle( Style::Movable ) )
  186. style |= Style::Movable;
  187. if( hatStyle( Style::Rotatable ) )
  188. style |= Style::Rotatable;
  189. if( hatStyle( Style::Zoomable ) )
  190. style |= Style::Zoomable;
  191. removeStyle( Style::Movable | Style::Rotatable | Style::Zoomable );
  192. bool result = Kam3D::tick( time );
  193. addStyle( style );
  194. return result;
  195. }