diff --git a/gamemap.py b/gamemap.py index 7ee6f60..f0b3568 100644 --- a/gamemap.py +++ b/gamemap.py @@ -2,6 +2,8 @@ import re import heapq import xml.etree.ElementTree as ET +import ruamel.yaml +from ruamel.yaml.comments import CommentedMap class Thing(object): @@ -34,6 +36,7 @@ class Thing(object): return self.name == other.name class Item(Thing): + yaml_tag = u'!Item' def __init__(self, name, x: int, y: int, description: str, useFunc: str, useOnFunc: str, customValues: dict, ranged: bool, graphic = ('clear', '#00BF00', '^')): super(Item, self).__init__('i', name, x, y, description, 13) @@ -46,6 +49,39 @@ class Item(Thing): def use(self): pass + @classmethod + def to_yaml(cls, representer, node): + graphic = {} + if node.graphic[0] != 'clear': + graphic['bgc'] = node.graphic[0] + if node.graphic[1] != '#00BF00': + graphic['fgc'] = node.graphic[1] + if node.graphic[2] != '^': + graphic['shape'] = node.graphic[2] + ret = {'name': node.name, 'location': (node.x, node.y), + 'description': node.description, 'graphic': graphic, + 'useFunc': node.useFunc, 'useOnFunc': node.useOnFunc, + 'customValues': node.customValues, 'ranged': node.ranged} + return representer.represent_mapping(cls.yaml_flag, ret) + + @classmethod + def from_yaml(cls, constructor, node): + parts = CommentedMap() + constructor.construct_mapping(node, parts, True) + bgc = 'clear' + fgc = '#00BF00' + shape = '^' + if 'bgc' in parts['graphic']: + bgc = parts['graphic']['bgc'] + if 'fgc' in parts['graphic']: + fgc = parts['graphic']['fgc'] + if 'shape' in parts['graphic']: + shape = parts['graphic']['shape'] + graphic = (bgc, fgc, shape) + return cls(parts['name'], parts['location'][0], parts['location'][1], + parts['description'], parts['useFunc'], parts['useOnFunc'], + dict(parts['customValues']), parts['ranged'], graphic) + class Useable(Thing): def __init__(self, name, x: int, y: int, description: str, useFunc: str, customValues: dict, playerx = None, playery = None, graphic = ('clear', '#0000FF', '#')): diff --git a/testing/test1.yml b/testing/test1.yml index ffce0c0..431cc70 100644 --- a/testing/test1.yml +++ b/testing/test1.yml @@ -32,12 +32,12 @@ layout: | w w w w w w0 w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w0 things: - - type: exit + - !Exit id: 1 location: [5, 10] destination: testing/test2.xml name: upstairs - - type: exit + - !Exit id: 2 location: [21, 23] destination: testing/test4.xml