p3he/levels.py

49 lines
1.2 KiB
Python

from os import makedirs
from time import time
import json
from serialize import default, object_hook
from numba import jit, c16, b1, byte
from numba.experimental import jitclass
from numba.types import UniTuple
segmentsig = [
('isFinite', b1),
('pointA', c16),
('pointB', c16),
('color', UniTuple(byte, 3))
]
@jitclass(segmentsig)
class Segment:
def __init__(self,bA,cA,cB,trColor):
self.isFinite = bA
self.pointA = cA
self.pointB = cB
self.color = trColor
def save_level(level):
makedirs("./levels/", exist_ok=True)
level_save = json.dumps(level, default=default)
unix_timestamp = int(time())
filename = f'{unix_timestamp}.json'
save = open(f"./levels/{filename}", "w")
save.write(level_save)
save.close()
return filename
def open_level(path):
save = open(path, "r")
r = json.loads(save.read(), object_hook=object_hook)
save.close()
return r
def make_wall(wall_buffer,level):
if (len(wall_buffer) != 2):
print("Warning: wall buffer doesn't contains 2 points.")
return
level.append(
Segment(True, wall_buffer[0], wall_buffer[1], (23,74,183))
)