p3he/levels.py

66 lines
1.5 KiB
Python
Raw Normal View History

from os import makedirs
2024-04-04 19:12:08 +00:00
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
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]),
float(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())
filename = f'{unix_timestamp}.p3hel'
save = open(f"./levels/{filename}", "w")
save.write(level_save)
save.close()
return filename
def open_level(path):
save = open(path, "r")
2024-04-04 19:12:08 +00:00
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))
)