Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import xml.etree.ElementTree as ET
- from player import Player
- class EventElement:
- def __init__(self, type, attributes, subelements, text):
- self.type = type
- self.attributes = attributes
- self.subelements = subelements
- self.text = text
- class Event:
- def __init__(self, name, idname, driver):
- self.name = name
- self.idname = idname
- self.driver = driver
- def out(text):
- print(text)
- def parse_element(eventEl):
- try:
- return EventElement(eventEl.tag, eventEl.attrib, [x for x in eventEl], eventEl.text)
- except:
- return EventElement(eventEl.tag, eventEl.attrib, [x for x in eventEl], False)
- def get_events():
- events = {}
- for event in ET.parse('data/events.xml').getroot():
- eventElements = []
- for subtag in event:
- if subtag.tag == 'name':
- eventName = subtag.text
- elif subtag.tag == 'eventDriver':
- eventDriver = subtag
- eventEls = []
- for eventEl in eventDriver:
- eventEls.append(parse_element(eventEl))
- events[event.attrib['name']] = Event(eventName, event.attrib['name'], eventEls)
- return events
- ops = {}
- opcounter = 0
- choice = ''
- results = {}
- def parse_eventElement(eventElement, player):
- global ops
- global opcounter
- global choice
- global results
- if eventElement.type == 'dialogueBox':
- out('===========')
- for subel in eventElement.subelements:
- parse_eventElement(parse_element(subel), player)
- out('===========')
- elif eventElement.type == 'choice':
- ops = {}
- opcounter = 0
- for i, subel in enumerate(eventElement.subelements):
- parse_eventElement(parse_element(subel), player)
- opcounter += 1
- for option in ops:
- out(f'[{option}] {ops[option]}')
- choice = ops[input()]
- chosenresult = results[choice]
- for result in chosenresult:
- parse_eventElement(parse_element(result), player)
- elif eventElement.type == 'rewardBox':
- out('===============')
- for rew in eventElement.subelements:
- parse_eventElement(parse_element(rew), player)
- out('===============')
- elif eventElement.type == 'option':
- ops[str(opcounter)] = eventElement.text
- elif eventElement.type == 'result':
- results[eventElement.attributes['id']] = eventElement.subelements
- elif eventElement.type == 'item':
- player.inventory.append(eventElement.text)
- out(f'You gained the item [{eventElement.text}]!')
- elif eventElement.type == 'gold':
- player.money += int(eventElement.text)
- out(f'You ganed [{eventElement.text}] gold!')
- elif eventElement.type == 'exp':
- player.exp += int(eventElement.text)
- out(f'You ganed [{eventElement.text}] EXP!')
- elif eventElement.type == 'text':
- out(eventElement.text)
- def run_eventdriver(eventDriver, player):
- for el in eventDriver:
- parse_eventElement(el, player)
- def run_event(evname, player):
- evs = get_events()
- out(evs[evname])
- run_eventdriver(evs[evname].driver, player)
- test = Player('Test Player', 0, 0, [])
- run_event('TEST_EVENT_1', test)
- test.info()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement