mirror of
https://forge.apps.education.fr/blender-edutech/ropy.git
synced 2024-01-27 08:23:20 +01:00
396 lines
14 KiB
Python
396 lines
14 KiB
Python
|
from rp_lib import * # Bibliothèque Ropy
|
||
|
|
||
|
###############################################################################
|
||
|
# rp_store.py
|
||
|
# @title: Magasin pour le Rover Ropy
|
||
|
# @project: Ropy (Blender-EduTech)
|
||
|
# @lang: fr
|
||
|
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
|
||
|
# @copyright: Copyright (C) 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.
|
||
|
#
|
||
|
###############################################################################
|
||
|
|
||
|
scene = bge.logic.getCurrentScene()
|
||
|
|
||
|
# Colors
|
||
|
color_store_fct = (0, 1, 0.857,1) # Turquoise
|
||
|
color_store_hl = (0.799, 0.617, 0.021, 1) # Jaune
|
||
|
color_store_activate = (0.936, 0.033, 1, 1) # Rose
|
||
|
color_store_inventory = (1, 0.192, 0.03, 1) # Orange
|
||
|
|
||
|
# 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
|
||
|
|
||
|
################################################################################
|
||
|
# Documentation
|
||
|
################################################################################
|
||
|
|
||
|
# Cards description pour le magasin
|
||
|
storecard_description ={}
|
||
|
|
||
|
# Vitesse
|
||
|
sc_vitesse_title="Vitesse"
|
||
|
sc_vitesse_text="Permet de modifier\nla vitesse des \ndéplacements."
|
||
|
storecard_description.update({"Store-vitesse-card" : [sc_vitesse_title, sc_vitesse_text]})
|
||
|
|
||
|
# Peinture
|
||
|
sc_peinture_title="Peinture"
|
||
|
sc_peinture_text="Permet de changer la \ncouleur des objets."
|
||
|
storecard_description.update({"Store-peinture-card" : [sc_peinture_title, sc_peinture_text]})
|
||
|
|
||
|
# Batterie +
|
||
|
sc_batterie_title="Batterie +"
|
||
|
sc_batterie_text="Augmente la capacité \nde la batterie à 200 \nmouvements (contre \n50)."
|
||
|
storecard_description.update({"Store-batterie-card" : [sc_batterie_title, sc_batterie_text]})
|
||
|
|
||
|
# Balise +
|
||
|
sc_balise_title="Balise +"
|
||
|
sc_balise_text="Porte le nombre de \nbasiles transportées\nà 200 (contre 50)."
|
||
|
storecard_description.update({"Store-balise-card" : [sc_balise_title, sc_balise_text]})
|
||
|
|
||
|
################################################################################
|
||
|
# Init, open et close
|
||
|
################################################################################
|
||
|
|
||
|
def init (cont):
|
||
|
obj=scene.objects['Rover']
|
||
|
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive and scene.objects['Terrain']['manip_mode']==0:
|
||
|
sound_play (snd_click)
|
||
|
thread_cmd_start(open)
|
||
|
|
||
|
##
|
||
|
# Fermeture
|
||
|
##
|
||
|
|
||
|
def close (cont):
|
||
|
pass
|
||
|
# obj=scene.objects['Rover']
|
||
|
# if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive and scene.objects['Terrain']['manip_mode']==0:
|
||
|
# sound_play (snd_click)
|
||
|
# thread_cmd_start(store)
|
||
|
|
||
|
##
|
||
|
# Ouverture
|
||
|
##
|
||
|
|
||
|
def open():
|
||
|
obj=scene.objects['Rover']
|
||
|
|
||
|
# Pathfinder pour le store
|
||
|
rover_go_store()
|
||
|
obj.worldPosition.x = -9.75
|
||
|
obj.worldPosition.y = -4
|
||
|
obj.worldPosition.z = 0.19
|
||
|
|
||
|
# Overlay
|
||
|
scene.objects['Terrain']['manip_mode']=8 # Fenêtre modale type Doc
|
||
|
scene.removeOverlayCollection(bpy.data.collections['Hud'])
|
||
|
scene.objects['Points'].setVisible(False,True)
|
||
|
scene.objects['Commands'].setVisible(False,True)
|
||
|
scene.active_camera = scene.objects["Camera-Store"]
|
||
|
scene.objects['Camera'].setVisible(False,True)
|
||
|
|
||
|
# Animation de la bulle
|
||
|
start = 1
|
||
|
end = 20
|
||
|
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
|
||
|
speed = 0.5
|
||
|
scene.objects['Bubble-1'].playAction('Bubble-1-ShaderAction', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
|
scene.objects['Bubble-1-text'].playAction('Bubble-1-text-ShaderAction', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
||
|
|
||
|
# Mise en couleurs
|
||
|
scene.objects['Store_close'].color= color_store_fct
|
||
|
scene.objects['Store_button'].color= color_store_fct
|
||
|
function_card=("balise", "batterie", "peinture", "vitesse")
|
||
|
for i in range(len(function_card)):
|
||
|
scene.objects["Store-"+function_card[i]+"-card"].color = color_store_fct
|
||
|
scene.objects["Store-"+function_card[i]+"-card-icon"].color = color_store_fct
|
||
|
scene.objects["Store-"+function_card[i]+"-card-text"].color = color_store_fct
|
||
|
scene.objects['Store']['page_fct'] =""
|
||
|
scene.objects['Store_text']['Text'] = " "
|
||
|
scene.objects['Store_text'].setVisible(False,True)
|
||
|
|
||
|
# Animation de la tablette
|
||
|
scene.objects['Store'].setVisible(True,True)
|
||
|
scene.objects['Store'].worldPosition = [-12.25, -2.7, 0.4]
|
||
|
scene.objects['Store'].worldScale = [0.01, 0.01, 0.01]
|
||
|
# applyRotationTo(scene.objects['Store'], None, (-0.2*math.pi)/180, (115*math.pi)/180, False)
|
||
|
applyRotationTo(scene.objects['Store'], None, (-2.38*math.pi)/180, (100*math.pi)/180, False)
|
||
|
# scene.objects['Store'].applyRotation((math.pi/2+(0.59189*math.pi)/180, 0, 0), True)
|
||
|
scene.objects['Store'].applyRotation((math.pi/2+(3*math.pi)/180, 0, 0), True)
|
||
|
scene.objects['Store']['timer'] = 0
|
||
|
scene.objects['Store']['anim'] = True
|
||
|
|
||
|
##
|
||
|
# Animation du store
|
||
|
##
|
||
|
|
||
|
def open_anim():
|
||
|
resol=100
|
||
|
x0 = -10.1722
|
||
|
y0 = -2.68957
|
||
|
z0 =0.42148
|
||
|
x1 = -9.05968
|
||
|
y1 = -5.01656
|
||
|
z1= 0.521224
|
||
|
xi = x0+((x1-x0)/resol)*scene.objects['Store']['timer']
|
||
|
yi = y0+((y1-y0)/resol)*scene.objects['Store']['timer']
|
||
|
zi = z0+((z1-z0)/resol)*scene.objects['Store']['timer']
|
||
|
scale0=0.01
|
||
|
# scale1=0.169
|
||
|
scale1=0.2
|
||
|
scalei = scale0+((scale1-scale0)/resol)*scene.objects['Store']['timer']
|
||
|
scene.objects['Store'].worldPosition = [xi, yi, zi]
|
||
|
scene.objects['Store'].worldScale = [scalei, scalei, scalei]
|
||
|
scene.objects['Store']['timer']+=1
|
||
|
# print ("Store : timer", scene.objects['Store']['timer'], xi, yi, zi, scalei)
|
||
|
if scene.objects['Store']['timer']== resol:
|
||
|
scene.objects['Store']['anim'] = False
|
||
|
|
||
|
###############################################################################
|
||
|
# Interface
|
||
|
###############################################################################
|
||
|
|
||
|
##
|
||
|
# Highlight du store
|
||
|
##
|
||
|
|
||
|
def hl (cont):
|
||
|
|
||
|
# Activation
|
||
|
if cont.sensors['MO'].status == JUST_ACTIVATED :
|
||
|
obj = cont.owner
|
||
|
name=obj.name[:-7]
|
||
|
print(name)
|
||
|
name_text=name+"-text"
|
||
|
name_icon=name+"-icon"
|
||
|
scene.objects[name].color = color_store_hl
|
||
|
|
||
|
# Close et button
|
||
|
if name == "Store_close" or name == "Store_button":
|
||
|
scene.objects[name].color = color_store_hl
|
||
|
else:
|
||
|
scene.objects[name].color = color_store_hl
|
||
|
scene.objects[name_text].color = color_store_hl
|
||
|
scene.objects[name_icon].color = color_store_hl
|
||
|
|
||
|
# Désactivation
|
||
|
if cont.sensors['MO'].status == JUST_RELEASED :
|
||
|
obj = cont.owner
|
||
|
name=obj.name[:-7]
|
||
|
name_text=obj.name[:-7]+"-text"
|
||
|
name_icon=obj.name[:-7]+"-icon"
|
||
|
|
||
|
# Close et button
|
||
|
if name == "Store_close" or name == "Store_button":
|
||
|
scene.objects[name].color = color_store_fct
|
||
|
else:
|
||
|
if name == scene.objects['Store']['page_fct'] :
|
||
|
scene.objects[name].color = color_store_activate
|
||
|
scene.objects[name_text].color = color_store_activate
|
||
|
scene.objects[name_icon].color = color_store_activate
|
||
|
else:
|
||
|
scene.objects[name].color = color_store_fct
|
||
|
scene.objects[name_text].color = color_store_fct
|
||
|
scene.objects[name_icon].color = color_store_fct
|
||
|
|
||
|
##
|
||
|
# Afficher les details de la fonction à partir d'une carte
|
||
|
##
|
||
|
|
||
|
def card (cont):
|
||
|
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive :
|
||
|
obj = cont.owner
|
||
|
name_fct= obj.name[:-7]
|
||
|
scene.objects['Store_text']['Text'] = " "
|
||
|
|
||
|
# Enlever l'ancienne carte
|
||
|
if scene.objects['Store']['page_fct'] !="":
|
||
|
scene.objects[scene.objects['Store']['page_fct']].color = color_store_fct
|
||
|
scene.objects[scene.objects['Store']['page_fct']+'-text'].color = color_store_fct
|
||
|
scene.objects[scene.objects['Store']['page_fct']+'-icon'].color = color_store_fct
|
||
|
|
||
|
# Afficher le texte de la carte
|
||
|
scene.objects['Store']['page_fct'] = name_fct
|
||
|
scene.objects[name_fct].color = color_store_activate
|
||
|
scene.objects[name_fct+'-icon'].color = color_store_activate
|
||
|
scene.objects[name_fct+'-text'].color = color_store_activate
|
||
|
scene.objects['Store_text']['Text'] = storecard_description[name_fct][1]
|
||
|
scene.objects['Store_text'].setVisible(True, False)
|
||
|
|
||
|
###############################################################################
|
||
|
# Rover
|
||
|
###############################################################################
|
||
|
|
||
|
##
|
||
|
# Direction
|
||
|
##
|
||
|
|
||
|
def rover_dir (direction):
|
||
|
obj=scene.objects['Rover']
|
||
|
step=math.pi/2 # Pas angulaire
|
||
|
# print ("rover_dir : ",direction)
|
||
|
|
||
|
# Nord
|
||
|
if direction=="n":
|
||
|
if round(obj.worldOrientation.to_euler().z, 2) == 0.00: # Sud
|
||
|
rp_gauche()
|
||
|
rp_gauche()
|
||
|
elif round(obj.worldOrientation.to_euler().z, 2) == round(math.pi,2) or round(obj.worldOrientation.to_euler().z, 2) == - round(math.pi,2) : # Nord
|
||
|
pass
|
||
|
elif 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_gauche()
|
||
|
elif 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_droite()
|
||
|
|
||
|
# Sud
|
||
|
if direction=="s":
|
||
|
if round(obj.worldOrientation.to_euler().z, 2) == 0.00: # Sud
|
||
|
pass
|
||
|
elif 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()
|
||
|
elif 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()
|
||
|
elif 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()
|
||
|
|
||
|
# Est
|
||
|
if direction=="e":
|
||
|
if round(obj.worldOrientation.to_euler().z, 2) == 0.00: # Sud
|
||
|
rp_gauche()
|
||
|
elif 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_droite()
|
||
|
elif 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
|
||
|
pass
|
||
|
elif 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()
|
||
|
rp_gauche()
|
||
|
|
||
|
# Ouest
|
||
|
if direction=="o":
|
||
|
if round(obj.worldOrientation.to_euler().z, 2) == 0.00: # Sud
|
||
|
rp_droite()
|
||
|
elif 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()
|
||
|
elif 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_gauche()
|
||
|
rp_gauche()
|
||
|
elif 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
|
||
|
pass
|
||
|
|
||
|
##
|
||
|
# Avancer pas
|
||
|
##
|
||
|
|
||
|
def rover_avancer_nbpas(pas):
|
||
|
for i in range (int(pas)):
|
||
|
rp_avancer()
|
||
|
|
||
|
def rover_dir_nbpas(direction, pas):
|
||
|
if pas>0:
|
||
|
rover_dir (direction)
|
||
|
rover_avancer_nbpas(pas)
|
||
|
|
||
|
##
|
||
|
# Pathfinder pour le store
|
||
|
##
|
||
|
|
||
|
def rover_go_store():
|
||
|
obj=scene.objects['Rover']
|
||
|
# print("Go store !!")
|
||
|
|
||
|
# Pathfinder : debug
|
||
|
obj.worldPosition.x=-13
|
||
|
obj.worldPosition.y=-8
|
||
|
|
||
|
x0 = obj.worldPosition.x
|
||
|
y0 = obj.worldPosition.y
|
||
|
z0 = obj.worldPosition.z
|
||
|
# print ("Position actuelle :", x0, y0)
|
||
|
|
||
|
# Pathfinder : zone 1
|
||
|
if x0<=-10 and (y0==2 or y0==3):
|
||
|
# print ("Store : zone 1 -> -8;4")
|
||
|
rover_dir_nbpas("n",4-y0)
|
||
|
rover_dir_nbpas("e",-x0-8)
|
||
|
x0 = obj.worldPosition.x
|
||
|
y0 = obj.worldPosition.y
|
||
|
# print ("Position actuelle :", x0, y0)
|
||
|
|
||
|
# Pathfinder : zone 2
|
||
|
if x0<=-9 and y0>=4:
|
||
|
# print ("Store : zone 2 -> -8;4")
|
||
|
rover_dir_nbpas("e",-x0-8)
|
||
|
rover_dir_nbpas("s",y0-4)
|
||
|
x0 = obj.worldPosition.x
|
||
|
y0 = obj.worldPosition.y
|
||
|
# print ("Position actuelle :", x0, y0)
|
||
|
|
||
|
# Pathfinder : zone 3
|
||
|
if x0==-9 and y0==1:
|
||
|
# print ("Store : zone 3 -> -8;4")
|
||
|
rover_dir_nbpas("e",1)
|
||
|
x0 = obj.worldPosition.x
|
||
|
y0 = obj.worldPosition.y
|
||
|
# print ("Position actuelle :", x0, y0)
|
||
|
|
||
|
# Pathfinder : zone 4
|
||
|
if x0>=14 and y0<=-1:
|
||
|
# print ("Store : zone 4 -> x;0")
|
||
|
rover_dir_nbpas("n",-y0)
|
||
|
x0 = obj.worldPosition.x
|
||
|
y0 = obj.worldPosition.y
|
||
|
# print ("Position actuelle :", x0, y0)
|
||
|
|
||
|
# Pathfinder : zone 5
|
||
|
if x0>=-8 and y0>=0:
|
||
|
# print ("Store : zone 5 -> -8;0 puis -8;-1")
|
||
|
rover_dir_nbpas("s",y0)
|
||
|
rover_dir_nbpas("o",8+x0)
|
||
|
rover_dir_nbpas("s",1)
|
||
|
x0 = obj.worldPosition.x
|
||
|
y0 = obj.worldPosition.y
|
||
|
# print ("Position actuelle :", x0, y0)
|
||
|
|
||
|
# Pathfinder : zone 6
|
||
|
if y0>=-4:
|
||
|
# print ("Store : zone 6 -> -10;-4")
|
||
|
rover_dir_nbpas("s",y0+4)
|
||
|
rover_dir_nbpas("o",10+x0)
|
||
|
x0 = obj.worldPosition.x
|
||
|
y0 = obj.worldPosition.y
|
||
|
# print ("Position actuelle :", x0, y0)
|
||
|
|
||
|
# Pathfinder : zone 7
|
||
|
if x0<=-11 and y0<=-5:
|
||
|
# print ("Store : zone 7 -> -10;y")
|
||
|
rover_dir_nbpas("e",-x0-10)
|
||
|
x0 = obj.worldPosition.x
|
||
|
y0 = obj.worldPosition.y
|
||
|
# print ("Position actuelle :", x0, y0)
|
||
|
|
||
|
# Pathfinder : zone 8
|
||
|
if y0<=-5:
|
||
|
# print ("Store : zone 8 -> -10;-4")
|
||
|
rover_dir_nbpas("n",-y0-4)
|
||
|
rover_dir_nbpas("o",10+x0)
|
||
|
x0 = obj.worldPosition.x
|
||
|
y0 = obj.worldPosition.y
|
||
|
# print ("Position actuelle :", x0, y0)
|
||
|
|
||
|
rover_dir ("o")
|