Change how games are initialized.

This commit is contained in:
bʰedoh₂ swé 2024-04-07 16:50:37 +05:00
parent f3f9f31a05
commit db3ea32c75
3 changed files with 72 additions and 83 deletions

View File

@ -2,10 +2,16 @@ import importlib.resources as impr
import sys import sys
import json import json
def init(serialize): def init(modules, aoEngineEvents):
serialize = modules['serialize']
EngineEvent = modules['engineevents'].EngineEvent
curm = sys.modules[__name__] curm = sys.modules[__name__]
files = impr.files(curm) files = impr.files(curm)
level = None level = None
with files.joinpath("squareroom.json").open('r') as leveldir: with files.joinpath("squareroom.json").open('r') as leveldir:
level = json.loads(leveldir.read(), object_hook=serialize.object_hook) level = json.loads(leveldir.read(), object_hook=serialize.object_hook)
return level aoEngineEvents.append(EngineEvent(
'level',
lambda _: level,
tsArguments=()
))

View File

@ -2,10 +2,16 @@ import importlib.resources as impr
import sys import sys
import json import json
def init(serialize): def init(modules, aoEngineEvents):
serialize = modules['serialize']
EngineEvent = modules['engineevents'].EngineEvent
curm = sys.modules[__name__] curm = sys.modules[__name__]
files = impr.files(curm) files = impr.files(curm)
level = None level = None
with files.joinpath("triangle.json").open('r') as leveldir: with files.joinpath("triangle.json").open('r') as leveldir:
level = json.loads(leveldir.read(), object_hook=serialize.object_hook) level = json.loads(leveldir.read(), object_hook=serialize.object_hook)
return level aoEngineEvents.append(EngineEvent(
'level',
lambda _: level,
tsArguments=()
))

135
main.py
View File

@ -2,21 +2,20 @@
from math import copysign, pi, acos from math import copysign, pi, acos
import sys import sys
from importlib import import_module
import pygame import pygame
import pygame.freetype import pygame.freetype
from gyro import GyroVector, Poincare2Klein from gyro import GyroVector, Poincare2Klein
from levels import open_level, save_level, make_wall
from constants import I, WHITE, BLACK, IROT, ROT, OFFSET from constants import I, WHITE, BLACK, IROT, ROT, OFFSET
from draw import draw from draw import draw
from alert import Alert from alert import Alert
from importlib import import_module from engineevents import EngineEvent, EngineEventProcessingError
from defaultcontrols import defaultcontrols
gOrigin = GyroVector(0,1) gOrigin = GyroVector(0,1)
SKY = (127,127,255)
GROUND = (102, 51, 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)
@ -33,67 +32,11 @@ def nonzerocap(n):
if n <= 0: return 1 if n <= 0: return 1
return n return n
class EngineEvent:
def __init__(self, sVariableToModify, fLambda, tsArguments=None):
self.sVariableToModify = sVariableToModify
self.fLambda = fLambda
if tsArguments is not None:
self.tsArguments = tsArguments
else:
self.tsArguments = (sVariableToModify,)
def defaultcontrols():
aoEngineEvents = list()
for event in pygame.event.get():
if event.type == pygame.QUIT:
aoEngineEvents.append(EngineEvent("bCont", lambda _: False))
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].rotate(-1), tsArguments=("gPlayer",)))
if event.key == pygame.K_F3:
aoEngineEvents.append(EngineEvent("debugInfo", lambda args: not args[0]))
if event.key == pygame.K_F1:
aoEngineEvents.append(EngineEvent("bCap", lambda args: not args[0]))
if event.key == pygame.K_F2:
def fX(args):
#0: wall_buffer
#1: gPlayer
#2: level
if (len(args[0]) == 1):
args[0].append(args[1].cPos)
make_wall(args[0], args[2])
args[0].clear()
else:
args[0].append(args[1].cPos)
aoEngineEvents.append(EngineEvent(None, fX, tsArguments=("wall_buffer", "gPlayer", "level") ))
if event.key == pygame.K_F5:
#0: level
#1: alert_append
#2: display
def fX(args):
filename = save_level(args[0])
alert = Alert(f"File saved as {filename}", args[2])
args[1](alert, 5)
aoEngineEvents.append(EngineEvent(None, fX, tsArguments=("level", "alert_append", "display") ))
if event.key == pygame.K_F4:
aoEngineEvents.append(EngineEvent(None, lambda args: print(args[0].cPos), tsArguments=("gPlayer",)))
keys = pygame.key.get_pressed()
if keys[pygame.K_d]:
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].rotate( ROT), tsArguments=("gPlayer",)))
if keys[pygame.K_a]:
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].rotate(IROT), tsArguments=("gPlayer",)))
if keys[pygame.K_q]:
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__iadd__(GyroVector(OFFSET * args[0].cRot*I, 1)), tsArguments=("gPlayer",)))
if keys[pygame.K_e]:
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__isub__(GyroVector(OFFSET * args[0].cRot*I, 1)), tsArguments=("gPlayer",)))
if keys[pygame.K_w]:
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__isub__(GyroVector(OFFSET * args[0].cRot , 1)), tsArguments=("gPlayer",)))
if keys[pygame.K_s]:
aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__iadd__(GyroVector(OFFSET * args[0].cRot , 1)), tsArguments=("gPlayer",)))
return aoEngineEvents
def mainLoop(): def mainLoop():
sky = (127,127,255)
ground = (102, 51, 0)
bCap = True bCap = True
bCont = True bCont = True
gPlayer = GyroVector(0,1) gPlayer = GyroVector(0,1)
@ -105,7 +48,8 @@ def mainLoop():
alerts = [] alerts = []
aoEngineEvents = [] aoEngineEvents = []
fvControl_ao = defaultcontrols fvControl_ao = defaultcontrols
level = []
def alert_append(alert, delay): def alert_append(alert, delay):
if len(alerts) >= 1: if len(alerts) >= 1:
alerts.pop() alerts.pop()
@ -113,15 +57,20 @@ def mainLoop():
alerts.append(alert) alerts.append(alert)
alert.start_hide_thread(alerts, delay) alert.start_hide_thread(alerts, delay)
try: def processevents():
game = import_module('games.' + sys.argv[1]) nonlocal bCap
except IndexError: nonlocal bCont
game = import_module("games.squareroom") nonlocal gPlayer
nonlocal display
level = game.init(import_module('serialize')) nonlocal fontSize
nonlocal debugInfo
while bCont: nonlocal wall_buffer
aoEngineEvents = fvControl_ao() nonlocal alerts
nonlocal alert_append
nonlocal aoEngineEvents
nonlocal level
nonlocal sky
nonlocal ground
state = { state = {
'bCap': bCap, 'bCap': bCap,
'bCont': bCont, 'bCont': bCont,
@ -133,24 +82,52 @@ def mainLoop():
'alerts': alerts, 'alerts': alerts,
'alert_append': alert_append, 'alert_append': alert_append,
'aoEngineEvents': aoEngineEvents, 'aoEngineEvents': aoEngineEvents,
'level': level 'level': level,
'sky': sky,
'ground': ground
} }
for i in aoEngineEvents: for i in aoEngineEvents:
if i.sVariableToModify == "bCont": if i.sVariableToModify == "bCont":
bCont = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments))) bCont = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
if i.sVariableToModify == "debugInfo": elif i.sVariableToModify == "debugInfo":
debugInfo = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments))) debugInfo = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
if i.sVariableToModify == "gPlayer": elif i.sVariableToModify == "gPlayer":
gPlayer = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments))) gPlayer = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
if i.sVariableToModify == "bCap": elif i.sVariableToModify == "bCap":
bCap = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments))) bCap = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
if i.sVariableToModify is None: elif i.sVariableToModify == "level":
level = 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))) 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(
{
'serialize': import_module('serialize'),
'alert': import_module('alert'),
'gyro': import_module('gyro'),
'engineevents': import_module('engineevents'),
'constants': import_module('constants'),
'lines': import_module('lines'),
},
aoEngineEvents
)
processevents()
while bCont:
aoEngineEvents = fvControl_ao()
processevents()
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
for i in drawn: 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((nonzerocap(i.dist)) * 360),8,int((1 -nonzerocap(i.dist)) * 1280)))