diff --git a/veripeditus/framework/model.py b/veripeditus/framework/model.py index 960f469daae290aaafe98760b19ac93806171bb1..44ddd08ddaabd3ca01dd9cd59bfcefa62ec39cc2 100644 --- a/veripeditus/framework/model.py +++ b/veripeditus/framework/model.py @@ -106,12 +106,27 @@ class GameObject(Base, metaclass=_GameObjectMeta): attributes = DB.relationship("Attribute", secondary="gameobjects_to_attributes") def attribute(self, key, value=None): + """ Set or get an attribute on this game object. + + Attributes can be used to store state information about the + game or this game object. + + Gotcha: Attributes can only be strings, and you are responsible + for casting. + + Returns the value of the attribute. + + :param key: key for the attribute + :param value: value of the attribute if it shall be set + :type key: str + :type value: str + :rtype: str + """ + attributes = [attribute for attribute in self.attributes if attribute.key == key] attribute = attributes[0] if attributes else None - if value is None: - return attribute.value if attribute else None - else: + if value is not None: if not attribute: attribute = Attribute() self.attributes.append(attribute) @@ -119,6 +134,8 @@ class GameObject(Base, metaclass=_GameObjectMeta): attribute.value = value self.commit() + return attribute.value if attribute else None + distance_max = None available_images_pattern = ["*.svg", "*.png"] @@ -137,7 +154,13 @@ class GameObject(Base, metaclass=_GameObjectMeta): return self.__tablename__ def distance_to(self, obj): - # Return distance to another gamobject + """ Return distance to another gamobject. + + :param obj: Object to determine the distance to + :type obj: :class:`veripeditus.framework.model.GameObject` + :rtype: float + """ + return get_gameobject_distance(self, obj) @property @@ -367,10 +390,17 @@ class Player(GameObject): self.image = "avatar_default" def new_item(self, itemclass): - """ Add a new item to the players inventory + """ Add a new item to the player's inventory. - :param itemclass: class of the Item + The new item will be a new instance of the item class. + Most often this will be a sub-class you defined in your + game. + + :param itemclass: class of the Item to create + :type itemclass: :class:`veripeditus.framework.model.Item` + :rtype: None """ + item = itemclass() item.world = self.world item.owner = self @@ -390,9 +420,11 @@ class Player(GameObject): self.commit() def has_item(self, itemclass): - """Get the amount of items the player has of a specific class + """Get the amount the player has of a specific item. :param itemclass: the class of which the checked items are + :type itemclass: :class:`veripeditus.framework.model.Item` + :rtype: int """ count = 0 for item in self.inventory: @@ -401,7 +433,12 @@ class Player(GameObject): return count def has_items(self, *itemclasses): - """Call has_item for every passed argument""" + """Check whether the player has at least one of each item queried for. + + :param itemclasses: All item types to check for + :type itemclasses: :class:`veripeditus.framework.model.Item` + :rtype: bool + """ # Return whether the player has every given item at least one time for itemclass in itemclasses: @@ -411,9 +448,11 @@ class Player(GameObject): return True def drop_item(self, itemclass): - """Drop every item of a class from the players inventory + """Drop every item of a class from the player's inventory :param itemclass: the class of which the items shall be removed + :type itemclasses: :class:`veripeditus.framework.model.Item` + :rtype: None """ # Remove every item on a class from the players inventory @@ -423,18 +462,28 @@ class Player(GameObject): DB.session.commit() def drop_items(self, *itemclasses): - """Call drop_item for every passed argument""" + """Srop every item of the classes from the player's inventory. + + :param itemclasses: All item types to drop + :type itemclasses: :class:`veripeditus.framework.model.Item` + :rtype: None + """ # Remove every item of every given class from the players inventory for itemclass in itemclasses: self.drop_item(itemclass) def may_accept_handover(self, item): - """Determine whether the player can accept a handover - of a specific item + """Determine whether the player can accept a handover of a specific item. + + This method defaults to always returning True and is designed to be + overridden in games. - :param item: item that is about to be handed over + :param item: item that is about to be handed over + :type item: :class:`veripeditus.framework.model.Item` + :rtype: bool """ + return True @api_method(authenticated=True) @@ -616,27 +665,42 @@ class Item(GameObject): return True def may_collect(self, player): - """Test whether the item can be collected by a player + """Determine whether this item can be collected by a specific player. + + This method defaults to always returning True and is designed to be + overridden in games. - :param player: the player that is about to collect the item - :type player: :class:`veripeditus.framework.Player` + :param player: the player trying to collect the item + :type player: :class:`veripeditus.framework.model.Player` + :rtype: bool """ + return True def may_handover(self, player): - """Test whether the item can be handed over by a player + """Determine whether this item can be handed over by a specific player. + + This method defaults to always returning True and is designed to be + overridden in games. - :param player: the player that is about to hand the item over - :type player: :class:`veripeditus.framework.Player` + :param player: the player trying to hand the item over + :type player: :class:`veripeditus.framework.model.Player` + :rtype: bool """ + return True def may_place(self, player): - """Test whether the item can be placed by a player + """Determine whether this item can be placed on the map by a specific player. + + This method defaults to always returning True and is designed to be + overridden in games. - :param player: the player that is about to place the item - :type player: :class:`veripeditus.framework.Player` + :param player: the player trying to collect the item + :type player: :class:`veripeditus.framework.model.Player` + :rtype: bool """ + return True def on_collected(self, **kwargs): @@ -684,17 +748,6 @@ class NPC(GameObject): # Attribute for determining if a player can talk to the NPC talkable = True - def say(self, message): - """ Make a textbox appear with the given message. - - :param message: the message that will appear withing the textbox - :type message: str: - :rtype: None - """ - - send_action("say", self, message) - return - def on_talk(self, **kwargs): """ Run as soon as a Player talks to an NPC. This method can trigger complex game logic. @@ -779,11 +832,16 @@ class Location(GameObject): return self.on_passed(player=current_player()) def may_pass(self, player): - """ Check whether a player is allowed to pass this Location + """Determine whether this location may be passed by a specific player. - :param player: the player that is about to pass this Location + This method defaults to always returning True and is designed to be + overridden in games. + + :param player: the player trying to collect the item :type player: :class:`veripeditus.framework.model.Player` + :rtype: bool """ + return True @hybrid_property