-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_reader.py
More file actions
76 lines (64 loc) · 2.52 KB
/
Copy pathgame_reader.py
File metadata and controls
76 lines (64 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import xml.etree.ElementTree as ET
from constants import Const
from place import Place
from character import Enemy
from objects import Weapon
from objects import Item
from objects import Shield
class GameReader:
def parse_game(self, file):
tree = ET.parse(file)
root = tree.getroot()
# Get all places
for place in root.iter('place'):
self.place_list.append(self.get_place(place))
def get_place(self, place):
place_name = place.find(self.CONST.NAME_TAG).text
description = place.find(self.CONST.DESCRIPTION_TAG).text
place_id = int(place.find('id').text)
connection_list = []
for connection in place.iter('connection'):
aux = int(connection.text)
connection_list.append(aux)
objects = place.find('objects')
if objects is not None:
object_list = self.get_objects(objects)
else:
object_list = []
enemy_list = []
for enemy in place.iter('enemy'):
enemy_list.append(self.get_enemy(enemy))
p = Place(place_name, description, place_id, connection_list, object_list, enemy_list)
return p
@staticmethod
def get_objects(place):
object_list = []
for obj in place.iter('weapon'):
attributes = obj.attrib
name = attributes['name']
description = attributes['description']
attack = int(attributes['attack'])
object_list.append(Weapon(name, description, attack))
for obj in place.iter('object'):
attributes = obj.attrib
name = attributes['name']
description = attributes['description']
object_list.append(Item(name, description))
for obj in place.iter('shield'):
attributes = obj.attrib
name = attributes['name']
description = attributes['description']
defense = int(attributes['defense'])
object_list.append(Shield(name, description, defense))
return object_list
def get_enemy(self, enemy):
name = enemy.find(self.CONST.NAME_TAG).text
description = enemy.find(self.CONST.DESCRIPTION_TAG).text
level = int(enemy.find(self.CONST.LEVEL_TAG).text)
# location = int(enemy.find(self.CONST.LOCATION_TAG).text)
object_list = self.get_objects(enemy)
return Enemy(name, description, level, object_list)
def __init__(self):
self.place_list = []
self.enemy_list = []
self.CONST = Const()