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

  1. import Bladex
  2. import Actions
  3. import ObjStore
  4. import GameStateAux
  5.  
  6. class ButtonCombination:
  7.  
  8.     ObjId=""
  9.     activates = 0
  10.     corrects = 0
  11.     n_buttons = 0
  12.     correct_func = None
  13.     failed_func = None
  14.     order = 0
  15.     state = 0    
  16.     combi = 0
  17.     first_button = 0
  18.     last_button = 0
  19.     Cycle       = -1
  20.  
  21.  
  22.  
  23.  
  24.     def __init__(self,correct_func,failed_func):
  25.         self.ObjId=ObjStore.GetNewId() # Para identificarlo al grabar/guardar
  26.         self.correct_func = correct_func
  27.         self.failed_func = failed_func
  28.         ObjStore.ObjectsStore[self.ObjId]=self
  29.  
  30.     def AddButton(self,button_name,time,dir,dist,order,state,correct_state):
  31.         button = CreateButton(button_name,time,dir,dist,order,state,correct_state,self)
  32.  
  33.         if (self.first_button):
  34.             last_button = Bladex.GetEntity(self.last_button).Data
  35.             last_button.next_button = button_name
  36.         else:
  37.             self.first_button = button_name
  38.  
  39.         self.last_button = button_name
  40.         return button
  41.  
  42.     def Reset(self,type,entity = "Player1"):
  43.         button = Bladex.GetEntity(self.first_button).Data
  44.         char = Bladex.GetEntity(entity)
  45.  
  46.         #if (self.state <> 0):
  47.         #    self.activates = 
  48.  
  49.         for i in range(self.n_buttons):
  50.             button_e = Bladex.GetEntity(button.entity)
  51.             char.Data.obj_used = button_e
  52.  
  53.             if (self.state == 0):
  54.                 if (type):
  55.                     ActivateButton(entity,0)
  56.                 else:
  57.                     button.state = 0
  58.             else:
  59.                 if (type):
  60.                     if(button.state <> button.initial_state):
  61.                         ActivateButton(enmtity,0)
  62.                 else:
  63.                     if(button.state <> button.initial_state):
  64.                         button.state = button.initial_state
  65.  
  66.             button = Bladex.GetEntity(button.next_button).Data            
  67.  
  68.         if (self.state == 0):
  69.             self.activates = 0
  70.             self.corrects = 0
  71.  
  72.     def __setstate__(self,parm):
  73.         if parm[0]==1:
  74.             self.ObjId=parm[1]
  75.             ObjStore.ObjectsStore[self.ObjId]=self
  76.  
  77.             self.activates=parm[2]
  78.             self.corrects=parm[3]
  79.             self.n_buttons=parm[4]
  80.             GameStateAux.LoadFunctionAux(parm[5],self,"correct_func")
  81.             GameStateAux.LoadFunctionAux(parm[6],self,"failed_func")
  82.             self.order=parm[7]
  83.             self.state=parm[8]
  84.             self.combi=parm[9]
  85.             self.first_button=parm[10]
  86.             self.last_button=parm[11]
  87.             self.Cycle=parm[12]
  88.  
  89.         else:
  90.             print "ButtonCombination.__setstate__() -> Version mismatch"
  91.             # Valores por si valen para algo.
  92.             self.ObjId=ObjStore.GetNewId()
  93.             ObjStore.ObjectsStore[self.ObjId]=self
  94.  
  95.     def __getstate__(self):
  96.             return (1,
  97.                     self.ObjId,
  98.                     self.activates,
  99.                     self.corrects,
  100.                     self.n_buttons,
  101.                     GameStateAux.SaveFunctionAux(self.correct_func),
  102.                     GameStateAux.SaveFunctionAux(self.failed_func),
  103.                     self.order,
  104.                     self.state,    
  105.                     self.combi,
  106.                     self.first_button,
  107.                     self.last_button,
  108.                     self.Cycle
  109.                     )
  110.  
  111.     def persistent_id(self):
  112.         return self.ObjId
  113.  
  114.     def __del__(self):
  115.         del ObjStore.ObjectsStore[self.ObjId]
  116.  
  117.  
  118. def CreateButtonCombination(type=0,correct_func="",failed_func=""):
  119.     combi = ButtonCombination(correct_func,failed_func)
  120.  
  121.     if (type == 1):
  122.         combi.order = 1
  123.  
  124.     if (type == 2):
  125.         combi.state = 1    
  126.  
  127.     return combi
  128.  
  129.  
  130. class BUTTON:
  131.  
  132.     ObjId=""
  133.     time=0
  134.     last_time = 0
  135.     finish_time = 0
  136.     state = 0
  137.     correct_state = 1
  138.     v = [0,0,0]
  139.     dir = [0,0,0]
  140.     order = 0
  141.     position_i = [0,0,0]
  142.     position_f = [0,0,0]
  143.     entity = 0
  144.     sound=None
  145.     combi=None
  146.  
  147.     def __init__(self):        
  148.         self.ObjId=ObjStore.GetNewId() # Para identificarlo al grabar/guardar
  149.         self.sound = Bladex.CreateSound('..\\..\\Sounds\\block-sliding.wav', 'Sound Button')
  150.         self.sound.Volume=1.0
  151.         self.sound.MinDistance=7000
  152.         self.sound.MaxDistance=20000
  153.         self.next_button = 0
  154.         self.position_i = [0,0,0]
  155.         self.position_f = [0,0,0]
  156.         self.v = [0,0,0]
  157.         self.dir = [0,0,0]
  158.         self.combi=None
  159.  
  160.  
  161.         ObjStore.ObjectsStore[self.ObjId]=self
  162.  
  163.     def __getstate__(self):
  164.         # Tiene que devolver cΣ«» poder guardar el estado de la clase
  165.         return (1,
  166.                 self.ObjId,
  167.                 self.last_time,
  168.                 self.finish_time,
  169.                 self.state,
  170.                 self.correct_state,
  171.                 self.v,
  172.                 self.dir,
  173.                 self.order,
  174.                 self.position_i,
  175.                 self.position_f,
  176.                 self.entity,
  177.                 self.sound.Name,
  178.                 self.next_button,
  179.                 self.combi,
  180.                 self.time
  181.                 )
  182.  
  183.  
  184.     def __setstate__(self,parm):
  185.         # Toma como parΓ«Ñtro lo que devuelve __getstate__() y debe recrear la clase
  186.         if parm[0]==1:
  187.             #self.ObjId=parm[1] En GameStateAux.PersistentObject()
  188. ##            GameStateAux.PersistentObject.__setstate__(self,parm)
  189.             self.ObjId=parm[1]
  190.             ObjStore.ObjectsStore[self.ObjId]=self
  191.             self.last_time=parm[2]
  192.             self.finish_time=parm[3]
  193.             self.state=parm[4]
  194.             self.correct_state=parm[5]
  195.             self.v=parm[6]
  196.             self.dir=parm[7]
  197.             self.order=parm[8]
  198.             self.position_i=parm[9]
  199.             self.position_f=parm[10]
  200.             self.entity=parm[11]
  201.             sound_Name=parm[12]
  202.             self.sound=Bladex.GetSound(sound_Name)
  203.             self.next_button=parm[13]
  204.             self.combi=parm[14]
  205.             self.time=parm[15]
  206.         else:
  207.             print "Door.__setstate__() -> Version mismatch"
  208.             # Valores por si valen para algo.
  209.             self.ObjId=ObjStore.GetNewId()
  210.             ObjStore.ObjectsStore[self.ObjId]=self
  211.             self.sound = Bladex.CreateSound('..\\..\\Sounds\\block-sliding.wav', 'Sound Button')
  212.             self.sound.Volume=1.0
  213.             self.sound.MinDistance=7000
  214.             self.sound.MaxDistance=20000
  215.             self.next_button = 0
  216.             self.combi=None
  217.             self.time=0
  218.  
  219.  
  220.     def persistent_id(self):
  221.         return self.ObjId
  222.  
  223.     def __del__(self):
  224.         del ObjStore.ObjectsStore[self.ObjId]
  225.  
  226.  
  227. def MoveButton(button_name,time):
  228.     button_e = Bladex.GetEntity(button_name)
  229.     button = button_e.Data
  230.  
  231.     timer = Bladex.GetTime()
  232.  
  233.     itime = timer - button.last_time
  234.  
  235.     if (timer < button.finish_time):
  236.         x = button_e.Position[0] + itime * button.v[0]
  237.         y = button_e.Position[1] + itime * button.v[1]
  238.         z = button_e.Position[2] + itime * button.v[2]
  239.  
  240.         button_e.Position = (x,y,z)
  241.     else:
  242.         if (button.state):
  243.             #button_e.Position = button.position_f
  244.             x = button.position_f[0]
  245.             y = button.position_f[1]
  246.             z = button.position_f[2]
  247.  
  248.             button_e.Position = (x,y,z)
  249.  
  250.         else:
  251.             button_e.Position = button.position_i
  252.         
  253.         button_e.RemoveFromList("TimerButton")
  254.         button.sound.Stop()
  255.  
  256.     button.last_time = timer
  257.  
  258. def ActivateButton(entity,event):
  259.     Char = Bladex.GetEntity(entity)
  260.  
  261.     button_e = Char.Data.obj_used
  262.     button = button_e.Data
  263.     combi = button.combi
  264.  
  265.     Bladex.CreateTimer("TimerButton",1.0/60.0)
  266.     button_e.SubscribeToList("TimerButton")
  267.  
  268.     button.sound.Play(button_e.Position[0],button_e.Position[1],button_e.Position[2],combi.Cycle)
  269.  
  270.     if (button.state):
  271.         button.state = 0
  272.     else:
  273.         button.state = 1
  274.  
  275.     button.v[0] = button.v[0] * -1
  276.     button.v[1] = button.v[1] * -1
  277.     button.v[2] = button.v[2] * -1
  278.  
  279.     combi.activates = combi.activates + 1
  280.  
  281.     print "Buttons Activados ",combi.activates
  282.  
  283.     if (combi.order and button.order == combi.activates):
  284.         combi.corrects = combi.corrects + 1
  285.     elif (combi.state):
  286.         if (button.correct_state == button.state):
  287.             combi.corrects = combi.corrects + 1
  288.         else:            
  289.             combi.corrects = combi.corrects - 1
  290.         combi.activates = combi.corrects    
  291.     
  292.     if (combi.activates == combi.n_buttons):
  293.         if (combi.order or combi.state):
  294.             if (combi.corrects == combi.n_buttons and combi.correct_func):
  295.                 apply(combi.correct_func,())
  296.             elif (combi.failed_func):
  297.                 apply(combi.failed_func,())
  298.         elif (combi.correct_func):
  299.             apply(combi.correct_func,())
  300.  
  301.     time = Bladex.GetTime()
  302.     button.last_time = time
  303.     button.finish_time = time + button.time
  304.     
  305.     Char.DelAnmEventFunc("Activate")
  306.     Char.Data.obj_used = None
  307.     
  308.  
  309. def UseButton(button_name,type):
  310.     button_e = Bladex.GetEntity(button_name)
  311.     button = button_e.Data;
  312.     
  313.     if button.state == 0 or button.combi.state:
  314.         Char = Bladex.GetEntity("Player1")
  315.         Actions.QuickTurnToFaceEntity ("Player1",button_name)
  316.         Char.LaunchAnmType("pulsador")
  317.         #Char.LaunchAnimation("Kgt_pulsador")
  318.         Char.Data.obj_used = button_e
  319.         Char.AddAnmEventFunc("Activate",ActivateButton)
  320.  
  321. def Turn(name):
  322.     button_e = Bladex.GetEntity(name)    
  323.     button = button_e.Data;
  324.     
  325.     if button.state == 0 or button.combi.state:    
  326.         Char = Bladex.GetEntity("Player1")
  327.         Char.Data.obj_used = button_e
  328.         apply(ActivateButton,("Player1","Activate"))
  329.  
  330. def HitFunc(stick,sticker,x,y,z,xc,yc,zc,wcx,wcy,wcz,wdx,wdy,wdz):
  331.     button_e = Bladex.GetEntity(stick)    
  332.     button = button_e.Data;    
  333.     if button.state == 0 or button.combi.state:    
  334.         Char = Bladex.GetEntity("Player1")
  335.         Char.Data.obj_used = button_e
  336.         apply(ActivateButton,("Player1","Activate"))
  337.     
  338. def StickArrow(sticker,stick):
  339.     button_e = Bladex.GetEntity(stick)    
  340.     button = button_e.Data;    
  341.     if button.state == 0 or button.combi.state:    
  342.         Char = Bladex.GetEntity("Player1")
  343.         Char.Data.obj_used = button_e
  344.         apply(ActivateButton,("Player1","Activate"))
  345.     
  346. def CreateButton(button_name,time,dir,dist,order,state,correct_state,combi):
  347.     button = BUTTON()
  348.     button_e = Bladex.GetEntity(button_name)
  349.     button_e.Static = 0
  350.     button_e.Data = button
  351.     button_e.UseFunc = UseButton
  352.     button_e.TimerFunc = MoveButton
  353.     button_e.StickFunc = StickArrow
  354.     button_e.HitFunc = HitFunc
  355.     button.entity = button_name
  356.  
  357.     button.position_i = button_e.Position
  358.     button.position_f[0] = button_e.Position[0] + dist * dir[0]
  359.     button.position_f[1] = button_e.Position[1] + dist * dir[1]
  360.     button.position_f[2] = button_e.Position[2] + dist * dir[2]
  361.  
  362.     button.combi = combi
  363.  
  364.     a = dist / time
  365.  
  366.     if (combi.state and correct_state == state):
  367.         combi.corrects = combi.corrects + 1
  368.         combi.activates = combi.activates + 1
  369.  
  370.     if (state):
  371.         button_e.Position = button.position_f
  372.         button.v[0] = a * dir[0]
  373.         button.v[1] = a * dir[1]
  374.         button.v[2] = a * dir[2]
  375.     else:
  376.         button.v[0] = a * dir[0] * -1
  377.         button.v[1] = a * dir[1] * -1
  378.         button.v[2] = a * dir[2] * -1
  379.  
  380.     button.order = order
  381.     button.time = time
  382.     button.state = state
  383.     button.initial_state = state
  384.     button.correct_state = correct_state
  385.     
  386.     combi.n_buttons = combi.n_buttons + 1
  387.  
  388.     return button