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

  1. import Bladex
  2. import InitDataField
  3. import Actions
  4. import Reference
  5. import GameStateAux
  6. import ObjStore
  7. import darfuncs
  8. import types
  9.  
  10. LOCK=0
  11. UNLOCK=1
  12.  
  13.     
  14. def ActivateLock(pj_name,event):
  15.     pj=Bladex.GetEntity(pj_name)
  16.     lock=pj.Data.obj_used.Data.lockdata
  17.     if(lock.state==UNLOCK):
  18.         lock.Lock()
  19.     elif(lock.state==LOCK):
  20.         lock.UnLock()
  21.     pj.Data.obj_used=None
  22.     pj.DelAnmEventFunc("Activate")
  23.  
  24. def PlayerHasKey(EntityName, key):
  25.     pj=Bladex.GetEntity("Player1")
  26.     inv = pj.GetInventory()    
  27.     for x in range (0, inv.nKeys):
  28.         if key == inv.GetKey(x):
  29.             return 1
  30.     return 0
  31.  
  32. def KeyDown(pj_name,event):
  33.     pj=Bladex.GetEntity(pj_name)    
  34.     inv = pj.GetInventory()    
  35.     inv.LinkRightHand("None")
  36.     pj.DelAnmEventFunc("Key_down")
  37.  
  38.  
  39. def LockUseFunc(lock_name,use_from):        
  40.     import GameText
  41.     import MenuText
  42.     
  43.     obj=Bladex.GetEntity(lock_name)    
  44.     lock=obj.Data.lockdata
  45.     pj=Bladex.GetEntity("Player1")
  46.     if pj.Wuea==Reference.WUEA_WAIT:
  47.         return
  48.     
  49.     if PlayerHasKey (pj.Name, lock.key):
  50.         if not pj.InvRight:
  51.             pj.Data.SwitchedWeapon = 0
  52.             Actions.QuickTurnToFaceEntity ("Player1", lock_name)
  53.             LockUseFunc2(pj.Name)
  54.         else:
  55.             if Actions.IsRightHandStandardObject(pj.Name):
  56.                 if Actions.TryDropRight(pj.Name):    
  57.                     Actions.DropReleaseEventHandler(pj.Name, "DropRightEvent")
  58.                 pj.Wuea=Reference.WUEA_ENDED                    
  59.                 if not pj.InvRight:
  60.                     pj.Data.SwitchedWeapon = 0
  61.                     Actions.QuickTurnToFaceEntity ("Player1", lock_name)
  62.                     LockUseFunc2(pj.Name)
  63.             else:
  64.                 pj.AddAnmEventFunc("ChangeREvent",Actions.ToggleWEvent)
  65.                 pj.LaunchAnmType("Chg_r")
  66.                 pj.Data.SwitchedWeapon = 1
  67.                 Actions.QuickTurnToFaceEntity ("Player1", lock_name)
  68.                 pj.AnmEndedFunc=LockUseFunc2
  69.     else:
  70.         GameText.WriteTextAux(MenuText.GetMenuText("You don't have the key."),2.0,255,255,255,[])
  71.  
  72.  
  73. def LockUseFunc2(EntityName):
  74.     pj=Bladex.GetEntity(EntityName)
  75.     obj = pj.Data.obj_used    
  76.     lock = obj.Data.lockdata
  77.     inv = pj.GetInventory()    
  78.     inv.LinkRightHand(lock.key)
  79.     pj.LaunchAnmType("key")    
  80.     pj.AddAnmEventFunc("Activate",ActivateLock)
  81.     pj.AddAnmEventFunc("Key_down",KeyDown)
  82.     pj.AnmEndedFunc=LockUseFunc3
  83.  
  84. def LockUseFunc3(EntityName):
  85.     pj=Bladex.GetEntity(EntityName)        
  86.     if pj.Data.SwitchedWeapon:
  87.         pj.AddAnmEventFunc("ChangeREvent",Actions.ToggleWEvent)
  88.         pj.LaunchAnmType("Chg_r")            
  89.  
  90.  
  91.  
  92. class Lock:
  93.  
  94.     ObjId=""
  95.     state=LOCK
  96.     key="Key"
  97.  
  98.     OnLockFunc=None
  99.     OnLockArgs=()
  100.     OnUnLockFunc=None
  101.     OnUnLockArgs=()
  102.  
  103.     UnUsed = 0
  104.  
  105.     obj=None
  106.  
  107.     def __init__(self):
  108.         self.ObjId=ObjStore.GetNewId()
  109.         ObjStore.ObjectsStore[self.ObjId]=self
  110.  
  111.     def __del__(self):
  112.         del ObjStore.ObjectsStore[self.ObjId]
  113.  
  114.     def DecUser(self):
  115.         if self.UnUsed:
  116.             ll = Bladex.GetEntity(self.key)
  117.             if type(ll.Data) is types.IntType:
  118.                 auxv = ll.Data
  119.                 ll.Data = darfuncs.EmptyClass()
  120.                 ll.Data.Used = auxv
  121.  
  122.             if ll.Data == None:
  123.                 ll.Data = darfuncs.EmptyClass()
  124.                 ll.Data.Used = 0
  125.             else:
  126.                 ll.Data.Used = ll.Data.Used-1
  127.             self.UnUsed = 0
  128.  
  129.     
  130.     def Lock(self):
  131.         if(self.state==UNLOCK):
  132.             self.state=LOCK
  133.             if(self.OnLockFunc):
  134.                 self.DecUser()
  135.                 apply(self.OnLockFunc,self.OnLockArgs)
  136.                 
  137.     def UnLock(self):
  138.         if(self.state==LOCK):
  139.             self.state=UNLOCK
  140.             if(self.OnUnLockFunc):
  141.                 self.DecUser()
  142.                 apply(self.OnUnLockFunc,self.OnUnLockArgs)
  143.  
  144.     def persistent_id(self):
  145.         return self.ObjId
  146.  
  147.     def __getstate__(self):
  148.         # Tiene que devolver cΣ«» poder guardar el estado de la clase
  149. ##        print "Lock._getstate__()",self
  150.  
  151.         return (1,
  152.                 self.ObjId,
  153.                 self.state,
  154.                 self.key,
  155.                 GameStateAux.SaveFunctionAux(self.OnLockFunc),
  156.                 self.OnLockArgs,
  157.                 GameStateAux.SaveFunctionAux(self.OnUnLockFunc),
  158.                 self.OnUnLockArgs,
  159.                 self.UnUsed,
  160.                 GameStateAux.SaveEntityAux(self.obj),
  161.                 GameStateAux.SaveNewMembers(self)
  162.                 )
  163.  
  164.     def __setstate__(self,parm):
  165.         # Toma como parΓ«Ñtro lo que devuelve __getstate__() y debe recrear la clase
  166.  
  167.         if parm[0]==1:
  168.             #self.ObjId=parm[1] En GameStateAux.PersistentObject()
  169. ##            GameStateAux.PersistentObject.__setstate__(self,parm)
  170.             self.ObjId=parm[1]
  171.             ObjStore.ObjectsStore[self.ObjId]=self
  172.             self.state=parm[2]
  173.             self.key=parm[3]
  174.             GameStateAux.LoadFunctionAux(parm[4],self,"OnLockFunc")
  175.             self.OnLockArgs=parm[5]
  176.             GameStateAux.LoadFunctionAux(parm[6],self,"OnUnLockFunc")
  177.             self.OnUnLockArgs=parm[7]            
  178.             self.UnUsed=parm[8]
  179.             self.obj=GameStateAux.LoadEntityAux(parm[9]),
  180.             GameStateAux.LoadNewMembers(self,parm[10])
  181.         else:
  182.             print "Lever.__setstate__() -> Version mismatch"
  183.             # Valores de creaciΣ«ápor si valen para algo.
  184.             self.ObjId=ObjStore.GetNewId() # Para identificarlo al grabar/guardar
  185.             ObjStore.ObjectsStore[self.ObjId]=self
  186.             self.state=LOCK
  187.             self.key="Key"
  188.  
  189.             self.OnLockFunc=None
  190.             self.OnLockArgs=()
  191.             self.OnUnLockFunc=None
  192.             self.OnUnLockArgs=()
  193.             self.UnUsed=0
  194.  
  195.  
  196.  
  197. def PlaceLock(name,type,position,orientation,scale):
  198.     lock=Lock()
  199.     lock.UnUsed = 1
  200.     lock.obj=Bladex.CreateEntity(name,type,position[0],position[1],position[2])
  201.     lock.obj.Orientation=orientation
  202.     lock.obj.Scale=scale
  203.     InitDataField.Initialise(lock.obj)
  204.     lock.obj.Data.lockdata=lock
  205.     lock.obj.UseFunc=LockUseFunc    
  206.     return lock
  207.  
  208.  
  209.