player.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #define SCALE_3D_RENDERING 8
  2. #include "helper.h"
  3. #define ROOM_SIZE 10000
  4. #define CUBE_SIZE 800
  5. TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
  6. {
  7. TPE_ENV_START( TPE_envAABoxInside(p,TPE_vec3(0,ROOM_SIZE / 4,0),TPE_vec3(ROOM_SIZE,ROOM_SIZE / 2,ROOM_SIZE)),p )
  8. TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(4000,160,4000),TPE_vec3(1000,160,1000)),p )
  9. TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(4000,80,2500),TPE_vec3(1000,80,500)),p )
  10. TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(-1000,270,4500),TPE_vec3(4000,270,250)),p )
  11. TPE_ENV_NEXT( TPE_envHalfPlane(p,TPE_vec3(0,0,-2000),TPE_vec3(0,255,255)),p )
  12. TPE_ENV_END
  13. }
  14. int jumpCountdown = 0;
  15. TPE_Unit rotation = 0;
  16. int onGroundCount = 0;
  17. TPE_Vec3 directionVec;
  18. void updateDirection(void)
  19. {
  20. directionVec.x = TPE_sin(rotation);
  21. directionVec.z = TPE_cos(rotation);
  22. directionVec.y = 0;
  23. }
  24. uint8_t collisionCallback(uint16_t b1, uint16_t j1, uint16_t b2, uint16_t j2,
  25. TPE_Vec3 p)
  26. {
  27. if (b1 == 0 && b1 == b2 && j1 == 0 &&
  28. p.y < tpe_world.bodies[0].joints[0].position.y -50)
  29. onGroundCount = 2;
  30. }
  31. int main(void)
  32. {
  33. helper_init();
  34. helper_debugDrawOn = 1;
  35. updateDirection();
  36. tpe_world.environmentFunction = environmentDistance;
  37. tpe_world.collisionCallback = collisionCallback;
  38. s3l_scene.camera.transform.translation.z -= ROOM_SIZE / 2;
  39. s3l_scene.camera.transform.translation.y += ROOM_SIZE / 3;
  40. s3l_scene.camera.transform.translation.x -= ROOM_SIZE / 4;
  41. s3l_scene.camera.transform.rotation.y = -1 * TPE_FRACTIONS_PER_UNIT / 16;
  42. helper_add2Line(400,300,400);
  43. TPE_bodyMove(&tpe_world.bodies[0],TPE_vec3(0,1000,0));
  44. TPE_bodyRotateByAxis(&tpe_world.bodies[0],TPE_vec3(0,0,TPE_FRACTIONS_PER_UNIT / 4));
  45. tpe_world.bodies[0].elasticity = 0;
  46. tpe_world.bodies[0].friction = 0;
  47. tpe_world.bodies[0].flags |= TPE_BODY_FLAG_NONROTATING;
  48. helper_addBall(1000,100);
  49. TPE_bodyMove(&tpe_world.bodies[1],TPE_vec3(-1000,1000,0));
  50. tpe_world.bodies[1].elasticity = 400;
  51. tpe_world.bodies[1].friction = 100;
  52. //helper_addBox(400,400,400,200,300);
  53. helper_addCenterRect(600,600,400,50);
  54. //helper_addBox(500,500,500,250,300);
  55. TPE_bodyMove(&tpe_world.bodies[2],TPE_vec3(-3000,1000,2000));
  56. tpe_world.bodies[2].elasticity = 400;
  57. tpe_world.bodies[2].friction = 50;
  58. while (helper_running)
  59. {
  60. if (onGroundCount > 0)
  61. onGroundCount--;
  62. if (jumpCountdown > 0)
  63. jumpCountdown--;
  64. helper_frameStart();
  65. s3l_scene.camera.transform.translation.x = tpe_world.bodies[0].joints[0].position.x;
  66. s3l_scene.camera.transform.translation.y = TPE_keepInRange(
  67. s3l_scene.camera.transform.translation.y,
  68. tpe_world.bodies[0].joints[1].position.y,
  69. tpe_world.bodies[0].joints[1].position.y + 10);
  70. TPE_bodyMultiplyNetSpeed(&tpe_world.bodies[0],onGroundCount ? 300 : 505);
  71. s3l_scene.camera.transform.translation.z = tpe_world.bodies[0].joints[0].position.z;
  72. s3l_scene.camera.transform.rotation.y = -1 * rotation;
  73. TPE_worldStep(&tpe_world);
  74. TPE_bodyActivate(&tpe_world.bodies[0]);
  75. if (onGroundCount)
  76. {
  77. if (sdl_keyboard[SDL_SCANCODE_SPACE] && jumpCountdown == 0 && onGroundCount)
  78. {
  79. tpe_world.bodies[0].joints[0].velocity[1] = 80;
  80. jumpCountdown = 8;
  81. onGroundCount = 0;
  82. }
  83. #define AAA 16
  84. if (sdl_keyboard[SDL_SCANCODE_UP] || sdl_keyboard[SDL_SCANCODE_W])
  85. {
  86. tpe_world.bodies[0].joints[0].velocity[0] += directionVec.x / AAA;
  87. tpe_world.bodies[0].joints[0].velocity[2] += directionVec.z / AAA;
  88. }
  89. else if (sdl_keyboard[SDL_SCANCODE_DOWN] || sdl_keyboard[SDL_SCANCODE_S])
  90. {
  91. tpe_world.bodies[0].joints[0].velocity[0] -= directionVec.x / AAA;
  92. tpe_world.bodies[0].joints[0].velocity[2] -= directionVec.z / AAA;
  93. }
  94. if (sdl_keyboard[SDL_SCANCODE_A])
  95. {
  96. tpe_world.bodies[0].joints[0].velocity[2] += directionVec.x / AAA;
  97. tpe_world.bodies[0].joints[0].velocity[0] -= directionVec.z / AAA;
  98. }
  99. else if (sdl_keyboard[SDL_SCANCODE_D])
  100. {
  101. tpe_world.bodies[0].joints[0].velocity[2] -= directionVec.x / AAA;
  102. tpe_world.bodies[0].joints[0].velocity[0] += directionVec.z / AAA;
  103. }
  104. #undef AAA
  105. }
  106. if (sdl_keyboard[SDL_SCANCODE_LEFT])
  107. rotation -= 8;
  108. else if (sdl_keyboard[SDL_SCANCODE_RIGHT])
  109. rotation += 8;
  110. updateDirection();
  111. helper_set3dColor(100,100,100);
  112. helper_draw3dCubeInside(TPE_vec3(0,ROOM_SIZE / 4,0),TPE_vec3(ROOM_SIZE,ROOM_SIZE / 2,ROOM_SIZE),TPE_vec3(0,0,0));
  113. helper_draw3dCube(TPE_vec3(4000,160,4000),TPE_vec3(2000,320,2000),TPE_vec3(0,0,0));
  114. helper_draw3dCube(TPE_vec3(4000,80,2500),TPE_vec3(2000,160,1000),TPE_vec3(0,0,0));
  115. helper_draw3dCube(TPE_vec3(-1000,270,4500),TPE_vec3(8000,540,500),TPE_vec3(0,0,0));
  116. helper_draw3dPlane(
  117. TPE_vec3(0,1500,-3500),
  118. TPE_vec3(10000,512,4000),
  119. TPE_vec3(-64,0,0));
  120. helper_set3dColor(200,50,0);
  121. helper_draw3dCube(
  122. TPE_bodyGetCenter(&tpe_world.bodies[2]),
  123. TPE_vec3(1200,800,1200),
  124. TPE_bodyGetOrientation(&tpe_world.bodies[2],0,2,1)
  125. );
  126. helper_draw3dSphere(
  127. tpe_world.bodies[1].joints[0].position
  128. ,TPE_vec3(1000,1000,1000),TPE_vec3(0,0,0) );
  129. helper_set3dColor(200,10,10);
  130. for (int i = 0; i < tpe_world.bodyCount; ++i)
  131. TPE_bodyApplyGravity(&tpe_world.bodies[i],5);
  132. if (helper_debugDrawOn)
  133. helper_debugDraw();
  134. helper_frameEnd();
  135. }
  136. helper_end();
  137. return 0;
  138. }