IntervalTest.py 7.1 KB

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