Compare commits

..

14 Commits

9 changed files with 88 additions and 28 deletions

40
alert.py Normal file
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()

View File

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

17
draw.py
View File

@ -4,8 +4,8 @@ from gyro import *
from lines import * from lines import *
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): def draw(level,gPlayer,fov,res):
@ -13,7 +13,7 @@ def draw(level,gPlayer,fov,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
@ -23,10 +23,13 @@ def draw(level,gPlayer,fov,res):
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)*1.1
if not (j.isFinite and cBetween(j.pointA,j.pointB,cInt)): if j.isFinite and not cBetween(j.pointA,j.pointB,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.append(m if m is not None else DrawnSegment(0, BLACK))
return drawn return drawn

View File

@ -18,8 +18,8 @@ def parse_level_from_string(string):
level.append( level.append(
Segment( Segment(
bool(raw_segment[0]), raw_segment[0] == "True",
float(raw_segment[1]), complex(raw_segment[1]),
complex(raw_segment[2]), complex(raw_segment[2]),
tuple(map(int, raw_segment[3][1:len(raw_segment[3])-1].split(', '))) tuple(map(int, raw_segment[3][1:len(raw_segment[3])-1].split(', ')))
) )
@ -43,9 +43,11 @@ def save_level(level):
level_save = save_level_to_string(level) level_save = save_level_to_string(level)
unix_timestamp = int(time()) unix_timestamp = int(time())
save = open(f"./levels/{unix_timestamp}.p3hel", "w") filename = f'{unix_timestamp}.p3hel'
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")

View File

@ -13,7 +13,7 @@ def cLineIntersection(cA0, cA1, cB0, cB1):
@jit @jit
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
def cBetween(cA,cB,cN): def cBetween(cA,cB,cN):

44
main.py
View File

@ -3,15 +3,17 @@ from numba import jit
from math import copysign, pi, acos from math import copysign, pi, acos
from time import time from time import time
import sys
import pygame import pygame
import pygame.freetype import pygame.freetype
from levels import Segment from levels import Segment, open_level, parse_level_from_string
from gyro import * from gyro import *
from levels import * from levels import *
from constants import * from constants import *
from draw import draw from draw import draw
from alert import Alert
bCap = True bCap = True
@ -20,14 +22,6 @@ gOrigin = GyroVector(0,1)
SKY = (127,127,255) SKY = (127,127,255)
GROUND = (102, 51, 0) 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)
font_fg = (255, 255, 255) font_fg = (255, 255, 255)
@ -39,7 +33,9 @@ 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():
gPlayer = GyroVector(0,1) gPlayer = GyroVector(0,1)
@ -48,6 +44,20 @@ def mainLoop():
fontSize = 18 fontSize = 18
debugInfo = True debugInfo = True
wall_buffer = [] wall_buffer = []
alerts = []
def alert_append(alert, delay):
if len(alerts) >= 1:
alerts.pop()
alerts.append(alert)
alert.start_hide_thread(alerts, delay)
try:
leveltoparse = open_level(sys.argv[1])
except IndexError:
leveltoparse = open_level("maps/squareroom.p3hel")
level = parse_level_from_string(leveltoparse)
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
@ -69,11 +79,10 @@ def mainLoop():
else: else:
wall_buffer.append(gPlayer.cPos) wall_buffer.append(gPlayer.cPos)
if event.key == pygame.K_F5: if event.key == pygame.K_F5:
save_level(level) filename = save_level(level)
if event.key == pygame.K_F9: alert = Alert(f"File saved as {filename}", display)
# TODO: @bedohswe make open file dialog
# open_level(path) alert_append(alert, 5)
pass
keys = pygame.key.get_pressed() keys = pygame.key.get_pressed()
if keys[pygame.K_d]: if keys[pygame.K_d]:
@ -95,7 +104,7 @@ def mainLoop():
pygame.draw.rect(display,GROUND, (0,360,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),8,int((1 -nonzerocap(i.dist)) * 1280)))
n += 4 n += 4
gPlayer.normalize() gPlayer.normalize()
if debugInfo: if debugInfo:
@ -112,6 +121,9 @@ def mainLoop():
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)

1
maps/cross.p3hel Normal file
View File

@ -0,0 +1 @@
False/1/0/(255, 0, 0 |False/1j/0/(0, 255, 0

1
maps/empty.p3hel Normal file
View File

@ -0,0 +1 @@
True/0/0/(0, 0, 0

1
maps/squareroom.p3hel Normal file
View File

@ -0,0 +1 @@
True/0.6/0.6j/(255, 0, 0)|True/0.6/-0.6j/(0, 255, 0)|True/-0.6/-0.6j/(0, 0, 255)|True/-0.6/0.6j/(255, 255, 0)