forked from bedohswe/p3he
Compare commits
No commits in common. "7cbe55bd7f7d82e8b381091685e6c9474325c551" and "56228bc8b12b3bdd9d297cbfe7dda7d007f05be7" have entirely different histories.
7cbe55bd7f
...
56228bc8b1
4
draw.py
4
draw.py
@ -1,7 +1,7 @@
|
||||
from cmath import exp
|
||||
|
||||
from constants import I, BLACK
|
||||
from gyro import MobiusInt, MobiusAdd, MobiusDist, cDot, Poincare2Klein
|
||||
from gyro import MobiusInt, MobiusAdd, MobiusDist, cDot
|
||||
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(Poincare2Klein(j.pointA),Poincare2Klein(j.pointB),Poincare2Klein(cInt)):
|
||||
if j.isFinite and not cBetween(j.pointA,j.pointB,cInt):
|
||||
continue
|
||||
if m is None:
|
||||
m = DrawnSegment(rDist,j.color)
|
||||
|
37
levels.py
37
levels.py
@ -1,7 +1,5 @@
|
||||
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
|
||||
@ -20,13 +18,42 @@ 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 = json.dumps(level, default=default)
|
||||
level_save = save_level_to_string(level)
|
||||
|
||||
unix_timestamp = int(time())
|
||||
filename = f'{unix_timestamp}.json'
|
||||
filename = f'{unix_timestamp}.p3hel'
|
||||
save = open(f"./levels/{filename}", "w")
|
||||
save.write(level_save)
|
||||
save.close()
|
||||
@ -34,7 +61,7 @@ def save_level(level):
|
||||
|
||||
def open_level(path):
|
||||
save = open(path, "r")
|
||||
r = json.loads(save.read(), object_hook=object_hook)
|
||||
r = save.read()
|
||||
save.close()
|
||||
return r
|
||||
|
||||
|
12
main.py
12
main.py
@ -8,8 +8,9 @@ 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, make_wall
|
||||
from levels import open_level, save_level, parse_level_from_string, make_wall
|
||||
from constants import I, WHITE, BLACK, IROT, ROT, OFFSET
|
||||
from draw import draw
|
||||
from alert import Alert
|
||||
@ -53,9 +54,10 @@ def mainLoop():
|
||||
alert.start_hide_thread(alerts, delay)
|
||||
|
||||
try:
|
||||
level = open_level(sys.argv[1])
|
||||
leveltoparse = open_level(sys.argv[1])
|
||||
except IndexError:
|
||||
level = open_level("maps/squareroom.json")
|
||||
leveltoparse = open_level("maps/squareroom.p3hel")
|
||||
level = parse_level_from_string(leveltoparse)
|
||||
|
||||
while bCont:
|
||||
for event in pygame.event.get():
|
||||
@ -114,8 +116,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 = Poincare2Klein(i.pointA)
|
||||
b = Poincare2Klein(i.pointB)
|
||||
a = complex(Poincare2Klein(i.pointA))
|
||||
b = complex(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 +0,0 @@
|
||||
[{"__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]}]
|
1
maps/cross.p3hel
Normal file
1
maps/cross.p3hel
Normal file
@ -0,0 +1 @@
|
||||
False/1/0/(255, 0, 0 |False/1j/0/(0, 255, 0
|
@ -1 +0,0 @@
|
||||
[{"__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]}]
|
1
maps/empty.p3hel
Normal file
1
maps/empty.p3hel
Normal file
@ -0,0 +1 @@
|
||||
True/0/0/(0, 0, 0
|
@ -1 +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": [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]}]
|
1
maps/squareroom.p3hel
Normal file
1
maps/squareroom.p3hel
Normal file
@ -0,0 +1 @@
|
||||
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 +0,0 @@
|
||||
[{"__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]}]
|
1
maps/triangle.p3hel
Normal file
1
maps/triangle.p3hel
Normal file
@ -0,0 +1 @@
|
||||
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
18
serialize.py
@ -1,18 +0,0 @@
|
||||
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.")
|
Loading…
Reference in New Issue
Block a user