ropy/rp.py

1030 lines
41 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import bge # Blender Game Engine (UPBGE)
import bpy # Blender
import aud # Sounds
import math
import mathutils
import time
import sys
import os
import webbrowser
import threading # Multithreading
import xml.etree.ElementTree as ET # Creating/parsing XML file
import runpy
import rp_map1 as rp_map # waves script
import rp_doc # documentation
# import ct_cmd # user script (commands)
###############################################################################
# rp.py
# @title: Ropy
# @project: Ropy (Blender-EduTech)
# @lang: fr
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
# @copyright: Copyright (C) 2020-2022 Philippe Roy
# @license: GNU GPL
#
# Ropy est destiné à la découverte de la programmation procédurale et du language Python.
# A travers plusieurs challenges, donc de manière graduée, les élèves vont apprendre à manipuler les structures algorithmiques de base et à les coder en Python.
#
# Commands trigged by button : cmd_*
# Commands trigged by 3D scene objects : scn_*
# Commands trigged by user (student or map designer) : rp_*
# 3D scene manipulation : manip_*
#
###############################################################################
# Debug flag
scene = bge.logic.getCurrentScene()
scene.objects['Commands']['debug_fps']=False
# Memory
sys.setrecursionlimit(10**5) # Limite sur la récursivité (valeur par défaut : 1000) -> segfault de Blender
# Dynamic import user file
# importlib.invalidate_caches()
# ct_map = importlib.import_module('ct_map1') # Waves script
# ct_cmd = importlib.import_module('ct_cmd') # User script (commands) -> segfault de Blender
# UPBGE scene
eevee = bpy.context.scene.eevee
fps_time=0.0
# Config file
# print (os.getcwd())
rp_config = ET.parse('rp_config.xml')
rp_config_tree = rp_config.getroot()
# Colors
color_magenta = (0.800, 0.005, 0.315,1)
color_orange = (0.799, 0.130, 0.063,1)
color_white = (0.8, 0.8, 0.8, 1)
color_yellow = (0.8, 0.619, 0.021, 1)
color_black = (0, 0, 0, 1)
color_kaykit_black = (0.019, 0.032, 0.037, 1)
color_text = (0, 0, 0, 1) # Noir
color_text_red = (0.799, 0.031, 0.038, 1)
color_text_orange = (0.799, 0.176, 0.054, 1)
color_text_yellow = (0.799, 0.617, 0.021, 1)
# Sounds
audiodev = aud.Device()
snd_click = aud.Sound('asset/sounds/rp_click.ogg')
# sndbuff_click = aud.Sound.cache(snd_click)
snd_open = aud.Sound('asset/sounds/rp_open.ogg')
# sndbuff_open = aud.Sound.cache(snd_open)
snd_close = aud.Sound('asset/sounds/rp_close.ogg')
# sndbuff_close = aud.Sound.cache(snd_close)
snd_grid = aud.Sound('asset/sounds/rp_grid.ogg')
# sndbuff_grid = aud.Sound.cache(snd_grid)
# UPBGE constants
JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
JUST_RELEASED = bge.logic.KX_INPUT_JUST_RELEASED
ACTIVATE = bge.logic.KX_INPUT_ACTIVE
# JUST_DEACTIVATED = bge.logic.KX_SENSOR_JUST_DEACTIVATED
###############################################################################
# User interface : texte info et compteurs
###############################################################################
##
# Mise à jour de l'affichage des compteurs
##
def points_maj (cont):
# Texte
scene.objects['Points-Step-text']['Text']=str(scene.objects['Points']['step'])
scene.objects['Points-Level-text']['Text']=str(scene.objects['Points']['level'])
scene.objects['Points-Nbligne-text']['Text']=str(scene.objects['Points']['nbligne'])
scene.objects['Points']['tics'] +=1
# Fin de la mission
# FIXME
###############################################################################
# Terrain
###############################################################################
##
# Initialisation lors du chargement du terrain
##
def terrain_init ():
# Ajout du Hud
scene.active_camera = scene.objects["Camera"]
scene.objects['Sun'].setVisible(True,True)
scene.addOverlayCollection(scene.cameras['Camera-Hud'], bpy.data.collections['Hud'])
# Mémorisation des positions par défaut
# <worldPosition.x>0.0057830810546875</worldPosition.x>
# <worldPosition.y>-26.440298080444336</worldPosition.y>
# <worldPosition.z>20.22315788269043</worldPosition.z>
scene.objects['Camera']['init_lx']=0.0057830810546875
scene.objects['Camera']['init_ly']=-26.440298080444336
scene.objects['Camera']['init_lz']=20.22315788269043
# scene.objects['Camera']['init_lx']=scene.objects['Camera'].worldPosition.x
# scene.objects['Camera']['init_ly']=scene.objects['Camera'].worldPosition.y
# scene.objects['Camera']['init_lz']=scene.objects['Camera'].worldPosition.z
scene.objects['Camera']['past_lx']=scene.objects['Camera'].worldPosition.x
scene.objects['Camera']['past_ly']=scene.objects['Camera'].worldPosition.y
scene.objects['Camera']['past_lz']=scene.objects['Camera'].worldPosition.z
scene.objects['Terrain']['init_lx']=scene.objects['Terrain'].worldPosition.x
scene.objects['Terrain']['init_ly']=scene.objects['Terrain'].worldPosition.y
scene.objects['Terrain']['init_lz']=scene.objects['Terrain'].worldPosition.z
scene.objects['Terrain']['init_rx']=scene.objects['Terrain'].worldOrientation.to_euler().x
scene.objects['Terrain']['init_ry']=scene.objects['Terrain'].worldOrientation.to_euler().y
scene.objects['Terrain']['init_rz']=scene.objects['Terrain'].worldOrientation.to_euler().z
# Configuration du moteur de rendu
eevee.use_eevee_smaa = True
# Init de la carte
rp_map.map_init()
rp_map.map_reset()
# Récupération de la position de la caméra
scene.objects['Camera'].worldPosition.x = float(rp_config_tree[0][2][0].text)
scene.objects['Camera'].worldPosition.y = float(rp_config_tree[0][2][1].text)
scene.objects['Camera'].worldPosition.z = float(rp_config_tree[0][2][2].text)
##
# Mise en route et pause du cycle
##
def terrain_run ():
# Pause
# FIXME : HL alors que c'est avec les touches
# FIXME : Stopper Ropy
if scene.objects['Terrain']['run'] == True:
scene.objects['Terrain']['run']=False
scene.objects['Pause'].setVisible(False,False)
scene.objects['Pause'].suspendPhysics()
scene.objects['Pause-Hl'].setVisible(False,False)
scene.objects['Run']. restorePhysics()
scene.objects['Run-Hl'].setVisible(True,False)
scene.objects['Cmd-text']['Text']= "Exécuter (F5)"
# scene.objects['Cmd-text']['Text']= "Run (F5)"
scene.objects['Cmd-text'].setVisible(True,False) # FIXME : suppresion de du message
# Run
# FIXME : HL alors que c'est avec les touches
else :
scene.objects['Terrain']['run']=True
scene.objects['Run'].setVisible(False,False)
scene.objects['Run'].suspendPhysics()
scene.objects['Run-Hl'].setVisible(False,False)
scene.objects['Pause']. restorePhysics()
scene.objects['Pause-Hl'].setVisible(True,False)
scene.objects['Cmd-text']['Text']= "Pause (F5)"
scene.objects['Cmd-text'].setVisible(True,False)
# Démarrage de la map
if scene.objects['Terrain']['thread_run']==False:
scene.objects['Stop'].setVisible(True,False)
scene.objects['Stop']. restorePhysics()
runpy.run_module('rp_cmd', run_name='stop') # Stop du script utilisateur
# Mise à zéro des compteurs
rp_map.map_reset()
# Scripts utilisateur et vagues
scene.objects['Terrain']['thread_run']=True
runpy.run_module('rp_cmd', run_name='start') # Execution du script utilisateur
# Arrêt de la pause
else:
# FIXME : Relancer Ropy
pass
##
# Arrêt et réinitialisation du cycle
##
def terrain_stop ():
# Arrêt des threads utilisateurs
scene.objects['Terrain']['run']=False
scene.objects['Terrain']['thread_run']=False
runpy.run_module('rp_cmd', run_name='stop') # Stop du script utilisateur
# Commandes
scene.objects['Pause'].setVisible(False,False)
scene.objects['Pause'].suspendPhysics()
scene.objects['Pause-Hl'].setVisible(False,False)
scene.objects['Run'].setVisible(True,False)
scene.objects['Run']. restorePhysics()
scene.objects['Stop'].setVisible(False,False)
scene.objects['Stop'].suspendPhysics()
scene.objects['Stop-Hl'].setVisible(False,False)
scene.objects['Cmd-text'].setVisible(False,False)
##
# Fin de la map
##
def terrain_end ():
scene.objects['Terrain']['run']=False
scene.objects['Terrain']['thread_run']=False
# Commandes
scene.objects['Pause'].setVisible(False,False)
scene.objects['Pause-Hl'].setVisible(False,False)
scene.objects['Run'].setVisible(True,False)
scene.objects['Stop'].setVisible(False,False)
scene.objects['Stop-Hl'].setVisible(False,False)
##
# Vitesse du jeu
##
def terrain_speed (obj):
speed_mode=[0.25, 0.5, 1,2,4,10]
speed_mode_txt=["1/4", "1/2", "1", "2","4","10"]
i=speed_mode.index(scene.objects['Commands']['speed'])
# Affichage
if obj.name=="Speed_up" and i<5:
scene.objects['Commands']['speed']=speed_mode[i+1]
scene.objects['Text_speed']['Text']=speed_mode_txt[i+1]
if obj.name=="Speed_down" and i>0:
scene.objects['Commands']['speed']=speed_mode[i-1]
scene.objects['Text_speed']['Text']=speed_mode_txt[i-1]
# Maj du fichier de config (vitesse du jeu : data/config/speed -> [0][0].text)
rp_config_tree[0][0].text=str(scene.objects['Commands']['speed'])
buffer_xml = ET.tostring(rp_config_tree)
with open("rp_config.xml", "wb") as f:
f.write(buffer_xml)
##
# Grille
##
def terrain_grid ():
# pass
if scene.objects['Grid-u'].visible:
scene.objects['Grid-u'].setVisible(False,True)
scene.objects['Grid-v'].setVisible(False,True)
else:
scene.objects['Grid-u']['timer'] = 0.3
bpy.data.materials["Grid"].node_tree.nodes["Emission"].inputs[1].default_value =scene.objects['Grid-u']['timer']
# bpy.data.materials["Grid"].node_tree.nodes["Emission"].inputs[1].default_value =0.3
scene.objects['Grid-u'].setVisible(True,True)
scene.objects['Grid-v'].setVisible(True,True)
scene.objects['Grid-u']['anim'] = True
def terrain_grid_anim ():
bpy.data.materials["Grid"].node_tree.nodes["Emission"].inputs[1].default_value =scene.objects['Grid-u']['timer']
scene.objects['Grid-u']['timer']+=0.01
if scene.objects['Grid-u']['timer']== 2:
scene.objects['Grid-u']['anim'] = False
###############################################################################
# Sons
###############################################################################
def sound_play (sound):
if scene.objects['Commands']['sound']:
# playsound('rp_click.ogg')
audiodev.play(sound)
def sound_set ():
scene.objects['NoSound-cmd'].suspendPhysics()
scene.objects['NoSound-cmd'].setVisible(False,False)
scene.objects['NoSound-cmd-Hl'].setVisible(False,False)
scene.objects['Sound-cmd']. restorePhysics()
scene.objects['Sound-cmd-Hl'].setVisible(True,False)
scene.objects['Commands']['sound']=True
# scene.objects['Cmd-text']['Text']= "Mute"
scene.objects['Cmd-text']['Text']= "Muet"
scene.objects['Cmd-text'].setVisible(True,False)
# Maj du fichier de config (sound : data/config/sound -> [0][1].text)
rp_config_tree[0][1].text=str(scene.objects['Commands']['sound'])
buffer_xml = ET.tostring(rp_config_tree)
with open("rp_config.xml", "wb") as f:
f.write(buffer_xml)
def sound_unset ():
scene.objects['Sound-cmd'].suspendPhysics()
scene.objects['Sound-cmd'].setVisible(False,False)
scene.objects['Sound-cmd-Hl'].setVisible(False,False)
scene.objects['NoSound-cmd']. restorePhysics()
scene.objects['NoSound-cmd-Hl'].setVisible(True,False)
scene.objects['Commands']['sound']=False
# scene.objects['Cmd-text']['Text']= "Unmute"
scene.objects['Cmd-text']['Text']= "Rétablir le son"
scene.objects['Cmd-text'].setVisible(True,False)
# Maj du fichier de config (sound : data/config/sound -> [0][1].text)
rp_config_tree[0][1].text=str(scene.objects['Commands']['sound'])
buffer_xml = ET.tostring(rp_config_tree)
with open("rp_config.xml", "wb") as f:
f.write(buffer_xml)
###############################################################################
# Commandes
###############################################################################
color_cmd = (0.8, 0.8, 0.8, 1) # Blanc
color_cmd_hl = (0.8, 0.619, 0.021, 1) # Jaune
##
# Init
##
def cmd_init():
# UI : Commands
scene.objects['Run-Hl'].setVisible(False,False)
scene.objects['Pause'].setVisible(False,False)
scene.objects['Pause'].suspendPhysics()
scene.objects['Pause-Hl'].setVisible(False,False)
scene.objects['Stop'].setVisible(False,False)
scene.objects['Stop'].suspendPhysics()
scene.objects['Stop-Hl'].setVisible(False,False)
scene.objects['Objective-Hl'].setVisible(False,False)
scene.objects['Doc-cmd-Hl'].setVisible(False,False)
scene.objects['ResetView-Hl'].setVisible(False,False)
scene.objects['About-cmd-Hl'].setVisible(False,False)
# UI : Sounds
# Read config (sound : data/config/sound -> [0][1].text)
if rp_config_tree[0][1].text == "True":
sound_set ()
else:
sound_unset ()
# audiodev.unlock()
# UI : Text, ...
scene.objects['Cmd-text'].setVisible(False,False)
scene.objects['Grid-u'].setVisible(False,True)
scene.objects['Grid-v'].setVisible(False,True)
scene.objects['Points-Map-text'].setVisible(True,False)
scene.objects['Points-Map-text']['Text']=""
# scene.objects['Points-Map-text'].setVisible(False,False)
# scene.objects['Info-1-text'].setVisible(False,False)
# scene.objects['Info-2-text'].setVisible(False,False)
# UI : Mouse
# Window size : 738.5 415.5
if scene.objects['Mouse_main']['mouse_graphic']:
bge.render.setMousePosition(int(bge.render.getWindowWidth() / 2), int(bge.render.getWindowHeight() / 2))
bge.render.showMouse(scene.objects['Commands']['debug_mouse'])
scene.objects['Mouse_main'].worldPosition = [0.07, -8.11, 4.71035] # Vielle version : [0.118161, -8.24305, 4.71035] ; [0, -3.5, 2], [0, -8, 6]
scene.objects['Mouse_main'].worldScale=[30, 30, 30]
scene.objects['Mouse_main']['past_x']=0
scene.objects['Mouse_main']['past_y']=0
scene.objects['Mouse_main']['mouse_up']=0
else:
scene.objects['Mouse_main'].setVisible(False,False)
bge.render.showMouse(True)
# Speed
# Read config (game speed : data/config/speed -> [0][0].text)
speed_mode=[0.25, 0.5, 1,2,4,10]
speed_mode_txt=["1/4", "1/2", "1", "2","4","10"]
scene.objects['Commands']['speed']=float(rp_config_tree[0][0].text)
i=speed_mode.index(scene.objects['Commands']['speed'])
scene.objects['Text_speed']['Text']=speed_mode_txt[i]
# Windows
windows=("Doc", "Doc_chap-general", "Doc_chap-missions", "Doc_chap-rover", "Doc_chap-python", "About")
for window in windows:
scene.objects[window].setVisible(False,True)
rp_doc.init()
# Missions
# Read config (mission actuelle : data/config/mission_current -> [1][0].text)
# Read config (niveau atteint : data/config/mission_success -> [1][1].text)
scene.objects['Terrain']['mission_current']=int(rp_config_tree[1][0].text)
scene.objects['Terrain']['mission_success']=int(rp_config_tree[1][1].text)
scene.objects['Points-Map-text']['Text']="Mission "+str(scene.objects['Terrain']['mission_current'])
##
# Highlight des commandes
##
def cmd_hl(cont):
obj = cont.owner
# Activation
if cont.sensors['MO'].status == JUST_ACTIVATED and scene.objects['Terrain']['manip_mode']==0:
if obj.name!="Run" and obj.name!="Pause" and obj.name!="Stop":
obj.setVisible(False,True)
scene.objects[obj.name+'-Hl'].setVisible(True,True)
# Run et pause
if obj.name=="Pause" or obj.name=="Run":
if scene.objects['Terrain']['run'] == True:
scene.objects['Pause'].setVisible(False,False)
scene.objects['Pause-Hl'].setVisible(True,False)
else:
scene.objects['Run'].setVisible(False,False)
scene.objects['Run-Hl'].setVisible(True,False)
# Stop
if obj.name=="Stop":
if scene.objects['Terrain']['thread_run']==True:
scene.objects['Stop'].setVisible(False,False)
scene.objects['Stop-Hl'].setVisible(True,False)
# Text
text_hl ={"Run":"Exécuter (F5)",
"Stop":"Stop et initialisation (F6)",
"Pause":"Pause (F5)",
"Objective":"Afficher/cacher l'objectif",
"Doc-cmd":"Documentation",
"ResetView": "Reset de la vue (Touche Début)",
"About-cmd": "A propos",
"Speed_down": "Moins vite (-)",
"Speed_up": "Plus vite (+)",
"Sound-cmd": "Muet",
"NoSound-cmd": "Rétablir le son"}
# text_hl ={"Run":"Run (F5)",
# "Stop":"Stop (F6)",
# "Pause":"Pause (F5)",
# "Distance":"Show objective",
# "Doc-cmd":"Documentation",
# "ResetView": "Reset view (Home key)",
# "About-cmd": "About",
# "Speed_down": "Speed down (-)",
# "Speed_up": "Speed up (+)",
# "Sound-cmd": "Mute",
# "NoSound-cmd": "Unmute"}
scene.objects['Cmd-text']['Text']= text_hl[obj.name]
scene.objects['Cmd-text'].setVisible(True,False)
# Désactivation
if cont.sensors['MO'].status == JUST_RELEASED and (scene.objects['Terrain']['manip_mode']==0 or scene.objects['Terrain']['manip_mode']==9):
if obj.name!="Run" and obj.name!="Pause" and obj.name!="Stop" and obj.name!="Sound-cmd" and obj.name!="NoSound-cmd" :
scene.objects[obj.name+'-Hl'].setVisible(False,True)
obj.setVisible(True,True)
scene.objects['Cmd-text'].setVisible(False,False)
# Run et pause
if obj.name=="Pause" or obj.name=="Run":
if scene.objects['Terrain']['run'] == True:
scene.objects['Pause-Hl'].setVisible(False,False)
scene.objects['Pause'].setVisible(True,False)
scene.objects['Cmd-text'].setVisible(False,False)
else:
scene.objects['Run-Hl'].setVisible(False,False)
scene.objects['Run'].setVisible(True,False)
scene.objects['Cmd-text'].setVisible(False,False)
# Stop
if obj.name=="Stop":
if scene.objects['Terrain']['thread_run']==True:
scene.objects['Stop-Hl'].setVisible(False,False)
scene.objects['Stop'].setVisible(True,False)
scene.objects['Cmd-text'].setVisible(False,False)
# Sound
if obj.name=="NoSound-cmd" and scene.objects['Commands']['sound']==False:
scene.objects['NoSound-cmd-Hl'].setVisible(False,False)
scene.objects['NoSound-cmd'].setVisible(True,False)
scene.objects['Cmd-text'].setVisible(False,False)
if obj.name=="Sound-cmd" and scene.objects['Commands']['sound']==True:
scene.objects['Sound-cmd-Hl'].setVisible(False,False)
scene.objects['Sound-cmd'].setVisible(True,False)
scene.objects['Cmd-text'].setVisible(False,False)
##
# Click sur les commandes
##
def cmd_click (cont):
obj = cont.owner
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive and scene.objects['Terrain']['manip_mode']==0:
if obj.name=="Pause" or obj.name=="Run":
sound_play (snd_click)
terrain_run ()
if obj.name=="Stop":
sound_play (snd_click)
terrain_stop ()
if obj.name=="Objective":
sound_play (snd_grid)
terrain_grid ()
if obj.name=="Speed_up" or obj.name=="Speed_down":
sound_play (snd_click)
terrain_speed (obj)
if obj.name=="ResetView":
sound_play (snd_click)
manip_reset()
if obj.name=="Sound-cmd":
sound_unset ()
if obj.name=="NoSound-cmd":
sound_set ()
sound_play (snd_click)
if obj.name=="Doc-cmd":
sound_play (snd_open)
tablet_open ()
if obj.name=="About-cmd":
sound_play (snd_open)
about_open ()
###############################################################################
# Gestion du clavier
###############################################################################
##
# Mode
#
# 0 : rien (par défaut)
# 1 : Pan avec Shift
# 2 : Zoom avec Ctrl
# 8 : Fenêtre Documentation
# 9 : Fenêtre About
##
def mode(cont):
obj = cont.owner
keyboard = bge.logic.keyboard
# Touche ESC
if JUST_ACTIVATED in keyboard.inputs[bge.events.ESCKEY].queue:
# Fenêtres modales
if scene.objects['Terrain']['manip_mode']==9:
if scene.objects['About'].visible:
about_close()
return
else: # Sortir du jeu
terrain_stop ()
# Maj du fichier de config (position de la camera : data/config/cam_x,y,z -> [0][2][0].text, [0][2][1].text, [0][2][2].text)
rp_config_tree[0][2][0].text=str(scene.objects['Camera'].worldPosition.x)
rp_config_tree[0][2][1].text=str(scene.objects['Camera'].worldPosition.y)
rp_config_tree[0][2][2].text=str(scene.objects['Camera'].worldPosition.z)
# print ("Fin :", scene.objects['Camera'].worldPosition.x, scene.objects['Camera'].worldPosition.y, scene.objects['Camera'].worldPosition.z)
buffer_xml = ET.tostring(rp_config_tree)
with open("rp_config.xml", "wb") as f:
f.write(buffer_xml)
bge.logic.endGame()
# Fenêtre modale (inhibition des touches hors ESC)
if scene.objects['Terrain']['manip_mode']==9:
return
# Shift -> mode 1 : Pan (clic milieu)
if JUST_ACTIVATED in keyboard.inputs[bge.events.LEFTSHIFTKEY].queue:
obj['manip_mode']=1
if JUST_ACTIVATED in keyboard.inputs[bge.events.RIGHTSHIFTKEY].queue:
obj['manip_mode']=1
# Ctrl -> mode 2 : Zoom (clic milieu)
if JUST_ACTIVATED in keyboard.inputs[bge.events.LEFTCTRLKEY].queue:
obj['manip_mode']=2
if JUST_ACTIVATED in keyboard.inputs[bge.events.RIGHTCTRLKEY].queue:
obj['manip_mode']=2
# Pas de modificateur -> mode 0 : Pas de Orbit (mode 0) ici
if JUST_RELEASED in keyboard.inputs[bge.events.LEFTSHIFTKEY].queue:
obj['manip_mode']=0
if JUST_RELEASED in keyboard.inputs[bge.events.RIGHTSHIFTKEY].queue:
obj['manip_mode']=0
if JUST_RELEASED in keyboard.inputs[bge.events.LEFTCTRLKEY].queue:
obj['manip_mode']=0
if JUST_RELEASED in keyboard.inputs[bge.events.RIGHTCTRLKEY].queue:
obj['manip_mode']=0
# Touche Home -> Reset de la vue
if JUST_ACTIVATED in keyboard.inputs[bge.events.HOMEKEY].queue:
manip_reset()
# if scene.objects['Mouse_main']['mouse_graphic']:
# bge.render.setMousePosition(int(bge.render.getWindowWidth() / 2), int(bge.render.getWindowHeight() / 2))
# scene.objects['Mouse_main'].worldPosition = [0.07, -8.11, 4.71035] # Vielle version : [0.118161, -8.24305, 4.71035] ; [0, -3.5, 2], [0, -8, 6]
# scene.objects['Mouse_main'].worldScale=[30, 30, 30]
# scene.objects['Mouse_main']['past_x']=0
# scene.objects['Mouse_main']['past_y']=0
# Touche F5 -> Run et Pause
if JUST_ACTIVATED in keyboard.inputs[bge.events.F5KEY].queue:
terrain_run ()
# Touche F6 -> Stop / Init
if JUST_ACTIVATED in keyboard.inputs[bge.events.F6KEY].queue:
if scene.objects['Terrain']['thread_run']==True:
terrain_stop ()
# Touche +/- du pad -> Vitesse + ou /
if JUST_ACTIVATED in keyboard.inputs[bge.events.PADPLUSKEY].queue:
terrain_speed (scene.objects['Speed_up'])
if JUST_ACTIVATED in keyboard.inputs[bge.events.PADMINUS].queue:
terrain_speed (scene.objects['Speed_down'])
###############################################################################
# Manipulation 3D de la scène
###############################################################################
##
# Atteindre une orientation (bas niveau)
##
def applyRotationTo(obj, rx=None, ry=None, rz=None, Local=True):
rres=0.001 # resolution rotation
# x
if rx is not None:
while (abs(rx-obj.worldOrientation.to_euler().x) > rres) :
if obj.worldOrientation.to_euler().x-rx > rres:
obj.applyRotation((-rres, 0, 0), Local)
if rx-obj.worldOrientation.to_euler().x > rres:
obj.applyRotation((rres, 0, 0), Local)
# print ("delta x ",rx-obj.worldOrientation.to_euler().x)
# y
if ry is not None:
while (abs(ry-obj.worldOrientation.to_euler().y) > rres) :
if obj.worldOrientation.to_euler().y-ry > rres:
obj.applyRotation((0, -rres, 0), Local)
if ry-obj.worldOrientation.to_euler().y > rres:
obj.applyRotation((0, rres, 0), Local)
# print ("delta y ",ry-obj.worldOrientation.to_euler().y)
# z
if rz is not None:
while (abs(rz-obj.worldOrientation.to_euler().z) > rres) :
if obj.worldOrientation.to_euler().z-rz > rres:
obj.applyRotation((0, 0, -rres), Local)
if rz-obj.worldOrientation.to_euler().z > rres:
obj.applyRotation((0, 0, rres), Local)
# print ("delta z ",rz-obj.worldOrientation.to_euler().z)
##
# Reset de la manipulation de la vue
##
def manip_reset():
scene.objects['Camera'].worldPosition.x = scene.objects['Camera']['init_lx']
scene.objects['Camera'].worldPosition.y = scene.objects['Camera']['init_ly']
scene.objects['Camera'].worldPosition.z = scene.objects['Camera']['init_lz']
applyRotationTo(scene.objects['Terrain'], 0, 0, 0)
scene.objects['Cmd-text']['Text']= ""
##
# Position de départ pour la manipulation de la vue
##
def manip_start(cont):
if scene.objects['Terrain']['manip_mode']!=9:
obj = cont.owner
obj['click_x']=cont.sensors['ClickM'].position[0]
obj['click_y']=cont.sensors['ClickM'].position[1]
##
# Cacher le cercle de la manipulation Orbit
##
def manip_stop(cont):
# scene.objects['Orbit'].setVisible(False,False)
pass
##
# Manipulation du modèle ou de la caméra
##
def manip(cont):
obj = cont.owner
sensibilite_orbit=0.00005 # Base : 0.0005
sensibilite_pan=0.001 # Base : 0.005
sensibilite_zoom=0.005 # Base : 0.01
delta_x=cont.sensors['DownM'].position[0]-obj['click_x']
delta_y=cont.sensors['DownM'].position[1]-obj['click_y']
# Pan
if obj['manip_mode']==1: # Shift
scene.objects['Camera'].applyMovement((delta_x*-sensibilite_pan, delta_y*sensibilite_pan, 0), True)
# scene.objects['Commands-colbox'].applyMovement((delta_x*-sensibilite_pan, delta_y*sensibilite_pan, 0), True)
# scene.objects['Commands-colbox'].applyMovement((delta_x*-sensibilite_pan, delta_y*sensibilite_pan*math.cos(50*2*math.pi*(1/360)), delta_y*sensibilite_pan*math.sin(50*2*math.pi*(1/360))), True)
# if scene.objects['Mouse_main']['mouse_graphic']:
# scene.objects['Mouse_main'].applyMovement((delta_x*-sensibilite_pan, delta_y*sensibilite_pan, 0), True)
# Zoom FIXME : marche pas au niveau de la souris
if obj['manip_mode']==2: # Ctrl
# if scene.objects['Mouse_main']['mouse_graphic']:
# position_scale_x = 0.0005
# position_scale_y = position_scale_x
# mouse_x=scene.objects['Mouse_main'].sensors["Mouse"].position[0]-int(bge.render.getWindowWidth() / 2)
# mouse_y=scene.objects['Mouse_main'].sensors["Mouse"].position[1]-int(bge.render.getWindowHeight() / 2)
# distance_cam_past= math.sqrt(scene.objects['Camera']['past_ly']**2+scene.objects['Camera']['past_lz']**2)
# distance_cam = math.sqrt((scene.objects['Camera'].worldPosition.y**2+scene.objects['Camera'].worldPosition.z**2))
# size_scale = (distance_cam/distance_cam_past) * 0.23
print ("scene.objects['Camera']", scene.objects['Camera'].worldPosition.x, scene.objects['Camera'].worldPosition.y, scene.objects['Camera'].worldPosition.z)
if scene.objects['Camera'].worldPosition.z >0.5 and scene.objects['Camera'].worldPosition.z <57:
scene.objects['Camera'].applyMovement((0, 0, (delta_x+delta_y)*sensibilite_zoom), True)
if scene.objects['Camera'].worldPosition.z <=0.5 or scene.objects['Camera'].worldPosition.z >=57 :
scene.objects['Camera'].applyMovement((0, 0, -(delta_x+delta_y)*sensibilite_zoom), True)
# if scene.objects['Mouse_main']['mouse_graphic']:
# scene.objects['Mouse_main'].applyMovement((mouse_x*(delta_x+delta_y)*sensibilite_zoom*position_scale_x, -mouse_y*(delta_x+delta_y)*sensibilite_zoom*position_scale_y, 0), True)
# scene.objects['Mouse_main'].worldScale *= (delta_x+delta_y)*sensibilite_zoom*size_scale
# scene.objects['Camera']['past_ly']=scene.objects['Camera'].worldPosition.y
# scene.objects['Camera']['past_lz']=scene.objects['Camera'].worldPosition.z
##
# Manipulation du modèle ou de la caméra
##
def manip_wheel(cont):
if scene.objects['Terrain']['manip_mode']!=9: # Fenêtre modale
obj = cont.owner
# sensibilite_wheel = 5 # Base : 20
sensibilite_wheel = scene.objects['Camera'].worldPosition.z/5 # Sensibilité progressive
# if scene.objects['Mouse_main']['mouse_graphic']:
# position_scale_x = 0.0005
# position_scale_y = position_scale_x
# mouse_x=scene.objects['Mouse_main'].sensors["Mouse"].position[0]-int(bge.render.getWindowWidth() / 2)
# mouse_y=scene.objects['Mouse_main'].sensors["Mouse"].position[1]-int(bge.render.getWindowHeight() / 2)
# distance_cam_past = math.sqrt((scene.objects['Camera'].worldPosition.y**2+scene.objects['Camera'].worldPosition.z**2))
# # size_scale = (distance_cam/distance_cam_past) * 0.23
# size_scale = 0.2
# # size_scale = 0.23
print ("scene.objects['Camera']", scene.objects['Camera'].worldPosition.x, scene.objects['Camera'].worldPosition.y, scene.objects['Camera'].worldPosition.z)
if cont.sensors['WheelUp'].positive :
scene.objects['Camera'].applyMovement((0, 0, -sensibilite_wheel), True)
# if scene.objects['Mouse_main']['mouse_graphic']:
# distance_cam = math.sqrt((scene.objects['Camera'].worldPosition.y**2+scene.objects['Camera'].worldPosition.z**2))
# scene.objects['Mouse_main'].applyMovement((-mouse_x*sensibilite_wheel*position_scale_x, mouse_y*sensibilite_wheel*position_scale_y, 0), True)
# scene.objects['Mouse_main'].worldScale *= sensibilite_wheel* (distance_cam/distance_cam_past) *size_scale
elif cont.sensors['WheelDown'].positive and scene.objects['Camera'].worldPosition.z<50:
scene.objects['Camera'].applyMovement((0, 0, sensibilite_wheel), True)
# if scene.objects['Mouse_main']['mouse_graphic']:
# distance_cam = math.sqrt((scene.objects['Camera'].worldPosition.y**2+scene.objects['Camera'].worldPosition.z**2))
# scene.objects['Mouse_main'].applyMovement((mouse_x*sensibilite_wheel*position_scale_x, -mouse_y*sensibilite_wheel*position_scale_y, 0), True)
# scene.objects['Mouse_main'].worldScale /= sensibilite_wheel* (distance_cam_past/distance_cam) *size_scale
else:
return
##
# Icone de la souris
##
def mouse(cont):
if scene.objects['Mouse_main']['mouse_graphic']==False:
return
obj = cont.owner
# Ancienne version basée sur la position de la caméra
# distance_cam_init= math.sqrt(scene.objects['Camera']['init_ly']**2+scene.objects['Camera']['init_lz']**2)
# distance_cam = math.sqrt((scene.objects['Camera'].worldPosition.y**2+scene.objects['Camera'].worldPosition.z**2))
# ratio = ((distance_cam_init - distance_cam)/distance_cam_init)*1.39 # trop vite -> +, pas assez vite -> -
# scale_x=0.0118-0.0118*ratio
# scale_y=scale_x
# delta_x=cont.sensors["Mouse"].position[0]-obj['past_x']
# delta_y=cont.sensors["Mouse"].position[1]-obj['past_y']
# if delta_x<500 and delta_y<500:
# scene.objects['Mouse_main'].worldPosition.x += delta_x*scale_x
# scene.objects['Mouse_main'].worldPosition.y -= delta_y*scale_y*math.cos(50*2*math.pi*(1/360))
# scene.objects['Mouse_main'].worldPosition.z -= delta_y*scale_y*math.sin(50*2*math.pi*(1/360))
# scene.objects['Mouse_main']['past_x']=cont.sensors["Mouse"].position[0]
# scene.objects['Mouse_main']['past_y']=cont.sensors["Mouse"].position[1]
# Version basée sur obj.getDistanceTo(scene.objects['Camera'])
delta_x=cont.sensors["Mouse"].position[0]-obj['past_x']
delta_y=cont.sensors["Mouse"].position[1]-obj['past_y']
vect=mathutils.Vector((1,1,1))-obj.getVectTo(scene.objects['Camera'])[1]
dist= obj.getDistanceTo(scene.objects['Camera'])
# print ("delta_x, delta_y, vect, dist : ", delta_x, delta_y, vect, dist)
if obj['past_dist']==0:
obj['past_dist']=obj.getDistanceTo(scene.objects['Camera'])
ratio = dist/obj['past_dist']
# print ("delta_x, delta_y, vect, dist : ", delta_x, delta_y, vect, dist)
scale_x=ratio*0.016
scale_y=ratio*0.0162
# scale_xy=ratio*0.0005
scale_xy=ratio*0
if delta_x<500 and delta_y<500:
# scene.objects['Mouse_main'].applyMovement((delta_x*scale_x, -vect[1]*delta_y*scale_y, -vect[1]*delta_y*scale_y), False)
scene.objects['Mouse_main'].applyMovement((delta_x*scale_x+delta_x*delta_y*scale_xy,
-delta_y*scale_y*math.cos(50*2*math.pi*(1/360)),
-delta_y*scale_y*math.sin(50*2*math.pi*(1/360))), False)
obj['past_x']=cont.sensors["Mouse"].position[0]
obj['past_y']=cont.sensors["Mouse"].position[1]
##
# Mise en avant de la souris
##
def mouse_up():
scene.objects['Mouse_main']['mouse_up']+=1
if scene.objects['Mouse_main']['mouse_up'] == 1:
decal = 18
size_scale = 0.2
# print (scene.objects['Mouse_main'].getVectTo(scene.objects['Camera'])[1])
vect=scene.objects['Mouse_main'].getVectTo(scene.objects['Camera'])[1]
dist_past= scene.objects['Mouse_main'].getDistanceTo(scene.objects['Camera'])
scene.objects['Mouse_main'].applyMovement((vect[0]*decal, vect[1]*decal, vect[2]*decal), False)
dist= scene.objects['Mouse_main'].getDistanceTo(scene.objects['Camera'])
scene.objects['Mouse_main'].worldScale *= (dist/dist_past) *size_scale
scene.objects['Mouse_main'].worldScale=[8, 8, 8]
##
# Mise en arrière de la souris
##
def mouse_down():
scene.objects['Mouse_main']['mouse_up']-=1
if scene.objects['Mouse_main']['mouse_up'] == 0:
decal = 18
size_scale = 0.2
# print (scene.objects['Mouse_main'].getVectTo(scene.objects['Camera'])[1])
vect=scene.objects['Mouse_main'].getVectTo(scene.objects['Camera'])[1]
dist_past= scene.objects['Mouse_main'].getDistanceTo(scene.objects['Camera'])
scene.objects['Mouse_main'].applyMovement((-vect[0]*decal, -vect[1]*decal, -vect[2]*decal), False)
dist= scene.objects['Mouse_main'].getDistanceTo(scene.objects['Camera'])
scene.objects['Mouse_main'].worldScale /= (dist_past/dist) *size_scale
scene.objects['Mouse_main'].worldScale=[30, 30, 30]
###############################################################################
# Documentation
###############################################################################
##
# Allumer la tablette
##
def tablet_open ():
scene.objects['Terrain']['manip_mode']=8 # Fenêtre modale Aide
manip_reset()
# Overlay
scene.removeOverlayCollection(bpy.data.collections['Hud'])
scene.objects['Points'].setVisible(False,True)
scene.objects['Commands'].setVisible(False,True)
scene.active_camera = scene.objects["Camera-Hud"]
scene.objects['Camera'].setVisible(False,True)
# Ouvrir la tablette
rp_doc.open()
##
# Enteindre la tablette
##
def tablet_close ():
# Fermeture de la tablette
rp_doc.close()
scene.objects['Terrain']['manip_mode']=0 # Enlever la fenêtre modale
# Overlay
scene.objects['Points'].setVisible(True,True)
scene.objects['Commands'].setVisible(True,True)
scene.objects['Camera'].setVisible(True,True)
scene.active_camera = scene.objects["Camera"]
scene.addOverlayCollection(scene.cameras['Camera-Hud'], bpy.data.collections['Hud'])
# UI : Commands
scene.objects['Run-Hl'].setVisible(False,False)
scene.objects['Pause'].setVisible(False,False)
scene.objects['Pause'].suspendPhysics()
scene.objects['Pause-Hl'].setVisible(False,False)
scene.objects['Stop'].setVisible(False,False)
scene.objects['Stop'].suspendPhysics()
scene.objects['Stop-Hl'].setVisible(False,False)
scene.objects['Objective-Hl'].setVisible(False,False)
scene.objects['Doc-cmd-Hl'].setVisible(False,False)
scene.objects['ResetView-Hl'].setVisible(False,False)
scene.objects['About-cmd-Hl'].setVisible(False,False)
# UI : Sounds
# Read config (sound : data/config/sound -> [0][1].text)
if rp_config_tree[0][1].text == "True":
sound_set ()
else:
sound_unset ()
# audiodev.unlock()
scene.objects['Cmd-text'].setVisible(False,False)
##
# Clic pour fermer la tablette
##
def tablet_close_click(cont):
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive :
sound_play (snd_close)
tablet_close()
###############################################################################
# About
###############################################################################
color_link = (0, 1, 0.857,1) # Turquoise
color_link_hl = (0.799, 0.617, 0.021, 1) # Jaune
##
# Ouvrir le about
##
def about_open():
scene.objects['Terrain']['manip_mode']=9 # Fenêtre modale About
manip_reset()
scene.objects['About_link-git'].color= color_link
scene.objects['About_link-gpl'].color= color_link
scene.objects['About_link-upbge'].color= color_link
# scene.objects['About_link-kay'].color= color_link
scene.objects['About_link-kenney'].color= color_link
scene.objects['About_link-polygonrunway'].color= color_link
scene.objects['About_link-icons'].color= color_link
scene.objects['About_close'].color= color_link
scene.objects['About'].setVisible(True,True)
scene.objects['About'].worldPosition = [0, 1.53623, -1.8] # old [0, 1.53623, -0.892838]
scene.objects['About']['timer'] = 0
scene.objects['About']['anim'] = True
##
# Animation du about
##
def about_open_anim():
resol=0.5
scene.objects['About'].localPosition.y -= 1.09*resol
scene.objects['About'].localPosition.z += 0.85*resol
scene.objects['About']['timer']+=1
if scene.objects['About']['timer']== 40:
scene.objects['About']['anim'] = False
##
# Highlight du about
##
def about_hl(cont):
if cont.sensors['MO'].status == JUST_ACTIVATED:
# if scene.objects['Mouse_main']['mouse_graphic']:
# mouse_up()
obj = cont.owner
name=obj.name[:-7]
scene.objects[name].color = color_link_hl
if cont.sensors['MO'].status == JUST_RELEASED:
# if scene.objects['Mouse_main']['mouse_graphic']:
# mouse_down()
obj = cont.owner
name=obj.name[:-7]
scene.objects[name].color = color_link
##
# Fermer le about
##
def about_close():
scene.objects['Terrain']['manip_mode']=0
scene.objects['About'].setVisible(False,True)
scene.objects['About'].worldPosition = [42, -2, 3]
scene.objects['About']['timer']= 0
##
# Click pour fermer le about
##
def about_close_click(cont):
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive :
sound_play (snd_close)
about_close()
##
# Liens du about
##
def about_link(cont):
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive :
obj = cont.owner
name=obj.name[:-7]
link={
'About_link-git' : 'https://gitlab.com/blender-edutech/ropy',
'About_link-gpl' : 'https://www.gnu.org/licenses/gpl-3.0.html',
'About_link-upbge' : 'https://www.upbge.org',
'About_link-kay' : 'https://www.kaylousberg.com',
'About_link-kenney' : 'https://www.kenney.nl',
'About_link-polygonrunway' : 'https://www.patreon.com/polygonrunway/',
'About_link-icons' : 'https://game-icons.net/'}
sound_play (snd_click)
webbrowser.open(link [name])
# FIXME: souris graphique trop compliqué
def about_link_hl(cont):
decal = 15
if cont.sensors['MO'].status == JUST_ACTIVATED :
obj = cont.owner
name=obj.name[:-7]
scene.objects[name].color = color_link_hl
# if scene.objects['Mouse_main']['mouse_graphic']:
# mouse_up()
if cont.sensors['MO'].status == JUST_RELEASED :
obj = cont.owner
name=obj.name[:-7]
scene.objects[name].color = color_link
# if scene.objects['Mouse_main']['mouse_graphic']:
# mouse_down()