Compare commits

...

4 Commits

12 changed files with 34 additions and 45 deletions

View File

@ -1,7 +1,7 @@
from cmath import exp
from constants import I, BLACK
from gyro import MobiusInt, MobiusAdd, MobiusDist, cDot
from gyro import MobiusInt, MobiusAdd, MobiusDist, cDot, Poincare2Klein
from lines import cBetween
from numba import jit, byte, double, prange, optional
@ -38,7 +38,7 @@ def draw(level,gPlayer,fov,res):
if cDot(rot,MobiusAdd(cInt,-gPlayer.cPos)) > 0:
continue
rDist = MobiusDist(cInt,gPlayer.cPos)*1.1
if j.isFinite and not cBetween(j.pointA,j.pointB,cInt):
if j.isFinite and not cBetween(Poincare2Klein(j.pointA),Poincare2Klein(j.pointB),Poincare2Klein(cInt)):
continue
if m is None:
m = DrawnSegment(rDist,j.color)

View File

@ -1,5 +1,7 @@
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
@ -18,42 +20,13 @@ class Segment:
self.pointB = cB
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):
makedirs("./levels/", exist_ok=True)
level_save = save_level_to_string(level)
level_save = json.dumps(level, default=default)
unix_timestamp = int(time())
filename = f'{unix_timestamp}.p3hel'
filename = f'{unix_timestamp}.json'
save = open(f"./levels/{filename}", "w")
save.write(level_save)
save.close()
@ -61,7 +34,7 @@ def save_level(level):
def open_level(path):
save = open(path, "r")
r = save.read()
r = json.loads(save.read(), object_hook=object_hook)
save.close()
return r

12
main.py
View File

@ -8,9 +8,8 @@ import sys
import pygame
import pygame.freetype
from levels import Segment, open_level, parse_level_from_string
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 draw import draw
from alert import Alert
@ -54,10 +53,9 @@ def mainLoop():
alert.start_hide_thread(alerts, delay)
try:
leveltoparse = open_level(sys.argv[1])
level = open_level(sys.argv[1])
except IndexError:
leveltoparse = open_level("maps/squareroom.p3hel")
level = parse_level_from_string(leveltoparse)
level = open_level("maps/squareroom.json")
while bCont:
for event in pygame.event.get():
@ -116,8 +114,8 @@ def mainLoop():
display.blit(cRot_surf, (0, fps_surf.get_height() + fontSize))
pygame.draw.rect(display, BLACK, (0, 100, 200, 200))
for i in level:
a = complex(Poincare2Klein(i.pointA))
b = complex(Poincare2Klein(i.pointB))
a = Poincare2Klein(i.pointA)
b = Poincare2Klein(i.pointB)
pygame.draw.line(display, i.color, (a.real * 100 + 100, (a.imag * 100 + 200)), (b.real * 100 + 100, (b.imag * 100 + 200)))
c = complex(Poincare2Klein(gPlayer.cPos))
pygame.draw.rect(display, WHITE, (c.real * 100 + 95, (c.imag * 100 + 195), 10, 10))

1
maps/cross.json Normal file
View File

@ -0,0 +1 @@
[{"__type__": "objSegment", "isFinite": false, "pointA": {"__type__": "complex", "real": 1.0, "imag": 0.0}, "pointB": {"__type__": "complex", "real": 0.0, "imag": 0.0}, "color": [255, 0, 0]}, {"__type__": "objSegment", "isFinite": false, "pointA": {"__type__": "complex", "real": 0.0, "imag": 1.0}, "pointB": {"__type__": "complex", "real": 0.0, "imag": 0.0}, "color": [0, 255, 0]}]

View File

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

1
maps/empty.json Normal file
View File

@ -0,0 +1 @@
[{"__type__": "objSegment", "isFinite": true, "pointA": {"__type__": "complex", "real": 0.0, "imag": 0.0}, "pointB": {"__type__": "complex", "real": 0.0, "imag": 0.0}, "color": [0, 0, 0]}]

View File

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

1
maps/squareroom.json Normal file
View File

@ -0,0 +1 @@
[{"__type__": "objSegment", "isFinite": true, "pointA": {"__type__": "complex", "real": 0.6, "imag": 0.0}, "pointB": {"__type__": "complex", "real": 0.0, "imag": 0.6}, "color": [255, 0, 0]}, {"__type__": "objSegment", "isFinite": true, "pointA": {"__type__": "complex", "real": 0.6, "imag": 0.0}, "pointB": {"__type__": "complex", "real": 0.0, "imag": -0.6}, "color": [0, 255, 0]}, {"__type__": "objSegment", "isFinite": true, "pointA": {"__type__": "complex", "real": -0.6, "imag": 0.0}, "pointB": {"__type__": "complex", "real": 0.0, "imag": -0.6}, "color": [0, 0, 255]}, {"__type__": "objSegment", "isFinite": true, "pointA": {"__type__": "complex", "real": -0.6, "imag": 0.0}, "pointB": {"__type__": "complex", "real": 0.0, "imag": 0.6}, "color": [255, 255, 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)

1
maps/triangle.json Normal file
View File

@ -0,0 +1 @@
[{"__type__": "objSegment", "isFinite": true, "pointA": {"__type__": "complex", "real": 0.38529100476842437, "imag": 0.0}, "pointB": {"__type__": "complex", "real": -0.2372040619364459, "imag": 0.36629343026935063}, "color": [255, 0, 0]}, {"__type__": "objSegment", "isFinite": true, "pointA": {"__type__": "complex", "real": -0.2372040619364459, "imag": 0.36629343026935063}, "pointB": {"__type__": "complex", "real": -0.22354939429564802, "imag": -0.26820653903460523}, "color": [0, 255, 0]}, {"__type__": "objSegment", "isFinite": true, "pointA": {"__type__": "complex", "real": -0.22354939429564802, "imag": -0.26820653903460523}, "pointB": {"__type__": "complex", "real": 0.39868528559672944, "imag": 0.017873216691274733}, "color": [0, 0, 255]}]

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)

18
serialize.py Normal file
View File

@ -0,0 +1,18 @@
import levels
def object_hook(dct):
if '__type__' not in dct:
return dct
if dct['__type__'] == 'complex':
return complex(dct['real'], dct['imag'])
if dct['__type__'] == 'objSegment':
return levels.Segment(dct['isFinite'], dct['pointA'], dct['pointB'], tuple(dct['color']))
return dct
def default(obj):
if isinstance(obj, complex):
return {"__type__": "complex", "real": obj.real, "imag": obj.imag}
elif isinstance(obj, levels.Segment):
return {"__type__": "objSegment", "isFinite": obj.isFinite, "pointA": obj.pointA, "pointB": obj.pointB, "color": obj.color}
else:
raise TypeError("This type is not JSON serializable.")