Compare commits

..

14 Commits
main ... main

17 changed files with 298 additions and 39 deletions

View File

@ -28,3 +28,6 @@ If no game is specified then "squareroom" will be loaded.
| Toggle debug info | F3 | | Toggle debug info | F3 |
| Print current position to the terminal | F4 | | Print current position to the terminal | F4 |
| Save level to `./levels` folder | F5 | | Save level to `./levels` folder | F5 |
| Snap to the closest point | F6 |
| Snap to the origin | F7 |
| Snap the angle to the zero | F8 |

View File

@ -3,6 +3,5 @@ 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.0078125
ROT = cNorm(exp(deg2rad(2)*I)) ROT = cNorm(exp(deg2rad(2)*I))
IROT = 1/ROT IROT = 1/ROT

View File

@ -1,7 +1,7 @@
import pygame import pygame
from engineevents import EngineEvent from engineevents import EngineEvent
from constants import ROT, IROT, OFFSET, I from constants import ROT, IROT, I
from gyro import GyroVector from gyro import GyroVector, MobiusDist
from levels import open_level, save_level, make_wall from levels import open_level, save_level, make_wall
from alert import Alert from alert import Alert
@ -13,8 +13,6 @@ def defaultcontrols():
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r: if event.key == pygame.K_r:
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].rotate(-1), tsArguments=("gPlayer",))) 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: if event.key == pygame.K_F1:
aoEngineEvents.append(EngineEvent("bCap", lambda args: not args[0])) aoEngineEvents.append(EngineEvent("bCap", lambda args: not args[0]))
if event.key == pygame.K_F2: if event.key == pygame.K_F2:
@ -29,6 +27,10 @@ def defaultcontrols():
else: else:
args[0].append(args[1].cPos) args[0].append(args[1].cPos)
aoEngineEvents.append(EngineEvent(None, fX, tsArguments=("wall_buffer", "gPlayer", "level") )) aoEngineEvents.append(EngineEvent(None, fX, tsArguments=("wall_buffer", "gPlayer", "level") ))
if event.key == pygame.K_F3:
aoEngineEvents.append(EngineEvent("debugInfo", lambda args: not args[0]))
if event.key == pygame.K_F4:
aoEngineEvents.append(EngineEvent(None, lambda args: print(args[0].cPos), tsArguments=("gPlayer",)))
if event.key == pygame.K_F5: if event.key == pygame.K_F5:
#0: level #0: level
#1: alert_append #1: alert_append
@ -39,19 +41,39 @@ def defaultcontrols():
args[1](alert, 5) args[1](alert, 5)
aoEngineEvents.append(EngineEvent(None, fX, tsArguments=("level", "alert_append", "display") )) aoEngineEvents.append(EngineEvent(None, fX, tsArguments=("level", "alert_append", "display") ))
if event.key == pygame.K_F4: if event.key == pygame.K_F6:
aoEngineEvents.append(EngineEvent(None, lambda args: print(args[0].cPos), tsArguments=("gPlayer",))) def fX(aArgs):
aLevel = aArgs[0]
gPlayer = aArgs[1]
fMin = 2
cMin = gPlayer.cPos
for i in aLevel:
fD = MobiusDist(gPlayer.cPos, i.pointA)
if fD < fMin:
fMin = fD
cMin = i.pointA
fD = MobiusDist(gPlayer.cPos, i.pointB)
if fD < fMin:
fMin = fD
cMin = i.pointB
return GyroVector(cMin,gPlayer.cRot)
aoEngineEvents.append(EngineEvent('gPlayer', fX, tsArguments=('level','gPlayer')))
if event.key == pygame.K_F7:
aoEngineEvents.append(EngineEvent('gPlayer', lambda tgPlayer: GyroVector(0, tgPlayer[0].cRot)))
if event.key == pygame.K_F8:
aoEngineEvents.append(EngineEvent('gPlayer', lambda tgPlayer: GyroVector(tgPlayer[0].cPos, 1)))
keys = pygame.key.get_pressed() keys = pygame.key.get_pressed()
if keys[pygame.K_d]: if keys[pygame.K_d]:
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].rotate( ROT), tsArguments=("gPlayer",))) aoEngineEvents.append(EngineEvent(None, lambda args: args[0].rotate( ROT), tsArguments=("gPlayer",)))
if keys[pygame.K_a]: if keys[pygame.K_a]:
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].rotate(IROT), tsArguments=("gPlayer",))) aoEngineEvents.append(EngineEvent(None, lambda args: args[0].rotate(IROT), tsArguments=("gPlayer",)))
if keys[pygame.K_q]: if keys[pygame.K_q]:
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__iadd__(GyroVector(OFFSET * args[0].cRot*I, 1)), tsArguments=("gPlayer",))) aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__iadd__(GyroVector(args[1] * args[0].cRot*I, 1)), tsArguments=("gPlayer",'offset')))
if keys[pygame.K_e]: if keys[pygame.K_e]:
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__isub__(GyroVector(OFFSET * args[0].cRot*I, 1)), tsArguments=("gPlayer",))) aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__isub__(GyroVector(args[1] * args[0].cRot*I, 1)), tsArguments=("gPlayer",'offset')))
if keys[pygame.K_w]: if keys[pygame.K_w]:
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__isub__(GyroVector(OFFSET * args[0].cRot , 1)), tsArguments=("gPlayer",))) aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__isub__(GyroVector(args[1] * args[0].cRot , 1)), tsArguments=("gPlayer",'offset')))
if keys[pygame.K_s]: if keys[pygame.K_s]:
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__iadd__(GyroVector(OFFSET * args[0].cRot , 1)), tsArguments=("gPlayer",))) aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__iadd__(GyroVector(args[1] * args[0].cRot , 1)), tsArguments=("gPlayer",'offset')))
return aoEngineEvents return aoEngineEvents

10
draw.py
View File

@ -1,8 +1,7 @@
from cmath import exp from cmath import exp
from constants import I, BLACK from constants import I, BLACK
from gyro import MobiusInt, MobiusAdd, MobiusDist, cDot, Poincare2Klein from gyro import MobiusInt, MobiusAdd, MobiusBetween, MobiusDist, cDot, Poincare2Klein
from lines import cBetween
from numba import jit, byte, double, prange, optional from numba import jit, byte, double, prange, optional
from numba.types import UniTuple from numba.types import UniTuple
@ -31,14 +30,11 @@ def draw(level,gPlayer,fov,res,dscale):
m = None 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 cInt = MobiusInt(j.pointA,j.pointB,gPlayer.cPos,rot)
cInt = MobiusInt(j.pointA,j.pointB,gPlayer.cPos,rot)
except:
continue
if cDot(rot,MobiusAdd(cInt,-gPlayer.cPos)) > 0: if cDot(rot,MobiusAdd(cInt,-gPlayer.cPos)) > 0:
continue continue
rDist = MobiusDist(cInt,gPlayer.cPos) * dscale rDist = MobiusDist(cInt,gPlayer.cPos) * dscale
if j.isFinite and not cBetween(Poincare2Klein(j.pointA),Poincare2Klein(j.pointB),Poincare2Klein(cInt)): if j.isFinite and not MobiusBetween(j.pointA,j.pointB,cInt):
continue continue
if m is None: if m is None:
m = DrawnSegment(rDist,j.color) m = DrawnSegment(rDist,j.color)

17
games/cross/__init__.py Normal file
View File

@ -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("cross.json").open('r') as leveldir:
level = json.loads(leveldir.read(), object_hook=serialize.object_hook)
aoEngineEvents.append(EngineEvent(
'level',
lambda _: level,
tsArguments=()
))

View File

@ -1 +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]}] [{"__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": [255, 255, 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

@ -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("strangeroom.json").open('r') as leveldir:
level = json.loads(leveldir.read(), object_hook=serialize.object_hook)
aoEngineEvents.append(EngineEvent(
'level',
lambda _: level,
tsArguments=()
))

View File

@ -0,0 +1,195 @@
[
{
"__type__": "objSegment",
"isFinite": true,
"pointA": {
"__type__": "complex",
"real": -0.1335,
"imag": -0.145
},
"pointB": {
"__type__": "complex",
"real": -0.2085,
"imag": 0.1675
},
"color": [
128,
128,
128
]
},
{
"__type__": "objSegment",
"isFinite": true,
"pointA": {
"__type__": "complex",
"real": 0.214,
"imag": 0.1875
},
"pointB": {
"__type__": "complex",
"real": -0.208,
"imag": 0.1675
},
"color": [
128,
128,
128
]
},
{
"__type__": "objSegment",
"isFinite": true,
"pointA": {
"__type__": "complex",
"real": 0.214,
"imag": 0.1875
},
"pointB": {
"__type__": "complex",
"real": 0.144,
"imag": -0.185
},
"color": [
128,
128,
128
]
},
{
"__type__": "objSegment",
"isFinite": true,
"pointA": {
"__type__": "complex",
"real": -0.1335,
"imag": -0.145
},
"pointB": {
"__type__": "complex",
"real": 0.144,
"imag": -0.185
},
"color": [
128,
128,
128
]
},
{
"__type__": "objSegment",
"isFinite": true,
"pointA": {
"__type__": "complex",
"real": -0.562,
"imag": -0.555
},
"pointB": {
"__type__": "complex",
"real": -0.682,
"imag": 0.400
},
"color": [
200,
200,
200
]
},
{
"__type__": "objSegment",
"isFinite": true,
"pointA": {
"__type__": "complex",
"real": -0.062,
"imag": 0.790
},
"pointB": {
"__type__": "complex",
"real": -0.682,
"imag": 0.400
},
"color": [
200,
200,
200
]
},
{
"__type__": "objSegment",
"isFinite": true,
"pointA": {
"__type__": "complex",
"real": -0.062,
"imag": 0.790
},
"pointB": {
"__type__": "complex",
"real": 0.583,
"imag": 0.540
},
"color": [
200,
200,
200
]
},
{
"__type__": "objSegment",
"isFinite": true,
"pointA": {
"__type__": "complex",
"real": 0.793,
"imag": -0.085
},
"pointB": {
"__type__": "complex",
"real": 0.583,
"imag": 0.540
},
"color": [
200,
200,
200
]
},
{
"__type__": "objSegment",
"isFinite": true,
"pointA": {
"__type__": "complex",
"real": 0.793,
"imag": -0.085
},
"pointB": {
"__type__": "complex",
"real": 0.333,
"imag": -0.720
},
"color": [
200,
200,
200
]
},
{
"__type__": "objSegment",
"isFinite": true,
"pointA": {
"__type__": "complex",
"real": -0.547,
"imag": -0.550
},
"pointB": {
"__type__": "complex",
"real": -0.562,
"imag": -0.555
},
"color": [
200,
200,
200
]
}
]

View File

@ -1 +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]}] [{"__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.237204061, "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]}]

11
gyro.py
View File

@ -2,7 +2,7 @@ from numba import jit, c16
from numba.experimental import jitclass 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, cBetween
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
@ -37,7 +37,14 @@ def cDist(cA, cB):
@jit(cache=True) @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))) try:
return Klein2Poincare(cLineIntersection(Poincare2Klein(cA),Poincare2Klein(cB),Poincare2Klein(cC),Poincare2Klein(cD)))
except:
return 0j
@jit(cache=True)
def MobiusBetween(cA,cB,cN):
return cBetween(Poincare2Klein(cA),Poincare2Klein(cB),Poincare2Klein(cN))
@jit(cache=True) @jit(cache=True)
def MobiusAdd(cA, cB): def MobiusAdd(cA, cB):

View File

@ -1,7 +1,8 @@
from os import makedirs from os import makedirs
from time import time from time import time
import json import json
from serialize import default, object_hook #from serialize import default, object_hook
import serialize
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
@ -23,7 +24,7 @@ class Segment:
def save_level(level): def save_level(level):
makedirs("./levels/", exist_ok=True) makedirs("./levels/", exist_ok=True)
level_save = json.dumps(level, default=default) level_save = json.dumps(level, default=serialize.default)
unix_timestamp = int(time()) unix_timestamp = int(time())
filename = f'{unix_timestamp}.json' filename = f'{unix_timestamp}.json'
@ -34,7 +35,7 @@ def save_level(level):
def open_level(path): def open_level(path):
save = open(path, "r") save = open(path, "r")
r = json.loads(save.read(), object_hook=object_hook) r = json.loads(save.read(), object_hook=serialize.object_hook)
save.close() save.close()
return r return r

23
main.py
View File

@ -9,7 +9,7 @@ import pygame
import pygame.freetype import pygame.freetype
from gyro import GyroVector, Poincare2Klein from gyro import GyroVector, Poincare2Klein
from constants import I, WHITE, BLACK, IROT, ROT, OFFSET from constants import I, WHITE, BLACK, IROT, ROT
from draw import draw, DrawnSegment from draw import draw, DrawnSegment
from alert import Alert from alert import Alert
from engineevents import EngineEvent, EngineEventProcessingError from engineevents import EngineEvent, EngineEventProcessingError
@ -35,7 +35,12 @@ def nonzerocap(n):
def mainLoop(): def main():
pygame.init()
pygame.display.set_caption('P3HE')
if not pygame.get_init():
return False
offset = 0.0078125
sky = (127,127,255) sky = (127,127,255)
ground = (102, 51, 0) ground = (102, 51, 0)
bCap = True bCap = True
@ -74,6 +79,7 @@ def mainLoop():
nonlocal sky nonlocal sky
nonlocal ground nonlocal ground
nonlocal iDistScale nonlocal iDistScale
nonlocal offset
state = { state = {
'bCap': bCap, 'bCap': bCap,
'bCont': bCont, 'bCont': bCont,
@ -88,7 +94,8 @@ def mainLoop():
'level': level, 'level': level,
'sky': sky, 'sky': sky,
'ground': ground, 'ground': ground,
'iDistScale': iDistScale 'iDistScale': iDistScale,
'offset': offset
} }
for i in aoEngineEvents: for i in aoEngineEvents:
if i.sVariableToModify == "bCap": if i.sVariableToModify == "bCap":
@ -113,6 +120,8 @@ def mainLoop():
level = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments))) level = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
elif i.sVariableToModify == "iDistScale": elif i.sVariableToModify == "iDistScale":
iDistScale = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments))) iDistScale = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
elif i.sVariableToModify == "offset":
offset = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
elif i.sVariableToModify is None: elif i.sVariableToModify is None:
i.fLambda(list(map(lambda oX: state[oX], i.tsArguments))) i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
else: else:
@ -166,14 +175,6 @@ def mainLoop():
clock.tick() clock.tick()
return True return True
def main():
pygame.init()
pygame.display.set_caption('P3HE')
if not pygame.get_init():
return False
retstatus = mainLoop()
return retstatus
if __name__ == "__main__": if __name__ == "__main__":
sys.path.append(os.path.realpath(__file__)) sys.path.append(os.path.realpath(__file__))
if not main(): if not main():

View File

@ -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]}]

View File

@ -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]}]

View File

@ -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]}]

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
llvmlite==0.42.0
numba==0.59.1
numpy==1.26.4
pygame==2.5.2