home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 56 / CDPowerplay56Disc2.iso / demos / blade / data1.cab / Program_Executable_Files / Lib / Sounds.py < prev    next >
Encoding:
Python Source  |  2000-10-27  |  2.5 KB  |  88 lines

  1.  
  2. import Bladex
  3. import whrandom
  4. import ObjStore
  5. import GameStateAux
  6.  
  7. def RepeatPeriodicSound(persound):
  8.  
  9.   if (persound.OnPlay):
  10.     persound.sound.PlaySound(0)
  11.     variation=whrandom.uniform(-persound.frec_variation, persound.frec_variation)
  12.     Bladex.AddScheduledFunc(Bladex.GetTime()+persound.frecuency+variation, RepeatPeriodicSound, (persound,),"RepeatPeriodicSound")
  13.  
  14.  
  15.  
  16. class PeriodicSound:
  17.  
  18.   OnPlay=0
  19.   ObjId=None
  20.   sound=None
  21.   frecuency=0
  22.   frec_variation=0
  23.  
  24.   def __init__(self):
  25.     self.ObjId=ObjStore.GetNewId() # Para identificarlo al grabar/guardar
  26.     ObjStore.ObjectsStore[self.ObjId]=self
  27.  
  28.   def PlayPeriodic(self):
  29.  
  30.     self.OnPlay=1
  31.     self.sound.PlaySound(0)
  32.     variation=whrandom.uniform(-self.frec_variation, self.frec_variation)
  33.     Bladex.AddScheduledFunc(Bladex.GetTime()+self.frecuency+variation, RepeatPeriodicSound, (self,),"PeriodicSound::PlayPeriodic")
  34.  
  35.   def StopPeriodic(self):
  36.  
  37.     self.OnPlay=0
  38.  
  39.   def persistent_id(self):
  40.     return self.ObjId
  41.  
  42.  
  43.   def __getstate__(self):
  44.     # Tiene que devolver cΣ«» poder guardar el estado de la clase
  45.     return (1,
  46.             self.ObjId,
  47.             self.OnPlay,
  48.             GameStateAux.SaveEntityAux(self.sound),
  49.             self.frecuency,
  50.             self.frec_variation
  51.            )
  52.  
  53.   def __setstate__(self,parm):
  54.     # Toma como parΓ«Ñtro lo que devuelve __getstate__() y debe recrear la clase
  55.     if parm[0]==1:
  56.       self.ObjId=parm[1]
  57.       ObjStore.ObjectsStore[self.ObjId]=self
  58.       self.sound=GameStateAux.LoadEntityAux(parm[2])
  59.       self.frecuency=parm[3]
  60.       self.frec_variation=parm[4]
  61.     else:
  62.       print "Warning -> Version mismatch in PeriodicSound.__setstate__()"
  63.       self.OnPlay=0
  64.       self.sound=None
  65.       self.frecuency=0
  66.       self.frec_variation=0
  67.       self.ObjId=ObjStore.GetNewId() # Para identificarlo al grabar/guardar
  68.       ObjStore.ObjectsStore[self.ObjId]=self
  69.  
  70.  
  71. def CreatePeriodicSound(file_name, sound_name, frecuency, frec_variation, focus):
  72.  
  73.   persound=PeriodicSound()
  74.   persound.sound=Bladex.CreateEntity(sound_name, "Entity Sound", focus[0], focus[1], focus[2])
  75.   persound.sound.SetSound(file_name)
  76.   persound.frecuency=frecuency
  77.   persound.frec_variation=frec_variation
  78.   persound.sound.Data=persound
  79.   return persound
  80.  
  81.  
  82.  
  83. def CreateEntitySound(file_name, sound_name, focus=(0.0, 0.0, 0.0)):
  84.  
  85.   entsound=Bladex.CreateEntity(sound_name, "Entity Sound", focus[0], focus[1], focus[2])
  86.   entsound.SetSound(file_name)
  87.   return entsound
  88.