frankie-on-platform/fop.py

387 lines
14 KiB
Python
Raw Normal View History

2023-07-19 01:32:03 +02:00
import bge # Bibliothèque Blender Game Engine (UPBGE)
import bpy # Blender
import math, time, random
import aud # Sounds
2023-07-18 12:16:31 +02:00
###############################################################################
# fop.py
# @title: module principale du jeu Frankie on Platform
# @project: Frankie on Platform
# @lang: fr
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
# @copyright: Copyright (C) 2023 Philippe Roy
# @license: GNU GPL
###############################################################################
# Scène
2023-07-18 12:16:31 +02:00
scene = bge.logic.getCurrentScene()
2023-07-19 01:32:03 +02:00
eevee = bpy.context.scene.eevee
audiodev = aud.Device()
2023-07-21 15:02:39 +02:00
# print("Objets de la scène : ", scene.objects) # Lister les objets de la scène
2023-07-18 12:16:31 +02:00
2023-07-21 15:02:39 +02:00
# Constantes UPBGE
2023-07-18 12:16:31 +02:00
JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
JUST_RELEASED = bge.logic.KX_INPUT_JUST_RELEASED
ACTIVATE = bge.logic.KX_INPUT_ACTIVE
2023-07-21 15:02:39 +02:00
PLAY = bge.logic.KX_ACTION_MODE_PLAY
LOOP = bge.logic.KX_ACTION_MODE_LOOP
2023-07-18 12:16:31 +02:00
2023-07-23 11:09:34 +02:00
# Sons d'action
snd_frankie_jump = aud.Sound('asset/sounds/frankie_jump.wav')
# Sons d'ambiance : fichier son , temps (en s) entre chaque impulsion, activation aléatoire à 50% lors de l'impulsion (oui/non)
snd_amb = [
[aud.Sound('asset/sounds/amb_bird_1.wav'),10, True],
[aud.Sound('asset/sounds/amb_bird_2.wav'),25, True],
[aud.Sound('asset/sounds/amb_cricket_1.wav'),2, True],
[aud.Sound('asset/sounds/amb_forest.wav'),9, False],
[aud.Sound('asset/sounds/amb_frog_1.wav'),4, True]]
2023-07-18 12:16:31 +02:00
###############################################################################
2023-07-19 01:32:03 +02:00
# Mouvements
2023-07-18 12:16:31 +02:00
###############################################################################
2023-07-19 01:32:03 +02:00
###
# Flèches pour avancer, reculer et tourner
2023-07-19 01:32:03 +02:00
###
2023-07-22 02:06:25 +02:00
def deplacement(cont):
2023-07-18 12:16:31 +02:00
obj = cont.owner
2023-07-21 15:02:39 +02:00
obj_rig = scene.objects['Frankie-rig']
2023-07-19 01:32:03 +02:00
obj_cam = scene.objects['Camera']
2023-07-22 02:06:25 +02:00
# Clavier
2023-07-18 12:16:31 +02:00
keyboard = bge.logic.keyboard
2023-07-22 02:06:25 +02:00
# Manette
2023-07-22 02:06:25 +02:00
joy_index = 0 #int from 0 to 6
joy = bge.logic.joysticks[joy_index]
if joy is not None :
joy_events = joy.activeButtons
joy_axis = joy.axisValues[0:4]
joy_leftstick_x = joy_axis[0]
joy_leftstick_y = joy_axis[1]
joy_rightstick_x = joy_axis[2]
joy_rightstick_y = joy_axis[3]
else:
joy_events=[]
joy_leftstick_x, joy_leftstick_y, joy_rightstick_x, joy_rightstick_y = 0, 0, 0, 0
2023-07-22 02:06:25 +02:00
# Evènements de la manette
# print ("leftstick : ", joy_leftstick_x , joy_leftstick_y)
# print ("rightstick : ", joy_rightstick_x , joy_rightstick_y)
# if joy_events:
# print(joy_events)
# Panneau de départ (Intro)
if obj_cam['intro']:
if keyboard.inputs[bge.events.UPARROWKEY].status[0] == ACTIVATE:
obj_cam['intro'] = False
if keyboard.inputs[bge.events.DOWNARROWKEY].status[0] == ACTIVATE:
obj_cam['intro'] = False
if keyboard.inputs[bge.events.LEFTARROWKEY].status[0] == ACTIVATE:
obj_cam['intro'] = False
if keyboard.inputs[bge.events.RIGHTARROWKEY].status[0] == ACTIVATE:
obj_cam['intro'] = False
if joy_leftstick_y<-0.5 or joy_leftstick_y>0.5 or joy_leftstick_x<-0.5 or joy_leftstick_x>0.5 :
obj_cam['intro'] = False
if obj_cam['intro'] == False:
scene.active_camera = scene.objects["Camera"]
return
# Paramètres de mouvement
2023-07-19 01:32:03 +02:00
pas_lineaire = 0.1
pas_courrir = 0.2
pas_angulaire = 0.05
pas_angulaire_fps = 0.025
force_saut = 400
2023-07-19 01:32:03 +02:00
recul_camera = 40
2023-07-21 15:02:39 +02:00
idle=True
2023-07-18 12:16:31 +02:00
2023-07-22 02:06:25 +02:00
# Avancer : Flèche haut - Up arrow
if (keyboard.inputs[bge.events.UPARROWKEY].status[0] == ACTIVATE or joy_leftstick_y<-0.5) and obj['chute']==False:
2023-07-21 15:02:39 +02:00
idle=False
2023-07-22 00:27:19 +02:00
# Planer
if obj['saut']:
2023-07-19 01:32:03 +02:00
obj.applyMovement((0,-pas_lineaire,0), True)
2023-07-22 00:27:19 +02:00
start, end, speed, layer = 1, 13, 2, 0
obj_rig.playAction('Frankie-planer', start, end, 0, 1, 1.0, PLAY, 0.0, 0, speed)
obj['courrir_tics']=0
obj['courrir']=False
2023-07-19 01:32:03 +02:00
else:
2023-07-22 00:27:19 +02:00
# Avancer normalement
if obj['courrir_tics']<40: # 40 : nombre de tics pour courrir
obj.applyMovement((0,-pas_lineaire,0), True)
obj['courrir_tics']+=1
start, end, speed, layer = 1, 18, 2, 0
obj_rig.playAction('Frankie-avancer', start, end, 0, 1, 1.0, PLAY, 0.0, 0, speed)
# Courrir
else:
obj.applyMovement((0,-pas_courrir,0), True)
obj['courrir']=True
start, end, speed, layer = 1, 18, 2, 0
obj_rig.playAction('Frankie-courrir', start, end, layer, 1, 1.0, PLAY, 0.0, 0, speed)
# Arrêt de la course
2023-07-22 02:06:25 +02:00
if (keyboard.inputs[bge.events.UPARROWKEY].status[0] != ACTIVATE and joy_leftstick_y>-0.5) and obj['courrir_tics']>0: # Arrêt de la course
2023-07-22 00:27:19 +02:00
obj['courrir_tics']=0
obj['courrir']=False
2023-07-22 02:06:25 +02:00
2023-07-19 01:32:03 +02:00
# Reculer : Flèche bas - Down arrow
2023-07-22 02:06:25 +02:00
if (keyboard.inputs[bge.events.DOWNARROWKEY].status[0] == ACTIVATE or joy_leftstick_y>0.5) and obj['saut']==False and obj['chute']==False:
2023-07-21 15:02:39 +02:00
idle=False
2023-07-19 01:32:03 +02:00
obj.applyMovement((0,pas_lineaire,0), True)
2023-07-21 21:32:22 +02:00
start, end, speed, layer = 1, 20, 2, 0
obj_rig.playAction('Frankie-reculer', start, end, layer, 1, 1.0, PLAY, 0.0, 0, speed)
2023-07-18 12:16:31 +02:00
2023-07-19 01:32:03 +02:00
# Tourner gauche : Flèche gauche - Left arrow
2023-07-22 02:06:25 +02:00
if (keyboard.inputs[bge.events.LEFTARROWKEY].status[0] == ACTIVATE or joy_leftstick_x<-0.5) and obj['chute']==False:
2023-07-21 15:02:39 +02:00
idle=False
if obj_cam['fps_mode']==0:
2023-07-19 01:32:03 +02:00
obj.applyRotation((0, 0,pas_angulaire), True)
else:
obj.applyRotation((0, 0,pas_angulaire_fps), True)
2023-07-21 21:32:22 +02:00
start, end, speed, layer = 1, 12, 2, 0
2023-07-21 15:02:39 +02:00
obj_rig.playAction('Frankie-gauche', start, end, 0, 1, 1.0, PLAY, 0.0, 0, speed)
2023-07-19 01:32:03 +02:00
# Tourner droite : Flèche droit - Right arrow
2023-07-22 02:06:25 +02:00
if (keyboard.inputs[bge.events.RIGHTARROWKEY].status[0] == ACTIVATE or joy_leftstick_x>0.5) and obj['chute']==False:
2023-07-21 15:02:39 +02:00
idle=False
if obj_cam['fps_mode']==0:
2023-07-19 01:32:03 +02:00
obj.applyRotation((0, 0,-pas_angulaire), True)
else:
obj.applyRotation((0, 0,-pas_angulaire_fps), True)
2023-07-21 21:32:22 +02:00
start, end, speed, layer = 1, 12, 2, 0
obj_rig.playAction('Frankie-droite', start, end, layer, 1, 1.0, PLAY, 0.0, 0, speed)
2023-07-19 01:32:03 +02:00
# Saut
if (keyboard.inputs[bge.events.SPACEKEY].status[0] == ACTIVATE or 3 in joy_events) and obj['chute']==False and obj['saut']==False:
2023-07-21 15:02:39 +02:00
idle=False
obj['saut']=True
obj.applyForce((0, 0,force_saut), True)
start, end, speed, layer = 1, 34, 2, 1
obj_rig.playAction('Frankie-sauter', start, end, layer, 1, 1.0, PLAY, 0.0, 0, speed)
2023-07-23 11:09:34 +02:00
audiodev.play(snd_frankie_jump)
2023-07-19 01:32:03 +02:00
2023-07-22 00:27:19 +02:00
# Touche le sol -> plus de saut ni de chute
2023-07-21 15:02:39 +02:00
objs_sol=('Terrain', 'Platforme', 'Pont 1', 'Pont 2')
for obj_sol in objs_sol:
if obj.collide(scene.objects[obj_sol])[0]:
2023-07-22 00:27:19 +02:00
if obj.collide(scene.objects[obj_sol])[1] is not None:
if len(obj.collide(scene.objects[obj_sol])[1])>0:
if obj['saut']:
obj['sol_tics']+=1
if obj['sol_tics']>3: # 3 : nombre de tics sur le sol pour annuler le saut
obj['saut']=False
obj['sol_tics']=0
obj['chute_tics']=0
obj['chute']=False
break
# Chute
if obj.worldPosition.z < obj['chute_z'] and obj['saut']==False:
obj['chute_tics']+=1
2023-07-21 15:02:39 +02:00
obj['chute_z'] = obj.worldPosition.z
2023-07-22 02:06:25 +02:00
if obj['chute_tics']>30: # 30 : nombre de tics pour déclarer la chute
2023-07-21 15:02:39 +02:00
idle=False
2023-07-22 00:27:19 +02:00
obj['chute']=True
2023-07-21 21:32:22 +02:00
start, end, speed, layer = 1, 33, 2, 0
obj_rig.playAction('Frankie-tomber', start, end, layer, 1, 1.0, LOOP, 0.0, 0, speed)
2023-07-21 15:02:39 +02:00
# Attente
if idle:
2023-07-21 21:32:22 +02:00
start, end, speed, layer = 1, 268, 2, 0
obj_rig.playAction('Frankie-attente', start, end, layer, 1, 1.0, LOOP, 0.0, 0, speed)
2023-07-21 15:02:39 +02:00
# Caméra FPS
2023-07-22 02:06:25 +02:00
if JUST_ACTIVATED in keyboard.inputs[bge.events.FKEY].queue :
if obj_cam['fps_mode']==0:
obj_cam['fps_mode'] = 1
scene.active_camera = scene.objects["Camera_fps"]
elif obj_cam['fps_mode']==1:
obj_cam['fps_mode'] = 2
scene.active_camera = scene.objects["Camera_fps2"]
else:
obj_cam['fps_mode'] = 0
scene.active_camera = scene.objects["Camera"]
2023-07-22 02:06:25 +02:00
if 0 in joy_events:
if obj_cam['fps_mode']==0 or obj_cam['fps_mode']==2:
obj_cam['fps_mode'] = 1
2023-07-22 02:06:25 +02:00
scene.active_camera = scene.objects["Camera_fps"]
time.sleep(0.15) # Change trop rapidement
else:
obj_cam['fps_mode'] = 0
scene.active_camera = scene.objects["Camera"]
time.sleep(0.15) # Change trop rapidement
if 1 in joy_events:
if obj_cam['fps_mode']==0 or obj_cam['fps_mode']==1:
obj_cam['fps_mode'] = 2
scene.active_camera = scene.objects["Camera_fps2"]
time.sleep(0.15) # Change trop rapidement
else:
obj_cam['fps_mode'] = 0
2023-07-22 02:06:25 +02:00
scene.active_camera = scene.objects["Camera"]
time.sleep(0.15) # Change trop rapidement
2023-07-19 01:32:03 +02:00
# Recul caméra
2023-07-22 02:06:25 +02:00
if (keyboard.inputs[bge.events.EKEY].status[0] == ACTIVATE or 10 in joy_events) and obj_cam['macro']==False:
2023-07-19 01:32:03 +02:00
obj_cam.applyMovement((0,0,recul_camera), True)
obj_cam['macro'] = True
2023-07-22 02:06:25 +02:00
if (keyboard.inputs[bge.events.EKEY].status[0] != ACTIVATE and 10 not in joy_events) and obj_cam['macro']==True:
2023-07-19 01:32:03 +02:00
obj_cam.applyMovement((0,0,-recul_camera), True)
obj_cam['macro'] = False
2023-07-22 02:06:25 +02:00
# Retour au dernier checkpoint
if keyboard.inputs[bge.events.RKEY].status[0] == ACTIVATE or 9 in joy_events :
obj_spawn = scene.objects['Spawn '+str(obj['spawn'])]
obj.worldPosition = obj_spawn.worldPosition
obj.applyRotation((0, 0, -obj.worldOrientation.to_euler().z+obj_spawn['sens']), False)
start, end, speed, layer = 1, 84, 2, 1
obj_rig.playAction('Frankie-respawn', start, end, layer, 1, 1.0, PLAY, 0.0, 0, speed)
2023-07-22 02:06:25 +02:00
obj['saut'] = False
obj['chute_tics'] = 0
obj['chute'] = False
obj['courrir_tics'] = 0
obj['courrir'] = False
2023-07-21 15:02:39 +02:00
# Saut sur les téléportes (mode debug) : avancer
if JUST_ACTIVATED in keyboard.inputs[bge.events.ZKEY].queue and obj['spawn']<8 and obj['debugg']:
obj['spawn'] +=1
2023-07-22 00:27:19 +02:00
obj['saut'] = False
obj['chute_tics'] = 0
obj['chute'] = False
obj['courrir_tics'] = 0
obj['courrir'] = False
obj_spawn = scene.objects['Spawn '+str(obj['spawn'])]
obj.worldPosition = obj_spawn.worldPosition
2023-07-20 22:44:24 +02:00
obj.applyRotation((0, 0, -obj.worldOrientation.to_euler().z+obj_spawn['sens']), False)
2023-07-21 15:02:39 +02:00
# Saut sur les téléportes (mode debug) : reculer
if JUST_ACTIVATED in keyboard.inputs[bge.events.AKEY].queue and obj['spawn']>0 and obj['debugg']:
obj['spawn'] -=1
2023-07-22 00:27:19 +02:00
obj['saut'] = False
obj['chute_tics'] = 0
obj['chute'] = False
obj['courrir_tics'] = 0
obj['courrir'] = False
obj_spawn = scene.objects['Spawn '+str(obj['spawn'])]
obj.worldPosition = obj_spawn.worldPosition
2023-07-20 22:44:24 +02:00
obj.applyRotation((0, 0, -obj.worldOrientation.to_euler().z+obj_spawn['sens']), False)
2023-07-19 01:32:03 +02:00
###
# Suivi par la caméra
###
def camera_track (cont):
cont.owner.worldPosition = scene.objects['Frankie'].worldPosition
2023-07-18 12:16:31 +02:00
###############################################################################
2023-07-19 01:32:03 +02:00
# Cycle
2023-07-18 12:16:31 +02:00
###############################################################################
###
2023-07-18 12:16:31 +02:00
# Initialisation de la scène
###
2023-07-22 02:06:25 +02:00
def init(cont):
obj = cont.owner
2023-07-18 12:16:31 +02:00
2023-07-19 01:32:03 +02:00
# Init EEVEE
eevee.use_taa_reprojection = True
eevee.use_ssr = True # Screen space reflection
eevee.use_ssr_refraction = True # Screen space refractions
eevee.use_ssr_halfres = True
eevee.use_gtao = False
eevee.taa_render_samples = 64
eevee.taa_samples = 16
eevee.use_volumetric_lights = True
eevee.use_volumetric_shadows = False
eevee.shadow_cascade_size='1024'
eevee.shadow_cube_size='512'
# Intro
scene.active_camera = scene.objects["Camera_depart"]
2023-07-21 15:02:39 +02:00
# Mode debug
2023-07-22 00:27:19 +02:00
scene.objects['Frankie']['debugg']=True # 'debug' est réservé
2023-07-21 15:02:39 +02:00
# Init de Frankie
scene.objects['Frankie']['chute_z'] = scene.objects['Frankie'].worldPosition.z
scene.objects['Frankie']['spawn']=0
2023-07-21 15:02:39 +02:00
# Cacher les pancartes
2023-07-19 01:32:03 +02:00
for i in range (1, 7):
scene.objects['Pancarte '+str(i)].setVisible(False,True)
2023-07-23 18:24:55 +02:00
# Afficher la végétation -> noeuds géométriques
2023-07-23 11:09:34 +02:00
2023-07-21 15:02:39 +02:00
# Mise en route de la plateforme
2023-07-21 21:32:22 +02:00
start, end, speed, layer = 1, 200, 0.5, 0
scene.objects['Platforme'].playAction('Platforme-action', start, end, layer, 1, 1.0, LOOP, 0.0, 0, speed)
2023-07-20 22:44:24 +02:00
###
2023-07-19 01:32:03 +02:00
# Chute
###
2023-07-22 02:06:25 +02:00
def chute(cont):
2023-07-19 01:32:03 +02:00
obj = cont.owner
obj_rig = scene.objects['Frankie-rig']
2023-07-22 02:06:25 +02:00
if obj.worldPosition.z<=-10 :
2023-07-19 01:32:03 +02:00
obj_spawn = scene.objects['Spawn '+str(obj['spawn'])]
obj.worldPosition = obj_spawn.worldPosition
2023-07-21 15:02:39 +02:00
obj.applyRotation((0, 0, -obj.worldOrientation.to_euler().z+obj_spawn['sens']), False)
start, end, speed, layer = 1, 84, 2, 1
obj_rig.playAction('Frankie-respawn', start, end, layer, 1, 1.0, PLAY, 0.0, 0, speed)
2023-07-22 00:27:19 +02:00
obj['saut'] = False
obj['chute_tics'] = 0
obj['chute'] = False
obj['courrir_tics'] = 0
obj['courrir'] = False
2023-07-19 01:32:03 +02:00
###
2023-07-19 01:32:03 +02:00
# Checkpoint
###
2023-07-22 02:06:25 +02:00
def checkpoint(cont):
2023-07-19 01:32:03 +02:00
obj = cont.owner
obj_i=int(obj.name[6:-7])
2023-07-19 01:32:03 +02:00
obj_frankie = scene.objects['Frankie']
if obj_i > obj_frankie['spawn']:
obj_frankie['spawn']=obj_i
scene.objects['Pancarte '+str(obj_i)].setVisible(True,True)
2023-07-18 12:16:31 +02:00
###
2023-07-20 22:44:24 +02:00
# Ressort
###
2023-07-22 02:06:25 +02:00
def ressort(cont):
2023-07-20 22:44:24 +02:00
obj = cont.owner
sensor = obj.sensors['Collision']
force_saut = 1000
obj_ressort=scene.objects[obj.name[:-7]]
obj_frankie = scene.objects['Frankie']
if sensor.positive:
2023-07-21 21:32:22 +02:00
start, end, speed, layer = 1, 16, 0.5, 0
obj_ressort.playAction(obj_ressort.name+'-action', start, end, layer, 1, 1.0, PLAY, 0.0, 0, speed)
2023-07-20 22:44:24 +02:00
obj_frankie.applyForce((0, 0,force_saut), True)
###
# Ambiance sonore
###
2023-07-20 22:44:24 +02:00
def snd_amb_play(cont):
obj = cont.owner
2023-07-23 11:09:34 +02:00
if obj['snd_amb']:
for snd in snd_amb:
if round(obj['snd_amb_timer'] % snd[1], 2) <= 0.02 :
if snd[2]:
if random.randint(0,1)==1:
audiodev.play(snd[0])
else:
audiodev.play(snd[0])