Added yaml constructor to Item

This commit is contained in:
Patrick Marsee 2019-03-08 00:38:57 -05:00
parent f80b9092a9
commit c5029ac0fc
2 changed files with 38 additions and 2 deletions

View file

@ -2,6 +2,8 @@
import re import re
import heapq import heapq
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
import ruamel.yaml
from ruamel.yaml.comments import CommentedMap
class Thing(object): class Thing(object):
@ -34,6 +36,7 @@ class Thing(object):
return self.name == other.name return self.name == other.name
class Item(Thing): 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', '^')): 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) super(Item, self).__init__('i', name, x, y, description, 13)
@ -46,6 +49,39 @@ class Item(Thing):
def use(self): def use(self):
pass 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): class Useable(Thing):
def __init__(self, name, x: int, y: int, description: str, useFunc: str, customValues: dict, playerx = None, playery = None, graphic = ('clear', '#0000FF', '#')): def __init__(self, name, x: int, y: int, description: str, useFunc: str, customValues: dict, playerx = None, playery = None, graphic = ('clear', '#0000FF', '#')):

View file

@ -32,12 +32,12 @@ layout: |
w w w w w w0 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 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: things:
- type: exit - !Exit
id: 1 id: 1
location: [5, 10] location: [5, 10]
destination: testing/test2.xml destination: testing/test2.xml
name: upstairs name: upstairs
- type: exit - !Exit
id: 2 id: 2
location: [21, 23] location: [21, 23]
destination: testing/test4.xml destination: testing/test4.xml