Repository navigation
Expand file tree
/
Copy pathLoader.py
More file actions
61 lines (54 loc) · 1.46 KB
/
Copy pathLoader.py
File metadata and controls
61 lines (54 loc) · 1.46 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
import re
from Vector2 import Vector2
from Segment import Segment
from Wall import Wall
class Loader():
def __init__(self, file_name):
self.lines = self.load_file(file_name)
self.objects = []
self.player_pos = Vector2()
self.player_rotation = 0
self.decode_lines()
self.visual_map = None
def load_file(self, file_name):
try:
with open("Custom_Maps/" + file_name, "r") as file:
lines = file.readlines()
return lines
except:
return []
def decode_lines(self):
for l in self.lines:
line = l.strip().lower()
line_text = re.sub(r"[,()]", "", line)
line = line_text.split()
try:
if line[0] == "s":
pos_a = Vector2(float(line[1]), float(line[2]))
pos_b = Vector2(float(line[3]), float(line[4]))
tags = self.handle_tags(line_text)
segment = Segment(pos_a, pos_b)
wall = Wall(segment, tags)
self.objects.append(wall)
if line[0] == "p":
self.player_pos = Vector2(float(line[1]), float(line[2]))
if line[0] == "r":
self.player_rotation = float(line[1])
except:
continue
if len(self.objects) == 0:
quit()
def handle_tags(self, line):
tags_text = re.search(r"\{.*\}", line)
if tags_text == None:
return {}
tags = {}
tags_strings = re.sub(r"\{|\}", "", tags_text.group(0)).split(";")
for tag_string in tags_strings:
tag = tag_string.strip()
if not ":" in tag:
continue
key = tag[:tag.find(":")]
val = tag[tag.find(":") + 1:]
tags[key] = val
return tags