Compare commits
37
Commits
7cbe55bd7f
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
188494bb25 | ||
|
|
31e1324fc0 | ||
|
|
536a8e1019 | ||
|
|
b8638b288d | ||
|
|
b560aeadc1 | ||
|
|
95cecdf5f0 | ||
|
|
9db9d4f889 | ||
|
|
b5eb207d38 | ||
|
|
457f0b9fa0 | ||
|
|
28868e5451 | ||
|
|
9ceaba94d7 | ||
|
|
e0e9b33098 | ||
|
|
d744d5f042 | ||
|
|
88d6d30436 | ||
|
|
eb6b4b987c | ||
|
|
f93dfb1fb2 | ||
|
|
00f930e151 | ||
|
|
e62f985e73 | ||
|
|
08347cdc3f | ||
|
|
1c342f163c | ||
|
|
3448684363 | ||
|
|
b29fc7d929 | ||
|
|
de6a555c94 | ||
|
|
db3ea32c75 | ||
|
|
f3f9f31a05 | ||
|
|
2d28957eaa | ||
|
|
13aefdde9a | ||
|
|
c3858c548e | ||
|
|
d9ce3fd3ab | ||
|
|
d28d1d10ed | ||
|
|
dfb6594ec5
|
||
|
|
b05a139cbf
|
||
|
|
f561ecc076 | ||
|
|
9e90b02f79
|
||
|
|
1494f28a78 | ||
|
|
c745c6a82b | ||
|
|
1cb4528165
|
@@ -1,3 +1,33 @@
|
||||
|
||||
# Pseudo-3D hyperbolic engine
|
||||
|
||||
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 |
|
||||
| Snap to the closest point | F6 |
|
||||
| Snap to the origin | F7 |
|
||||
| Snap the angle to the zero | F8 |
|
||||
|
||||
@@ -3,6 +3,5 @@ from cmath import exp
|
||||
I = complex(0,1)
|
||||
WHITE = (255,255,255)
|
||||
BLACK = (0,0,0)
|
||||
OFFSET = 0.0078125
|
||||
ROT = cNorm(exp(deg2rad(2)*I))
|
||||
IROT = 1/ROT
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import pygame
|
||||
from engineevents import EngineEvent
|
||||
from constants import ROT, IROT, I
|
||||
from gyro import GyroVector, MobiusDist
|
||||
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_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_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:
|
||||
#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_F6:
|
||||
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()
|
||||
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(args[1] * args[0].cRot*I, 1)), tsArguments=("gPlayer",'offset')))
|
||||
if keys[pygame.K_e]:
|
||||
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]:
|
||||
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__isub__(GyroVector(args[1] * args[0].cRot , 1)), tsArguments=("gPlayer",'offset')))
|
||||
if keys[pygame.K_s]:
|
||||
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__iadd__(GyroVector(args[1] * args[0].cRot , 1)), tsArguments=("gPlayer",'offset')))
|
||||
return aoEngineEvents
|
||||
@@ -1,8 +1,7 @@
|
||||
from cmath import exp
|
||||
|
||||
from constants import I, BLACK
|
||||
from gyro import MobiusInt, MobiusAdd, MobiusDist, cDot, Poincare2Klein
|
||||
from lines import cBetween
|
||||
from gyro import MobiusInt, MobiusAdd, MobiusBetween, MobiusDist, cDot, Poincare2Klein
|
||||
|
||||
from numba import jit, byte, double, prange, optional
|
||||
from numba.types import UniTuple
|
||||
@@ -22,7 +21,7 @@ class DrawnSegment:
|
||||
self.color = color
|
||||
|
||||
@jit#(cache=True,parallel=True)
|
||||
def draw(level,gPlayer,fov,res):
|
||||
def draw(level,gPlayer,fov,res,dscale):
|
||||
EMPTYDRAWN = DrawnSegment(0, BLACK)
|
||||
drawn = [EMPTYDRAWN] * res
|
||||
irot = exp(fov/res*I)
|
||||
@@ -31,14 +30,11 @@ def draw(level,gPlayer,fov,res):
|
||||
m = None
|
||||
rot = irot**(i-(res/2)) * iprot
|
||||
for j in level:
|
||||
try: # Function "LineIntersection" faults from time to time
|
||||
cInt = MobiusInt(j.pointA,j.pointB,gPlayer.cPos,rot)
|
||||
except:
|
||||
continue
|
||||
cInt = MobiusInt(j.pointA,j.pointB,gPlayer.cPos,rot)
|
||||
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)):
|
||||
rDist = MobiusDist(cInt,gPlayer.cPos) * dscale
|
||||
if j.isFinite and not MobiusBetween(j.pointA,j.pointB,cInt):
|
||||
continue
|
||||
if m is None:
|
||||
m = DrawnSegment(rDist,j.color)
|
||||
|
||||
@@ -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,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=()
|
||||
))
|
||||
@@ -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=()
|
||||
))
|
||||
@@ -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]}]
|
||||
@@ -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=()
|
||||
))
|
||||
@@ -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
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -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.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]}]
|
||||
@@ -2,7 +2,7 @@ from numba import jit, c16
|
||||
from numba.experimental import jitclass
|
||||
from cmath import sqrt
|
||||
from math import tanh, atanh, pi
|
||||
from lines import cLineIntersection
|
||||
from lines import cLineIntersection, cBetween
|
||||
|
||||
def deg2rad(rA): return rA/180*pi
|
||||
def rad2degd(rA): return rA*180/pi
|
||||
@@ -37,7 +37,14 @@ def cDist(cA, cB):
|
||||
|
||||
@jit(cache=True)
|
||||
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)
|
||||
def MobiusAdd(cA, cB):
|
||||
@@ -100,6 +107,17 @@ class GyroVector:
|
||||
cAdd, cGyr = MobiusAddGyr(gA.cPos, gB.cPos)
|
||||
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):
|
||||
return GyroVector(-(self.cRot * self.cPos), 1/self.cRot)
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
from os import makedirs
|
||||
from time import time
|
||||
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.experimental import jitclass
|
||||
from numba.types import UniTuple
|
||||
@@ -23,7 +24,7 @@ class Segment:
|
||||
def save_level(level):
|
||||
makedirs("./levels/", exist_ok=True)
|
||||
|
||||
level_save = json.dumps(level, default=default)
|
||||
level_save = json.dumps(level, default=serialize.default)
|
||||
|
||||
unix_timestamp = int(time())
|
||||
filename = f'{unix_timestamp}.json'
|
||||
@@ -34,7 +35,7 @@ def save_level(level):
|
||||
|
||||
def open_level(path):
|
||||
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()
|
||||
return r
|
||||
|
||||
|
||||
@@ -1,23 +1,22 @@
|
||||
#!/usr/bin/env python3
|
||||
from numba import jit
|
||||
|
||||
from math import copysign, pi, acos
|
||||
from time import time
|
||||
import sys
|
||||
import os
|
||||
from importlib import import_module
|
||||
|
||||
import pygame
|
||||
import pygame.freetype
|
||||
|
||||
from gyro import GyroVector, Poincare2Klein
|
||||
from levels import open_level, save_level, make_wall
|
||||
from constants import I, WHITE, BLACK, IROT, ROT, OFFSET
|
||||
from draw import draw
|
||||
from constants import I, WHITE, BLACK, IROT, ROT
|
||||
from draw import draw, DrawnSegment
|
||||
from alert import Alert
|
||||
from engineevents import EngineEvent, EngineEventProcessingError
|
||||
from defaultcontrols import defaultcontrols
|
||||
|
||||
gOrigin = GyroVector(0,1)
|
||||
|
||||
SKY = (127,127,255)
|
||||
GROUND = (102, 51, 0)
|
||||
|
||||
def renderDebugInfo(gPlayer, clock, fontSize = 18):
|
||||
font = pygame.freetype.Font(None, fontSize)
|
||||
@@ -34,7 +33,16 @@ def nonzerocap(n):
|
||||
if n <= 0: return 1
|
||||
return 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)
|
||||
ground = (102, 51, 0)
|
||||
bCap = True
|
||||
bCont = True
|
||||
gPlayer = GyroVector(0,1)
|
||||
@@ -44,7 +52,11 @@ def mainLoop():
|
||||
debugInfo = True
|
||||
wall_buffer = []
|
||||
alerts = []
|
||||
|
||||
aoEngineEvents = []
|
||||
fvControl_ao = defaultcontrols
|
||||
level = []
|
||||
iDistScale = 1.1
|
||||
|
||||
def alert_append(alert, delay):
|
||||
if len(alerts) >= 1:
|
||||
alerts.pop()
|
||||
@@ -52,58 +64,91 @@ def mainLoop():
|
||||
alerts.append(alert)
|
||||
alert.start_hide_thread(alerts, delay)
|
||||
|
||||
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
|
||||
nonlocal offset
|
||||
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,
|
||||
'offset': offset
|
||||
}
|
||||
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 == "offset":
|
||||
offset = 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:
|
||||
level = open_level(sys.argv[1])
|
||||
game = import_module('games.' + sys.argv[1])
|
||||
except IndexError:
|
||||
level = open_level("maps/squareroom.json")
|
||||
game = import_module("games.squareroom")
|
||||
|
||||
game.init(aoEngineEvents)
|
||||
processevents()
|
||||
|
||||
while bCont:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
bCont = False
|
||||
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:
|
||||
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:
|
||||
filename = save_level(level)
|
||||
alert = Alert(f"File saved as {filename}", display)
|
||||
|
||||
alert_append(alert, 5)
|
||||
if event.key == pygame.K_F4:
|
||||
print(gPlayer.cPos)
|
||||
|
||||
keys = pygame.key.get_pressed()
|
||||
if keys[pygame.K_d]:
|
||||
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)
|
||||
|
||||
aoEngineEvents = fvControl_ao()
|
||||
processevents()
|
||||
display.fill(WHITE)
|
||||
drawn = draw(level,gPlayer,pi/4,320)
|
||||
pygame.draw.rect(display,SKY, (0,0,1280,360))
|
||||
pygame.draw.rect(display,GROUND, (0,360,1280,360))
|
||||
if len(level) != 0:
|
||||
drawn = draw(level,gPlayer,pi/4,320,iDistScale)
|
||||
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
|
||||
for i in drawn:
|
||||
pygame.draw.rect(display,i.color, (n,int((nonzerocap(i.dist)) * 360),8,int((1 -nonzerocap(i.dist)) * 1280)))
|
||||
pygame.draw.rect(display,i.color, (n,int((nonzerocap(i.dist)) * 360),4,int((1 -nonzerocap(i.dist)) * 1280)))
|
||||
n += 4
|
||||
gPlayer.normalize()
|
||||
if debugInfo:
|
||||
@@ -112,7 +157,7 @@ def mainLoop():
|
||||
display.blit(fps_surf, (0, 0))
|
||||
display.blit(cPos_surf, (0, fps_surf.get_height()))
|
||||
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:
|
||||
a = Poincare2Klein(i.pointA)
|
||||
b = Poincare2Klein(i.pointB)
|
||||
@@ -130,14 +175,7 @@ def mainLoop():
|
||||
clock.tick()
|
||||
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__":
|
||||
sys.path.append(os.path.realpath(__file__))
|
||||
if not main():
|
||||
print("An error occured")
|
||||
|
||||
@@ -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 +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]}]
|
||||
@@ -0,0 +1,4 @@
|
||||
llvmlite==0.42.0
|
||||
numba==0.59.1
|
||||
numpy==1.26.4
|
||||
pygame==2.5.2
|
||||
Reference in New Issue
Block a user