Compare commits

..

No commits in common. "43b2d31980336e6978809445f379520dd217fe47" and "799165b2256417dfb2bf6321a747c362db918785" have entirely different histories.

9 changed files with 28 additions and 88 deletions

View File

@ -1,40 +0,0 @@
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)
WHITE = (255,255,255)
BLACK = (0,0,0)
OFFSET = 0.0078125
OFFSET = 0.015625
ROT = cNorm(exp(deg2rad(2)*I))
IROT = 1/ROT

17
draw.py
View File

@ -4,8 +4,8 @@ from gyro import *
from lines import *
class DrawnSegment:
def __init__(self,dist,color):
self.dist = dist
def __init__(self,height,color):
self.height = height
self.color = color
def draw(level,gPlayer,fov,res):
@ -13,7 +13,7 @@ def draw(level,gPlayer,fov,res):
irot = exp(fov/res*I)
iprot = gPlayer.cRot
for i in range(res):
m = None
m = DrawnSegment(0,BLACK)
rot = irot**(i-(res/2)) * iprot
for j in level:
try: # Function "LineIntersection" faults from time to time
@ -23,13 +23,10 @@ def draw(level,gPlayer,fov,res):
if cDot(rot,MobiusAdd(cInt,-gPlayer.cPos)) > 0:
continue
rDist = MobiusDist(cInt,gPlayer.cPos)*1.1
if j.isFinite and not cBetween(j.pointA,j.pointB,cInt):
if not (j.isFinite and cBetween(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.append(m if m is not None else DrawnSegment(0, BLACK))
if (1 - m.height) > rDist:
m = DrawnSegment(1-rDist,j.color)
drawn.append(m)
return drawn

View File

@ -18,8 +18,8 @@ def parse_level_from_string(string):
level.append(
Segment(
raw_segment[0] == "True",
complex(raw_segment[1]),
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(', ')))
)
@ -43,11 +43,9 @@ def save_level(level):
level_save = save_level_to_string(level)
unix_timestamp = int(time())
filename = f'{unix_timestamp}.p3hel'
save = open(f"./levels/{filename}", "w")
save = open(f"./levels/{unix_timestamp}.p3hel", "w")
save.write(level_save)
save.close()
return filename
def open_level(path):
save = open(path, "r")

View File

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

44
main.py
View File

@ -3,17 +3,15 @@ from numba import jit
from math import copysign, pi, acos
from time import time
import sys
import pygame
import pygame.freetype
from levels import Segment, open_level, parse_level_from_string
from levels import Segment
from gyro import *
from levels import *
from constants import *
from draw import draw
from alert import Alert
bCap = True
@ -22,6 +20,14 @@ 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):
font = pygame.freetype.Font(None, fontSize)
font_fg = (255, 255, 255)
@ -33,9 +39,7 @@ def renderDebugInfo(gPlayer, clock, fontSize = 18):
return fps_surf, cPos_surf, cRot_surf
def nonzerocap(n):
if n <= 0: return 1
return n
def mainLoop():
gPlayer = GyroVector(0,1)
@ -44,20 +48,6 @@ def mainLoop():
fontSize = 18
debugInfo = True
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:
for event in pygame.event.get():
@ -79,10 +69,11 @@ def mainLoop():
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)
save_level(level)
if event.key == pygame.K_F9:
# TODO: @bedohswe make open file dialog
# open_level(path)
pass
keys = pygame.key.get_pressed()
if keys[pygame.K_d]:
@ -104,7 +95,7 @@ def mainLoop():
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((1 - cap(i.height)) * 360),8,int(cap(i.height) * 1280)))
n += 4
gPlayer.normalize()
if debugInfo:
@ -121,9 +112,6 @@ def mainLoop():
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)

View File

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

View File

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

View File

@ -1 +0,0 @@
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)