Changeset 611 for trunk/game/scripts/objects
- Timestamp:
- 08/01/10 08:59:18 (9 years ago)
- Location:
- trunk/game/scripts/objects
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/game/scripts/objects/actors.py
r609 r611 305 305 CharacterBase.__init__(self, ID, agent_layer, inventory, text, **kwargs) 306 306 self.people_i_know = set() 307 self. is_PC = True307 self.attributes.append("PC") 308 308 309 309 def getStateForSaving(self): … … 361 361 Scriptable.__init__(self, **kwargs) 362 362 363 self. is_NPC = True363 self.attributes.append("NPC") 364 364 self.dialogue = dialogue 365 365 … … 376 376 """ 377 377 ret_dict = CharacterBase.getStateForSaving(self) 378 ret_dict["Lives"] = self. is_living378 ret_dict["Lives"] = self.lives 379 379 ret_dict["State"] = self.behaviour.state 380 380 return ret_dict -
trunk/game/scripts/objects/base.py
r610 r611 27 27 2. In __init__() **ALWAYS** call the parent's __init__(**kwargs), preferably 28 28 *at the end* of your __init__() (makes it easier to follow) 29 3. There should always be an is_x class member set to Trueon __init__29 3. There should always be an attributes.append(x) call on __init__ 30 30 (where X is the name of the class) 31 31 … … 34 34 class Openable(object): 35 35 def __init__ (self, is_open = True, **kwargs): 36 self. is_openable = True36 self.attribbutes.append("openable") 37 37 self.is_open = is_open 38 38 super(Openable,self).__init__ (**kwargs) … … 54 54 self.name = 'Tin Can'""" 55 55 56 class DynamicObject (object): 57 """A base class that only supports dynamic attributes functionality""" 56 class BaseObject(object): 57 """A base class that supports dynamic attributes functionality""" 58 def __init__ (self): 59 if not self.__dict__.has_key("attributes"): 60 self.attributes = [] 61 62 def trueAttr(self, attr): 63 """Method that checks if the instance has an attribute""" 64 return attr in self.attributes 65 66 def getStateForSaving(self): 67 """Returns state for saving 68 """ 69 state = {} 70 state["attributes"] = self.attributes 71 return state 72 73 class DynamicObject (BaseObject): 74 """Class with basic attributes""" 58 75 def __init__ (self, name="Dynamic object", real_name=None, image=None, **kwargs): 59 76 """Initialise minimalistic set of data … … 62 79 @type image: String or None 63 80 @param name: Filename of image to use in inventory""" 81 BaseObject.__init__(self) 64 82 self.name = name 65 83 self.real_name = real_name or name … … 87 105 def __setstate__(self, state): 88 106 self.restoreState(state) 89 90 def trueAttr(self, attr):91 """Shortcut function to check if the current object has a member named92 is_%attr and if that attribute evaluates to True"""93 return hasattr(self,'is_%s' % attr) and getattr(self, 'is_%s' % attr)94 107 95 108 def getStateForSaving(self): 96 109 """Returns state for saving 97 110 """ 98 state = {}111 state = BaseObject.getStateForSaving(self) 99 112 state["Name"] = self.name 100 113 state["RealName"] = self.real_name … … 163 176 164 177 165 class Scriptable ( object):178 class Scriptable (BaseObject): 166 179 """Allows objects to have predefined scripts executed on certain events""" 167 180 def __init__ (self, scripts = None, **kwargs): … … 170 183 @param scripts: Dictionary where the event strings are keys. The 171 184 values are 3-item tuples (function, positional_args, keyword_args)""" 172 self.is_scriptable = True 185 BaseObject.__init__(self) 186 self.attributes.append("scriptable") 173 187 self.scripts = scripts or {} 174 188 … … 194 208 DynamicObject.__init__(self, **kwargs) 195 209 Scriptable.__init__(self, **kwargs) 196 self. is_openable = True210 self.attributes.append("openable") 197 211 self.is_open = is_open 198 212 … … 225 239 It is ignored if locked is True -- locked objects 226 240 are always closed.""" 227 self. is_lockable = True241 self.attributes.append("lockable") 228 242 self.locked = locked 229 243 if locked : … … 251 265 def __init__ (self, weight=0.0, bulk=0.0, **kwargs): 252 266 DynamicObject.__init__(self, **kwargs) 253 self. is_carryable = True267 self.attributes.append("carryable") 254 268 self.in_container = None 255 269 self.on_map = None … … 284 298 DynamicObject.__init__(self, **kwargs) 285 299 Scriptable.__init__(self, **kwargs) 286 self. is_container = True300 self.attributes.append("container") 287 301 self.items = {} 288 302 self.capacity = capacity … … 441 455 ret_state["Items"] = self.serializeItems() 442 456 return ret_state 443 444 class SingleItemContainer (Container) : 445 """Container that can only store a single item. 446 This class can represent single-item inventory slots""" 447 def __init__ (self, **kwargs): 448 Container.__init__(self, **kwargs) 449 450 def placeItem(self,item, index=None): 451 if len(self.items) > 0 : 452 raise self.SlotBusy ('%s is already busy' % self) 453 Container.placeItem(self, item) 454 455 class Living (object): 457 458 class Living (BaseObject): 456 459 """Objects that 'live'""" 457 460 def __init__ (self, **kwargs): 458 self.is_living = True 461 BaseObject.__init__(self) 462 self.attributes.append("living") 463 self.lives = True 459 464 460 465 def die(self): 461 466 """Kills the object""" 462 self. is_living= False463 464 class CharStats ( object):467 self.lives = False 468 469 class CharStats (BaseObject): 465 470 """Provides the object with character statistics""" 466 471 def __init__ (self, **kwargs): 467 self.is_charstats = True 468 469 class Wearable (object): 472 BaseObject.__init__(self) 473 self.attributes.append("charstats") 474 475 class Wearable (BaseObject): 470 476 """Objects than can be weared""" 471 477 def __init__ (self, slots, **kwargs): 472 478 """Allows the object to be worn somewhere on the body (e.g. pants)""" 473 self.is_wearable = True 479 BaseObject.__init__(self) 480 self.attributes.append("wearable") 474 481 if isinstance(slots, tuple) : 475 482 self.slots = slots … … 477 484 self.slots = (slots,) 478 485 479 class Usable ( object):486 class Usable (BaseObject): 480 487 """Allows the object to be used in some way (e.g. a Zippo lighter 481 488 to make a fire)""" 482 489 def __init__ (self, actions = None, **kwargs): 483 self.is_usable = True 490 BaseObject.__init__(self) 491 self.attributes.append("usable") 484 492 self.actions = actions or {} 485 493 486 class Weapon ( object):494 class Weapon (BaseObject): 487 495 """Allows the object to be used as a weapon""" 488 496 def __init__ (self, **kwargs): 489 self.is_weapon = True 490 491 class Destructable (object): 497 BaseObject.__init__(self) 498 self.attributes.append("weapon") 499 500 class Destructable (BaseObject): 492 501 """Allows the object to be destroyed""" 493 502 def __init__ (self, **kwargs): 494 self.is_destructable = True 495 496 class Trappable (object): 503 BaseObject.__init__(self) 504 self.attributes.append("destructable") 505 506 class Trapable (BaseObject): 497 507 """Provides trap slots to the object""" 498 508 def __init__ (self, **kwargs): 499 self.is_trappable = True 509 BaseObject.__init__(self) 510 self.attributes.append("trapable") -
trunk/game/scripts/objects/composed.py
r610 r611 19 19 20 20 from base import GameObject, Container, Lockable, \ 21 Scriptable, Trap pable, Destructable, Carryable, \22 Usable , SingleItemContainer21 Scriptable, Trapable, Destructable, Carryable, \ 22 Usable 23 23 24 24 class ImmovableContainer(GameObject, Container, Lockable, Scriptable, 25 Trap pable, Destructable):25 Trapable, Destructable): 26 26 """Composite class that can be used for crates, chests, etc.""" 27 27 def __init__ (self, **kwargs): … … 30 30 Lockable .__init__(self, **kwargs) 31 31 Scriptable .__init__(self, **kwargs) 32 Trap pable .__init__(self, **kwargs)32 Trapable .__init__(self, **kwargs) 33 33 Destructable .__init__(self, **kwargs) 34 34 self.blocking = True 35 36 class SingleItemContainer (Container) : 37 """Container that can only store a single item. 38 This class can represent single-item inventory slots""" 39 def __init__ (self, **kwargs): 40 Container.__init__(self, **kwargs) 41 42 def placeItem(self,item, index=None): 43 if len(self.items) > 0 : 44 raise self.SlotBusy ('%s is already busy' % self) 45 Container.placeItem(self, item) 35 46 36 47 class CarryableItem (GameObject, Carryable, Usable): … … 113 124 CarryableContainer.__init__(self, item_type, **kwargs) 114 125 115 class Door(GameObject, Lockable, Scriptable, Trap pable):126 class Door(GameObject, Lockable, Scriptable, Trapable): 116 127 """Composite class that can be used to create doors on a map.""" 117 128 def __init__ (self, target_map_name = 'my-map', … … 120 131 Lockable.__init__(self, **kwargs) 121 132 Scriptable.__init__(self, **kwargs) 122 Trap pable.__init__(self, **kwargs)123 self. is_door = True133 Trapable.__init__(self, **kwargs) 134 self.attributes.append("door") 124 135 self.target_map_name = target_map_name 125 136 self.target_pos = (target_x, target_y) -
trunk/game/scripts/objects/containers.py
r584 r611 86 86 self.behaviour = None 87 87 88 self. is_AnimatedContainer = True88 self.attributes.append("AnimatedContainer") 89 89 self.createBehaviour(agent_layer) 90 90
Note: See TracChangeset
for help on using the changeset viewer.