Switch from p3hel to json.

This commit is contained in:
bʰedoh₂ swé 2024-04-05 23:06:56 +05:00
parent 2b0e9cbeef
commit 14c7100b4c
6 changed files with 8 additions and 41 deletions

View File

@ -1,5 +1,7 @@
from os import makedirs from os import makedirs
from time import time from time import time
import json
from serialize import default, object_hook
from numba import jit, c16, b1, byte from numba import jit, c16, b1, byte
from numba.experimental import jitclass from numba.experimental import jitclass
from numba.types import UniTuple from numba.types import UniTuple
@ -18,42 +20,13 @@ class Segment:
self.pointB = cB self.pointB = cB
self.color = trColor self.color = trColor
def parse_level_from_string(string):
level = []
raw_segments = string.split('|')
for segment in raw_segments:
raw_segment = segment.split('/')
level.append(
Segment(
raw_segment[0] == "True",
complex(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): def save_level(level):
makedirs("./levels/", exist_ok=True) makedirs("./levels/", exist_ok=True)
level_save = save_level_to_string(level) level_save = json.dumps(level, default=default)
unix_timestamp = int(time()) unix_timestamp = int(time())
filename = f'{unix_timestamp}.p3hel' filename = f'{unix_timestamp}.json'
save = open(f"./levels/{filename}", "w") save = open(f"./levels/{filename}", "w")
save.write(level_save) save.write(level_save)
save.close() save.close()
@ -61,7 +34,7 @@ def save_level(level):
def open_level(path): def open_level(path):
save = open(path, "r") save = open(path, "r")
r = save.read() r = json.loads(save.read(), object_hook=object_hook)
save.close() save.close()
return r return r

View File

@ -8,9 +8,8 @@ import sys
import pygame import pygame
import pygame.freetype import pygame.freetype
from levels import Segment, open_level, parse_level_from_string
from gyro import GyroVector, Poincare2Klein from gyro import GyroVector, Poincare2Klein
from levels import open_level, save_level, parse_level_from_string, make_wall from levels import open_level, save_level, make_wall
from constants import I, WHITE, BLACK, IROT, ROT, OFFSET from constants import I, WHITE, BLACK, IROT, ROT, OFFSET
from draw import draw from draw import draw
from alert import Alert from alert import Alert
@ -54,10 +53,9 @@ def mainLoop():
alert.start_hide_thread(alerts, delay) alert.start_hide_thread(alerts, delay)
try: try:
leveltoparse = open_level(sys.argv[1]) level = open_level(sys.argv[1])
except IndexError: except IndexError:
leveltoparse = open_level("maps/squareroom.p3hel") level = open_level("maps/squareroom.json")
level = parse_level_from_string(leveltoparse)
while bCont: while bCont:
for event in pygame.event.get(): for event in pygame.event.get():

View File

@ -1 +0,0 @@
False/1/0/(255, 0, 0 |False/1j/0/(0, 255, 0

View File

@ -1 +0,0 @@
True/0/0/(0, 0, 0

View File

@ -1 +0,0 @@
True/0.6/0.6j/(255, 0, 0)|True/0.6/-0.6j/(0, 255, 0)|True/-0.6/-0.6j/(0, 0, 255)|True/-0.6/0.6j/(255, 255, 0)

View File

@ -1 +0,0 @@
True/(0.38529100476842437+0j)/(-0.2372040619364459+0.36629343026935063j)/(255, 0, 0)|True/(-0.2372040619364459+0.36629343026935063j)/(-0.22354939429564802-0.26820653903460522j)/(0, 255, 0)|True/(-0.22354939429564802-0.26820653903460523j)/(0.39868528559672944+0.017873216691274733j)/(0, 0, 255)