FPS_ShootatBoxTarget.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "raylib.h"
  2. #include <stdlib.h>
  3. #include <math.h>
  4. int main(void)
  5. {
  6. // Initialization
  7. //--------------------------------------------------------------------------------------
  8. const int screenWidth = 800;
  9. const int screenHeight = 450;
  10. InitWindow(screenWidth, screenHeight, "raylib example.");
  11. // We generate a checked image for texturing
  12. Image checked = GenImageChecked(2, 2, 1, 1, BLACK, DARKGRAY);
  13. Texture2D texture = LoadTextureFromImage(checked);
  14. UnloadImage(checked);
  15. Model model = { 0 };
  16. model = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  17. // Set checked texture as default diffuse component for all models material
  18. model.materials[0].maps[MAP_DIFFUSE].texture = texture;
  19. // Define the camera to look into our 3d world
  20. Camera camera = { { 5.0f, 5.0f, 5.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  21. SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set camera mode
  22. // Our bullet setup
  23. //
  24. Vector3 bulletpos;
  25. bulletpos = camera.position;
  26. Vector3 bulletinc;
  27. bulletinc.x = 0.05;
  28. bulletinc.y = 0.00;
  29. bulletinc.z = 0.0;
  30. // Our bullet model
  31. Model bullet = { 0 };
  32. bullet = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  33. // Set checked texture as default diffuse component for all models material
  34. bullet.materials[0].maps[MAP_DIFFUSE].texture = texture;
  35. // Our target model
  36. //
  37. Vector3 targetboxposition;
  38. targetboxposition.x = 20;
  39. targetboxposition.y = 10;
  40. targetboxposition.z = 20;
  41. Model targetbox = { 0 };
  42. targetbox = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  43. // Set checked texture as default diffuse component for all models material
  44. targetbox.materials[0].maps[MAP_DIFFUSE].texture = texture;
  45. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  46. //--------------------------------------------------------------------------------------
  47. // Main game loop
  48. while (!WindowShouldClose()) // Detect window close button or ESC key
  49. {
  50. // Update
  51. //----------------------------------------------------------------------------------
  52. UpdateCamera(&camera); // Update internal camera and our camera
  53. // Shoot the bullet.
  54. if(IsKeyDown(KEY_SPACE)){
  55. bulletpos = camera.position;
  56. // Get the direction of where to shoot the bullet at.
  57. float x1 = camera.position.x;
  58. float y1 = camera.position.y;
  59. float z1 = camera.position.z;
  60. float x2 = camera.target.x;
  61. float y2 = camera.target.y;
  62. float z2 = camera.target.z;
  63. float delta_x = x2 - x1;
  64. float delta_y = y2 - y1;
  65. float delta_z = z2 - z1;
  66. float len = sqrt(delta_x*delta_x+delta_y*delta_y+delta_z*delta_z);
  67. delta_x = delta_x / len;
  68. delta_y = delta_y / len;
  69. delta_z = delta_z / len;
  70. bulletinc.x = delta_x;
  71. bulletinc.y = delta_y;
  72. bulletinc.z = delta_z;
  73. }
  74. // update the bullet position.
  75. bulletpos.x += bulletinc.x;
  76. bulletpos.y += bulletinc.y;
  77. bulletpos.z += bulletinc.z;
  78. // collision between the bullet and the targetbox.
  79. // Check collisions player vs enemy-box
  80. if (CheckCollisionBoxes(
  81. (BoundingBox){(Vector3){ bulletpos.x - 0.5f,
  82. bulletpos.y - 0.5f,
  83. bulletpos.z - 0.5f },
  84. (Vector3){ bulletpos.x + 0.5f,
  85. bulletpos.y + 0.5f,
  86. bulletpos.z + 0.5f }},
  87. (BoundingBox){(Vector3){ targetboxposition.x - 0.5f,
  88. targetboxposition.y - 0.5f,
  89. targetboxposition.z - 0.5f },
  90. (Vector3){ targetboxposition.x + 0.5f,
  91. targetboxposition.y + 0.5f,
  92. targetboxposition.z + 0.5f }})){
  93. // Put the target box at a new position.
  94. targetboxposition = (Vector3){(float)GetRandomValue(5,40),(float)GetRandomValue(2,10),(float)GetRandomValue(5,40)};
  95. }
  96. //----------------------------------------------------------------------------------
  97. // Draw
  98. //----------------------------------------------------------------------------------
  99. BeginDrawing();
  100. ClearBackground(RAYWHITE);
  101. BeginMode3D(camera);
  102. // Draw a floor.
  103. for(int x=0;x<50;x++){
  104. for(int z=0;z<50;z++){
  105. Vector3 position;
  106. position.x = x;
  107. position.y = 0;
  108. position.z = z;
  109. DrawModel(model, position, 1.0f, WHITE);
  110. }
  111. }
  112. // Draw our bullet.
  113. DrawModel(bullet, bulletpos, 1.0f, WHITE);
  114. // Draw our targetbox.
  115. DrawModel(targetbox, targetboxposition, 1.0f, WHITE);
  116. EndMode3D();
  117. // Draw our Crosshair - Simply a PLUS character drawn with DrawText in RED.
  118. DrawText("+",screenWidth/2-20,screenHeight/2-20,40,RED);
  119. DrawText("Press space and aim to shoot bullet.",100,100,30,RED);
  120. DrawFPS(0,0);
  121. EndDrawing();
  122. //----------------------------------------------------------------------------------
  123. }
  124. // De-Initialization
  125. UnloadTexture(texture); // Unload texture
  126. // Unload models data (GPU VRAM)
  127. UnloadModel(model);
  128. UnloadModel(bullet);
  129. UnloadModel(targetbox);
  130. //--------------------------------------------------------------------------------------
  131. CloseWindow(); // Close window and OpenGL context
  132. //--------------------------------------------------------------------------------------
  133. return 0;
  134. }