p3he/levels.py

64 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(
2024-04-04 20:31:01 +00:00
raw_segment[0] == "True",
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 += '|'
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()
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))
)