2024-04-04 19:03:41 +00:00
|
|
|
from os import makedirs
|
2024-04-04 19:12:08 +00:00
|
|
|
from time import time
|
|
|
|
|
2024-04-04 19:03:41 +00:00
|
|
|
class Segment:
|
|
|
|
def __init__(self,bA,cA,cB,trColor):
|
|
|
|
self.isFinite = bA
|
|
|
|
self.pointA = cA
|
|
|
|
self.pointB = cB
|
|
|
|
self.color = trColor
|
2024-04-04 18:41:10 +00:00
|
|
|
|
|
|
|
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]),
|
2024-04-04 19:40:37 +00:00
|
|
|
complex(raw_segment[1]),
|
2024-04-04 18:41:10 +00:00
|
|
|
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 += '|'
|
|
|
|
|
2024-04-04 19:03:41 +00:00
|
|
|
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")
|
2024-04-04 19:12:08 +00:00
|
|
|
r = save.read()
|
2024-04-04 19:03:41 +00:00
|
|
|
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))
|
|
|
|
)
|