test_particlesystem.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import pytest
  2. pytest.importorskip("panda3d.physics")
  3. from panda3d.core import NodePath, PandaNode
  4. from direct.particles.ParticleEffect import ParticleEffect
  5. from direct.particles.Particles import Particles
  6. def test_particle_birth_rate():
  7. # Tests a system with a standard birth rate of 0.5, that it is
  8. # indeed birthing at that rate. It serves as a control for the
  9. # next test as well.
  10. system = Particles("testSystem", 2)
  11. system.set_render_parent(NodePath(PandaNode("test")))
  12. system.set_spawn_render_node_path(NodePath(PandaNode("test")))
  13. assert system.get_birth_rate() == 0.5
  14. assert system.get_tics_since_birth() == 0
  15. assert system.get_living_particles() == 0
  16. system.update(0.6)
  17. assert system.get_living_particles() == 1
  18. system.update(0.5)
  19. assert system.get_living_particles() == 2
  20. # Should still be 2, since the pool size was 2.
  21. system.update(0.5)
  22. assert system.get_living_particles() == 2
  23. def test_particle_soft_start():
  24. # Create a particle effect and a particle system.
  25. # The effect serves to test the Python-level "soft_start" method,
  26. # while the system serves to test the C++-level "soft_start" method
  27. # (via the associated Python "soft_start" method)
  28. effect = ParticleEffect()
  29. system = Particles("testSystem", 10)
  30. # Setup some dummy nodes, since it seems to want them
  31. system.set_render_parent(NodePath(PandaNode("test")))
  32. system.set_spawn_render_node_path(NodePath(PandaNode("test")))
  33. # Add the system to the effect
  34. effect.add_particles(system)
  35. # Re-assign the system, just to make sure that we have the
  36. # right object.
  37. system = effect.get_particles_list()[0]
  38. # First, standard "soft_start"--i.e. without either changing
  39. # the birth-rate or applying a delay. This should work as it
  40. # used to.
  41. effect.soft_start()
  42. assert system.get_birth_rate() == 0.5
  43. # Now, check that the pre-existing single-parameter soft-start,
  44. # which alters the birth-rate, still does so.
  45. system.soft_start(1)
  46. assert system.get_birth_rate() == 1
  47. # Next, birth-delaying.
  48. # Run a standard soft-start, then check that the birth-timer
  49. # is zero, as used to be the case on running this command.
  50. effect.soft_start()
  51. assert system.get_tics_since_birth() == 0
  52. # Run an delayed soft-start via the system, then check that the
  53. # birth-timer has the assigned value, and that the birth-rate is
  54. # unchanged.
  55. # (We pass in a birth-rate ("br") of -1 because the related code
  56. # checks for a birth-rate greater than 0, I believe. This allows
  57. # us to change the delay without affecting the birth-rate.)
  58. system.soft_start(br=-1, first_birth_delay=-2)
  59. assert system.get_birth_rate() == 1
  60. assert system.get_tics_since_birth() == 2
  61. # Now, run a delayed soft-start via the effect, and
  62. # again check that the birth-timer has changed as intended,
  63. # and the birth-rate hasn't changed at all.
  64. effect.soft_start(firstBirthDelay=0.25)
  65. assert system.get_birth_rate() == 1
  66. assert system.get_tics_since_birth() == -0.25
  67. # Update the system, advancing it far enough that it should
  68. # have birthed a particle if not for the delay, but not
  69. # so far that it should have birthed a particle >with<
  70. # the delay. Check thus that no particles have been birthed.
  71. system.update(1)
  72. assert system.get_living_particles() == 0
  73. # Update the system again, this time far enough that with the
  74. # delay it should have birthed just one particle, and
  75. # then check that this is the case.
  76. system.update(1)
  77. assert system.get_living_particles() == 1
  78. def test_particle_burst_emission():
  79. effect = ParticleEffect()
  80. system = Particles("testSystem", 10)
  81. effect.add_particles(system)
  82. # Setup some dummy nodes, since it seems to want them
  83. # We might normally call "start", but that calls "enable", which
  84. # seems to assume that "base" exists and has physics and particle managers...
  85. system.setRenderParent(NodePath(PandaNode("test")))
  86. system.setSpawnRenderNodePath(NodePath(PandaNode("test")))
  87. # However, we don't want normal emission, so we now soft-stop it immediately,
  88. # before the system has a chance to update and emit.
  89. effect.softStop()
  90. # Now, a sanity-check: assert that we have no particles,
  91. # Then update the system, and assert again that we
  92. # have no particles. If so, then we're (hopefully)
  93. # not emitting normally!
  94. assert system.getLivingParticles() == 0
  95. system.update(1)
  96. assert system.getLivingParticles() == 0
  97. # Now, the real test: emit a particle-burst!
  98. effect.birthLitter()
  99. # And assert that a particle has, in fact, been emitted.
  100. assert system.getLivingParticles() == 1
  101. # Check the snake-case version, too.
  102. effect.birth_litter()
  103. assert system.getLivingParticles() == 2