p3he/levels.py

64 lines
1.5 KiB
Python

from os import makedirs
from time import time
class Segment:
def __init__(self,bA,cA,cB,trColor):
self.isFinite = bA
self.pointA = cA
self.pointB = cB
self.color = trColor
def parse_level_from_string(string):
level = []
raw_segments = string.split('|')
for segment in raw_segments:
raw_segment = segment.split('/')
level.append(
Segment(
bool(raw_segment[0]),
complex(raw_segment[1]),
complex(raw_segment[2]),
tuple(map(int, raw_segment[3][1:len(raw_segment[3])-1].split(', ')))
)
)
return level
def save_level_to_string(level):
string_level = ""
for segment in level:
string_level += f'{segment.isFinite}/{segment.pointA}/{segment.pointB}/{segment.color}'
if segment != level[len(level) - 1]:
string_level += '|'
return string_level
def save_level(level):
makedirs("./levels/", exist_ok=True)
level_save = save_level_to_string(level)
unix_timestamp = int(time())
save = open(f"./levels/{unix_timestamp}.p3hel", "w")
save.write(level_save)
save.close()
def open_level(path):
save = open(path, "r")
r = save.read()
save.close()
return r
def make_wall(wall_buffer,level):
if (len(wall_buffer) != 2):
print('Warning: wall buffer not contains 2 points.')
return
level.append(
Segment(True, wall_buffer[0], wall_buffer[1], (23,74,183))
)