p3he/main.py

182 lines
6.3 KiB
Python
Executable File

#!/usr/bin/env python3
from math import copysign, pi, acos
import sys
import os
from importlib import import_module
import pygame
import pygame.freetype
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
gOrigin = GyroVector(0,1)
def renderDebugInfo(gPlayer, clock, fontSize = 18):
font = pygame.freetype.Font(None, fontSize)
font_fg = (255, 255, 255)
font_bg = (255, 255, 255, 80)
fps_surf = font.render("FPS: " + str(int(clock.get_fps())), font_fg, font_bg)[0]
cPos_surf = font.render("cPos: " + str(gPlayer.cPos), font_fg, font_bg)[0]
cRot_surf = font.render("cRot: " + str(copysign(acos(gPlayer.cRot.real),gPlayer.cRot.imag) / pi * 180), font_fg, font_bg)[0]
return fps_surf, cPos_surf, cRot_surf
def nonzerocap(n):
if n <= 0: return 1
return n
def main():
pygame.init()
pygame.display.set_caption('P3HE')
if not pygame.get_init():
return False
offset = 0.0078125
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__":
sys.path.append(os.path.realpath(__file__))
if not main():
print("An error occured")