Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

112 linhas
4.3 KiB

  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib example - particles trail blending
  4. --
  5. -- This example has been created using raylib 1.6 (www.raylib.com)
  6. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. --
  8. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  9. --
  10. -------------------------------------------------------------------------------------------
  11. MAX_PARTICLES = 200
  12. -- Initialization
  13. -------------------------------------------------------------------------------------------
  14. local screenWidth = 800
  15. local screenHeight = 450
  16. InitWindow(screenWidth, screenHeight, "raylib [textures] example - particles trail blending")
  17. -- Particles pool, reuse them!
  18. local mouseTail = {}
  19. -- Initialize particles
  20. for i = 1, MAX_PARTICLES do
  21. mouseTail[i] = {}
  22. mouseTail[i].position = Vector2(0, 0)
  23. mouseTail[i].color = Color(GetRandomValue(0, 255), GetRandomValue(0, 255), GetRandomValue(0, 255), 255)
  24. mouseTail[i].alpha = 1.0
  25. mouseTail[i].size = GetRandomValue(1, 30)/20.0
  26. mouseTail[i].rotation = GetRandomValue(0, 360)
  27. mouseTail[i].active = false
  28. end
  29. local gravity = 3.0
  30. local smoke = LoadTexture("resources/smoke.png")
  31. local blending = BlendMode.ALPHA
  32. SetTargetFPS(60)
  33. -------------------------------------------------------------------------------------------
  34. -- Main game loop
  35. while not WindowShouldClose() do -- Detect window close button or ESC key
  36. -- Update
  37. ---------------------------------------------------------------------------------------
  38. -- Activate one particle every frame and Update active particles
  39. -- NOTE: Particles initial position should be mouse position when activated
  40. -- NOTE: Particles fall down with gravity and rotation... and disappear after 2 seconds (alpha = 0)
  41. -- NOTE: When a particle disappears, active = false and it can be reused.
  42. for i = 1, MAX_PARTICLES do
  43. if (not mouseTail[i].active) then
  44. mouseTail[i].active = true
  45. mouseTail[i].alpha = 1.0
  46. mouseTail[i].position = GetMousePosition()
  47. break
  48. end
  49. end
  50. for i = 1, MAX_PARTICLES do
  51. if (mouseTail[i].active) then
  52. mouseTail[i].position.y = mouseTail[i].position.y + gravity
  53. mouseTail[i].alpha = mouseTail[i].alpha - 0.01
  54. if (mouseTail[i].alpha <= 0.0) then mouseTail[i].active = false end
  55. mouseTail[i].rotation = mouseTail[i].rotation + 5.0
  56. end
  57. end
  58. if (IsKeyPressed(KEY.SPACE)) then
  59. if (blending == BlendMode.ALPHA) then blending = BlendMode.ADDITIVE
  60. else blending = BlendMode.ALPHA end
  61. end
  62. ---------------------------------------------------------------------------------------
  63. -- Draw
  64. ---------------------------------------------------------------------------------------
  65. BeginDrawing()
  66. ClearBackground(DARKGRAY)
  67. BeginBlendMode(blending)
  68. -- Draw active particles
  69. for i = 1, MAX_PARTICLES do
  70. if (mouseTail[i].active) then
  71. DrawTexturePro(smoke, Rectangle(0, 0, smoke.width, smoke.height),
  72. Rectangle(mouseTail[i].position.x, mouseTail[i].position.y,
  73. smoke.width*mouseTail[i].size//1, smoke.height*mouseTail[i].size//1),
  74. Vector2(smoke.width*mouseTail[i].size/2, smoke.height*mouseTail[i].size/2),
  75. mouseTail[i].rotation, Fade(mouseTail[i].color, mouseTail[i].alpha)) end
  76. end
  77. EndBlendMode()
  78. DrawText("PRESS SPACE to CHANGE BLENDING MODE", 180, 20, 20, BLACK)
  79. if (blending == BlendMode.ALPHA) then DrawText("ALPHA BLENDING", 290, screenHeight - 40, 20, BLACK)
  80. else DrawText("ADDITIVE BLENDING", 280, screenHeight - 40, 20, RAYWHITE) end
  81. EndDrawing()
  82. ---------------------------------------------------------------------------------------
  83. end
  84. -- De-Initialization
  85. -------------------------------------------------------------------------------------------
  86. UnloadTexture(smoke)
  87. CloseWindow() -- Close window and OpenGL context
  88. -------------------------------------------------------------------------------------------