FPS_Shoot_Bullet.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. Vector3 bulletpos;
  23. bulletpos = camera.position;
  24. Vector3 bulletinc;
  25. bulletinc.x = 0.05;
  26. bulletinc.y = 0.00;
  27. bulletinc.z = 0.0;
  28. Model bullet = { 0 };
  29. bullet = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  30. // Set checked texture as default diffuse component for all models material
  31. bullet.materials[0].maps[MAP_DIFFUSE].texture = texture;
  32. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  33. //--------------------------------------------------------------------------------------
  34. // Main game loop
  35. while (!WindowShouldClose()) // Detect window close button or ESC key
  36. {
  37. // Update
  38. //----------------------------------------------------------------------------------
  39. UpdateCamera(&camera); // Update internal camera and our camera
  40. // Shoot the bullet.
  41. if(IsKeyDown(KEY_SPACE)){
  42. bulletpos = camera.position;
  43. // Get the direction of where to shoot the bullet at.
  44. float x1 = camera.position.x;
  45. float y1 = camera.position.y;
  46. float z1 = camera.position.z;
  47. float x2 = camera.target.x;
  48. float y2 = camera.target.y;
  49. float z2 = camera.target.z;
  50. float delta_x = x2 - x1;
  51. float delta_y = y2 - y1;
  52. float delta_z = z2 - z1;
  53. float len = sqrt(delta_x*delta_x+delta_y*delta_y+delta_z*delta_z);
  54. delta_x = delta_x / len;
  55. delta_y = delta_y / len;
  56. delta_z = delta_z / len;
  57. bulletinc.x = delta_x;
  58. bulletinc.y = delta_y;
  59. bulletinc.z = delta_z;
  60. }
  61. // update the bullet.
  62. bulletpos.x += bulletinc.x;
  63. bulletpos.y += bulletinc.y;
  64. bulletpos.z += bulletinc.z;
  65. //----------------------------------------------------------------------------------
  66. // Draw
  67. //----------------------------------------------------------------------------------
  68. BeginDrawing();
  69. ClearBackground(RAYWHITE);
  70. BeginMode3D(camera);
  71. // Draw a floor.
  72. for(int x=0;x<50;x++){
  73. for(int z=0;z<50;z++){
  74. Vector3 position;
  75. position.x = x;
  76. position.y = 0;
  77. position.z = z;
  78. DrawModel(model, position, 1.0f, WHITE);
  79. }
  80. }
  81. // Draw our bullet.
  82. DrawModel(bullet, bulletpos, 1.0f, WHITE);
  83. EndMode3D();
  84. DrawText("Press space and aim to shoot bullet.",100,100,30,YELLOW);
  85. DrawFPS(0,0);
  86. EndDrawing();
  87. //----------------------------------------------------------------------------------
  88. }
  89. // De-Initialization
  90. UnloadTexture(texture); // Unload texture
  91. // Unload models data (GPU VRAM)
  92. UnloadModel(model);
  93. UnloadModel(bullet);
  94. //--------------------------------------------------------------------------------------
  95. CloseWindow(); // Close window and OpenGL context
  96. //--------------------------------------------------------------------------------------
  97. return 0;
  98. }