Compare commits

..

No commits in common. "2f562e051f5855d9aae3b14dfe705e7a0cb0eb84" and "35e7819818ed0da3e0ef466ecc836eb4a221ad78" have entirely different histories.

5 changed files with 83 additions and 91 deletions

View File

@ -1,8 +0,0 @@
from gyro import cNorm, deg2rad
from cmath import exp
I = complex(0,1)
WHITE = (255,255,255)
BLACK = (0,0,0)
OFFSET = 0.015625
ROT = cNorm(exp(deg2rad(2)*I))
IROT = 1/ROT

32
draw.py
View File

@ -1,32 +0,0 @@
from cmath import exp
from constants import *
from gyro import *
from lines import *
class DrawnSegment:
def __init__(self,height,color):
self.height = height
self.color = color
def draw(level,gPlayer,fov,res):
drawn = list()
irot = exp(fov/res*I)
iprot = gPlayer.cRot
for i in range(res):
m = DrawnSegment(0,BLACK)
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 ZeroDivisionError:
continue
if cDot(rot,MobiusAdd(cInt,-gPlayer.cPos)) > 0:
continue
rDist = MobiusDist(cInt,gPlayer.cPos)*1.1
if not (j.isFinite and cBetween(j.pointA,j.pointB,cInt)):
continue
if (1 - m.height) > rDist:
m = DrawnSegment(1-rDist,j.color)
drawn.append(m)
return drawn

10
gyro.py
View File

@ -1,16 +1,8 @@
from numba import jit from numba import jit
from cmath import sqrt from cmath import sqrt
from math import tanh, atanh, pi from math import tanh, atanh
from lines import cLineIntersection from lines import cLineIntersection
def deg2rad(rA): return rA/180*pi
def rad2degd(rA): return rA*180/pi
@jit
def cap(rN):
if rN < 0:
return 0
return rN
@jit @jit
def ZeroCheck(cN): def ZeroCheck(cN):
if cN == complex(0,0): if cN == complex(0,0):

View File

@ -1,10 +1,4 @@
from os import makedirs from main import Segment
class Segment:
def __init__(self,bA,cA,cB,trColor):
self.isFinite = bA
self.pointA = cA
self.pointB = cB
self.color = trColor
def parse_level_from_string(string): def parse_level_from_string(string):
level = [] level = []
@ -34,28 +28,3 @@ def save_level_to_string(level):
string_level += '|' string_level += '|'
return string_level return string_level
def save_level(level):
makedirs("./levels/", exist_ok=True)
level_save = save_level_to_string(level)
unix_timestamp = int(time())
save = open(f"./levels/{unix_timestamp}.p3hel", "w")
save.write(level_save)
save.close()
def open_level(path):
save = open(path, "r")
r = save.read()
save.close()
return r
def make_wall(wall_buffer,level):
if (len(wall_buffer) != 2):
print('Warning: wall buffer not contains 2 points.')
return
level.append(
Segment(True, wall_buffer[0], wall_buffer[1], (23,74,183))
)

89
main.py
View File

@ -1,18 +1,28 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from numba import jit from numba import jit
from math import copysign, pi, acos from time import time_ns, sleep
from time import time from cmath import exp, pi
from math import acos, copysign
import pygame import pygame
import pygame.freetype import pygame.freetype
from os import makedirs
from levels import Segment from time import time
from gyro import * from gyro import *
from lines import *
from levels import * from levels import *
from constants import *
from draw import draw
def deg2rad(rA): return rA/180*PI
I = complex(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
bCap = True bCap = True
gOrigin = GyroVector(0,1) gOrigin = GyroVector(0,1)
@ -20,6 +30,18 @@ gOrigin = GyroVector(0,1)
SKY = (127,127,255) SKY = (127,127,255)
GROUND = (102, 51, 0) GROUND = (102, 51, 0)
class Segment:
def __init__(self,bA,cA,cB,trColor):
self.isFinite = bA
self.pointA = cA
self.pointB = cB
self.color = trColor
class DrawnSegment:
def __init__(self,height,color):
self.height = height
self.color = color
level = [ level = [
Segment(True,.6,I*.6,(255,0,0)), Segment(True,.6,I*.6,(255,0,0)),
Segment(True,.6,-I*.6,(0,255,0)), Segment(True,.6,-I*.6,(0,255,0)),
@ -27,6 +49,32 @@ level = [
Segment(True,-.6,I*.6,(255,255,0)) Segment(True,-.6,I*.6,(255,255,0))
] ]
def draw(level,gPlayer,fov,res):
drawn = list()
irot = exp(fov/res*I)
iprot = gPlayer.cRot
for i in range(res):
m = DrawnSegment(0,BLACK)
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 ZeroDivisionError:
continue
if cDot(rot,MobiusAdd(cInt,-gPlayer.cPos)) > 0:
continue
rDist = MobiusDist(cInt,gPlayer.cPos)*1.1
if not (j.isFinite and cBetween(j.pointA,j.pointB,cInt)):
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 renderDebugInfo(gPlayer, clock, fontSize = 18): def renderDebugInfo(gPlayer, clock, fontSize = 18):
font = pygame.freetype.Font(None, fontSize) font = pygame.freetype.Font(None, fontSize)
@ -39,7 +87,30 @@ def renderDebugInfo(gPlayer, clock, fontSize = 18):
return fps_surf, cPos_surf, cRot_surf return fps_surf, cPos_surf, cRot_surf
def make_wall(wall_buffer):
if (len(wall_buffer) != 2):
print('Warning: wall buffer not contains 2 points.')
return
level.append(
Segment(True, wall_buffer[0], wall_buffer[1], (23,74,183))
)
def save_level(level):
makedirs("./levels/", exist_ok=True)
level_save = save_level_to_string(level)
unix_timestamp = int(time())
save = open(f"./levels/{unix_timestamp}.p3hel", "w")
save.write(level_save)
save.close()
def open_level(path):
save = open(path, "r")
level = save.read() # don't know it working
save.close()
def mainLoop(): def mainLoop():
gPlayer = GyroVector(0,1) gPlayer = GyroVector(0,1)
@ -64,7 +135,7 @@ def mainLoop():
if event.key == pygame.K_F2: if event.key == pygame.K_F2:
if (len(wall_buffer) == 1): if (len(wall_buffer) == 1):
wall_buffer.append(gPlayer.cPos) wall_buffer.append(gPlayer.cPos)
make_wall(wall_buffer, level) make_wall(wall_buffer)
wall_buffer.clear() wall_buffer.clear()
else: else:
wall_buffer.append(gPlayer.cPos) wall_buffer.append(gPlayer.cPos)
@ -90,7 +161,7 @@ def mainLoop():
gPlayer += GyroVector(OFFSET * gPlayer.cRot, 1) gPlayer += GyroVector(OFFSET * gPlayer.cRot, 1)
display.fill(WHITE) display.fill(WHITE)
drawn = draw(level,gPlayer,pi/4,320) drawn = draw(level,gPlayer,PI/4,320)
pygame.draw.rect(display,SKY, (0,0,1280,360)) pygame.draw.rect(display,SKY, (0,0,1280,360))
pygame.draw.rect(display,GROUND, (0,360,1280,360)) pygame.draw.rect(display,GROUND, (0,360,1280,360))
n = 0 n = 0