PlayerKam.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if( te.taste >= '0' && te.taste <= '9' )
  67. {
  68. char action[ 5 ];
  69. action[ 0 ] = 3;
  70. *(int*)(action + 1) = te.taste - '1';
  71. if( *(int*)(action + 1) < 0 )
  72. *(int*)(action + 1) = 9;
  73. network->zFactoryClient()->sendPlayerAction( &action, 5 );
  74. }
  75. }
  76. if( te.id == TE_Release )
  77. {
  78. action[ 0 ] = 0;
  79. if( te.taste == 'w' || te.taste == 'W' )
  80. {
  81. action[ 1 ] = 0;
  82. network->zFactoryClient()->sendPlayerAction( action, 2 );
  83. }
  84. if( te.taste == 'a' || te.taste == 'A' )
  85. {
  86. action[ 1 ] = 1;
  87. network->zFactoryClient()->sendPlayerAction( action, 2 );
  88. }
  89. if( te.taste == 's' || te.taste == 'S' )
  90. {
  91. action[ 1 ] = 2;
  92. network->zFactoryClient()->sendPlayerAction( action, 2 );
  93. }
  94. if( te.taste == 'd' || te.taste == 'D' )
  95. {
  96. action[ 1 ] = 3;
  97. network->zFactoryClient()->sendPlayerAction( action, 2 );
  98. }
  99. if( te.taste == T_Shift )
  100. {
  101. action[ 1 ] = 4;
  102. network->zFactoryClient()->sendPlayerAction( action, 2 );
  103. }
  104. if( te.taste == 'q' || te.taste == 'Q' )
  105. {
  106. action[ 1 ] = 5;
  107. network->zFactoryClient()->sendPlayerAction( action, 2 );
  108. }
  109. if( te.taste == 'e' || te.taste == 'E' )
  110. {
  111. action[ 1 ] = 6;
  112. network->zFactoryClient()->sendPlayerAction( action, 2 );
  113. }
  114. if( te.taste == T_Space )
  115. {
  116. action[ 1 ] = 7;
  117. network->zFactoryClient()->sendPlayerAction( action, 2 );
  118. }
  119. if( te.taste == T_Esc )
  120. {
  121. kameraControll = 0;
  122. ShowCursor( true );
  123. }
  124. }
  125. }
  126. void PlayerKam::doMausEreignis( Framework::MausEreignis& me )
  127. {
  128. if( me.verarbeitet )
  129. {
  130. kameraControll = 0;
  131. ShowCursor( true );
  132. }
  133. else
  134. {
  135. if( !kameraControll )
  136. {
  137. if( me.id == ME_PLinks )
  138. {
  139. lastMousePos = { me.originalX, me.originalY };
  140. kameraControll = 1;
  141. }
  142. }
  143. else
  144. {
  145. if( kameraControll )
  146. {
  147. if( me.id == ME_Bewegung )
  148. {
  149. int yDir = lastMousePos.y - me.originalY;
  150. int xDir = lastMousePos.x - me.originalX;
  151. setRotation( { min( max( getRotation().x - yDir * 0.005f, 0.1f ), 2.5f ), getRotation().y, getRotation().z - xDir * 0.005f } );
  152. if( getRotation().z > 2 * PI )
  153. setRotation( { getRotation().x, getRotation().y, getRotation().z - 2.f * (float)PI } );
  154. if( getRotation().z < -2 * PI )
  155. setRotation( { getRotation().x, getRotation().y, getRotation().z + 2.f * (float)PI } );
  156. Vec3<float> direction = getWorldDirection( getScreenPos() + getScreenSize() / 2 );
  157. char action[ 13 ];
  158. action[ 0 ] = 2;
  159. *(float*)(action + 1) = direction.x;
  160. *(float*)(action + 5) = direction.y;
  161. *(float*)(action + 9) = direction.z;
  162. network->zFactoryClient()->sendPlayerAction( action, 13 );
  163. SetCursorPos( lastMousePos.x + window->getPosition().x, lastMousePos.y + window->getPosition().y );
  164. ShowCursor( false );
  165. }
  166. if( me.id == ME_PLinks )
  167. {
  168. char action[ 2 ] = { 1, 8 };
  169. network->zFactoryClient()->sendPlayerAction( action, 2 );
  170. }
  171. if( me.id == ME_RLinks )
  172. {
  173. char action[ 2 ] = { 0, 8 };
  174. network->zFactoryClient()->sendPlayerAction( action, 2 );
  175. }
  176. if( me.id == ME_PRechts )
  177. {
  178. char action[ 2 ] = { 1, 9 };
  179. network->zFactoryClient()->sendPlayerAction( action, 2 );
  180. }
  181. if( me.id == ME_RRechts )
  182. {
  183. char action[ 2 ] = { 0, 9 };
  184. network->zFactoryClient()->sendPlayerAction( action, 2 );
  185. }
  186. }
  187. }
  188. me.verarbeitet = 1;
  189. }
  190. }
  191. bool PlayerKam::tick( double time )
  192. {
  193. __int64 style = 0;
  194. if( hatStyle( Style::Movable ) )
  195. style |= Style::Movable;
  196. if( hatStyle( Style::Rotatable ) )
  197. style |= Style::Rotatable;
  198. if( hatStyle( Style::Zoomable ) )
  199. style |= Style::Zoomable;
  200. removeStyle( Style::Movable | Style::Rotatable | Style::Zoomable );
  201. bool result = Kam3D::tick( time );
  202. addStyle( style );
  203. return result;
  204. }