forked from bedohswe/p3he
Compare commits
46
Commits
799165b225
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eb6b4b987c | ||
|
|
f93dfb1fb2 | ||
|
|
00f930e151 | ||
|
|
e62f985e73 | ||
|
|
08347cdc3f | ||
|
|
1c342f163c | ||
|
|
3448684363 | ||
|
|
b29fc7d929 | ||
|
|
de6a555c94 | ||
|
|
db3ea32c75 | ||
|
|
f3f9f31a05 | ||
|
|
2d28957eaa | ||
|
|
13aefdde9a | ||
|
|
c3858c548e | ||
|
|
d9ce3fd3ab | ||
|
|
d28d1d10ed | ||
|
|
dfb6594ec5
|
||
|
|
b05a139cbf
|
||
|
|
f561ecc076 | ||
|
|
9e90b02f79
|
||
|
|
1494f28a78 | ||
|
|
c745c6a82b | ||
|
|
1cb4528165
|
||
|
|
7cbe55bd7f | ||
|
|
7000cfa685 | ||
|
|
14c7100b4c | ||
|
|
2b0e9cbeef | ||
|
|
56228bc8b1 | ||
|
|
9e69321399 | ||
|
|
837e732efd | ||
|
|
043606c965 | ||
|
|
64e48bb0f7 | ||
|
|
43b2d31980
|
||
|
|
fcabed5a19
|
||
|
|
9ebde906c2 | ||
|
|
d54377e3d6 | ||
|
|
53ecde3e0c | ||
|
|
04f0f7329c | ||
|
|
7f1799acfe | ||
|
|
18e4dec821 | ||
|
|
bd0aea1728 | ||
|
|
9121959b25 | ||
|
|
181492cc68 | ||
|
|
1845425c16 | ||
|
|
3b2522b12b | ||
|
|
371491a607 |
@@ -1,3 +1,30 @@
|
|||||||
|
|
||||||
# Pseudo-3D hyperbolic engine
|
# Pseudo-3D hyperbolic engine
|
||||||
|
|
||||||
My school project
|
My school project
|
||||||
|
|
||||||
|
## Loading games
|
||||||
|
|
||||||
|
You can load a game by passing the name of the game as an argument:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
python3 main.py triangle
|
||||||
|
```
|
||||||
|
|
||||||
|
Game can be loaded only if it is located in the games/ directory.
|
||||||
|
If no game is specified then "squareroom" will be loaded.
|
||||||
|
|
||||||
|
|
||||||
|
## Controls (might be overriden by games)
|
||||||
|
|
||||||
|
| Descripton | Key |
|
||||||
|
|----------------------------------------|:---:|
|
||||||
|
| Move forward | W |
|
||||||
|
| Move backwards | D |
|
||||||
|
| Strafe left | Q |
|
||||||
|
| Strafe right | E |
|
||||||
|
| Turn 180 degrees | R |
|
||||||
|
| Toggle FPS cap | F1 |
|
||||||
|
| Create a wall | F2 |
|
||||||
|
| Toggle debug info | F3 |
|
||||||
|
| Print current position to the terminal | F4 |
|
||||||
|
| Save level to `./levels` folder | F5 |
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import pygame
|
||||||
|
import pygame.freetype
|
||||||
|
import threading
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
class Alert():
|
||||||
|
def __init__(self, text, display):
|
||||||
|
self.text = text
|
||||||
|
|
||||||
|
font = pygame.freetype.Font(None, 24)
|
||||||
|
font_fg = (255, 255, 255)
|
||||||
|
font_bg = (255, 255, 255, 120)
|
||||||
|
|
||||||
|
text_image = font.render(text, font_fg)[0]
|
||||||
|
|
||||||
|
self.background = pygame.Surface((text_image.get_width() + 12, text_image.get_height() + 12), pygame.SRCALPHA)
|
||||||
|
self.background.fill(font_bg)
|
||||||
|
|
||||||
|
self.rect = self.background.get_rect()
|
||||||
|
text_rect = text_image.get_rect(center = self.rect.center)
|
||||||
|
|
||||||
|
self.background.blit(text_image, text_rect)
|
||||||
|
|
||||||
|
self.rect.topleft = (display.get_width() / 2 - self.background.get_width() / 2, display.get_height() - self.background.get_height() - 8)
|
||||||
|
|
||||||
|
def draw(self, surface):
|
||||||
|
surface.blit(self.background, self.rect)
|
||||||
|
|
||||||
|
def start_hide_thread(self, alerts, delay):
|
||||||
|
def hide_delay(delay):
|
||||||
|
sleep(delay)
|
||||||
|
|
||||||
|
try:
|
||||||
|
alerts.pop(alerts.index(self))
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
p = threading.Thread(target=hide_delay, args=(delay,))
|
||||||
|
p.daemon = True
|
||||||
|
p.start()
|
||||||
+1
-1
@@ -3,6 +3,6 @@ from cmath import exp
|
|||||||
I = complex(0,1)
|
I = complex(0,1)
|
||||||
WHITE = (255,255,255)
|
WHITE = (255,255,255)
|
||||||
BLACK = (0,0,0)
|
BLACK = (0,0,0)
|
||||||
OFFSET = 0.015625
|
OFFSET = 0.0078125
|
||||||
ROT = cNorm(exp(deg2rad(2)*I))
|
ROT = cNorm(exp(deg2rad(2)*I))
|
||||||
IROT = 1/ROT
|
IROT = 1/ROT
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
import pygame
|
||||||
|
from engineevents import EngineEvent
|
||||||
|
from constants import ROT, IROT, OFFSET, I
|
||||||
|
from gyro import GyroVector
|
||||||
|
from levels import open_level, save_level, make_wall
|
||||||
|
from alert import Alert
|
||||||
|
|
||||||
|
def defaultcontrols():
|
||||||
|
aoEngineEvents = list()
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
aoEngineEvents.append(EngineEvent("bCont", lambda _: False))
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].rotate(-1), tsArguments=("gPlayer",)))
|
||||||
|
if event.key == pygame.K_F3:
|
||||||
|
aoEngineEvents.append(EngineEvent("debugInfo", lambda args: not args[0]))
|
||||||
|
if event.key == pygame.K_F1:
|
||||||
|
aoEngineEvents.append(EngineEvent("bCap", lambda args: not args[0]))
|
||||||
|
if event.key == pygame.K_F2:
|
||||||
|
def fX(args):
|
||||||
|
#0: wall_buffer
|
||||||
|
#1: gPlayer
|
||||||
|
#2: level
|
||||||
|
if (len(args[0]) == 1):
|
||||||
|
args[0].append(args[1].cPos)
|
||||||
|
make_wall(args[0], args[2])
|
||||||
|
args[0].clear()
|
||||||
|
else:
|
||||||
|
args[0].append(args[1].cPos)
|
||||||
|
aoEngineEvents.append(EngineEvent(None, fX, tsArguments=("wall_buffer", "gPlayer", "level") ))
|
||||||
|
if event.key == pygame.K_F5:
|
||||||
|
#0: level
|
||||||
|
#1: alert_append
|
||||||
|
#2: display
|
||||||
|
def fX(args):
|
||||||
|
filename = save_level(args[0])
|
||||||
|
alert = Alert(f"File saved as {filename}", args[2])
|
||||||
|
|
||||||
|
args[1](alert, 5)
|
||||||
|
aoEngineEvents.append(EngineEvent(None, fX, tsArguments=("level", "alert_append", "display") ))
|
||||||
|
if event.key == pygame.K_F4:
|
||||||
|
aoEngineEvents.append(EngineEvent(None, lambda args: print(args[0].cPos), tsArguments=("gPlayer",)))
|
||||||
|
keys = pygame.key.get_pressed()
|
||||||
|
if keys[pygame.K_d]:
|
||||||
|
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].rotate( ROT), tsArguments=("gPlayer",)))
|
||||||
|
if keys[pygame.K_a]:
|
||||||
|
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].rotate(IROT), tsArguments=("gPlayer",)))
|
||||||
|
if keys[pygame.K_q]:
|
||||||
|
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__iadd__(GyroVector(OFFSET * args[0].cRot*I, 1)), tsArguments=("gPlayer",)))
|
||||||
|
if keys[pygame.K_e]:
|
||||||
|
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__isub__(GyroVector(OFFSET * args[0].cRot*I, 1)), tsArguments=("gPlayer",)))
|
||||||
|
if keys[pygame.K_w]:
|
||||||
|
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__isub__(GyroVector(OFFSET * args[0].cRot , 1)), tsArguments=("gPlayer",)))
|
||||||
|
if keys[pygame.K_s]:
|
||||||
|
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__iadd__(GyroVector(OFFSET * args[0].cRot , 1)), tsArguments=("gPlayer",)))
|
||||||
|
return aoEngineEvents
|
||||||
@@ -1,32 +1,50 @@
|
|||||||
from cmath import exp
|
from cmath import exp
|
||||||
from constants import *
|
|
||||||
from gyro import *
|
|
||||||
from lines import *
|
|
||||||
|
|
||||||
|
from constants import I, BLACK
|
||||||
|
from gyro import MobiusInt, MobiusAdd, MobiusDist, cDot, Poincare2Klein
|
||||||
|
from lines import cBetween
|
||||||
|
|
||||||
|
from numba import jit, byte, double, prange, optional
|
||||||
|
from numba.types import UniTuple
|
||||||
|
from numba.typed import List as nlist
|
||||||
|
from numba.experimental import jitclass
|
||||||
|
|
||||||
|
from numpy import full
|
||||||
|
|
||||||
|
drawnsegmentsig = [
|
||||||
|
('dist', double),
|
||||||
|
('color', UniTuple(byte, 3))
|
||||||
|
]
|
||||||
|
@jitclass(drawnsegmentsig)
|
||||||
class DrawnSegment:
|
class DrawnSegment:
|
||||||
def __init__(self,height,color):
|
def __init__(self,dist,color):
|
||||||
self.height = height
|
self.dist = dist
|
||||||
self.color = color
|
self.color = color
|
||||||
|
|
||||||
def draw(level,gPlayer,fov,res):
|
@jit#(cache=True,parallel=True)
|
||||||
drawn = list()
|
def draw(level,gPlayer,fov,res,dscale):
|
||||||
|
EMPTYDRAWN = DrawnSegment(0, BLACK)
|
||||||
|
drawn = [EMPTYDRAWN] * res
|
||||||
irot = exp(fov/res*I)
|
irot = exp(fov/res*I)
|
||||||
iprot = gPlayer.cRot
|
iprot = gPlayer.cRot
|
||||||
for i in range(res):
|
for i in range(res):
|
||||||
m = DrawnSegment(0,BLACK)
|
m = None
|
||||||
rot = irot**(i-(res/2)) * iprot
|
rot = irot**(i-(res/2)) * iprot
|
||||||
for j in level:
|
for j in level:
|
||||||
try: # Function "LineIntersection" faults from time to time
|
try: # Function "LineIntersection" faults from time to time
|
||||||
cInt = MobiusInt(j.pointA,j.pointB,gPlayer.cPos,rot)
|
cInt = MobiusInt(j.pointA,j.pointB,gPlayer.cPos,rot)
|
||||||
except ZeroDivisionError:
|
except:
|
||||||
continue
|
continue
|
||||||
if cDot(rot,MobiusAdd(cInt,-gPlayer.cPos)) > 0:
|
if cDot(rot,MobiusAdd(cInt,-gPlayer.cPos)) > 0:
|
||||||
continue
|
continue
|
||||||
rDist = MobiusDist(cInt,gPlayer.cPos)*1.1
|
rDist = MobiusDist(cInt,gPlayer.cPos) * dscale
|
||||||
if not (j.isFinite and cBetween(j.pointA,j.pointB,cInt)):
|
if j.isFinite and not cBetween(Poincare2Klein(j.pointA),Poincare2Klein(j.pointB),Poincare2Klein(cInt)):
|
||||||
continue
|
continue
|
||||||
if (1 - m.height) > rDist:
|
if m is None:
|
||||||
m = DrawnSegment(1-rDist,j.color)
|
m = DrawnSegment(rDist,j.color)
|
||||||
drawn.append(m)
|
continue
|
||||||
|
if m.dist > rDist:
|
||||||
|
m = DrawnSegment(rDist,j.color)
|
||||||
|
drawn[i] = (m if m is not None else EMPTYDRAWN)
|
||||||
return drawn
|
return drawn
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
class EngineEvent:
|
||||||
|
def __init__(self, sVariableToModify, fLambda, tsArguments=None):
|
||||||
|
self.sVariableToModify = sVariableToModify
|
||||||
|
self.fLambda = fLambda
|
||||||
|
if tsArguments is not None:
|
||||||
|
self.tsArguments = tsArguments
|
||||||
|
else:
|
||||||
|
self.tsArguments = (sVariableToModify,)
|
||||||
|
|
||||||
|
class EngineEventProcessingError(Exception):
|
||||||
|
pass
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
from engineevents import EngineEvent
|
||||||
|
|
||||||
|
def init(aoEngineEvents):
|
||||||
|
aoEngineEvents.append(EngineEvent('level', lambda _: []))
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import importlib.resources as impr
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
import serialize
|
||||||
|
from engineevents import EngineEvent
|
||||||
|
|
||||||
|
def init(aoEngineEvents):
|
||||||
|
curm = sys.modules[__name__]
|
||||||
|
files = impr.files(curm)
|
||||||
|
level = None
|
||||||
|
with files.joinpath("squareroom.json").open('r') as leveldir:
|
||||||
|
level = json.loads(leveldir.read(), object_hook=serialize.object_hook)
|
||||||
|
aoEngineEvents.append(EngineEvent(
|
||||||
|
'level',
|
||||||
|
lambda _: level,
|
||||||
|
tsArguments=()
|
||||||
|
))
|
||||||
@@ -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]}]
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import importlib.resources as impr
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
import serialize
|
||||||
|
from engineevents import EngineEvent
|
||||||
|
|
||||||
|
def init(aoEngineEvents):
|
||||||
|
curm = sys.modules[__name__]
|
||||||
|
files = impr.files(curm)
|
||||||
|
level = None
|
||||||
|
with files.joinpath("triangle.json").open('r') as leveldir:
|
||||||
|
level = json.loads(leveldir.read(), object_hook=serialize.object_hook)
|
||||||
|
aoEngineEvents.append(EngineEvent(
|
||||||
|
'level',
|
||||||
|
lambda _: level,
|
||||||
|
tsArguments=()
|
||||||
|
))
|
||||||
@@ -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]}]
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
from numba import jit
|
from numba import jit, c16
|
||||||
|
from numba.experimental import jitclass
|
||||||
from cmath import sqrt
|
from cmath import sqrt
|
||||||
from math import tanh, atanh, pi
|
from math import tanh, atanh, pi
|
||||||
from lines import cLineIntersection
|
from lines import cLineIntersection
|
||||||
@@ -6,64 +7,64 @@ from lines import cLineIntersection
|
|||||||
def deg2rad(rA): return rA/180*pi
|
def deg2rad(rA): return rA/180*pi
|
||||||
def rad2degd(rA): return rA*180/pi
|
def rad2degd(rA): return rA*180/pi
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def cap(rN):
|
def cap(rN):
|
||||||
if rN < 0:
|
if rN < 0:
|
||||||
return 0
|
return 0
|
||||||
return rN
|
return rN
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def ZeroCheck(cN):
|
def ZeroCheck(cN):
|
||||||
if cN == complex(0,0):
|
if cN == complex(0,0):
|
||||||
return complex(1,0)
|
return complex(1,0)
|
||||||
else:
|
else:
|
||||||
return cN
|
return cN
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def c_tr(cN):
|
def c_tr(cN):
|
||||||
return cN.real, cN.imag
|
return cN.real, cN.imag
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def cNorm(cN):
|
def cNorm(cN):
|
||||||
return ZeroCheck(cN)/abs(ZeroCheck(cN))
|
return ZeroCheck(cN)/abs(ZeroCheck(cN))
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def cDot(cA, cB):
|
def cDot(cA, cB):
|
||||||
return (cA * cB.conjugate()).real
|
return (cA * cB.conjugate()).real
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def cDist(cA, cB):
|
def cDist(cA, cB):
|
||||||
return abs(cA-cB)
|
return abs(cA-cB)
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def MobiusInt(cA,cB,cC,cD): # Bruh
|
def MobiusInt(cA,cB,cC,cD): # Bruh
|
||||||
return Klein2Poincare(cLineIntersection(Poincare2Klein(cA),Poincare2Klein(cB),Poincare2Klein(cC),Poincare2Klein(cD)))
|
return Klein2Poincare(cLineIntersection(Poincare2Klein(cA),Poincare2Klein(cB),Poincare2Klein(cC),Poincare2Klein(cD)))
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def MobiusAdd(cA, cB):
|
def MobiusAdd(cA, cB):
|
||||||
return (cA + cB) / (1 + cA.conjugate() * cB)
|
return (cA + cB) / (1 + cA.conjugate() * cB)
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def MobiusGyr(cA, cB):
|
def MobiusGyr(cA, cB):
|
||||||
return (1 + cA * cB.conjugate()) / (1 + cA.conjugate() * cB)
|
return (1 + cA * cB.conjugate()) / (1 + cA.conjugate() * cB)
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def MobiusAddGyr(cA, cB):
|
def MobiusAddGyr(cA, cB):
|
||||||
return MobiusAdd(cA, cB), MobiusGyr(cA, cB)
|
return MobiusAdd(cA, cB), MobiusGyr(cA, cB)
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def MobiusScalar(cA, rT):
|
def MobiusScalar(cA, rT):
|
||||||
m = abs(cA)
|
m = abs(cA)
|
||||||
return tanh(rT * atanh(m)) * cA / m
|
return tanh(rT * atanh(m)) * cA / m
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def MobiusCoadd(cA,cB):
|
def MobiusCoadd(cA,cB):
|
||||||
return MobiusAdd(cA, (MobiusGyr(cA,-cB) * cB))
|
return MobiusAdd(cA, (MobiusGyr(cA,-cB) * cB))
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def MobiusCosub(cA,cB):
|
def MobiusCosub(cA,cB):
|
||||||
return MobiusAdd(cA, -(MobiusGyr(cA,cB) * cB))
|
return MobiusAdd(cA, -(MobiusGyr(cA,cB) * cB))
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def MobiusLine(cA,cB,rT):
|
def MobiusLine(cA,cB,rT):
|
||||||
return MobiusAdd(
|
return MobiusAdd(
|
||||||
cA,
|
cA,
|
||||||
@@ -71,18 +72,23 @@ def MobiusLine(cA,cB,rT):
|
|||||||
MobiusAdd(-cA,cB),
|
MobiusAdd(-cA,cB),
|
||||||
rT))
|
rT))
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def MobiusDist(cA,cB):
|
def MobiusDist(cA,cB):
|
||||||
return abs(MobiusAdd(-cB,cA))
|
return abs(MobiusAdd(-cB,cA))
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def Poincare2Klein(cN):
|
def Poincare2Klein(cN):
|
||||||
return 2*cN / (1 + cDot(cN,cN))
|
return 2*cN / (1 + cDot(cN,cN))
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def Klein2Poincare(cN):
|
def Klein2Poincare(cN):
|
||||||
return (1-sqrt(1-cDot(cN,cN)))*cN/cDot(cN,cN)
|
return (1-sqrt(1-cDot(cN,cN)))*cN/cDot(cN,cN)
|
||||||
|
|
||||||
|
gyrosig = [
|
||||||
|
('cPos', c16),
|
||||||
|
('cRot', c16)
|
||||||
|
]
|
||||||
|
@jitclass(gyrosig)
|
||||||
class GyroVector:
|
class GyroVector:
|
||||||
def __init__(self, cPos, cRot):
|
def __init__(self, cPos, cRot):
|
||||||
self.cPos = complex(cPos)
|
self.cPos = complex(cPos)
|
||||||
@@ -94,6 +100,17 @@ class GyroVector:
|
|||||||
cAdd, cGyr = MobiusAddGyr(gA.cPos, gB.cPos)
|
cAdd, cGyr = MobiusAddGyr(gA.cPos, gB.cPos)
|
||||||
return GyroVector(cAdd, gA.cRot * gB.cRot * cGyr)
|
return GyroVector(cAdd, gA.cRot * gB.cRot * cGyr)
|
||||||
|
|
||||||
|
def __iadd__(self, gA):
|
||||||
|
cAdd, cGyr = MobiusAddGyr(self.cPos, gA.cPos)
|
||||||
|
self.cPos = cAdd
|
||||||
|
self.cRot = self.cRot * gA.cRot * cGyr
|
||||||
|
|
||||||
|
def __isub__(self, gA):
|
||||||
|
ngA = -gA
|
||||||
|
cAdd, cGyr = MobiusAddGyr(self.cPos, ngA.cPos)
|
||||||
|
self.cPos = cAdd
|
||||||
|
self.cRot = self.cRot * ngA.cRot * cGyr
|
||||||
|
|
||||||
def __neg__(self):
|
def __neg__(self):
|
||||||
return GyroVector(-(self.cRot * self.cPos), 1/self.cRot)
|
return GyroVector(-(self.cRot * self.cPos), 1/self.cRot)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,18 @@
|
|||||||
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.experimental import jitclass
|
||||||
|
from numba.types import UniTuple
|
||||||
|
|
||||||
|
segmentsig = [
|
||||||
|
('isFinite', b1),
|
||||||
|
('pointA', c16),
|
||||||
|
('pointB', c16),
|
||||||
|
('color', UniTuple(byte, 3))
|
||||||
|
]
|
||||||
|
@jitclass(segmentsig)
|
||||||
class Segment:
|
class Segment:
|
||||||
def __init__(self,bA,cA,cB,trColor):
|
def __init__(self,bA,cA,cB,trColor):
|
||||||
self.isFinite = bA
|
self.isFinite = bA
|
||||||
@@ -8,54 +20,27 @@ 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(
|
|
||||||
bool(raw_segment[0]),
|
|
||||||
float(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())
|
||||||
save = open(f"./levels/{unix_timestamp}.p3hel", "w")
|
filename = f'{unix_timestamp}.json'
|
||||||
|
save = open(f"./levels/{filename}", "w")
|
||||||
save.write(level_save)
|
save.write(level_save)
|
||||||
save.close()
|
save.close()
|
||||||
|
return filename
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
def make_wall(wall_buffer,level):
|
def make_wall(wall_buffer,level):
|
||||||
if (len(wall_buffer) != 2):
|
if (len(wall_buffer) != 2):
|
||||||
print('Warning: wall buffer not contains 2 points.')
|
print("Warning: wall buffer doesn't contains 2 points.")
|
||||||
return
|
return
|
||||||
|
|
||||||
level.append(
|
level.append(
|
||||||
|
|||||||
@@ -1,20 +1,20 @@
|
|||||||
from numba import jit
|
from numba import jit
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def LineIntersection(rX1,rY1,rX2,rY2,rX3,rY3,rX4,rY4):
|
def LineIntersection(rX1,rY1,rX2,rY2,rX3,rY3,rX4,rY4):
|
||||||
rX = ( (rX1*rY2-rY1*rX2)*(rX3-rX4)-(rX1-rX2)*(rX3*rY4-rY3*rX4) ) / ( (rX1-rX2)*(rY3-rY4)-(rY1-rY2)*(rX3-rX4) )
|
rX = ( (rX1*rY2-rY1*rX2)*(rX3-rX4)-(rX1-rX2)*(rX3*rY4-rY3*rX4) ) / ( (rX1-rX2)*(rY3-rY4)-(rY1-rY2)*(rX3-rX4) )
|
||||||
rY = ( (rX1*rY2-rY1*rX2)*(rY3-rY4)-(rY1-rY2)*(rX3*rY4-rY3*rX4) ) / ( (rX1-rX2)*(rY3-rY4)-(rY1-rY2)*(rX3-rX4) )
|
rY = ( (rX1*rY2-rY1*rX2)*(rY3-rY4)-(rY1-rY2)*(rX3*rY4-rY3*rX4) ) / ( (rX1-rX2)*(rY3-rY4)-(rY1-rY2)*(rX3-rX4) )
|
||||||
return (rX, rY)
|
return (rX, rY)
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def cLineIntersection(cA0, cA1, cB0, cB1):
|
def cLineIntersection(cA0, cA1, cB0, cB1):
|
||||||
rX, rY = LineIntersection(cA0.real, cA0.imag, cA1.real, cA1.imag, cB0.real, cB0.imag, cB1.real, cB1.imag)
|
rX, rY = LineIntersection(cA0.real, cA0.imag, cA1.real, cA1.imag, cB0.real, cB0.imag, cB1.real, cB1.imag)
|
||||||
return complex(rX, rY)
|
return complex(rX, rY)
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def Between(rA,rB,rN):
|
def Between(rA,rB,rN):
|
||||||
return (rA < rN and rN < rB) or (rB < rN and rN < rA)
|
return (rA <= rN and rN <= rB) or (rB <= rN and rN <= rA)
|
||||||
|
|
||||||
@jit
|
@jit(cache=True)
|
||||||
def cBetween(cA,cB,cN):
|
def cBetween(cA,cB,cN):
|
||||||
return Between(cA.real,cB.real,cN.real) and Between(cA.imag,cB.imag,cN.imag)
|
return Between(cA.real,cB.real,cN.real) and Between(cA.imag,cB.imag,cN.imag)
|
||||||
|
|||||||
@@ -1,32 +1,22 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from numba import jit
|
|
||||||
|
|
||||||
from math import copysign, pi, acos
|
from math import copysign, pi, acos
|
||||||
from time import time
|
import sys
|
||||||
|
import os
|
||||||
|
from importlib import import_module
|
||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
import pygame.freetype
|
import pygame.freetype
|
||||||
|
|
||||||
from levels import Segment
|
from gyro import GyroVector, Poincare2Klein
|
||||||
from gyro import *
|
from constants import I, WHITE, BLACK, IROT, ROT, OFFSET
|
||||||
from levels import *
|
from draw import draw, DrawnSegment
|
||||||
from constants import *
|
from alert import Alert
|
||||||
from draw import draw
|
from engineevents import EngineEvent, EngineEventProcessingError
|
||||||
|
from defaultcontrols import defaultcontrols
|
||||||
bCap = True
|
|
||||||
|
|
||||||
gOrigin = GyroVector(0,1)
|
gOrigin = GyroVector(0,1)
|
||||||
|
|
||||||
SKY = (127,127,255)
|
|
||||||
GROUND = (102, 51, 0)
|
|
||||||
|
|
||||||
level = [
|
|
||||||
Segment(True,.6,I*.6,(255,0,0)),
|
|
||||||
Segment(True,.6,-I*.6,(0,255,0)),
|
|
||||||
Segment(True,-.6,-I*.6,(0,0,255)),
|
|
||||||
Segment(True,-.6,I*.6,(255,255,0))
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def renderDebugInfo(gPlayer, clock, fontSize = 18):
|
def renderDebugInfo(gPlayer, clock, fontSize = 18):
|
||||||
font = pygame.freetype.Font(None, fontSize)
|
font = pygame.freetype.Font(None, fontSize)
|
||||||
@@ -39,63 +29,117 @@ def renderDebugInfo(gPlayer, clock, fontSize = 18):
|
|||||||
|
|
||||||
return fps_surf, cPos_surf, cRot_surf
|
return fps_surf, cPos_surf, cRot_surf
|
||||||
|
|
||||||
|
def nonzerocap(n):
|
||||||
|
if n <= 0: return 1
|
||||||
|
return n
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def mainLoop():
|
def mainLoop():
|
||||||
|
sky = (127,127,255)
|
||||||
|
ground = (102, 51, 0)
|
||||||
|
bCap = True
|
||||||
|
bCont = True
|
||||||
gPlayer = GyroVector(0,1)
|
gPlayer = GyroVector(0,1)
|
||||||
display = pygame.display.set_mode((1280,720))
|
display = pygame.display.set_mode((1280,720))
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
fontSize = 18
|
fontSize = 18
|
||||||
debugInfo = True
|
debugInfo = True
|
||||||
wall_buffer = []
|
wall_buffer = []
|
||||||
|
alerts = []
|
||||||
|
aoEngineEvents = []
|
||||||
|
fvControl_ao = defaultcontrols
|
||||||
|
level = []
|
||||||
|
iDistScale = 1.1
|
||||||
|
|
||||||
while True:
|
def alert_append(alert, delay):
|
||||||
for event in pygame.event.get():
|
if len(alerts) >= 1:
|
||||||
if event.type == pygame.QUIT:
|
alerts.pop()
|
||||||
return True
|
|
||||||
if event.type == pygame.KEYDOWN:
|
|
||||||
if event.key == pygame.K_r:
|
|
||||||
gPlayer.cRot *= -1
|
|
||||||
if event.key == pygame.K_F3:
|
|
||||||
debugInfo = not debugInfo
|
|
||||||
if event.key == pygame.K_F1:
|
|
||||||
global bCap
|
|
||||||
bCap = not bCap
|
|
||||||
if event.key == pygame.K_F2:
|
|
||||||
if (len(wall_buffer) == 1):
|
|
||||||
wall_buffer.append(gPlayer.cPos)
|
|
||||||
make_wall(wall_buffer, level)
|
|
||||||
wall_buffer.clear()
|
|
||||||
else:
|
|
||||||
wall_buffer.append(gPlayer.cPos)
|
|
||||||
if event.key == pygame.K_F5:
|
|
||||||
save_level(level)
|
|
||||||
if event.key == pygame.K_F9:
|
|
||||||
# TODO: @bedohswe make open file dialog
|
|
||||||
# open_level(path)
|
|
||||||
pass
|
|
||||||
|
|
||||||
keys = pygame.key.get_pressed()
|
alerts.append(alert)
|
||||||
if keys[pygame.K_d]:
|
alert.start_hide_thread(alerts, delay)
|
||||||
gPlayer.rotate(ROT)
|
|
||||||
if keys[pygame.K_a]:
|
|
||||||
gPlayer.rotate(IROT)
|
|
||||||
if keys[pygame.K_q]:
|
|
||||||
gPlayer += GyroVector(OFFSET * gPlayer.cRot*I, 1)
|
|
||||||
if keys[pygame.K_e]:
|
|
||||||
gPlayer -= GyroVector(OFFSET * gPlayer.cRot*I, 1)
|
|
||||||
if keys[pygame.K_w]:
|
|
||||||
gPlayer -= GyroVector(OFFSET * gPlayer.cRot, 1)
|
|
||||||
if keys[pygame.K_s]:
|
|
||||||
gPlayer += GyroVector(OFFSET * gPlayer.cRot, 1)
|
|
||||||
|
|
||||||
|
def processevents():
|
||||||
|
nonlocal bCap
|
||||||
|
nonlocal bCont
|
||||||
|
nonlocal gPlayer
|
||||||
|
nonlocal display
|
||||||
|
nonlocal fontSize
|
||||||
|
nonlocal debugInfo
|
||||||
|
nonlocal wall_buffer
|
||||||
|
nonlocal alerts
|
||||||
|
nonlocal alert_append
|
||||||
|
nonlocal aoEngineEvents
|
||||||
|
nonlocal level
|
||||||
|
nonlocal sky
|
||||||
|
nonlocal ground
|
||||||
|
nonlocal iDistScale
|
||||||
|
state = {
|
||||||
|
'bCap': bCap,
|
||||||
|
'bCont': bCont,
|
||||||
|
'gPlayer': gPlayer,
|
||||||
|
'display': display,
|
||||||
|
'fontSize': fontSize,
|
||||||
|
'debugInfo': debugInfo,
|
||||||
|
'wall_buffer': wall_buffer,
|
||||||
|
'alerts': alerts,
|
||||||
|
'alert_append': alert_append,
|
||||||
|
'aoEngineEvents': aoEngineEvents,
|
||||||
|
'level': level,
|
||||||
|
'sky': sky,
|
||||||
|
'ground': ground,
|
||||||
|
'iDistScale': iDistScale
|
||||||
|
}
|
||||||
|
for i in aoEngineEvents:
|
||||||
|
if i.sVariableToModify == "bCap":
|
||||||
|
bCap = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
||||||
|
elif i.sVariableToModify == "bCont":
|
||||||
|
bCont = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
||||||
|
elif i.sVariableToModify == "gPlayer":
|
||||||
|
gPlayer = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
||||||
|
elif i.sVariableToModify == "fontSize":
|
||||||
|
fontSize = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
||||||
|
elif i.sVariableToModify == "wall_buffer":
|
||||||
|
wall_buffer = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
||||||
|
elif i.sVariableToModify == "alerts":
|
||||||
|
alerts = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
||||||
|
elif i.sVariableToModify == "sky":
|
||||||
|
sky = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
||||||
|
elif i.sVariableToModify == "ground":
|
||||||
|
ground = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
||||||
|
elif i.sVariableToModify == "debugInfo":
|
||||||
|
debugInfo = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
||||||
|
elif i.sVariableToModify == "level":
|
||||||
|
level = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
||||||
|
elif i.sVariableToModify == "iDistScale":
|
||||||
|
iDistScale = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
||||||
|
elif i.sVariableToModify is None:
|
||||||
|
i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
||||||
|
else:
|
||||||
|
raise EngineEventProcessingError('Unknown variable: ' + i.sVariableToModify)
|
||||||
|
|
||||||
|
aoEngineEvents.clear()
|
||||||
|
try:
|
||||||
|
game = import_module('games.' + sys.argv[1])
|
||||||
|
except IndexError:
|
||||||
|
game = import_module("games.squareroom")
|
||||||
|
|
||||||
|
game.init(aoEngineEvents)
|
||||||
|
processevents()
|
||||||
|
|
||||||
|
while bCont:
|
||||||
|
aoEngineEvents = fvControl_ao()
|
||||||
|
processevents()
|
||||||
display.fill(WHITE)
|
display.fill(WHITE)
|
||||||
drawn = draw(level,gPlayer,pi/4,320)
|
if len(level) != 0:
|
||||||
pygame.draw.rect(display,SKY, (0,0,1280,360))
|
drawn = draw(level,gPlayer,pi/4,320,iDistScale)
|
||||||
pygame.draw.rect(display,GROUND, (0,360,1280,360))
|
else:
|
||||||
|
drawn = [DrawnSegment(0, BLACK)] * 320
|
||||||
|
pygame.draw.rect(display,sky, (0,0,1280,360))
|
||||||
|
pygame.draw.rect(display,ground, (0,360,1280,360))
|
||||||
n = 0
|
n = 0
|
||||||
for i in drawn:
|
for i in drawn:
|
||||||
pygame.draw.rect(display,i.color, (n,int((1 - cap(i.height)) * 360),8,int(cap(i.height) * 1280)))
|
pygame.draw.rect(display,i.color, (n,int((nonzerocap(i.dist)) * 360),4,int((1 -nonzerocap(i.dist)) * 1280)))
|
||||||
n += 4
|
n += 4
|
||||||
gPlayer.normalize()
|
gPlayer.normalize()
|
||||||
if debugInfo:
|
if debugInfo:
|
||||||
@@ -104,14 +148,17 @@ def mainLoop():
|
|||||||
display.blit(fps_surf, (0, 0))
|
display.blit(fps_surf, (0, 0))
|
||||||
display.blit(cPos_surf, (0, fps_surf.get_height()))
|
display.blit(cPos_surf, (0, fps_surf.get_height()))
|
||||||
display.blit(cRot_surf, (0, fps_surf.get_height() + fontSize))
|
display.blit(cRot_surf, (0, fps_surf.get_height() + fontSize))
|
||||||
pygame.draw.rect(display, BLACK, (0, 100, 200, 200))
|
pygame.draw.circle(display, BLACK, (100, 200), 100)
|
||||||
for i in level:
|
for i in level:
|
||||||
a = complex(Poincare2Klein(i.pointA))
|
a = Poincare2Klein(i.pointA)
|
||||||
b = complex(Poincare2Klein(i.pointB))
|
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)))
|
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))
|
c = complex(Poincare2Klein(gPlayer.cPos))
|
||||||
pygame.draw.rect(display, WHITE, (c.real * 100 + 95, (c.imag * 100 + 195), 10, 10))
|
pygame.draw.rect(display, WHITE, (c.real * 100 + 95, (c.imag * 100 + 195), 10, 10))
|
||||||
|
|
||||||
|
for alert in alerts:
|
||||||
|
alert.draw(display)
|
||||||
|
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
if bCap:
|
if bCap:
|
||||||
clock.tick(60)
|
clock.tick(60)
|
||||||
@@ -128,5 +175,6 @@ def main():
|
|||||||
return retstatus
|
return retstatus
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
sys.path.append(os.path.realpath(__file__))
|
||||||
if not main():
|
if not main():
|
||||||
print("An error occured")
|
print("An error occured")
|
||||||
|
|||||||
@@ -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]}]
|
||||||
@@ -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]}]
|
||||||
@@ -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]}]
|
||||||
@@ -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]}]
|
||||||
@@ -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.")
|
||||||
Reference in New Issue
Block a user