Compare commits

...
90 Commits
Author SHA1 Message Date
bedohswe 188494bb25 Add a new level. 2024-05-18 18:16:15 +05:00
bedohswe 31e1324fc0 Change how serialize.py is imported in levels.py. 2024-05-18 17:58:50 +05:00
bedohswe 536a8e1019 Remove accidental color of a wall in squareroom.json. 2024-05-18 17:57:29 +05:00
bedohswe b8638b288d Combine "main" and "mainLoop". 2024-04-16 16:20:20 +05:00
bedohswe b560aeadc1 Remove unnecessary exception handling in the draw function. 2024-04-11 01:39:33 +05:00
bedohswe 95cecdf5f0 Add requirements.txt. 2024-04-11 00:59:38 +05:00
bedohswe 9db9d4f889 Add exception handling to MobiusInt. 2024-04-11 00:58:27 +05:00
bedohswe b5eb207d38 Make a separate function for properly determining whether a Poincare point is between two other points. 2024-04-09 00:02:59 +05:00
bedohswe 457f0b9fa0 Make speed changable via engine events. 2024-04-08 19:55:18 +05:00
bedohswe 28868e5451 Add buttons to reset coordinates and angle. 2024-04-08 00:10:08 +05:00
bedohswe 9ceaba94d7 Add a button to snap the player coordinates to the closest point. 2024-04-08 00:05:00 +05:00
bedohswe e0e9b33098 Remove maps/ since it is no longer necessary. 2024-04-07 23:53:21 +05:00
bedohswe d744d5f042 Add "cross" as one of the "games". 2024-04-07 23:52:58 +05:00
bedohswe 88d6d30436 Change the triangle room level to get rid of that strange bug. 2024-04-07 23:51:36 +05:00
bedohswe eb6b4b987c Make module loading in game more sane. 2024-04-07 23:42:27 +05:00
bedohswe f93dfb1fb2 Add an empty game. 2024-04-07 21:11:48 +05:00
bedohswe 00f930e151 Make it possible to render an empty level. 2024-04-07 21:11:31 +05:00
bedohswe e62f985e73 Make processevent() look a tiny bit better. 2024-04-07 21:03:00 +05:00
bedohswe 08347cdc3f Fix the size of drawn segments. 2024-04-07 19:14:32 +05:00
bedohswe 1c342f163c . 2024-04-07 17:48:14 +05:00
bedohswe 3448684363 Make more variable modifiable through engine events. 2024-04-07 17:22:41 +05:00
bedohswe b29fc7d929 Change README. 2024-04-07 17:17:19 +05:00
bedohswe de6a555c94 Add files that I forgot to add in previous commit. 2024-04-07 16:51:56 +05:00
bedohswe db3ea32c75 Change how games are initialized. 2024-04-07 16:50:37 +05:00
bedohswe f3f9f31a05 Merge pull request 'docs(readme): Added info about loading levels and controls' (#9) from n3tael/p3he:main into main
Reviewed-on: bedohswe/p3he#9
2024-04-07 11:06:30 +00:00
bedohswe 2d28957eaa Merge branch 'branch'. 2024-04-07 16:05:20 +05:00
bedohswe 13aefdde9a Changes to README.md. 2024-04-07 16:04:13 +05:00
bedohswe c3858c548e Change how paths to games are specified and add a new one. 2024-04-07 15:59:26 +05:00
bedohswe d9ce3fd3ab Start making this thing into an actual game engine. 2024-04-07 01:38:06 +05:00
bedohswe d28d1d10ed Make code worse. 2024-04-07 01:12:28 +05:00
n3tael dfb6594ec5 Merge remote-tracking branch 'upstream/main' 2024-04-06 22:40:53 +03:00
n3tael b05a139cbf docs(readme): Added info about loading levels and controls 2024-04-06 22:38:12 +03:00
bedohswe f561ecc076 Merge pull request 'Remove useless imports' (#8) from n3tael/p3he:main into main
Reviewed-on: bedohswe/p3he#8
2024-04-06 19:15:46 +00:00
n3tael 9e90b02f79 Remove useless imports 2024-04-06 22:13:53 +03:00
bedohswe 1494f28a78 Add __iadd__ and __isub__ to GyroVector. 2024-04-06 23:46:43 +05:00
bedohswe c745c6a82b Merge pull request 'feat(debug/mini_map): round mini map' (#7) from n3tael/p3he:main into main
Reviewed-on: bedohswe/p3he#7
2024-04-06 18:44:59 +00:00
n3tael 1cb4528165 feat(debug/mini_map): round mini map 2024-04-06 21:43:51 +03:00
bedohswe 7cbe55bd7f Remove a useless cast. 2024-04-06 19:22:34 +05:00
bedohswe 7000cfa685 Add missing files. 2024-04-05 23:15:45 +05:00
bedohswe 14c7100b4c Switch from p3hel to json. 2024-04-05 23:06:56 +05:00
bedohswe 2b0e9cbeef A partial fix for rendering finite lines. 2024-04-05 19:24:50 +05:00
bedohswe 56228bc8b1 Make some optimizations. 2024-04-05 17:40:54 +05:00
bedohswe 9e69321399 Fix grammar. 2024-04-05 16:02:17 +05:00
bedohswe 837e732efd Print coordinates when pressing F4 2024-04-05 16:01:05 +05:00
bedohswe 043606c965 Add a new level. 2024-04-05 13:27:49 +05:00
bedohswe 64e48bb0f7 Merge pull request 'feat(save_level): now displays a alert with filename' (#6) from n3tael/p3he:main into main
Reviewed-on: bedohswe/p3he#6
2024-04-04 21:08:47 +00:00
n3tael 43b2d31980 Merge conflict fix. 2024-04-04 23:57:57 +03:00
n3tael fcabed5a19 feat(save_level): now displays a alert with filename 2024-04-04 23:55:54 +03:00
bedohswe 9ebde906c2 Slow down a bit. 2024-04-05 01:51:26 +05:00
bedohswe d54377e3d6 Change rendering a bit. 2024-04-05 01:46:39 +05:00
bedohswe 53ecde3e0c Add a map. 2024-04-05 01:33:15 +05:00
bedohswe 04f0f7329c . 2024-04-05 01:31:27 +05:00
bedohswe 7f1799acfe Fix level loading. 2024-04-05 01:31:01 +05:00
bedohswe 18e4dec821 Nope. 2024-04-05 01:08:01 +05:00
bedohswe bd0aea1728 Fix a typo. 2024-04-05 01:04:37 +05:00
bedohswe 9121959b25 Fix rendering a bit. 2024-04-05 01:00:08 +05:00
bedohswe 181492cc68 Rename "cuberoom" into "squareroom". 2024-04-05 00:53:56 +05:00
bedohswe 1845425c16 Fix level loading and add an empty level. 2024-04-05 00:52:39 +05:00
bedohswe 3b2522b12b Implement level loading. 2024-04-05 00:40:37 +05:00
bedohswe 371491a607 Merge pull request 'fix(levels): time is not defined' (#5) from n3tael/p3he:main into main
Reviewed-on: bedohswe/p3he#5
2024-04-04 19:21:39 +00:00
n3tael 799165b225 Merge remote-tracking branch 'upstream/main' 2024-04-04 22:13:11 +03:00
n3tael 1e18630c44 fix(levels): time is not defined 2024-04-04 22:12:08 +03:00
bedohswe 7786e953ab Actually normalize gPlayer this time. 2024-04-05 00:08:44 +05:00
bedohswe 2f562e051f Return acos import into it's place. 2024-04-05 00:05:23 +05:00
bedohswe 6a89558273 Move some stuff around and make it so gPlayer is normalized every frame. 2024-04-05 00:03:41 +05:00
bedohswe c79b5b2b5b Merge pull request 'feat(levels): f5 to save current level' (#4) from n3tael/p3he:main into main
Reviewed-on: bedohswe/p3he#4
2024-04-04 18:44:45 +00:00
n3tael 35e7819818 Merge remote-tracking branch 'upstream/main' 2024-04-04 21:41:42 +03:00
n3tael 4208597ed3 feat(levels): f5 to save current level 2024-04-04 21:41:10 +03:00
bedohswe adf47220c7 Fix rotation display. 2024-04-04 23:31:45 +05:00
bedohswe 8169e9e7c3 Add a button to disable FPS cap. 2024-04-04 23:25:24 +05:00
bedohswe 84baa55a0c Add FPS cap. 2024-04-04 23:06:31 +05:00
bedohswe ac6330efd0 Use Numba to increase performance. 2024-04-04 23:03:47 +05:00
bedohswe 359edf4742 Make the minimap a bit more colourful. 2024-04-04 22:10:35 +05:00
bedohswe 69e8ee5569 Merge pull request 'feat(walls): player can create walls by pressing F3' (#3) from n3tael/p3he:main into main
Reviewed-on: bedohswe/p3he#3
2024-04-04 17:06:21 +00:00
n3tael 4604f8224c Merge remote-tracking branch 'upstream/main' 2024-04-04 20:04:21 +03:00
n3tael 3b2359aa41 Merge confilcts fixed 2024-04-04 20:03:20 +03:00
bedohswe 4d7a95545b Fix the minimap. 2024-04-04 22:02:44 +05:00
n3tael e1010930b5 feat(walls): player can create walls by pressing F3 2024-04-04 20:01:33 +03:00
bedohswe 4c7572208a Remove some comments. 2024-04-04 21:33:14 +05:00
bedohswe 983bad10a5 Show angle in degrees. 2024-04-04 21:32:01 +05:00
bedohswe f231973984 Add strafe. 2024-04-04 21:17:24 +05:00
bedohswe f7ee5715c7 Render natively in Poincaré. 2024-04-04 21:05:20 +05:00
bedohswe 380b9f96a4 Bruh. 2024-04-03 19:22:44 +05:00
bedohswe 42f88ff93b Oops. 2024-04-03 19:22:05 +05:00
bedohswe a2447c8f72 Merge branch 'n3tael-main' 2024-04-03 19:20:15 +05:00
bedohswe afee869fbf Fix merge conflict. 2024-04-03 19:20:04 +05:00
bedohswe f077c39130 A. 2024-04-03 19:15:42 +05:00
n3tael d834775835 Merge remote-tracking branch 'upstream/main' 2024-04-03 17:13:09 +03:00
n3tael 1b25a258cf feat(fps_counter): Added FPS counter, removed slowdown
you can hide debug info by pressing f3
2024-04-03 17:07:30 +03:00
bedohswe 753022bfb3 Merge pull request 'feat(ui); cPos, cRot with normal sizes, position' (#1) from n3tael/p3he:main into main
Reviewed-on: bedohswe/p3he#1
2024-04-03 13:24:49 +00:00
22 changed files with 807 additions and 109 deletions
+1
View File
@@ -1 +1,2 @@
__pycache__/ __pycache__/
levels/
+31 -1
View File
@@ -1,3 +1,33 @@
# 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 |
| Snap to the closest point | F6 |
| Snap to the origin | F7 |
| Snap the angle to the zero | F8 |
+40
View File
@@ -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()
+7
View File
@@ -0,0 +1,7 @@
from gyro import cNorm, deg2rad
from cmath import exp
I = complex(0,1)
WHITE = (255,255,255)
BLACK = (0,0,0)
ROT = cNorm(exp(deg2rad(2)*I))
IROT = 1/ROT
+79
View File
@@ -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
+46
View File
@@ -0,0 +1,46 @@
from cmath import exp
from constants import I, BLACK
from gyro import MobiusInt, MobiusAdd, MobiusBetween, MobiusDist, cDot, Poincare2Klein
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:
def __init__(self,dist,color):
self.dist = dist
self.color = color
@jit#(cache=True,parallel=True)
def draw(level,gPlayer,fov,res,dscale):
EMPTYDRAWN = DrawnSegment(0, BLACK)
drawn = [EMPTYDRAWN] * res
irot = exp(fov/res*I)
iprot = gPlayer.cRot
for i in range(res):
m = None
rot = irot**(i-(res/2)) * iprot
for j in level:
cInt = MobiusInt(j.pointA,j.pointB,gPlayer.cPos,rot)
if cDot(rot,MobiusAdd(cInt,-gPlayer.cPos)) > 0:
continue
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)
continue
if m.dist > rDist:
m = DrawnSegment(rDist,j.color)
drawn[i] = (m if m is not None else EMPTYDRAWN)
return drawn
+11
View File
@@ -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
+17
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=()
))
+1
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]}]
+4
View File
@@ -0,0 +1,4 @@
from engineevents import EngineEvent
def init(aoEngineEvents):
aoEngineEvents.append(EngineEvent('level', lambda _: []))
+17
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("squareroom.json").open('r') as leveldir:
level = json.loads(leveldir.read(), object_hook=serialize.object_hook)
aoEngineEvents.append(EngineEvent(
'level',
lambda _: level,
tsArguments=()
))
+1
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": [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]}]
+17
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=()
))
+195
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
]
}
]
+17
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("triangle.json").open('r') as leveldir:
level = json.loads(leveldir.read(), object_hook=serialize.object_hook)
aoEngineEvents.append(EngineEvent(
'level',
lambda _: level,
tsArguments=()
))
+1
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.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]}]
+86 -5
View File
@@ -1,38 +1,105 @@
from numpy import complex256 from numba import jit, c16
from numba.experimental import jitclass
from cmath import sqrt
from math import tanh, atanh, pi
from lines import cLineIntersection, cBetween
def deg2rad(rA): return rA/180*pi
def rad2degd(rA): return rA*180/pi
@jit(cache=True)
def cap(rN):
if rN < 0:
return 0
return rN
@jit(cache=True)
def ZeroCheck(cN): def ZeroCheck(cN):
if cN == complex256(complex(0,0)): if cN == complex(0,0):
return complex256(complex(1,0)) return complex(1,0)
else: else:
return cN return cN
@jit(cache=True)
def c_tr(cN): def c_tr(cN):
return cN.real, cN.imag return cN.real, cN.imag
@jit(cache=True)
def cNorm(cN): def cNorm(cN):
return ZeroCheck(cN)/abs(ZeroCheck(cN)) return ZeroCheck(cN)/abs(ZeroCheck(cN))
@jit(cache=True)
def cDot(cA, cB): def cDot(cA, cB):
return (cA * cB.conjugate()).real return (cA * cB.conjugate()).real
@jit(cache=True)
def cDist(cA, cB): def cDist(cA, cB):
return abs(cA-cB) return abs(cA-cB)
@jit(cache=True)
def MobiusInt(cA,cB,cC,cD): # Bruh
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): def MobiusAdd(cA, cB):
return (cA + cB) / (1 + cA.conjugate() * cB) return (cA + cB) / (1 + cA.conjugate() * cB)
@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(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(cache=True)
def MobiusScalar(cA, rT):
m = abs(cA)
return tanh(rT * atanh(m)) * cA / m
@jit(cache=True)
def MobiusCoadd(cA,cB):
return MobiusAdd(cA, (MobiusGyr(cA,-cB) * cB))
@jit(cache=True)
def MobiusCosub(cA,cB):
return MobiusAdd(cA, -(MobiusGyr(cA,cB) * cB))
@jit(cache=True)
def MobiusLine(cA,cB,rT):
return MobiusAdd(
cA,
MobiusScalar(
MobiusAdd(-cA,cB),
rT))
@jit(cache=True)
def MobiusDist(cA,cB):
return abs(MobiusAdd(-cB,cA))
@jit(cache=True)
def Poincare2Klein(cN): def Poincare2Klein(cN):
return 2*cN / (1 + cDot(cN,cN)) return 2*cN / (1 + cDot(cN,cN))
@jit(cache=True)
def Klein2Poincare(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 = complex256(cPos) self.cPos = complex(cPos)
self.cRot = complex256(cRot) self.cRot = complex(cRot)
self.normalize() self.normalize()
def __add__(gA, gB): def __add__(gA, gB):
@@ -40,6 +107,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)
@@ -70,3 +148,6 @@ class GyroVector:
def nrtransformed(self, cA): def nrtransformed(self, cA):
return MobiusAdd(self.cPos, cA / self.cRot) return MobiusAdd(self.cPos, cA / self.cRot)
def dist(self,gA):
return abs((-self+gA).cPos)
+49
View File
@@ -0,0 +1,49 @@
from os import makedirs
from time import time
import json
#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
segmentsig = [
('isFinite', b1),
('pointA', c16),
('pointB', c16),
('color', UniTuple(byte, 3))
]
@jitclass(segmentsig)
class Segment:
def __init__(self,bA,cA,cB,trColor):
self.isFinite = bA
self.pointA = cA
self.pointB = cB
self.color = trColor
def save_level(level):
makedirs("./levels/", exist_ok=True)
level_save = json.dumps(level, default=serialize.default)
unix_timestamp = int(time())
filename = f'{unix_timestamp}.json'
save = open(f"./levels/{filename}", "w")
save.write(level_save)
save.close()
return filename
def open_level(path):
save = open(path, "r")
r = json.loads(save.read(), object_hook=serialize.object_hook)
save.close()
return r
def make_wall(wall_buffer,level):
if (len(wall_buffer) != 2):
print("Warning: wall buffer doesn't contains 2 points.")
return
level.append(
Segment(True, wall_buffer[0], wall_buffer[1], (23,74,183))
)
+7 -1
View File
@@ -1,14 +1,20 @@
from numba import 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(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(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(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)
+158 -102
View File
@@ -1,125 +1,181 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from numpy import complex256 from math import copysign, pi, acos
from time import time_ns, sleep import sys
from cmath import exp, pi import os
from importlib import import_module
import pygame import pygame
import pygame.freetype import pygame.freetype
from gyro import *
from lines import *
def deg2rad(rA): return rA/180*PI from gyro import GyroVector, Poincare2Klein
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
I = complex256(complex(0,1)) gOrigin = GyroVector(0,1)
WHITE = (255,255,255)
BLACK = (0,0,0)
OFFSET = 0.015625
PI = pi
ROT = cNorm(exp(deg2rad(2)*I))
IROT = 1/ROT
F = 5 * 10**7
F_ = 10**9
SKY = (127,127,255)
GROUND = (102, 51, 0)
class Segment: def renderDebugInfo(gPlayer, clock, fontSize = 18):
def __init__(self,bA,cA,cB,trColor): font = pygame.freetype.Font(None, fontSize)
self.isFinite = bA font_fg = (255, 255, 255)
self.pointA = cA font_bg = (255, 255, 255, 80)
self.pointB = cB
self.color = trColor
class DrawnSegment: fps_surf = font.render("FPS: " + str(int(clock.get_fps())), font_fg, font_bg)[0]
def __init__(self,height,color): cPos_surf = font.render("cPos: " + str(gPlayer.cPos), font_fg, font_bg)[0]
self.height = height cRot_surf = font.render("cRot: " + str(copysign(acos(gPlayer.cRot.real),gPlayer.cRot.imag) / pi * 180), font_fg, font_bg)[0]
self.color = color
level = [ return fps_surf, cPos_surf, cRot_surf
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 draw(level,gPlayer,fov,res): def nonzerocap(n):
drawn = list() if n <= 0: return 1
irot = exp(fov/res*I) return n
iprot = gPlayer.cRot
for i in range(res):
m = DrawnSegment(0,BLACK)
rot = irot**(i-(res/2)) * iprot
for j in level:
tA = Poincare2Klein(MobiusAdd(j.pointA,-gPlayer.cPos))
tB = Poincare2Klein(MobiusAdd(j.pointB,-gPlayer.cPos))
try: # This function faults from time to time
cInt = cLineIntersection(tA,tB,complex256(0),rot)
except ZeroDivisionError:
continue
rDist = cDist(0,cInt)
if not (j.isFinite and cBetween(tA,tB,cInt)):
continue
if cDot(cInt,rot) > 0:
continue
if (1 - m.height) > rDist:
m = DrawnSegment(1-rDist,j.color)
drawn.append(m)
return drawn
def cap(rN):
if rN < 0:
return 0
return rN
def mainLoop():
gPlayer = GyroVector(0,1)
display = pygame.display.set_mode((1280,720))
font = pygame.freetype.Font(None,18)
while True:
framestart = time_ns()
for event in pygame.event.get():
if event.type == pygame.QUIT:
return True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gPlayer.cRot *= -1
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_w]:
gPlayer -= GyroVector(OFFSET * gPlayer.cRot, 1)
if keys[pygame.K_s]:
gPlayer += GyroVector(OFFSET * gPlayer.cRot, 1)
display.fill(WHITE)
#pygame.draw.rect(display,BLACK, c_tr(Poincare2Klein(gPlayer.cPos) * -100) + (100,100),0)
drawn = draw(level,gPlayer,PI/2,160)
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,360 - (cap(i.height) * 360),10,cap(i.height) * 1000))
n += 8
font.render_to(display, (8, 8), "cPos: " + str(gPlayer.cPos), (255, 255, 255), (0, 0, 0))
font.render_to(display, (8, 26), "cRot: " + str(gPlayer.cRot), (255, 255, 255), (0, 0, 0))
pygame.display.update()
frameend = time_ns()
sleep((frameend-framestart) / F_)
return True
def main(): def main():
pygame.init() pygame.init()
pygame.display.set_caption('P3HE') pygame.display.set_caption('P3HE')
if not pygame.get_init(): if not pygame.get_init():
return False return False
retstatus = mainLoop() offset = 0.0078125
return retstatus sky = (127,127,255)
ground = (102, 51, 0)
bCap = True
bCont = True
gPlayer = GyroVector(0,1)
display = pygame.display.set_mode((1280,720))
clock = pygame.time.Clock()
fontSize = 18
debugInfo = True
wall_buffer = []
alerts = []
aoEngineEvents = []
fvControl_ao = defaultcontrols
level = []
iDistScale = 1.1
def alert_append(alert, delay):
if len(alerts) >= 1:
alerts.pop()
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:
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)
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),4,int((1 -nonzerocap(i.dist)) * 1280)))
n += 4
gPlayer.normalize()
if debugInfo:
fps_surf, cPos_surf, cRot_surf = renderDebugInfo(gPlayer, clock, fontSize)
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.circle(display, BLACK, (100, 200), 100)
for i in level:
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))
for alert in alerts:
alert.draw(display)
pygame.display.update()
if bCap:
clock.tick(60)
else:
clock.tick()
return True
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")
+4
View File
@@ -0,0 +1,4 @@
llvmlite==0.42.0
numba==0.59.1
numpy==1.26.4
pygame==2.5.2
+18
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.")