Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. void update(float elapsed, bool isFirstPerson)
  2. {
  3. if (elapsed > 1.0f / 60.0f)
  4. elapsed = 1.0f / 60.0f;
  5.  
  6. if (Pick)
  7. {
  8. float t = 0.0f;
  9. int xp = 0;
  10. int yp = 0;
  11. int zp = 0;
  12. // Update picking target
  13. target = updatePickingTarget();
  14. if (MMy_Physics::GetNearestPickableCube(Position, target, World, t, xp, yp, zp))
  15. {
  16. World->deleteCube(xp, yp, zp);
  17. }
  18.  
  19. Pick = false;
  20. }
  21.  
  22. //Par defaut, on applique la gravité (-100 sur Z), la moitie si dans l'eau
  23. YVec3f force = YVec3f(0, 0, -1) * 9.81f * gravityMultiplier;
  24. if (InWater)
  25. force = YVec3f(0, 0, -1) * 0.5f;
  26.  
  27. float lastheight = CurrentHeight;
  28. CurrentHeight = Height;
  29. if (Crouch)
  30. CurrentHeight = Height / 2;
  31.  
  32. //Pour ne pas s'enfoncer dans le sol en une frame quand on se releve
  33. if (CurrentHeight > lastheight)
  34. Position.Z += Height / 4;
  35.  
  36. ////Si l'avatar n'est pas au sol, alors il ne peut pas sauter
  37. //if (!Standing && !InWater) //On jump tout le temps
  38. // Jump = false;
  39.  
  40. float accel = 40;
  41. if (Crouch)
  42. accel = 20;
  43. if (!Standing)
  44. accel = 5;
  45. if (Run)
  46. accel = 80;
  47.  
  48. YVec3f forward(Cam->Direction.X, Cam->Direction.Y, 0);
  49. forward.normalize();
  50. YVec3f right(Cam->RightVec.X, Cam->RightVec.Y, 0);
  51. right.normalize();
  52.  
  53. //On applique les controles en fonction de l'accélération
  54. if (avance)
  55. force += forward * accel;
  56. if (recule)
  57. force += forward * -accel;
  58. if (gauche)
  59. force += right * -accel;
  60. if (droite)
  61. force += right * accel;
  62.  
  63. //On applique le jump
  64. if (Jump)
  65. {
  66. force += Cam->UpRef * 15.0f / elapsed; //(impulsion, pas fonction du temps)
  67. Jump = false;
  68. }
  69.  
  70. //On applique les forces en fonction du temps écoulé
  71. Speed += force * elapsed;
  72.  
  73. //On met une limite a sa vitesse horizontale
  74. float speedmax = 70;
  75. if (Crouch)
  76. speedmax = 45;
  77. if (!Standing)
  78. speedmax = 70;
  79. if (Run)
  80. speedmax = 140;
  81.  
  82. YVec3f horSpeed = Speed;
  83. horSpeed.Z = 0;
  84. if (horSpeed.getSize() > speedmax)
  85. {
  86. horSpeed.normalize();
  87. horSpeed *= speedmax;
  88. Speed.X = horSpeed.X;
  89. Speed.Y = horSpeed.Y;
  90. }
  91.  
  92. //On le déplace, en sauvegardant son ancienne position
  93. YVec3f oldPosition = Position;
  94. Position += (Speed * elapsed);
  95.  
  96. //YLog::log(YLog::ENGINE_INFO, ("zS " + toString(Speed.Z)).c_str());
  97.  
  98. if (_TimerStanding.getElapsedSeconds() > 0.01)
  99. Standing = false;
  100. for (int pass = 0; pass < 2; pass++)
  101. {
  102. for (int i = 0; i < 6; i++)
  103. {
  104. float valueColMin = 0;
  105. MWorld::MAxis axis = World->getMinCol(Position, Speed, Width, CurrentHeight, valueColMin, i < 3);
  106. //YLog::log(YLog::ENGINE_INFO,"check");
  107. if (axis != 0)
  108. {
  109. //valueColMin = nymax(nyabs(valueColMin), 0.0001f) * (valueColMin > 0 ? 1.0f : -1.0f);
  110. if (axis & MWorld::AXIS_X)
  111. {
  112. //YLog::log(YLog::ENGINE_INFO,("x " + toString(valueColMin)).c_str());
  113. Position.X += valueColMin + 0.001*sign(valueColMin);
  114. Speed.X = 0;
  115. }
  116. if (axis & MWorld::AXIS_Y)
  117. {
  118. //YLog::log(YLog::ENGINE_INFO, ("y " + toString(valueColMin)).c_str());
  119. Position.Y += valueColMin + 0.001*sign(valueColMin);
  120. Speed.Y = 0;
  121. }
  122. if (axis & MWorld::AXIS_Z)
  123. {
  124. //YLog::log(YLog::ENGINE_INFO, ("z " + toString(valueColMin)).c_str());
  125. Speed.Z = 0;
  126. Position.Z += valueColMin + 0.001*sign(valueColMin);
  127. Standing = true;
  128. _TimerStanding.start();
  129. }
  130. }
  131. }
  132. }
  133.  
  134.  
  135. int x = (int)(Position.X / MCube::CUBE_SIZE);
  136. int y = (int)(Position.Y / MCube::CUBE_SIZE);
  137. int z = (int)(Position.Z / MCube::CUBE_SIZE);
  138.  
  139. //Escaliers
  140. float floatheight = 1.0f;
  141. float zpied = Position.Z - (Height / 2.0f);
  142. float zfloatpied = zpied - floatheight;
  143. int izCubeDessousFloat = (int)((zfloatpied) / MCube::CUBE_SIZE);
  144. float zCubeDessous2Float = zfloatpied - MCube::CUBE_SIZE;
  145. int izCubeDessous2Float = (int)(zCubeDessous2Float / MCube::CUBE_SIZE);
  146.  
  147.  
  148. // If we are in water
  149. InWater = false;
  150. if (World->getCube(x, y, z)->getType() == MCube::CUBE_EAU)
  151. InWater = true;
  152.  
  153. if (InWater)
  154. {
  155. //Standing = true;
  156. Speed *= pow(0.2f, elapsed);
  157. }
  158.  
  159. if (Standing)
  160. Speed *= pow(0.01f, elapsed);
  161.  
  162. // Update camera last, once all movement have been made (smoother)
  163. if (isFirstPerson) // FP camera
  164. {
  165. Cam->moveTo(Position);
  166. }
  167. else // TP camera
  168. {
  169. Cam->setPosition(Position - (Cam->Direction * 5.0f));
  170. Cam->setLookAt(Position);
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement