IntervalTest.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. """Undocumented Module"""
  2. __all__ = ()
  3. if __name__ == "__main__":
  4. from panda3d.core import Filename, Point3, Vec3
  5. from direct.showbase.DirectObject import DirectObject
  6. from direct.showbase.ShowBase import ShowBase
  7. from direct.actor.Actor import Actor
  8. from direct.directutil import Mopath
  9. from direct.showbase.MessengerGlobal import messenger
  10. from .ActorInterval import ActorInterval
  11. from .FunctionInterval import (
  12. AcceptInterval,
  13. EventInterval,
  14. FunctionInterval,
  15. IgnoreInterval,
  16. PosHprInterval,
  17. )
  18. from .LerpInterval import LerpPosInterval, LerpHprInterval, LerpPosHprInterval
  19. from .MopathInterval import MopathInterval
  20. from .SoundInterval import SoundInterval
  21. from .MetaInterval import PREVIOUS_END, PREVIOUS_START, TRACK_START, Track
  22. base = ShowBase()
  23. boat = base.loader.loadModel('models/misc/smiley')
  24. boat.reparentTo(base.render)
  25. donald = Actor()
  26. donald.loadModel("phase_6/models/char/donald-wheel-1000")
  27. donald.loadAnims({"steer":"phase_6/models/char/donald-wheel-wheel"})
  28. donald.reparentTo(boat)
  29. dock = base.loader.loadModel('models/misc/smiley')
  30. dock.reparentTo(base.render)
  31. sound = base.loader.loadSfx('phase_6/audio/sfx/SZ_DD_waterlap.mp3')
  32. foghorn = base.loader.loadSfx('phase_6/audio/sfx/SZ_DD_foghorn.mp3')
  33. mp = Mopath.Mopath()
  34. mp.loadFile(Filename('phase_6/paths/dd-e-w'))
  35. # Set up the boat
  36. boatMopath = MopathInterval(mp, boat, 'boatpath')
  37. boatTrack = Track([boatMopath], 'boattrack')
  38. BOAT_START = boatTrack.getIntervalStartTime('boatpath')
  39. BOAT_END = boatTrack.getIntervalEndTime('boatpath')
  40. # This will create an anim interval that is posed every frame
  41. donaldSteerInterval = ActorInterval(donald, 'steer')
  42. # This will create an anim interval that is started at t = 0 and then
  43. # loops for 10 seconds
  44. donaldLoopInterval = ActorInterval(donald, 'steer', loop=1, duration = 10.0)
  45. donaldSteerTrack = Track([donaldSteerInterval, donaldLoopInterval],
  46. name = 'steerTrack')
  47. # Make the dock lerp up so that it's up when the boat reaches the end of
  48. # its mopath
  49. dockLerp = LerpPosHprInterval(dock, 5.0,
  50. pos=Point3(0, 0, -5),
  51. hpr=Vec3(0, 0, 0),
  52. name='dock-lerp')
  53. # We need the dock's state to be defined before the lerp
  54. dockPos = PosHprInterval(dock, dock.getPos(), dock.getHpr(), 1.0, 'dockpos')
  55. dockUpTime = BOAT_END - dockLerp.getDuration()
  56. hpr2 = Vec3(90.0, 90.0, 90.0)
  57. dockLerp2 = LerpHprInterval(dock, 3.0, hpr2, name='hpr-lerp')
  58. dockTrack = Track([dockLerp2, dockPos, dockLerp], 'docktrack')
  59. dockTrack.setIntervalStartTime('dock-lerp', dockUpTime)
  60. dockTrack.setIntervalStartTime('hpr-lerp', BOAT_START)
  61. # Start the water sound 5 seconds after the boat starts moving
  62. waterStartTime = BOAT_START + 5.0
  63. waterSound = SoundInterval(sound, name='watersound')
  64. soundTrack = Track([waterSound], 'soundtrack')
  65. soundTrack.setIntervalStartTime('watersound', waterStartTime)
  66. # Throw an event when the water track ends
  67. eventTime = soundTrack.getIntervalEndTime('watersound')
  68. waterDone = EventInterval('water-is-done')
  69. waterEventTrack = Track([waterDone])
  70. waterEventTrack.setIntervalStartTime('water-is-done', eventTime)
  71. def handleWaterDone():
  72. print('water is done')
  73. # Interval can handle its own event
  74. messenger.accept('water-is-done', waterDone, handleWaterDone)
  75. foghornStartTime = BOAT_START + 4.0
  76. foghornSound = SoundInterval(foghorn, name='foghorn')
  77. soundTrack2 = Track([(foghornStartTime, foghornSound)], 'soundtrack2')
  78. mtrack = MultiTrack([boatTrack, dockTrack, soundTrack, soundTrack2, waterEventTrack, # type: ignore[name-defined]
  79. donaldSteerTrack])
  80. # Print out MultiTrack parameters
  81. print(mtrack)
  82. ### Using lambdas and functions ###
  83. # Using a lambda
  84. i1 = FunctionInterval(lambda: base.transitions.fadeOut())
  85. i2 = FunctionInterval(lambda: base.transitions.fadeIn())
  86. def caughtIt():
  87. print('Caught here-is-an-event')
  88. class DummyAcceptor(DirectObject):
  89. pass
  90. da = DummyAcceptor()
  91. i3 = AcceptInterval(da, 'here-is-an-event', caughtIt)
  92. i4 = EventInterval('here-is-an-event')
  93. i5 = IgnoreInterval(da, 'here-is-an-event')
  94. # Using a function
  95. def printDone():
  96. print('done')
  97. i6 = FunctionInterval(printDone)
  98. # Create track
  99. t1 = Track([
  100. # Fade out
  101. (0.0, i1),
  102. # Fade in
  103. (2.0, i2),
  104. # Accept event
  105. (4.0, i3),
  106. # Throw it,
  107. (5.0, i4),
  108. # Ignore event
  109. (6.0, i5),
  110. # Throw event again and see if ignore worked
  111. (7.0, i4),
  112. # Print done
  113. (8.0, i6)], name = 'demo')
  114. print(t1)
  115. ### Specifying interval start times during track construction ###
  116. # Interval start time can be specified relative to three different points:
  117. # PREVIOUS_END
  118. # PREVIOUS_START
  119. # TRACK_START
  120. startTime = 0.0
  121. def printStart():
  122. global startTime
  123. startTime = base.clock.getFrameTime()
  124. print('Start')
  125. def printPreviousStart():
  126. global startTime
  127. currTime = base.clock.getFrameTime()
  128. print('PREVIOUS_END %0.2f' % (currTime - startTime))
  129. def printPreviousEnd():
  130. global startTime
  131. currTime = base.clock.getFrameTime()
  132. print('PREVIOUS_END %0.2f' % (currTime - startTime))
  133. def printTrackStart():
  134. global startTime
  135. currTime = base.clock.getFrameTime()
  136. print('TRACK_START %0.2f' % (currTime - startTime))
  137. def printArguments(a, b, c):
  138. print('My args were %d, %d, %d' % (a, b, c))
  139. i1 = FunctionInterval(printStart)
  140. # Just to take time
  141. i2 = LerpPosInterval(base.camera, 2.0, Point3(0, 10, 5))
  142. # This will be relative to end of camera move
  143. i3 = FunctionInterval(printPreviousEnd) # type: ignore[assignment]
  144. # Just to take time
  145. i4 = LerpPosInterval(base.camera, 2.0, Point3(0, 0, 5))
  146. # This will be relative to the start of the camera move
  147. i5 = FunctionInterval(printPreviousStart) # type: ignore[assignment]
  148. # This will be relative to track start
  149. i6 = FunctionInterval(printTrackStart)
  150. # This will print some arguments
  151. # This will be relative to track start
  152. i7 = FunctionInterval(printArguments, extraArgs = [1, 10, 100])
  153. # Create the track, if you don't specify offset type in tuple it defaults to
  154. # relative to TRACK_START (first entry below)
  155. t2 = Track([(0.0, i1), # i1 start at t = 0, duration = 0.0
  156. (1.0, i2, TRACK_START), # i2 start at t = 1, duration = 2.0
  157. (2.0, i3, PREVIOUS_END), # i3 start at t = 5, duration = 0.0
  158. (1.0, i4, PREVIOUS_END), # i4 start at t = 6, duration = 2.0
  159. (3.0, i5, PREVIOUS_START), # i5 start at t = 9, duration = 0.0
  160. (10.0, i6, TRACK_START), # i6 start at t = 10, duration = 0.0
  161. (12.0, i7)], # i7 start at t = 12, duration = 0.0
  162. name = 'startTimeDemo')
  163. print(t2)
  164. # Play tracks
  165. # mtrack.play()
  166. # t1.play()
  167. # t2.play()
  168. def test(n):
  169. lerps = []
  170. for i in range(n):
  171. lerps.append(LerpPosHprInterval(dock, 5.0,
  172. pos=Point3(0, 0, -5),
  173. hpr=Vec3(0, 0, 0),
  174. startPos=dock.getPos(),
  175. startHpr=dock.getHpr(),
  176. name='dock-lerp'))
  177. lerps.append(EventInterval("joe"))
  178. t = Track(lerps)
  179. mt = MultiTrack([t])
  180. # return mt
  181. test(5)
  182. base.run()