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 ct_doc # documentation # import ct_cmd # user script (commands) ############################################################################### # rp.py # @title: Ropy # @project: Ropy (Blender-EduTech) # @lang: fr # @authors: Philippe Roy # @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/click.ogg') sndbuff_click = aud.Sound.cache(snd_click) # 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 ############################################################################### ## # Mouse over du terrain ## def scn_terrain_mo (cont): pass ## # Initialisation lors du chargement du terrain ## def terrain_init (): # Cacher les fenêtres # 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']) # Configuration du moteur de rendu eevee.use_eevee_smaa = True # Init de la carte rp_map.map_init() rp_map.map_reset() ## # Mise en route et pause du cycle ## def terrain_run (): sound_play (sndbuff_click) # 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']= "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 (): sound_play (sndbuff_click) # 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): sound_play (sndbuff_click) 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['Terrain']['speed']) # Affichage if obj.name=="Speed_up" and i<5: scene.objects['Terrain']['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['Terrain']['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) rp_config_tree[0][0].text=str(scene.objects['Terrain']['speed']) buffer_xml = ET.tostring(rp_config_tree) with open("rp_config.xml", "wb") as f: f.write(buffer_xml) ############################################################################### # Sons ############################################################################### def sound_play (sound): if scene.objects['Commands']['sound']: 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'].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'].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['ResetView-Hl'].setVisible(False,False) scene.objects['About-cmd-Hl'].setVisible(False,False) scene.objects['About'].setVisible(False,True) # 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['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['Terrain']['speed']=float(rp_config_tree[0][0].text) i=speed_mode.index(scene.objects['Terrain']['speed']) scene.objects['Text_speed']['Text']=speed_mode_txt[i] ## # 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":"Run (F5)", "Stop":"Stop (F6)", "Pause":"Pause (F5)", "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": terrain_run () if obj.name=="Stop": terrain_stop () if obj.name=="Speed_up" or obj.name=="Speed_down": terrain_speed (obj) if obj.name=="ResetView": manip_reset() if obj.name=="Sound-cmd": sound_unset () if obj.name=="NoSound-cmd": sound_set () if obj.name=="About-cmd": about_open () ############################################################################### # Gestion du clavier ############################################################################### ## # Mode # # 0 : rien (par défaut) # 1 : Pan avec Shift # 2 : Zoom avec Ctrl # 9 : Fenêtre modal ## 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 jeux terrain_stop () 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 ############################################################################### ## # Mémorisation de la position et orientation initiales du modèle 3D et de la caméra ## def manip_init(cont): 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 # scene.objects['Points-Map-text']['init_relativ_lx']=scene.objects['Points-Map-text'].worldPosition.x-scene.objects['Points'].worldPosition.x # scene.objects['Points-Map-text']['init_relativ_ly']=scene.objects['Points-Map-text'].worldPosition.y-scene.objects['Points'].worldPosition.y # scene.objects['Points-Map-text']['init_relativ_lz']=scene.objects['Points-Map-text'].worldPosition.z-scene.objects['Points'].worldPosition.z ## # 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 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 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 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: 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] ############################################################################### # About ############################################################################### color_link = (0.799, 0.617, 0.021, 1) # Jaune color_link_hl = (0.8, 0.8, 0.8, 1) # Blanc ## # Ouvrir le about ## def about_open(): scene.objects['Terrain']['manip_mode']=9 # Fenêtre modale manip_reset() scene.objects['About_title'].color = color_black scene.objects['About_text'].color = color_black scene.objects['About_copyright'].color = color_black 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_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(): pas=0.5 scene.objects['About'].localPosition.y -= pas scene.objects['About'].localPosition.z += 0.85*pas # scene.objects['About'].localPosition.y=scene.objects['About'].localPosition.y-1*pas # scene.objects['About'].localPosition.z=scene.objects['About'].localPosition.z+0.85*pas 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() scene.objects['Aboutbanner'].color = color_white scene.objects['About_close'].color = color_white scene.objects['About_title'].color = color_white if cont.sensors['MO'].status == JUST_RELEASED: if scene.objects['Mouse_main']['mouse_graphic']: mouse_down() scene.objects['Aboutbanner'].color = color_endbanner_bluelight scene.objects['About_close'].color = color_link scene.objects['About_title'].color = color_black ## # Fermer le about ## def about_close(): sound_play (sndbuff_click) scene.objects['Terrain']['manip_mode']=0 scene.objects['About'].setVisible(False,True) scene.objects['About'].worldPosition = [22, 1.53623, -0.892838] scene.objects['Aboutbanner'].color = [0.592, 0.68, 0.407, 1] 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 : 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/phroy/ropy', 'About_link-gpl' : 'https://www.gnu.org/licenses/gpl-3.0.html', 'About_link-blender': 'https://www.blender.org', 'About_link-upbge' : 'https://www.upbge.org', 'About_link-kay' : 'https://www.kaylousberg.com', 'About_link-kenney' : 'https://www.kenney.nl'} 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()