p3he/levels.py

49 lines
1.2 KiB
Python
Raw Permalink Normal View History

from os import makedirs
2024-04-04 19:12:08 +00:00
from time import time
2024-04-05 18:06:56 +00:00
import json
from serialize import default, object_hook
2024-04-05 12:40:54 +00:00
from numba import jit, c16, b1, byte
from numba.experimental import jitclass
from numba.types import UniTuple
2024-04-04 19:12:08 +00:00
2024-04-05 12:40:54 +00:00
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
2024-04-04 18:41:10 +00:00
def save_level(level):
makedirs("./levels/", exist_ok=True)
2024-04-05 18:06:56 +00:00
level_save = json.dumps(level, default=default)
unix_timestamp = int(time())
2024-04-05 18:06:56 +00:00
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")
2024-04-05 18:06:56 +00:00
r = json.loads(save.read(), object_hook=object_hook)
save.close()
return r
def make_wall(wall_buffer,level):
if (len(wall_buffer) != 2):
2024-04-05 11:02:17 +00:00
print("Warning: wall buffer doesn't contains 2 points.")
return
level.append(
Segment(True, wall_buffer[0], wall_buffer[1], (23,74,183))
)