Package pyzzle :: Module Switch
[hide private]
[frames] | no frames]

Source Code for Module pyzzle.Switch

 1  """A collection of slides and hotspots that represent  
 2  an object within on/off modes  
 3  """ 
 4  import pyzzle 
 5  from Slide import Slide 
 6  from Hotspot import Hotspot 
 7  from DB import Table,Row 
8 9 -class Switch:
10 """A collection of slides and hotspots that together represent 11 an object within the game world that has persistant on/off modes 12 (switch, lever, button, etc.) 13 14 @warning: The Switch class is still very early in design, 15 and I reserve the right to radically change as I see fit. 16 You have been warned. 17 """ 18 __metaclass__=Table 19 20 21 @staticmethod
22 - def _load(cells):
23 row=Row(cells) 24 onslide=Slide[row.onslide] if row.onslide else None 25 offslide=Slide[row.offslide] if row.offslide else None 26 switches=[] 27 if row.onhotspot: switches+=[Hotspot[row.onhotspot]] 28 if row.offhotspot: switches+=[Hotspot[row.offhotspot]] 29 return Switch(onslide,offslide, switches, on=row.on, id=row.id)
30 - def __init__(self, onslide, offslide, hotspots, on=False, 31 onSwitch=lambda self:Switch.switch(self), id=None):
32 if id: Switch.rows[id]=self 33 self.id=id 34 self.onslide=onslide 35 self.offslide=offslide 36 self.hotspots=hotspots 37 self.on=on 38 self._hotspot=None 39 self.onSwitch=onSwitch 40 def onClick(hotspot): 41 self._hotspot=hotspot 42 self.onSwitch(self)
43 for hotspot in self.hotspots: 44 hotspot.onClick=onClick
45
46 - def switch(self):
47 self.on=not self.on 48 if self.onslide != self.offslide and all([self.onslide, self.offslide]): 49 oldslide =self.offslide if self.on else self.onslide 50 newslide =self.onslide if self.on else self.offslide 51 for link in oldslide.links: 52 if link not in self.hotspots: 53 link.link=newslide 54 if self._hotspot: 55 self._hotspot.transition()
56