mirror of
https://forge.apps.education.fr/blender-edutech/ropy.git
synced 2024-01-27 08:23:20 +01:00
684 lines
28 KiB
Python
684 lines
28 KiB
Python
import bge # Blender Game Engine (UPBGE)
|
||
import bpy # Blender
|
||
import aud # Sounds
|
||
import threading # Multithreading
|
||
import trace
|
||
import sys
|
||
import time
|
||
import math
|
||
import mathutils
|
||
import random
|
||
|
||
import rp_map1 as rp_map # Map definition
|
||
|
||
###############################################################################
|
||
# rp_lib.py
|
||
# @title: Bibliothèque du Rover Ropy (rp_*)
|
||
# @project: Ropy (Blender-EduTech)
|
||
# @lang: fr
|
||
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
|
||
# @copyright: Copyright (C) 2020-2022 Philippe Roy
|
||
# @license: GNU GPL
|
||
#
|
||
# Bibliothèque des actions du robot
|
||
#
|
||
# 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.
|
||
#
|
||
###############################################################################
|
||
|
||
scene = bge.logic.getCurrentScene()
|
||
debug_mvt = scene.objects['Terrain']['debug_mvt']
|
||
|
||
# Sounds
|
||
audiodev = aud.Device()
|
||
snd_click = aud.Sound('asset/sounds/rp_click.ogg')
|
||
|
||
# Threads
|
||
threads_cmd=[]
|
||
threads_gostore=[]
|
||
debug_thread = scene.objects['Terrain']['debug_thread']
|
||
|
||
# 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
|
||
|
||
###############################################################################
|
||
# Méthode kill pour les tâches (threads)
|
||
###############################################################################
|
||
|
||
class thread_with_trace(threading.Thread):
|
||
def __init__(self, *args, **keywords):
|
||
threading.Thread.__init__(self, *args, **keywords)
|
||
self.killed = False
|
||
|
||
def start(self):
|
||
self.__run_backup = self.run
|
||
self.run = self.__run
|
||
threading.Thread.start(self)
|
||
|
||
def __run(self):
|
||
sys.settrace(self.globaltrace)
|
||
self.__run_backup()
|
||
self.run = self.__run_backup
|
||
|
||
def globaltrace(self, frame, event, arg):
|
||
if event == 'call':
|
||
return self.localtrace
|
||
else:
|
||
return None
|
||
|
||
def localtrace(self, frame, event, arg):
|
||
if self.killed:
|
||
if event == 'line':
|
||
raise SystemExit()
|
||
return self.localtrace
|
||
|
||
def kill(self):
|
||
self.killed = True
|
||
|
||
###############################################################################
|
||
# Start et stop des tâches (threads)
|
||
###############################################################################
|
||
|
||
def thread_start(threads, type_txt, fct):
|
||
threads.append(thread_with_trace(target = fct))
|
||
threads[len(threads)-1].start()
|
||
if (debug_thread):
|
||
print ("Thread",type_txt, "#", len(threads)-1, "open.")
|
||
|
||
def thread_stop(threads, type_txt):
|
||
i=0
|
||
zombie_flag=False
|
||
for t in threads:
|
||
if not t.is_alive():
|
||
if (debug_thread):
|
||
print ("Thread",type_txt, "#",i,"closed.")
|
||
else:
|
||
if (debug_thread):
|
||
print ("Thread",type_txt, "#",i,"still open ...")
|
||
t.kill()
|
||
t.join()
|
||
if not t.is_alive():
|
||
if (debug_thread):
|
||
print ("Thread",type_txt, "#",i,"killed.")
|
||
else:
|
||
if (debug_thread):
|
||
print ("Thread",type_txt, "#",i,"zombie...")
|
||
zombie_flag=True
|
||
i +=1
|
||
if zombie_flag==False:
|
||
if (debug_thread):
|
||
print ("All threads",type_txt, "are closed.")
|
||
scene.objects['Terrain']['thread_cmd']=False
|
||
return True
|
||
else:
|
||
if (debug_thread):
|
||
print ("There are zombies threads",type_txt, ".")
|
||
return False
|
||
|
||
def thread_cmd_start(fct):
|
||
thread_start(threads_cmd, "commands", fct)
|
||
|
||
def thread_cmd_stop():
|
||
thread_stop(threads_cmd, "commands")
|
||
|
||
def thread_gostore_start(fct):
|
||
thread_start(threads_gostore, "go store", fct)
|
||
|
||
def thread_gostore_stop():
|
||
thread_stop(threads_gostore, "go store")
|
||
|
||
def rp_end():
|
||
if (debug_thread):
|
||
print ("Thread commands is arrived.")
|
||
scene.objects['Terrain']['thread_cmd']=False
|
||
|
||
def rp_fin():
|
||
rp_end()
|
||
|
||
def rp_quit():
|
||
rp_end()
|
||
|
||
###############################################################################
|
||
# Sounds
|
||
###############################################################################
|
||
|
||
# FIXME : Sound crash in Windows (very strange : blender, UPBGE, python ?), no music for Bill
|
||
def sound_play (sound):
|
||
if scene.objects['Commands']['sound'] and sys.platform!="win32":
|
||
audiodev.play(sound)
|
||
|
||
###############################################################################
|
||
# Rover fonction élèves
|
||
###############################################################################
|
||
|
||
##
|
||
# Avancer le rover
|
||
##
|
||
|
||
def rp_avancer ():
|
||
obj=scene.objects['Rover']
|
||
|
||
# Pas de mouvement si colision ou objectif
|
||
if obj['stop']:
|
||
return False
|
||
|
||
# Contrôle colision
|
||
x0 = obj.worldPosition.x
|
||
y0 = obj.worldPosition.y
|
||
z0 = obj.worldPosition.z
|
||
if round(obj.worldOrientation.to_euler().z, 2) == 0.00: # Sud
|
||
x1 = x0
|
||
y1 = y0-1
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(math.pi,2) or round(obj.worldOrientation.to_euler().z, 2) == - round(math.pi,2) : # Nord
|
||
x1 = x0
|
||
y1 = y0+1
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(math.pi/2,2) or round(obj.worldOrientation.to_euler().z, 2) == -round(3*(math.pi/2),2) : # Est
|
||
x1 = x0+1
|
||
y1 = y0
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(-math.pi/2,2) or round(obj.worldOrientation.to_euler().z, 2) == round(3*(math.pi/2),2) : # Ouest
|
||
x1 = x0-1
|
||
y1 = y0
|
||
if [x1,y1] in scene.objects['Terrain']['map_tile_montain']:
|
||
print ("Crash dans la montagne !")
|
||
rover_colision_montain ()
|
||
obj['stop'] = True
|
||
if [x1,y1] in scene.objects['Terrain']['map_tile_station']:
|
||
print ("Crash dans la station !")
|
||
rover_colision_station ()
|
||
obj['stop'] = True
|
||
if x1 < scene.objects['Terrain']['size'][0] or x1 > scene.objects['Terrain']['size'][2] or y1 < scene.objects['Terrain']['size'][1] or y1 > scene.objects['Terrain']['size'][3] :
|
||
print ("Sortie de carte !")
|
||
obj['stop'] = True
|
||
if obj['stop']:
|
||
return False
|
||
|
||
# Points et console
|
||
if (debug_mvt):
|
||
print ("rp_avancer()")
|
||
scene.objects['Points']['step'] +=1
|
||
|
||
# Animation rapide
|
||
if scene.objects['Commands']['speed'] == 10 and scene.objects['Points']['step']> 2: # A tendance à planter sur les premiers mouvements en rapide + balisage
|
||
x0 = obj.worldPosition.x
|
||
y0 = obj.worldPosition.y
|
||
z0 = obj.worldPosition.z
|
||
if round(obj.worldOrientation.to_euler().z, 2) == 0.00: # Sud
|
||
obj.worldPosition=[x0, y0-1, z0]
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(math.pi,2) or round(obj.worldOrientation.to_euler().z, 2) == - round(math.pi,2) : # Nord
|
||
obj.worldPosition=[x0, y0+1, z0]
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(math.pi/2,2) or round(obj.worldOrientation.to_euler().z, 2) == -round(3*(math.pi/2),2) : # Est
|
||
obj.worldPosition=[x0+1, y0, z0]
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(-math.pi/2,2) or round(obj.worldOrientation.to_euler().z, 2) == round(3*(math.pi/2),2) : # Ouest
|
||
obj.worldPosition=[x0-1, y0, z0]
|
||
rp_tempo (0.1)
|
||
|
||
# FIXME : Animation sacadée
|
||
# step =1/100
|
||
# print (obj.worldOrientation.to_euler().z)
|
||
# x0 = obj.worldPosition.x
|
||
# y0 = obj.worldPosition.y
|
||
# z0 = obj.worldPosition.z
|
||
# for i in range (100) :
|
||
# if round(obj.worldOrientation.to_euler().z, 2) == 0.00: # Sud
|
||
# obj.worldPosition=[x0, y0-step*i, z0]
|
||
# if round(obj.worldOrientation.to_euler().z, 2) == round(math.pi,2) or round(obj.worldOrientation.to_euler().z, 2) == - round(math.pi,2) : # Nord
|
||
# obj.worldPosition=[x0, y0+step*i, z0]
|
||
# if round(obj.worldOrientation.to_euler().z, 2) == round(math.pi/2,2) or round(obj.worldOrientation.to_euler().z, 2) == -round(3*(math.pi/2),2) : # Est
|
||
# obj.worldPosition=[x0+step*i, y0, z0]
|
||
# if round(obj.worldOrientation.to_euler().z, 2) == round(-math.pi/2,2) or round(obj.worldOrientation.to_euler().z, 2) == round(3*(math.pi/2),2) : # Ouest
|
||
# obj.worldPosition=[x0-step*i, y0, z0]
|
||
# rp_tempo (0.1*step)
|
||
|
||
# Animation
|
||
if scene.objects['Commands']['speed'] != 10:
|
||
start = 1
|
||
end = 100
|
||
layer = 0
|
||
priority = 1
|
||
blendin = 1.0
|
||
mode = bge.logic.KX_ACTION_MODE_PLAY
|
||
layerWeight = 0.0
|
||
ipoFlags = 0
|
||
speed = scene.objects['Commands']['speed']*8
|
||
if round(obj.worldOrientation.to_euler().z, 2) == 0.00: # Sud
|
||
obj.playAction('Rover-Avancer-Y-', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
# obj.worldPosition=[x0, y0-step*i, z0]
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(math.pi,2) or round(obj.worldOrientation.to_euler().z, 2) == - round(math.pi,2) : # Nord
|
||
obj.playAction('Rover-Avancer-Y+', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
# obj.worldPosition=[x0, y0+step*i, z0]
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(math.pi/2,2) or round(obj.worldOrientation.to_euler().z, 2) == -round(3*(math.pi/2),2) : # Est
|
||
obj.playAction('Rover-Avancer-X+', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
# obj.worldPosition=[x0+step*i, y0, z0]
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(-math.pi/2,2) or round(obj.worldOrientation.to_euler().z, 2) == round(3*(math.pi/2),2) : # Ouest
|
||
obj.playAction('Rover-Avancer-X-', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
# obj.worldPosition=[x0-step*i, y0, z0]
|
||
scene.objects['Wheel-right-front'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-right-mid'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-right-rear'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-front'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-mid'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-rear'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
while scene.objects['Wheel-right-front'].isPlayingAction(): # Forçage du redraw
|
||
# scene.objects['Camera'].applyMovement((0, 0, 0), True)
|
||
scene.objects['Sun'].applyMovement((0, 0, 0), True)
|
||
rp_tempo (0.1)
|
||
|
||
# Contrôle objectif
|
||
if rp_map.objectif_control(x1,y1):
|
||
rover_goal ()
|
||
return True
|
||
|
||
##
|
||
# Tourner à gauche
|
||
##
|
||
|
||
def rp_gauche ():
|
||
obj=scene.objects['Rover']
|
||
|
||
# Pas de mouvement si colision ou objectif
|
||
if obj['stop']:
|
||
return False
|
||
|
||
# Points et console
|
||
if (debug_mvt):
|
||
print ("rp_gauche()")
|
||
scene.objects['Points']['step'] +=1
|
||
step=math.pi/2 # Pas angulaire
|
||
|
||
# Animation rapide
|
||
if scene.objects['Commands']['speed'] == 10:
|
||
obj.applyRotation((0, 0, step), True)
|
||
rp_tempo (0.1)
|
||
return True
|
||
|
||
# Animation
|
||
start = 1
|
||
end = 100
|
||
layer = 0
|
||
priority = 1
|
||
blendin = 1.0
|
||
mode = bge.logic.KX_ACTION_MODE_PLAY
|
||
layerWeight = 0.0
|
||
ipoFlags = 0
|
||
speed = scene.objects['Commands']['speed']*8
|
||
obj.playAction('Rover-Gauche', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-right-front'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-right-mid'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-right-rear'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-front'].playAction('Wheel-Reculer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-mid'].playAction('Wheel-Reculer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-rear'].playAction('Wheel-Reculer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
while scene.objects['Wheel-right-front'].isPlayingAction(): # Forçage du redraw
|
||
# scene.objects['Camera'].applyMovement((0, 0, 0), True)
|
||
scene.objects['Sun'].applyMovement((0, 0, 0), True)
|
||
rp_tempo (0.1)
|
||
return True
|
||
|
||
##
|
||
# Tourner à droite
|
||
##
|
||
|
||
def rp_droite ():
|
||
obj=scene.objects['Rover']
|
||
|
||
# Pas de mouvement si colision ou objectif
|
||
if obj['stop']:
|
||
return False
|
||
|
||
# Points et console
|
||
if (debug_mvt):
|
||
print ("rp_droite()")
|
||
scene.objects['Points']['step'] +=1
|
||
step=math.pi/2 # Pas angulaire
|
||
|
||
# Rapide
|
||
if scene.objects['Commands']['speed'] == 10:
|
||
obj.applyRotation((0, 0, -step), True)
|
||
rp_tempo (0.1)
|
||
return True
|
||
|
||
# Animation
|
||
start = 1
|
||
end = 100
|
||
layer = 0
|
||
priority = 1
|
||
blendin = 1.0
|
||
mode = bge.logic.KX_ACTION_MODE_PLAY
|
||
layerWeight = 0.0
|
||
ipoFlags = 0
|
||
speed = scene.objects['Commands']['speed']*8
|
||
obj.playAction('Rover-Droite', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-right-front'].playAction('Wheel-Reculer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-right-mid'].playAction('Wheel-Reculer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-right-rear'].playAction('Wheel-Reculer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-front'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-mid'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-rear'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
while scene.objects['Wheel-right-front'].isPlayingAction(): # Forçage du redraw
|
||
# scene.objects['Camera'].applyMovement((0, 0, 0), True)
|
||
scene.objects['Sun'].applyMovement((0, 0, 0), True)
|
||
rp_tempo (0.1)
|
||
return True
|
||
|
||
##
|
||
# Marquer
|
||
##
|
||
|
||
def rp_marquer ():
|
||
obj=scene.objects['Rover']
|
||
|
||
# Pas de mouvement si colision ou objectif
|
||
if obj['stop']:
|
||
return False
|
||
|
||
# Points et console
|
||
if (debug_mvt):
|
||
print ("rp_marquer() -> balise #"+ str(len(scene.objects['Terrain']['map_tile_beacon'])))
|
||
rp_tempo (0.1)
|
||
x = obj.worldPosition.x
|
||
y = obj.worldPosition.y
|
||
z = obj.worldPosition.z
|
||
|
||
# Vérification de l'absence de balise sur la tuile
|
||
if [x,y] in scene.objects['Terrain']['map_tile_beacon'] :
|
||
print ("Case déjà marquée !")
|
||
return False
|
||
|
||
# Posage
|
||
if scene.objects['Points']['upgrade_beacon']:
|
||
beacon_max= 200
|
||
else:
|
||
beacon_max= 20
|
||
for i in range (beacon_max):
|
||
beacon = scene.objects["Beacon-"+str(i)]
|
||
if beacon['activated']==False:
|
||
beacon.worldPosition=[x,y,0.2]
|
||
beacon['activated']=True
|
||
beacon.setVisible(True, True)
|
||
scene.objects['Terrain']['map_tile_beacon'].append([x,y])
|
||
break
|
||
if i ==beacon_max-1 :
|
||
print ("Plus de balise disponible !")
|
||
rp_tempo (0.1)
|
||
return True
|
||
|
||
##
|
||
# Détecter
|
||
##
|
||
|
||
def rp_detect ():
|
||
obj=scene.objects['Rover']
|
||
|
||
# Pas de mouvement si colision ou objectif
|
||
if obj['stop']:
|
||
return True
|
||
|
||
# Points et console
|
||
if (debug_mvt):
|
||
print ("rp_detect")
|
||
|
||
# Détection
|
||
x0 = obj.worldPosition.x
|
||
y0 = obj.worldPosition.y
|
||
z0 = obj.worldPosition.z
|
||
if round(obj.worldOrientation.to_euler().z, 2) == 0.00: # Sud
|
||
x1 = x0
|
||
y1 = y0-1
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(math.pi,2) or round(obj.worldOrientation.to_euler().z, 2) == - round(math.pi,2) : # Nord
|
||
x1 = x0
|
||
y1 = y0+1
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(math.pi/2,2) or round(obj.worldOrientation.to_euler().z, 2) == -round(3*(math.pi/2),2) : # Est
|
||
x1 = x0+1
|
||
y1 = y0
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(-math.pi/2,2) or round(obj.worldOrientation.to_euler().z, 2) == round(3*(math.pi/2),2) : # Ouest
|
||
x1 = x0-1
|
||
y1 = y0
|
||
if [x1,y1] in scene.objects['Terrain']['map_tile_montain']:
|
||
if (debug_mvt):
|
||
print ("Présence de montage devant !")
|
||
if scene.objects['Points']['mission']==3: # Contrôle objectif mission 3
|
||
rover_goal ()
|
||
return True
|
||
|
||
if [x1,y1] in scene.objects['Terrain']['map_tile_station']:
|
||
if (debug_mvt):
|
||
print ("Présence de la station devant !")
|
||
if scene.objects['Points']['mission']==3: # Contrôle objectif mission 3
|
||
rover_goal ()
|
||
return True
|
||
|
||
if [x1,y1] in scene.objects['Terrain']['map_tile_station']:
|
||
if (debug_mvt):
|
||
print ("Sortie de carte devant !")
|
||
if scene.objects['Points']['mission']==3: # Contrôle objectif mission 3
|
||
rover_goal ()
|
||
return True
|
||
return False
|
||
|
||
##
|
||
# Prendre
|
||
##
|
||
|
||
def rp_prendre ():
|
||
obj=scene.objects['Rover']
|
||
|
||
# Pas de mouvement si colision ou objectif
|
||
if obj['stop']:
|
||
return False
|
||
|
||
# Points et console
|
||
if (debug_mvt):
|
||
print ("rp_prendre")
|
||
# FIXME
|
||
|
||
##
|
||
# Radar
|
||
##
|
||
|
||
def rp_radar ():
|
||
obj=scene.objects['Rover']
|
||
|
||
# Pas de mouvement si colision ou objectif
|
||
if obj['stop']:
|
||
return False
|
||
|
||
# Points et console
|
||
if (debug_mvt):
|
||
print ("rp_radar")
|
||
# FIXME
|
||
|
||
|
||
###############################################################################
|
||
# Colision
|
||
###############################################################################
|
||
|
||
##
|
||
# Montagne
|
||
##
|
||
|
||
def rover_colision_montain ():
|
||
obj=scene.objects['Rover']
|
||
|
||
# Animation
|
||
start = 1
|
||
end = 120
|
||
layer = 0
|
||
priority = 1
|
||
blendin = 1.0
|
||
mode = bge.logic.KX_ACTION_MODE_PLAY
|
||
layerWeight = 0.0
|
||
ipoFlags = 0
|
||
speed = scene.objects['Commands']['speed']*2
|
||
if round(obj.worldOrientation.to_euler().z, 2) == 0.00: # Sud
|
||
obj.playAction('Rover-Crash-Y-', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(math.pi,2) or round(obj.worldOrientation.to_euler().z, 2) == - round(math.pi,2) : # Nord
|
||
obj.playAction('Rover-Crash-Y+', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(math.pi/2,2) or round(obj.worldOrientation.to_euler().z, 2) == -round(3*(math.pi/2),2) : # Est
|
||
obj.playAction('Rover-Crash-X+', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(-math.pi/2,2) or round(obj.worldOrientation.to_euler().z, 2) == round(3*(math.pi/2),2) : # Ouest
|
||
obj.playAction('Rover-Crash-X-', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
|
||
scene.objects['Wheel-right-front'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-right-mid'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-right-rear'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-front'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-mid'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-rear'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
while scene.objects['Wheel-right-front'].isPlayingAction(): # Forçage du redraw
|
||
scene.objects['Sun'].applyMovement((0, 0, 0), True)
|
||
rp_tempo (0.1)
|
||
return True
|
||
|
||
##
|
||
# Station
|
||
##
|
||
|
||
def rover_colision_station ():
|
||
obj=scene.objects['Rover']
|
||
|
||
# Animation
|
||
start = 1
|
||
end = 120
|
||
layer = 0
|
||
priority = 1
|
||
blendin = 1.0
|
||
mode = bge.logic.KX_ACTION_MODE_PLAY
|
||
layerWeight = 0.0
|
||
ipoFlags = 0
|
||
speed = scene.objects['Commands']['speed']*2
|
||
if round(obj.worldOrientation.to_euler().z, 2) == 0.00: # Sud
|
||
obj.playAction('Rover-CrashStation-Y-', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(math.pi,2) or round(obj.worldOrientation.to_euler().z, 2) == - round(math.pi,2) : # Nord
|
||
obj.playAction('Rover-CrashStation-Y+', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(math.pi/2,2) or round(obj.worldOrientation.to_euler().z, 2) == -round(3*(math.pi/2),2) : # Est
|
||
obj.playAction('Rover-CrashStation-X+', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(-math.pi/2,2) or round(obj.worldOrientation.to_euler().z, 2) == round(3*(math.pi/2),2) : # Ouest
|
||
obj.playAction('Rover-CrashStation-X-', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
|
||
scene.objects['Wheel-right-front'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-right-mid'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-right-rear'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-front'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-mid'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-rear'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
while scene.objects['Wheel-right-front'].isPlayingAction(): # Forçage du redraw
|
||
scene.objects['Sun'].applyMovement((0, 0, 0), True)
|
||
rp_tempo (0.1)
|
||
return True
|
||
|
||
###############################################################################
|
||
# Goal
|
||
###############################################################################
|
||
|
||
def rover_goal ():
|
||
obj=scene.objects['Rover']
|
||
if (debug_mvt):
|
||
print ("Goal !!")
|
||
obj['stop'] = True
|
||
|
||
# Animation
|
||
start = 1
|
||
end = 160
|
||
layer = 0
|
||
priority = 1
|
||
blendin = 1.0
|
||
mode = bge.logic.KX_ACTION_MODE_PLAY
|
||
layerWeight = 0.0
|
||
ipoFlags = 0
|
||
speed = scene.objects['Commands']['speed']*4
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(math.pi,2) or round(obj.worldOrientation.to_euler().z, 2) == - round(math.pi,2) : # Nord
|
||
rp_gauche()
|
||
rp_gauche()
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(math.pi/2,2) or round(obj.worldOrientation.to_euler().z, 2) == -round(3*(math.pi/2),2) : # Est
|
||
rp_droite()
|
||
if round(obj.worldOrientation.to_euler().z, 2) == round(-math.pi/2,2) or round(obj.worldOrientation.to_euler().z, 2) == round(3*(math.pi/2),2) : # Ouest
|
||
rp_gauche()
|
||
|
||
speed = scene.objects['Commands']['speed']
|
||
scene.objects['Rover'].playAction('Rover-Aim1', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Mast'].playAction('Mast-Aim1', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Mast-cap'].playAction('Mast-cap-Aim1', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
|
||
scene.objects['Wheel-right-front'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-right-mid'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-right-rear'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-front'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-mid'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
scene.objects['Wheel-left-rear'].playAction('Wheel-Avancer', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
|
||
while scene.objects['Wheel-right-front'].isPlayingAction(): # Forçage du redraw
|
||
scene.objects['Sun'].applyMovement((0, 0, 0), True)
|
||
rp_tempo (0.1)
|
||
|
||
# Revenir à une position propre
|
||
x0 = obj.worldPosition.x
|
||
y0 = obj.worldPosition.y
|
||
obj.worldPosition.x = round(x0)
|
||
obj.worldPosition.y = round(y0)
|
||
# print ("Position actuelle :", x0, y0, obj.worldPosition.x, obj.worldPosition.y)
|
||
|
||
# Level
|
||
# print ("scene.objects['Points']['mission'] :", scene.objects['Points']['mission'])
|
||
# print ("scene.objects['Points']['level'] :", scene.objects['Points']['level'])
|
||
if scene.objects['Points']['mission']==scene.objects['Points']['level']:
|
||
scene.objects['Points']['level']+=1
|
||
|
||
###############################################################################
|
||
# Temporisation
|
||
###############################################################################
|
||
|
||
# Temporisation basée sur l'horloge de l'OS
|
||
def rp_sleep (duration):
|
||
time.sleep(duration)
|
||
|
||
# Temporisation basée par l'horloge de UPBGE
|
||
def rp_tempo (duration):
|
||
# time.sleep(duration*(1/scene.objects['Commands']['speed']))
|
||
scene.objects['Commands']['time']=0
|
||
while scene.objects['Commands']['time']<duration*(1/scene.objects['Commands']['speed']):
|
||
# print("Temporization commands :",scene.objects['Terrain']['delay_cmd'])
|
||
time.sleep(0.001)
|
||
# # pass
|
||
|
||
###############################################################################
|
||
# Fonction bas niveau
|
||
###############################################################################
|
||
|
||
##
|
||
# Atteindre une orientation
|
||
##
|
||
|
||
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)
|