Move some stuff around and make it so gPlayer is normalized every frame.

This commit is contained in:
bʰedoh₂ swé 2024-04-05 00:03:41 +05:00
parent c79b5b2b5b
commit 6a89558273
5 changed files with 91 additions and 83 deletions

8
constants.py Normal file
View File

@ -0,0 +1,8 @@
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 Normal file
View File

@ -0,0 +1,32 @@
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,8 +1,16 @@
from numba import jit
from cmath import sqrt
from math import tanh, atanh
from math import tanh, atanh, pi
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
def ZeroCheck(cN):
if cN == complex(0,0):

View File

@ -1,4 +1,10 @@
from main import Segment
from os import makedirs
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):
level = []
@ -28,3 +34,28 @@ def save_level_to_string(level):
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,28 +1,18 @@
#!/usr/bin/env python3
from numba import jit
from time import time_ns, sleep
from cmath import exp, pi
from math import acos, copysign
from math import copysign, pi
from time import time
import pygame
import pygame.freetype
from os import makedirs
from time import time
from levels import Segment
from gyro import *
from lines 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
gOrigin = GyroVector(0,1)
@ -30,18 +20,6 @@ gOrigin = GyroVector(0,1)
SKY = (127,127,255)
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 = [
Segment(True,.6,I*.6,(255,0,0)),
Segment(True,.6,-I*.6,(0,255,0)),
@ -49,32 +27,6 @@ level = [
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):
font = pygame.freetype.Font(None, fontSize)
@ -87,30 +39,7 @@ def renderDebugInfo(gPlayer, clock, fontSize = 18):
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():
gPlayer = GyroVector(0,1)
@ -135,7 +64,7 @@ def mainLoop():
if event.key == pygame.K_F2:
if (len(wall_buffer) == 1):
wall_buffer.append(gPlayer.cPos)
make_wall(wall_buffer)
make_wall(wall_buffer, level)
wall_buffer.clear()
else:
wall_buffer.append(gPlayer.cPos)
@ -161,7 +90,7 @@ def mainLoop():
gPlayer += GyroVector(OFFSET * gPlayer.cRot, 1)
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,GROUND, (0,360,1280,360))
n = 0