2022-04-06 06:25:03 +02:00
# import importlib
2022-04-03 06:21:42 +02:00
# import imp
2022-03-29 14:12:58 +02:00
import bge # Blender Game Engine (UPBGE)
import bpy # Blender
import aud # Sounds
2022-03-10 01:08:08 +01:00
import math
2022-04-11 22:42:09 +02:00
import mathutils
2022-03-10 01:08:08 +01:00
import time
import sys
2022-03-23 00:27:11 +01:00
import os
2022-03-10 01:08:08 +01:00
import webbrowser
import threading # Multithreading
2022-03-23 00:27:11 +01:00
import xml . etree . ElementTree as ET # Creating/parsing XML file
2022-04-06 06:25:03 +02:00
import runpy
2022-03-10 01:08:08 +01:00
2022-04-03 06:21:42 +02:00
import ct_map1 as ct_map # waves script
# import ct_cmd # user script (commands)
2022-03-10 01:08:08 +01:00
###############################################################################
# ct.py
2022-03-20 05:57:07 +01:00
# @title: the CodeTower game
2022-04-01 01:34:54 +02:00
# @project: CodeTower
2022-03-24 17:00:09 +01:00
# @lang: fr,en
2022-04-11 22:42:09 +02:00
# @authors: Philippe Roy <phroy@phroy.org>
2022-03-10 01:08:08 +01:00
# @copyright: Copyright (C) 2022 Philippe Roy
# @license: GNU GPL
2022-04-01 01:34:54 +02:00
#
# This game is a tower defense coding game. The towers are driven by Python code.
2022-03-10 01:08:08 +01:00
# Ce simulateur est un jeu du type tower defense où les tours sont à piloter par la programmation Python.
2022-03-30 03:10:33 +02:00
#
# Commands trigged by button : cmd_*
# Commands trigged by 3D scene objects : scn_*
# Commands trigged by user (student or map designer) : ct_*
# 3D scene manipulation : manip_*
2022-03-10 01:08:08 +01:00
#
###############################################################################
2022-04-24 03:34:27 +02:00
# Debug flag
scene = bge . logic . getCurrentScene ( )
scene . objects [ ' Commands ' ] [ ' debug_fps ' ] = False
# Memory
2022-04-03 21:15:43 +02:00
sys . setrecursionlimit ( 10 * * 5 ) # Limite sur la récursivité (valeur par défaut : 1000) -> segfault de Blender
2022-04-06 06:25:03 +02:00
# Dynamic import user file
# importlib.invalidate_caches()
2022-04-03 06:21:42 +02:00
# ct_map = importlib.import_module('ct_map1') # Waves script
2022-04-06 06:25:03 +02:00
# ct_cmd = importlib.import_module('ct_cmd') # User script (commands) -> segfault de Blender
2022-03-10 01:08:08 +01:00
2022-03-29 14:12:58 +02:00
# UPBGE scene
eevee = bpy . context . scene . eevee
2022-04-03 06:21:42 +02:00
fps_time = 0.0
2022-03-23 00:27:11 +01:00
2022-03-29 14:12:58 +02:00
# Config file
2022-03-23 00:27:11 +01:00
# print (os.getcwd())
ct_config = ET . parse ( ' ct_config.xml ' )
ct_config_tree = ct_config . getroot ( )
2022-03-10 01:08:08 +01:00
2022-03-29 14:12:58 +02:00
# Colors
2022-04-03 06:21:42 +02:00
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 )
2022-03-10 01:08:08 +01:00
2022-04-03 06:21:42 +02:00
color_kaykit_black = ( 0.019 , 0.032 , 0.037 , 1 )
2022-03-30 03:10:33 +02:00
2022-04-03 06:21:42 +02:00
color_endbanner_bluelight = ( 0.361 , 0.527 , 0.716 , 1 )
color_endbanner_bluedark = ( 0.130 , 0.254 , 0.407 , 1 )
2022-03-10 01:08:08 +01:00
2022-04-03 06:21:42 +02:00
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 )
2022-03-23 00:27:11 +01:00
2022-03-29 14:12:58 +02:00
# Sounds
audiodev = aud . Device ( )
snd_click = aud . Sound ( ' asset/sounds/click.ogg ' )
sndbuff_click = aud . Sound . cache ( snd_click )
snd_construct = aud . Sound ( ' asset/sounds/click_construct.ogg ' )
sndbuff_construct = aud . Sound . cache ( snd_construct )
2022-04-22 16:54:49 +02:00
snd_book_open = aud . Sound ( ' asset/sounds/book_open.ogg ' )
sndbuff_book_open = aud . Sound . cache ( snd_book_open )
snd_book_close = aud . Sound ( ' asset/sounds/book_close.ogg ' )
sndbuff_book_close = aud . Sound . cache ( snd_book_close )
2022-03-10 01:08:08 +01:00
2022-03-29 14:12:58 +02:00
# UPBGE constants
2022-03-10 01:08:08 +01:00
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
2022-03-11 00:21:00 +01:00
###############################################################################
2022-03-23 00:27:11 +01:00
# Tour
2022-03-11 00:21:00 +01:00
###############################################################################
2022-03-23 00:27:11 +01:00
# Commande pour afficher la position de la tour de construction
def cmd_tower_construct ( cont ) :
2022-03-11 00:21:00 +01:00
obj = cont . owner
obj_Hl = scene . objects [ obj . name + " -Hl " ]
if cont . sensors [ ' Click ' ] . status == JUST_ACTIVATED and cont . sensors [ ' MO ' ] . positive and scene . objects [ ' Terrain ' ] [ ' manip_mode ' ] == 0 :
2022-04-22 16:54:49 +02:00
sound_play ( sndbuff_construct )
2022-03-11 00:21:00 +01:00
if scene . objects [ ' Terrain ' ] [ ' construct_mode ' ] == True :
scene . objects [ ' Terrain ' ] [ ' construct_mode ' ] = False
obj . worldScale = [ 1 , 1 , 1 ]
2022-03-24 17:00:09 +01:00
obj . color = color_cmd
2022-03-11 00:21:00 +01:00
obj_Hl . worldScale = [ 1 , 1 , 1 ]
2022-03-24 17:00:09 +01:00
obj_Hl . color = color_cmd
2022-03-20 05:57:07 +01:00
scene . objects [ ' Tower_construc_mode ' ] . setVisible ( False , False )
2022-03-24 17:00:09 +01:00
scene . objects [ ' Tower_construc_mode ' ] . color = color_cmd
2022-03-23 00:27:11 +01:00
text_info ( " " )
2022-03-11 00:21:00 +01:00
else :
scene . objects [ ' Terrain ' ] [ ' construct_mode ' ] = True
obj . worldScale = [ 1.25 , 1.25 , 1.25 ]
2022-03-24 17:00:09 +01:00
obj . color = color_cmd_hl
2022-03-11 00:21:00 +01:00
obj_Hl . worldScale = [ 1.25 , 1.25 , 1.25 ]
2022-03-24 17:00:09 +01:00
obj_Hl . color = color_cmd_hl
2022-03-20 05:57:07 +01:00
scene . objects [ ' Tower_construc_mode ' ] . setVisible ( True , False )
2022-03-24 17:00:09 +01:00
scene . objects [ ' Tower_construc_mode ' ] . color = color_cmd
2022-03-23 00:27:11 +01:00
text_info ( " Tower position : " )
2022-04-22 16:54:49 +02:00
scene . objects [ ' Cmd_text ' ] [ ' Text ' ] = " "
2022-03-31 18:26:55 +02:00
2022-03-10 01:08:08 +01:00
###############################################################################
2022-03-23 00:27:11 +01:00
# User interface : texte info et compteurs
2022-03-10 01:08:08 +01:00
###############################################################################
2022-03-23 00:27:11 +01:00
# Affichage sur la boite de texte sur 6 lignes
2022-03-24 17:00:09 +01:00
def text_info ( text ) :
if text == " " :
2022-03-23 00:27:11 +01:00
scene . objects [ ' Text_info-1 ' ] . setVisible ( False , False )
scene . objects [ ' Text_info-2 ' ] . setVisible ( False , False )
else :
2022-03-24 17:00:09 +01:00
lines_txt = text . split ( " \n " , 6 )
for i in range ( len ( lines_txt ) , 6 ) :
lines_txt . append ( " " )
2022-03-23 00:27:11 +01:00
scene . objects [ ' Text_info-1 ' ] . setVisible ( True , False )
scene . objects [ ' Text_info-2 ' ] . setVisible ( True , False )
2022-03-24 17:00:09 +01:00
scene . objects [ ' Text_info-1 ' ] [ ' Text ' ] = lines_txt [ 0 ] + " \n " + lines_txt [ 1 ] + " \n " + lines_txt [ 2 ]
scene . objects [ ' Text_info-2 ' ] [ ' Text ' ] = lines_txt [ 3 ] + " \n " + lines_txt [ 4 ] + " \n " + lines_txt [ 5 ]
2022-03-23 00:27:11 +01:00
2022-03-13 05:32:55 +01:00
# Mise à jour de l'affichage des compteurs
def points_maj ( cont ) :
2022-04-03 06:21:42 +02:00
2022-04-06 06:25:03 +02:00
global fps_time
2022-04-24 03:34:27 +02:00
# Synchronisation des threads : attente dela création d'une tour ou de l'apparition d'un minion
2022-04-11 22:42:09 +02:00
while scene . objects [ ' Terrain ' ] [ ' thread_cmd_lock ' ] == True :
# print ("UPBGE : thread_cmd_lock =True")
time . sleep ( 0.01 )
2022-04-03 06:21:42 +02:00
# Texte
2022-03-20 05:57:07 +01:00
scene . objects [ ' Lifes_text ' ] [ ' Text ' ] = str ( scene . objects [ ' Points ' ] [ ' lifes ' ] ) + " / " + str ( scene . objects [ ' Points ' ] [ ' lifes_max ' ] )
scene . objects [ ' Coins_text ' ] [ ' Text ' ] = str ( scene . objects [ ' Points ' ] [ ' coins ' ] )
scene . objects [ ' Level_text ' ] [ ' Text ' ] = str ( scene . objects [ ' Points ' ] [ ' level ' ] ) + " / " + str ( scene . objects [ ' Points ' ] [ ' level_max ' ] )
scene . objects [ ' Minions_text ' ] [ ' Text ' ] = str ( scene . objects [ ' Points ' ] [ ' minions ' ] )
2022-03-29 14:12:58 +02:00
scene . objects [ ' Points ' ] [ ' tics ' ] + = 1
2022-03-13 05:32:55 +01:00
2022-04-03 06:21:42 +02:00
# Texte de la vague
if scene . objects [ ' Map_text ' ] [ ' anim ' ] :
2022-04-24 03:34:27 +02:00
if scene . objects [ ' Map_text ' ] [ ' timer ' ] < 120 :
2022-04-24 04:36:43 +02:00
decal = 0.1
vect = scene . objects [ ' Map_text ' ] . getVectTo ( scene . objects [ ' Camera ' ] ) [ 1 ]
scene . objects [ ' Map_text ' ] . applyMovement ( ( vect [ 0 ] * decal , vect [ 1 ] * decal , vect [ 2 ] * decal ) , False )
2022-04-24 03:34:27 +02:00
scene . objects [ ' Map_text ' ] [ ' timer ' ] + = 1
if int ( scene . objects [ ' Map_text ' ] [ ' timer ' ] ) > = 120 :
2022-04-03 06:21:42 +02:00
scene . objects [ ' Map_text ' ] . color = color_text
2022-04-24 04:36:43 +02:00
scene . objects [ ' Map_text ' ] . worldPosition . x = scene . objects [ ' Points ' ] . worldPosition . x + scene . objects [ ' Map_text ' ] [ ' init_relativ_lx ' ]
scene . objects [ ' Map_text ' ] . worldPosition . y = scene . objects [ ' Points ' ] . worldPosition . y + scene . objects [ ' Map_text ' ] [ ' init_relativ_ly ' ]
scene . objects [ ' Map_text ' ] . worldPosition . z = scene . objects [ ' Points ' ] . worldPosition . z + scene . objects [ ' Map_text ' ] [ ' init_relativ_lz ' ]
2022-04-03 06:21:42 +02:00
scene . objects [ ' Map_text ' ] [ ' anim ' ] = False
2022-04-24 03:34:27 +02:00
# Gestion du FPS - Tous les tics
2022-04-11 22:42:09 +02:00
if scene . objects [ ' Commands ' ] [ ' debug_fps ' ] :
2022-04-24 03:34:27 +02:00
# if scene.objects['Points']['tics']%60 ==0: # Toutes les 60 tics
milliseconds = int ( time . time ( ) * 1000 ) # Tous les tics
if milliseconds != fps_time :
fps = int ( 1000 / ( milliseconds - fps_time ) )
else :
fps = " ---- "
print ( " Durée entre deux tics (16 ms), fps (60), coins : " , milliseconds - fps_time , fps , str ( scene . objects [ ' Points ' ] [ ' coins ' ] ) )
fps_time = milliseconds
2022-04-03 06:21:42 +02:00
2022-03-30 03:10:33 +02:00
# Augmentation d'un niveau
2022-04-01 01:34:54 +02:00
if scene . objects [ ' Points ' ] [ ' coins ' ] > = 100 :
2022-03-30 03:10:33 +02:00
scene . objects [ ' Points ' ] [ ' level_max ' ] + = 1
scene . objects [ ' Points ' ] [ ' coins ' ] - = 100
2022-03-23 00:27:11 +01:00
# Level trop élevé
if scene . objects [ ' Points ' ] [ ' level ' ] > scene . objects [ ' Points ' ] [ ' level_max ' ] :
2022-03-24 17:00:09 +01:00
scene . objects [ ' Level_text ' ] . color = color_text_red
2022-04-03 06:21:42 +02:00
if scene . objects [ ' Points ' ] [ ' level ' ] < scene . objects [ ' Points ' ] [ ' level_max ' ] :
scene . objects [ ' Level_text ' ] . color = color_text_yellow
if scene . objects [ ' Points ' ] [ ' level ' ] == scene . objects [ ' Points ' ] [ ' level_max ' ] :
2022-03-24 17:00:09 +01:00
scene . objects [ ' Level_text ' ] . color = color_text
2022-03-23 00:27:11 +01:00
2022-07-07 17:49:08 +02:00
# Ramassage des minions perdues ou zombis
2022-04-11 22:42:09 +02:00
if scene . objects [ ' Terrain ' ] [ ' run ' ] == True : # Pas en pause
if scene . objects [ ' Points ' ] [ ' tics ' ] % 240 == 0 : # Toutes les 4 secondes
scene . objects [ ' Points ' ] [ ' minions_lost ' ] = [ ]
for obj_i in scene . objects :
2022-07-07 17:49:08 +02:00
if " type_minion " in obj_i . getPropertyNames ( ) and " type_towerminion " not in obj_i . getPropertyNames ( ) and obj_i . visible == True :
2022-04-11 22:42:09 +02:00
# print ("Minion lost : (dist, dist_old, x, last_x, y, last_y) : ", obj_i.name, obj_i['dist'], obj_i['dist_old'], obj_i.worldPosition.x, obj_i['dist_last_x'], obj_i.worldPosition.y, obj_i['dist_last_y'])
if obj_i [ ' dist ' ] == obj_i [ ' dist_old ' ] and obj_i [ ' dist_new ' ] == False :
print ( " Minion lost : dist =dist_old (dist, dist_old) : " , obj_i . name , obj_i [ ' dist ' ] , obj_i [ ' dist_old ' ] )
obj_i [ ' dead ' ] = True
elif obj_i . worldLinearVelocity . x > 0 and abs ( obj_i . worldLinearVelocity . x ) < 0.001 and obj_i . worldLinearVelocity . y > 0 and abs ( obj_i . worldLinearVelocity . y ) < 0.001 :
print ( " Minion lost : x ' or y ' very slow (x, y, z, x ' , y ' ) : " , obj_i . name , obj_i . worldPosition . x , obj_i . worldPosition . y , obj_i . worldPosition . z , abs ( obj_i . worldLinearVelocity . x ) , abs ( obj_i . worldLinearVelocity . y ) )
obj_i [ ' dead ' ] = True
elif obj_i . worldPosition . x < scene . objects [ ' Terrain ' ] [ ' size ' ] [ 0 ] or obj_i . worldPosition . x > scene . objects [ ' Terrain ' ] [ ' size ' ] [ 1 ] or obj_i . worldPosition . y < scene . objects [ ' Terrain ' ] [ ' size ' ] [ 2 ] or obj_i . worldPosition . y > scene . objects [ ' Terrain ' ] [ ' size ' ] [ 3 ] :
print ( " Minion lost : x or y outside the map (x, y, z, x ' , y ' ) : " , obj_i . name , obj_i . worldPosition . x , obj_i . worldPosition . y , obj_i . worldPosition . z , abs ( obj_i . worldLinearVelocity . x ) , abs ( obj_i . worldLinearVelocity . y ) )
obj_i [ ' dead ' ] = True
obj_i [ ' dist_old ' ] = obj_i [ ' dist ' ]
obj_i [ ' dist_new ' ] = False
2022-03-31 18:26:55 +02:00
2022-03-30 03:10:33 +02:00
# Fin de la vague
2022-03-29 14:12:58 +02:00
if scene . objects [ ' Terrain ' ] [ ' thread_wave ' ] == False and scene . objects [ ' Terrain ' ] [ ' map_run ' ] == True :
if scene . objects [ ' Points ' ] [ ' minions_run ' ] == 0 :
2022-03-30 03:10:33 +02:00
2022-03-31 18:26:55 +02:00
# Fin ou vague suivante
if scene . objects [ ' Points ' ] [ ' lifes ' ] == 0 or scene . objects [ ' Points ' ] [ ' wave ' ] == scene . objects [ ' Terrain ' ] [ ' nb_waves ' ] :
terrain_end ( )
else :
2022-04-03 06:21:42 +02:00
scene . objects [ ' Points ' ] [ ' minions ' ] = 0
2022-03-30 03:10:33 +02:00
scene . objects [ ' Points ' ] [ ' wave ' ] + = 1
ct_map . start ( scene . objects [ ' Points ' ] [ ' wave ' ] ) # Lancement du script de la vague
2022-03-13 05:32:55 +01:00
2022-03-23 00:27:11 +01:00
###############################################################################
# Terrain
###############################################################################
# Mouse over du terrain
2022-03-30 03:10:33 +02:00
def scn_terrain_mo ( cont ) :
2022-03-23 00:27:11 +01:00
# Affiche la position de la tour de construction
if scene . objects [ ' Terrain ' ] [ ' construct_mode ' ] == True :
hitObject = cont . sensors [ ' MO ' ] . hitObject
hitPosition = cont . sensors [ ' MO ' ] . hitPosition
if hitObject is not None :
2022-03-30 03:10:33 +02:00
if round ( hitPosition . x ) > = scene . objects [ ' Terrain ' ] [ ' size ' ] [ 0 ] and round ( hitPosition . x ) < = scene . objects [ ' Terrain ' ] [ ' size ' ] [ 1 ] and round ( hitPosition . y ) > = scene . objects [ ' Terrain ' ] [ ' size ' ] [ 2 ] and round ( hitPosition . y ) < = scene . objects [ ' Terrain ' ] [ ' size ' ] [ 3 ] :
2022-03-23 00:27:11 +01:00
if [ round ( hitPosition . x ) , round ( hitPosition . y ) ] in scene . objects [ ' Terrain ' ] [ ' scene_tile_noncontruct ' ] or [ round ( hitPosition . x ) , round ( hitPosition . y ) ] in scene . objects [ ' Terrain ' ] [ ' scene_tile_tower ' ] :
pass
else :
scene . objects [ ' Tower_construc_mode ' ] . worldPosition . x = round ( hitPosition . x )
scene . objects [ ' Tower_construc_mode ' ] . worldPosition . y = round ( hitPosition . y )
scene . objects [ ' Tower_construc_mode ' ] . worldPosition . z = 0.2
text_info ( " Tower position : " + str ( round ( hitPosition . x ) ) + " , " + str ( round ( hitPosition . y ) ) )
2022-04-22 16:54:49 +02:00
scene . objects [ ' Cmd_text ' ] [ ' Text ' ] = " "
2022-03-23 00:27:11 +01:00
# Affiche les informations sur la tour
2022-03-27 10:28:03 +02:00
# FIXME : High-light sur la tower sélectionnée
2022-03-23 00:27:11 +01:00
else :
hitPosition = cont . sensors [ ' MO ' ] . hitPosition
if cont . sensors [ ' Click ' ] . status == JUST_ACTIVATED and cont . sensors [ ' MO ' ] . positive and scene . objects [ ' Terrain ' ] [ ' manip_mode ' ] == 0 :
if [ round ( hitPosition . x ) , round ( hitPosition . y ) ] in scene . objects [ ' Terrain ' ] [ ' scene_tile_tower ' ] :
for obj_i in scene . objects :
if " type_tower " in obj_i . getPropertyNames ( ) :
if round ( hitPosition . x ) == obj_i . worldPosition . x and round ( hitPosition . y ) == obj_i . worldPosition . y :
text = obj_i [ ' tower_name ' ] + " \n " + " - level : " + str ( obj_i [ ' lvl ' ] ) + " \n " + " - damage : " + str ( obj_i [ ' damage ' ] ) + " \n " + " - speed : " + str ( round ( obj_i [ ' speed ' ] , 2 ) ) + " \n " + " - range : " + str ( obj_i [ ' range ' ] )
text_info ( text )
break
else :
text_info ( " " )
2022-03-13 05:32:55 +01:00
# Initialisation lors du chargement du terrain
def terrain_init ( cont ) :
2022-03-31 18:26:55 +02:00
2022-03-29 14:12:58 +02:00
# Cacher la bannière de fin
scene . objects [ ' End ' ] . setVisible ( False , True )
scene . objects [ ' End ' ] [ ' timer ' ] = 0
2022-04-22 16:54:49 +02:00
scene . objects [ ' Doc ' ] . setVisible ( False , True )
2022-03-29 14:12:58 +02:00
2022-03-27 10:28:03 +02:00
# Pile des draws
scene . objects [ ' Terrain ' ] [ ' draw_process ' ] = False
scene . objects [ ' Terrain ' ] [ ' draw_list ' ] = [ ]
2022-03-29 14:12:58 +02:00
2022-03-31 18:26:55 +02:00
# Ramasse-miettes
scene . objects [ ' Points ' ] [ ' minions_lost ' ] = [ ]
2022-03-29 14:12:58 +02:00
# Configuration du moteur de rendu
2022-04-06 06:25:03 +02:00
eevee . use_eevee_smaa = False
2022-04-06 17:28:16 +02:00
if scene . objects [ ' Terrain ' ] [ ' speed ' ] < 10 : # smaa avec en vitesse 4 et 10 -> tendance au plantage
2022-03-29 14:12:58 +02:00
eevee . use_eevee_smaa = True
2022-03-13 05:32:55 +01:00
2022-03-20 05:57:07 +01:00
# Recherche les tuiles non constructibles (chemin)
2022-03-23 00:27:11 +01:00
scene . objects [ ' Terrain ' ] [ ' scene_tile_noncontruct ' ] = [ ]
for obj_i in scene . objects :
if " tile_straight " in obj_i . name :
scene . objects [ ' Terrain ' ] [ ' scene_tile_noncontruct ' ] . append ( [ obj_i . worldPosition . x , obj_i . worldPosition . y ] )
obj_i . collisionGroup = 2
if " tile_cornerRound " in obj_i . name :
scene . objects [ ' Terrain ' ] [ ' scene_tile_noncontruct ' ] . append ( [ obj_i . worldPosition . x , obj_i . worldPosition . y ] )
obj_i . collisionGroup = 2
if " tile_hill " in obj_i . name :
obj_i . collisionGroup = 2
2022-03-29 14:12:58 +02:00
scene . objects [ ' Terrain ' ] [ ' scene_tile_tower ' ] = [ ]
2022-03-20 05:57:07 +01:00
2022-03-30 03:10:33 +02:00
# Init de la carte
ct_map . map_init ( )
ct_map . map_reset ( )
scene . objects [ ' Level_text ' ] . color = color_text
2022-03-10 01:08:08 +01:00
# Mise en route et pause du cycle
2022-03-29 14:12:58 +02:00
def terrain_run ( ) :
2022-04-22 16:54:49 +02:00
sound_play ( sndbuff_click )
2022-03-10 01:08:08 +01:00
2022-03-29 14:12:58 +02:00
# Pause
2022-04-22 17:51:58 +02:00
# FIXME : HL alors que c'est avec les touches
2022-03-29 14:12:58 +02:00
if scene . objects [ ' Terrain ' ] [ ' run ' ] == True :
scene . objects [ ' Terrain ' ] [ ' run ' ] = False
scene . objects [ ' Pause ' ] . setVisible ( False , False )
2022-04-16 05:43:32 +02:00
scene . objects [ ' Pause ' ] . suspendPhysics ( )
2022-03-29 14:12:58 +02:00
scene . objects [ ' Pause-Hl ' ] . setVisible ( False , False )
2022-04-16 05:43:32 +02:00
scene . objects [ ' Run ' ] . restorePhysics ( )
2022-03-29 14:12:58 +02:00
scene . objects [ ' Run-Hl ' ] . setVisible ( True , False )
2022-04-22 17:51:58 +02:00
scene . objects [ ' Cmd_text ' ] [ ' Text ' ] = " Run (F5) "
scene . objects [ ' Cmd_text ' ] . setVisible ( True , False ) # FIXME : suppresion de du message
2022-03-29 14:12:58 +02:00
for obj_i in scene . objects : # Pause des Steerings
if " type_minion " in obj_i . getPropertyNames ( ) and " type_towerminion " not in obj_i . getPropertyNames ( ) :
obj_i . actuators [ ' Steering ' ] . velocity = 0
2022-03-10 01:08:08 +01:00
2022-03-29 14:12:58 +02:00
# Run
2022-04-22 17:51:58 +02:00
# FIXME : HL alors que c'est avec les touches
2022-04-06 17:28:16 +02:00
else :
2022-03-29 14:12:58 +02:00
scene . objects [ ' Terrain ' ] [ ' run ' ] = True
scene . objects [ ' Run ' ] . setVisible ( False , False )
2022-04-16 05:43:32 +02:00
scene . objects [ ' Run ' ] . suspendPhysics ( )
2022-03-29 14:12:58 +02:00
scene . objects [ ' Run-Hl ' ] . setVisible ( False , False )
2022-04-16 05:43:32 +02:00
scene . objects [ ' Pause ' ] . restorePhysics ( )
2022-03-29 14:12:58 +02:00
scene . objects [ ' Pause-Hl ' ] . setVisible ( True , False )
2022-04-22 17:51:58 +02:00
scene . objects [ ' Cmd_text ' ] [ ' Text ' ] = " Pause (F5) "
scene . objects [ ' Cmd_text ' ] . setVisible ( True , False )
2022-03-23 00:27:11 +01:00
2022-03-29 14:12:58 +02:00
# Démarrage de la map
if scene . objects [ ' Terrain ' ] [ ' thread_run ' ] == False :
scene . objects [ ' Stop ' ] . setVisible ( True , False )
2022-04-16 05:43:32 +02:00
scene . objects [ ' Stop ' ] . restorePhysics ( )
2022-04-06 17:28:16 +02:00
runpy . run_module ( ' ct_cmd ' , run_name = ' stop ' ) # Stop du script utilisateur
ct_map . stop ( ) # Stop du script des vagues
2022-03-23 00:27:11 +01:00
2022-03-29 14:12:58 +02:00
# Mise à zéro des compteurs
2022-03-30 03:10:33 +02:00
ct_map . map_reset ( )
2022-03-29 14:12:58 +02:00
# Supprimer les tours
for obj_i in scene . objects :
if " type_tower " in obj_i . getPropertyNames ( ) :
obj_i . endObject ( )
if " type_towerminion " in obj_i . getPropertyNames ( ) :
obj_i . endObject ( )
scene . objects [ ' Terrain ' ] [ ' scene_tile_tower ' ] = [ ]
2022-04-06 17:28:16 +02:00
# Scripts utilisateur et vagues
2022-03-29 14:12:58 +02:00
scene . objects [ ' Terrain ' ] [ ' map_run ' ] = True
scene . objects [ ' Terrain ' ] [ ' thread_run ' ] = True
scene . objects [ ' Points ' ] [ ' time_begin ' ] = time . localtime ( )
2022-04-06 06:25:03 +02:00
# scene.objects['Commands']['cmd_start']=True
# Execution du script utilisateur par importlib -> Segfault de Blender
# importlib.reload(ct_cmd)
# ct_cmd.start() # Execution du script utilisateur
runpy . run_module ( ' ct_cmd ' , run_name = ' start ' ) # Execution du script utilisateur
2022-03-30 03:10:33 +02:00
ct_map . start ( 1 ) # Lancement du script de la permière vague
2022-03-29 14:12:58 +02:00
# Arrêt de la pause
else :
for obj_i in scene . objects : # Relance des Steerings
if " type_minion " in obj_i . getPropertyNames ( ) and " type_towerminion " not in obj_i . getPropertyNames ( ) :
2022-03-31 18:26:55 +02:00
obj_i . actuators [ ' Steering ' ] . velocity = obj_i [ ' speed_base ' ] * scene . objects [ ' Terrain ' ] [ ' speed ' ]
2022-03-20 05:57:07 +01:00
2022-03-10 01:08:08 +01:00
# Arrêt et réinitialisation du cycle
2022-03-29 14:12:58 +02:00
def terrain_stop ( ) :
2022-04-22 16:54:49 +02:00
sound_play ( sndbuff_click )
2022-03-10 01:08:08 +01:00
2022-03-29 14:12:58 +02:00
# Arrêt des threads utilisateurs
scene . objects [ ' Terrain ' ] [ ' run ' ] = False
scene . objects [ ' Terrain ' ] [ ' thread_run ' ] = False
scene . objects [ ' Terrain ' ] [ ' map_run ' ] = False # Ne pas afficher la bannière de fin
2022-04-06 17:28:16 +02:00
runpy . run_module ( ' ct_cmd ' , run_name = ' stop ' ) # Stop du script utilisateur
2022-03-30 03:10:33 +02:00
ct_map . stop ( ) # Stop du script des vagues
2022-03-10 01:08:08 +01:00
2022-03-29 14:12:58 +02:00
# Supprimer les enemis
for obj_i in scene . objects :
if " type_minion " in obj_i . getPropertyNames ( ) and " type_towerminion " not in obj_i . getPropertyNames ( ) :
obj_i . endObject ( )
scene . objects [ ' Points ' ] [ ' minions ' ] = 0
scene . objects [ ' Points ' ] [ ' minions_run ' ] = 0
# Commandes
scene . objects [ ' Pause ' ] . setVisible ( False , False )
2022-04-16 05:43:32 +02:00
scene . objects [ ' Pause ' ] . suspendPhysics ( )
2022-03-29 14:12:58 +02:00
scene . objects [ ' Pause-Hl ' ] . setVisible ( False , False )
scene . objects [ ' Run ' ] . setVisible ( True , False )
2022-04-16 05:43:32 +02:00
scene . objects [ ' Run ' ] . restorePhysics ( )
2022-03-29 14:12:58 +02:00
scene . objects [ ' Stop ' ] . setVisible ( False , False )
2022-04-16 05:43:32 +02:00
scene . objects [ ' Stop ' ] . suspendPhysics ( )
2022-03-29 14:12:58 +02:00
scene . objects [ ' Stop-Hl ' ] . setVisible ( False , False )
2022-04-22 17:51:58 +02:00
scene . objects [ ' Cmd_text ' ] . setVisible ( False , False )
2022-03-10 01:08:08 +01:00
2022-03-30 03:10:33 +02:00
# Fin de la map
2022-03-29 14:12:58 +02:00
def terrain_end ( ) :
2022-03-11 00:21:00 +01:00
scene . objects [ ' Terrain ' ] [ ' run ' ] = False
scene . objects [ ' Terrain ' ] [ ' thread_run ' ] = False
# Commandes
scene . objects [ ' Pause ' ] . setVisible ( False , False )
2022-03-20 05:57:07 +01:00
scene . objects [ ' Pause-Hl ' ] . setVisible ( False , False )
2022-03-11 00:21:00 +01:00
scene . objects [ ' Run ' ] . setVisible ( True , False )
scene . objects [ ' Stop ' ] . setVisible ( False , False )
scene . objects [ ' Stop-Hl ' ] . setVisible ( False , False )
2022-03-10 01:08:08 +01:00
2022-03-29 14:12:58 +02:00
# Affichage des résultats
if scene . objects [ ' End ' ] [ ' timer ' ] == 0 :
scene . objects [ ' Terrain ' ] [ ' manip_mode ' ] = 9 # Fenêtre modale
2022-04-22 16:54:49 +02:00
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']
2022-04-06 06:25:03 +02:00
# Wave
2022-04-06 17:28:16 +02:00
if scene . objects [ ' Points ' ] [ ' wave ' ] == scene . objects [ ' Terrain ' ] [ ' nb_waves ' ] and scene . objects [ ' Points ' ] [ ' lifes ' ] > 0 :
2022-04-06 06:25:03 +02:00
scene . objects [ ' Endbanner_wave ' ] [ ' Text ' ] = " Victory "
else :
scene . objects [ ' Endbanner_wave ' ] [ ' Text ' ] = " Wave \n " + str ( scene . objects [ ' Points ' ] [ ' wave ' ] )
# Time
2022-03-29 14:12:58 +02:00
maptime_mn = time . localtime ( ) . tm_min - scene . objects [ ' Points ' ] [ ' time_begin ' ] . tm_min
maptime_sec = time . localtime ( ) . tm_sec - scene . objects [ ' Points ' ] [ ' time_begin ' ] . tm_sec
if maptime_sec < 0 :
maptime_mn - = 1
maptime_sec + = 60
if maptime_mn < = 99 :
2022-04-03 06:21:42 +02:00
scene . objects [ ' Endbanner_points ' ] [ ' Text ' ] = " Level " + str ( scene . objects [ ' Points ' ] [ ' level_max ' ] ) + " \n Time " + str ( maptime_mn ) + " ' " + str ( maptime_sec ) + " \" "
2022-03-29 14:12:58 +02:00
else :
2022-04-03 06:21:42 +02:00
scene . objects [ ' Endbanner_points ' ] [ ' Text ' ] = " Level " + str ( scene . objects [ ' Points ' ] [ ' level_max ' ] ) + " \n Time " + str ( maptime_mn ) + " ' "
2022-04-06 06:25:03 +02:00
# Object 3D
2022-03-29 14:12:58 +02:00
scene . objects [ ' Endbanner ' ] . color = color_endbanner_bluelight
2022-04-03 06:21:42 +02:00
scene . objects [ ' Endbanner_wave ' ] . color = color_black
scene . objects [ ' Endbanner_points ' ] . color = color_black
scene . objects [ ' Endbanner_ok ' ] . color = color_black
2022-03-29 14:12:58 +02:00
scene . objects [ ' End ' ] . setVisible ( True , True )
2022-04-11 22:42:09 +02:00
scene . objects [ ' End ' ] . worldPosition = [ 0 , 1.53623 , - 1.8 ]
scene . objects [ ' End ' ] [ ' timer ' ] = 0
scene . objects [ ' End ' ] [ ' anim ' ] = True
2022-04-06 06:25:03 +02:00
# Animation
2022-03-29 14:12:58 +02:00
pas = 0.5
2022-04-11 22:42:09 +02:00
scene . objects [ ' End ' ] . localPosition . y - = pas
scene . objects [ ' End ' ] . localPosition . z + = ( 0.85 * pas )
# scene.objects['End'].localPosition.y=scene.objects['End'].localPosition.y-1*pas
# scene.objects['End'].localPosition.z=scene.objects['End'].localPosition.z+0.85*pas
2022-04-03 06:21:42 +02:00
scene . objects [ ' End ' ] [ ' timer ' ] + = 1
2022-03-29 14:12:58 +02:00
if scene . objects [ ' End ' ] [ ' timer ' ] == 40 :
scene . objects [ ' Terrain ' ] [ ' map_run ' ] = False
# Vitesse du jeu
def terrain_speed ( obj ) :
2022-04-22 16:54:49 +02:00
sound_play ( sndbuff_click )
2022-03-29 14:12:58 +02:00
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 ' ] )
2022-04-16 05:43:32 +02:00
# Affichage
2022-03-29 14:12:58 +02:00
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 des Nears (Towers) et des Steerings (Minions)
for obj_i in scene . objects :
if " type_tower " in obj_i . getPropertyNames ( ) and " Near " in obj_i . sensors :
2022-03-31 18:26:55 +02:00
obj_i . sensors [ ' Near ' ] . skippedTicks = round ( 1 / ( obj_i [ ' speed ' ] * scene . objects [ ' Terrain ' ] [ ' speed ' ] ) )
2022-04-11 22:42:09 +02:00
if scene . objects [ ' Terrain ' ] [ ' run ' ] == True : # Pas en pause
if " type_minion " in obj_i . getPropertyNames ( ) and " type_towerminion " not in obj_i . getPropertyNames ( ) :
obj_i . actuators [ ' Steering ' ] . velocity = obj_i [ ' speed_base ' ] * scene . objects [ ' Terrain ' ] [ ' speed ' ]
2022-03-29 14:12:58 +02:00
# Configuration du moteur de rendu
2022-04-06 17:28:16 +02:00
if scene . objects [ ' Terrain ' ] [ ' speed ' ] < 10 : # smaa avec en vitesse 4 et 10 -> tendance au plantage
2022-03-29 14:12:58 +02:00
eevee . use_eevee_smaa = True
else :
eevee . use_eevee_smaa = False
# Maj du fichier de config (vitesse du jeu : data/config/speed)
ct_config_tree [ 0 ] [ 0 ] . text = str ( scene . objects [ ' Terrain ' ] [ ' speed ' ] )
buffer_xml = ET . tostring ( ct_config_tree )
with open ( " ct_config.xml " , " wb " ) as f :
f . write ( buffer_xml )
2022-04-22 16:54:49 +02:00
# Highlight de la page de fin
2022-03-29 14:12:58 +02:00
def endbanner_hl ( cont ) :
if cont . sensors [ ' MO ' ] . status == JUST_ACTIVATED :
2022-04-12 01:45:23 +02:00
if scene . objects [ ' Mouse_main ' ] [ ' mouse_graphic ' ] :
mouse_up ( )
2022-03-29 14:12:58 +02:00
scene . objects [ ' Endbanner ' ] . color = color_white
2022-04-03 06:21:42 +02:00
scene . objects [ ' Endbanner_wave ' ] . color = color_white
scene . objects [ ' Endbanner_points ' ] . color = color_white
scene . objects [ ' Endbanner_ok ' ] . color = color_white
2022-03-29 14:12:58 +02:00
if cont . sensors [ ' MO ' ] . status == JUST_RELEASED :
2022-04-12 01:45:23 +02:00
if scene . objects [ ' Mouse_main ' ] [ ' mouse_graphic ' ] :
mouse_down ( )
2022-03-29 14:12:58 +02:00
scene . objects [ ' Endbanner ' ] . color = color_endbanner_bluelight
2022-04-03 06:21:42 +02:00
scene . objects [ ' Endbanner_wave ' ] . color = color_black
scene . objects [ ' Endbanner_points ' ] . color = color_black
scene . objects [ ' Endbanner_ok ' ] . color = color_black
2022-03-29 14:12:58 +02:00
2022-04-22 16:54:49 +02:00
# Fermer la page de fin
def endbanner_close ( ) :
sound_play ( sndbuff_click )
scene . objects [ ' Terrain ' ] [ ' manip_mode ' ] = 0
scene . objects [ ' End ' ] . setVisible ( False , True )
scene . objects [ ' End ' ] . worldPosition = [ 20 , 1.53623 , - 0.892838 ] # Position dans Blender [20, 1.53623, -0.892838]
scene . objects [ ' Endbanner ' ] . color = [ 0.592 , 0.68 , 0.407 , 1 ]
scene . objects [ ' Endbanner_wave ' ] . color = color_black
scene . objects [ ' Endbanner_points ' ] . color = color_black
scene . objects [ ' End ' ] [ ' timer ' ] = 0
# Click pour fermer la page de fin
def endbanner_close_click ( cont ) :
2022-03-29 14:12:58 +02:00
if cont . sensors [ ' Click ' ] . status == JUST_ACTIVATED and cont . sensors [ ' MO ' ] . positive :
2022-04-22 16:54:49 +02:00
endbanner_close ( )
###############################################################################
# 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
2022-04-22 17:51:58 +02:00
scene . objects [ ' Cmd_text ' ] [ ' Text ' ] = " Mute "
2022-04-22 16:54:49 +02:00
scene . objects [ ' Cmd_text ' ] . setVisible ( True , False )
# Maj du fichier de config (sound : data/config/sound -> [0][1].text)
ct_config_tree [ 0 ] [ 1 ] . text = str ( scene . objects [ ' Commands ' ] [ ' sound ' ] )
buffer_xml = ET . tostring ( ct_config_tree )
with open ( " ct_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
2022-04-22 17:51:58 +02:00
scene . objects [ ' Cmd_text ' ] [ ' Text ' ] = " Unmute "
2022-04-22 16:54:49 +02:00
scene . objects [ ' Cmd_text ' ] . setVisible ( True , False )
# Maj du fichier de config (sound : data/config/sound -> [0][1].text)
ct_config_tree [ 0 ] [ 1 ] . text = str ( scene . objects [ ' Commands ' ] [ ' sound ' ] )
buffer_xml = ET . tostring ( ct_config_tree )
with open ( " ct_config.xml " , " wb " ) as f :
f . write ( buffer_xml )
2022-03-29 14:12:58 +02:00
2022-03-10 01:08:08 +01:00
###############################################################################
# Commandes
###############################################################################
2022-03-29 14:12:58 +02:00
2022-04-22 16:54:49 +02:00
color_cmd = ( 0.8 , 0.8 , 0.8 , 1 ) # Blanc
color_cmd_hl = ( 0.8 , 0.619 , 0.021 , 1 ) # Jaune
2022-03-10 01:08:08 +01:00
# Init
def cmd_init ( ) :
2022-04-11 22:42:09 +02:00
2022-04-22 16:54:49 +02:00
# UI : Commands
2022-03-10 01:08:08 +01:00
scene . objects [ ' Run-Hl ' ] . setVisible ( False , False )
scene . objects [ ' Pause ' ] . setVisible ( False , False )
2022-04-16 05:43:32 +02:00
scene . objects [ ' Pause ' ] . suspendPhysics ( )
2022-03-10 01:08:08 +01:00
scene . objects [ ' Pause-Hl ' ] . setVisible ( False , False )
scene . objects [ ' Stop ' ] . setVisible ( False , False )
2022-04-16 05:43:32 +02:00
scene . objects [ ' Stop ' ] . suspendPhysics ( )
2022-03-10 01:08:08 +01:00
scene . objects [ ' Stop-Hl ' ] . setVisible ( False , False )
2022-03-11 00:21:00 +01:00
scene . objects [ ' Construc-Hl ' ] . setVisible ( False , False )
2022-04-22 02:04:48 +02:00
scene . objects [ ' Book-cmd-Hl ' ] . setVisible ( False , False )
2022-04-22 17:51:58 +02:00
scene . objects [ ' ResetView-Hl ' ] . setVisible ( False , False )
2022-04-03 21:15:43 +02:00
scene . objects [ ' About-cmd-Hl ' ] . setVisible ( False , False )
2022-04-06 06:25:03 +02:00
scene . objects [ ' About ' ] . setVisible ( False , True )
2022-03-11 00:21:00 +01:00
2022-04-22 16:54:49 +02:00
# UI : Sounds
# Read config (sound : data/config/sound -> [0][1].text)
if ct_config_tree [ 0 ] [ 1 ] . text == " True " :
sound_set ( )
else :
sound_unset ( )
audiodev . unlock ( )
# UI : Text, ...
2022-04-16 05:43:32 +02:00
scene . objects [ ' Cmd_text ' ] . setVisible ( False , False )
2022-03-20 05:57:07 +01:00
scene . objects [ ' Map_text ' ] . setVisible ( False , False )
2022-03-23 00:27:11 +01:00
scene . objects [ ' Text_info-1 ' ] . setVisible ( False , False )
scene . objects [ ' Text_info-2 ' ] . setVisible ( False , False )
2022-03-29 14:12:58 +02:00
scene . objects [ ' Tower_construc_mode ' ] . setVisible ( False , False )
scene . objects [ ' Terrain ' ] [ ' map_run ' ] = False
2022-03-20 05:57:07 +01:00
2022-04-11 22:42:09 +02:00
# UI : Mouse
# Window size : 738.5 415.5
2022-04-12 01:45:23 +02:00
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 )
2022-04-11 22:42:09 +02:00
2022-04-22 16:54:49 +02:00
# Speed
# Read config (game speed : data/config/speed -> [0][0].text)
2022-03-23 00:27:11 +01:00
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 ( ct_config_tree [ 0 ] [ 0 ] . text )
i = speed_mode . index ( scene . objects [ ' Terrain ' ] [ ' speed ' ] )
scene . objects [ ' Text_speed ' ] [ ' Text ' ] = speed_mode_txt [ i ]
2022-04-22 16:54:49 +02:00
# Highlight des commandes
2022-03-10 01:08:08 +01:00
def cmd_hl ( cont ) :
obj = cont . owner
# Activation
if cont . sensors [ ' MO ' ] . status == JUST_ACTIVATED and scene . objects [ ' Terrain ' ] [ ' manip_mode ' ] == 0 :
2022-04-22 16:54:49 +02:00
if obj . name != " Run " and obj . name != " Pause " and obj . name != " Stop " :
2022-03-10 01:08:08 +01:00
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 )
2022-04-16 05:43:32 +02:00
# Text
text_hl = { " Run " : " Run (F5) " ,
" Stop " : " Stop (F6) " ,
" Pause " : " Pause (F5) " ,
" Construc " : " Show tower position " ,
2022-04-22 17:51:58 +02:00
" ResetView " : " Reset view (Home key) " ,
2022-04-22 02:04:48 +02:00
" Book-cmd " : " Documentation " ,
2022-04-16 05:43:32 +02:00
" About-cmd " : " About " ,
" Speed_down " : " Speed down (-) " ,
2022-04-22 16:54:49 +02:00
" Speed_up " : " Speed up (+) " ,
2022-04-22 17:51:58 +02:00
" Sound-cmd " : " Mute " ,
" NoSound-cmd " : " Unmute " }
2022-04-22 16:54:49 +02:00
text = text_hl [ obj . name ]
if obj . name == " Construc " and scene . objects [ ' Terrain ' ] [ ' construct_mode ' ] == True : # Bascule le construct
text = " Hide tower position "
scene . objects [ ' Cmd_text ' ] [ ' Text ' ] = text
2022-04-16 05:43:32 +02:00
scene . objects [ ' Cmd_text ' ] . setVisible ( True , False )
2022-03-10 01:08:08 +01:00
# Désactivation
2022-04-11 22:42:09 +02:00
if cont . sensors [ ' MO ' ] . status == JUST_RELEASED and ( scene . objects [ ' Terrain ' ] [ ' manip_mode ' ] == 0 or scene . objects [ ' Terrain ' ] [ ' manip_mode ' ] == 9 ) :
2022-04-22 16:54:49 +02:00
if obj . name != " Run " and obj . name != " Pause " and obj . name != " Stop " and obj . name != " Sound-cmd " and obj . name != " NoSound-cmd " :
2022-03-10 01:08:08 +01:00
scene . objects [ obj . name + ' -Hl ' ] . setVisible ( False , True )
obj . setVisible ( True , True )
2022-04-22 16:54:49 +02:00
scene . objects [ ' Cmd_text ' ] . setVisible ( False , False )
2022-03-10 01:08:08 +01:00
# 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 )
2022-04-22 16:54:49 +02:00
scene . objects [ ' Cmd_text ' ] . setVisible ( False , False )
2022-03-10 01:08:08 +01:00
else :
scene . objects [ ' Run-Hl ' ] . setVisible ( False , False )
scene . objects [ ' Run ' ] . setVisible ( True , False )
2022-04-22 16:54:49 +02:00
scene . objects [ ' Cmd_text ' ] . setVisible ( False , False )
2022-03-10 01:08:08 +01:00
# 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 )
2022-04-22 16:54:49 +02:00
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 )
2022-03-10 01:08:08 +01:00
2022-03-29 14:12:58 +02:00
# 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 :
2022-04-22 16:54:49 +02:00
if obj . name == " Pause " or obj . name == " Run " :
2022-03-29 14:12:58 +02:00
terrain_run ( )
if obj . name == " Stop " :
terrain_stop ( )
if obj . name == " Speed_up " or obj . name == " Speed_down " :
terrain_speed ( obj )
2022-04-22 17:51:58 +02:00
if obj . name == " ResetView " :
manip_reset ( )
2022-04-22 16:54:49 +02:00
if obj . name == " Sound-cmd " :
sound_unset ( )
if obj . name == " NoSound-cmd " :
sound_set ( )
2022-04-22 17:51:58 +02:00
if obj . name == " About-cmd " :
about_open ( )
2022-03-29 14:12:58 +02:00
2022-03-10 01:08:08 +01:00
###############################################################################
# Gestion du clavier
###############################################################################
2022-03-29 14:12:58 +02:00
# Mode :
# 0 : rien (par défaut)
# 1 : Pan avec Shift
# 2 : Zoom avec Ctrl
# 9 : Fenêtre modale
2022-03-10 01:08:08 +01:00
def mode ( cont ) :
obj = cont . owner
keyboard = bge . logic . keyboard
2022-04-11 22:42:09 +02:00
2022-04-06 06:25:03 +02:00
# Touche ESC
if JUST_ACTIVATED in keyboard . inputs [ bge . events . ESCKEY ] . queue :
2022-04-22 16:54:49 +02:00
# Fenêtres modales
if scene . objects [ ' Terrain ' ] [ ' manip_mode ' ] == 9 :
if scene . objects [ ' Book ' ] . visible :
doc_close ( )
if scene . objects [ ' About ' ] . visible :
about_close ( )
if scene . objects [ ' End ' ] . visible :
endbanner_close ( )
return
else : # Sortir du jeux
terrain_stop ( )
bge . logic . endGame ( )
2022-04-06 06:25:03 +02:00
2022-04-22 16:54:49 +02:00
# Fenêtre modale (inhibition des touches hors ESC)
2022-04-06 06:25:03 +02:00
if scene . objects [ ' Terrain ' ] [ ' manip_mode ' ] == 9 :
return
2022-03-10 01:08:08 +01:00
# 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 :
2022-04-22 16:54:49 +02:00
manip_reset ( )
2022-04-11 22:42:09 +02:00
2022-04-12 01:45:23 +02:00
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
2022-03-10 01:08:08 +01:00
# Touche F5 -> Run et Pause
if JUST_ACTIVATED in keyboard . inputs [ bge . events . F5KEY ] . queue :
2022-03-29 14:12:58 +02:00
terrain_run ( )
2022-03-23 00:27:11 +01:00
2022-03-10 01:08:08 +01:00
# Touche F6 -> Stop / Init
if JUST_ACTIVATED in keyboard . inputs [ bge . events . F6KEY ] . queue :
if scene . objects [ ' Terrain ' ] [ ' thread_run ' ] == True :
2022-03-29 14:12:58 +02:00
terrain_stop ( )
2022-03-10 01:08:08 +01:00
2022-04-03 06:21:42 +02:00
# 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 ' ] )
2022-03-10 01:08:08 +01:00
###############################################################################
2022-03-27 10:28:03 +02:00
# Manipulation 3D de la scène
2022-03-10 01:08:08 +01:00
###############################################################################
# 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
2022-04-11 22:42:09 +02:00
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
2022-03-10 01:08:08 +01:00
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
2022-04-22 16:54:49 +02:00
scene . objects [ ' Commands ' ] [ ' init_lx ' ] = scene . objects [ ' Commands ' ] . worldPosition . x
scene . objects [ ' Commands ' ] [ ' init_ly ' ] = scene . objects [ ' Commands ' ] . worldPosition . y
scene . objects [ ' Commands ' ] [ ' init_lz ' ] = scene . objects [ ' Commands ' ] . worldPosition . z
scene . objects [ ' Points ' ] [ ' init_lx ' ] = scene . objects [ ' Points ' ] . worldPosition . x
scene . objects [ ' Points ' ] [ ' init_ly ' ] = scene . objects [ ' Points ' ] . worldPosition . y
scene . objects [ ' Points ' ] [ ' init_lz ' ] = scene . objects [ ' Points ' ] . worldPosition . z
2022-04-24 04:36:43 +02:00
scene . objects [ ' Map_text ' ] [ ' init_relativ_lx ' ] = scene . objects [ ' Map_text ' ] . worldPosition . x - scene . objects [ ' Points ' ] . worldPosition . x
scene . objects [ ' Map_text ' ] [ ' init_relativ_ly ' ] = scene . objects [ ' Map_text ' ] . worldPosition . y - scene . objects [ ' Points ' ] . worldPosition . y
scene . objects [ ' Map_text ' ] [ ' init_relativ_lz ' ] = scene . objects [ ' Map_text ' ] . worldPosition . z - scene . objects [ ' Points ' ] . worldPosition . z
2022-03-10 01:08:08 +01:00
# 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
2022-04-22 16:54:49 +02:00
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 ' ]
scene . objects [ ' Commands ' ] . worldPosition . x = scene . objects [ ' Commands ' ] [ ' init_lx ' ]
scene . objects [ ' Commands ' ] . worldPosition . y = scene . objects [ ' Commands ' ] [ ' init_ly ' ]
scene . objects [ ' Commands ' ] . worldPosition . z = scene . objects [ ' Commands ' ] [ ' init_lz ' ]
scene . objects [ ' Points ' ] . worldPosition . x = scene . objects [ ' Points ' ] [ ' init_lx ' ]
scene . objects [ ' Points ' ] . worldPosition . y = scene . objects [ ' Points ' ] [ ' init_ly ' ]
scene . objects [ ' Points ' ] . worldPosition . z = scene . objects [ ' Points ' ] [ ' init_lz ' ]
applyRotationTo ( scene . objects [ ' Terrain ' ] , 0 , 0 , 0 )
scene . objects [ ' Cmd_text ' ] [ ' Text ' ] = " "
2022-03-10 01:08:08 +01:00
# Position de départ pour la manipulation de la vue
def manip_start ( cont ) :
2022-03-30 03:10:33 +02:00
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 ]
2022-03-10 01:08:08 +01:00
# 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 )
2022-04-22 16:54:49 +02:00
scene . objects [ ' Commands ' ] . 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 )
scene . objects [ ' Points ' ] . 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 )
2022-04-12 01:45:23 +02:00
if scene . objects [ ' Mouse_main ' ] [ ' mouse_graphic ' ] :
scene . objects [ ' Mouse_main ' ] . applyMovement ( ( delta_x * - sensibilite_pan , delta_y * sensibilite_pan , 0 ) , True )
2022-03-10 01:08:08 +01:00
2022-04-11 22:42:09 +02:00
# Zoom FIXME : marche pas au niveau de la souris
2022-03-10 01:08:08 +01:00
if obj [ ' manip_mode ' ] == 2 : # Ctrl
2022-04-12 01:45:23 +02:00
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
2022-03-10 01:08:08 +01:00
scene . objects [ ' Camera ' ] . applyMovement ( ( 0 , 0 , ( delta_x + delta_y ) * sensibilite_zoom ) , True )
2022-04-12 01:45:23 +02:00
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
2022-04-11 22:42:09 +02:00
scene . objects [ ' Camera ' ] [ ' past_ly ' ] = scene . objects [ ' Camera ' ] . worldPosition . y
scene . objects [ ' Camera ' ] [ ' past_lz ' ] = scene . objects [ ' Camera ' ] . worldPosition . z
2022-03-10 01:08:08 +01:00
# Manipulation du modèle ou de la caméra
def manip_wheel ( cont ) :
2022-03-30 03:10:33 +02:00
if scene . objects [ ' Terrain ' ] [ ' manip_mode ' ] != 9 : # Fenêtre modale
obj = cont . owner
sensibilite_wheel = 5 # Base : 20
2022-04-12 01:45:23 +02:00
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
2022-03-30 03:10:33 +02:00
if cont . sensors [ ' WheelUp ' ] . positive :
scene . objects [ ' Camera ' ] . applyMovement ( ( 0 , 0 , - sensibilite_wheel ) , True )
2022-04-12 01:45:23 +02:00
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
2022-04-11 22:42:09 +02:00
elif cont . sensors [ ' WheelDown ' ] . positive :
2022-03-30 03:10:33 +02:00
scene . objects [ ' Camera ' ] . applyMovement ( ( 0 , 0 , sensibilite_wheel ) , True )
2022-04-12 01:45:23 +02:00
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
2022-04-11 22:42:09 +02:00
else :
return
2022-03-10 01:08:08 +01:00
2022-04-11 22:42:09 +02:00
# Icone de la souris
def mouse ( cont ) :
2022-04-12 01:45:23 +02:00
if scene . objects [ ' Mouse_main ' ] [ ' mouse_graphic ' ] == False :
return
2022-04-11 22:42:09 +02:00
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 ]
2022-04-22 02:04:48 +02:00
###############################################################################
# Documentation
###############################################################################
2022-04-22 16:54:49 +02:00
color_doc_chap = ( 0.153 , 0.116 , 0.105 , 1 ) # WoodDark
color_doc_fct = ( 0.326 , 0.101 , 0.0592 , 1 ) # BrownDark
color_doc_hl = ( 0.13 , 0.254 , 0.407 , 1 ) # BlueDark
color_doc_activate = ( 1.0 , 0.025 , 0.116 , 1 ) # Red
# Ouvrir le livre
2022-04-22 02:04:48 +02:00
def doc ( cont ) :
if cont . sensors [ ' Click ' ] . status == JUST_ACTIVATED and cont . sensors [ ' MO ' ] . positive and scene . objects [ ' Terrain ' ] [ ' manip_mode ' ] == 0 :
scene . objects [ ' Terrain ' ] [ ' manip_mode ' ] = 9 # Fenêtre modale
2022-04-22 16:54:49 +02:00
sound_play ( sndbuff_book_open )
manip_reset ( )
2022-04-22 02:04:48 +02:00
scene . objects [ ' Book ' ] . worldPosition = [ 0 , - 22 , 16.5 ]
scene . objects [ ' Book_close ' ] . color = color_doc_chap
scene . objects [ ' Book_chap-screen ' ] . color = color_doc_chap
scene . objects [ ' Book_chap-map ' ] . color = color_doc_chap
scene . objects [ ' Book_chap-tower ' ] . color = color_doc_chap
scene . objects [ ' Book_chap-tech ' ] . color = color_doc_chap
scene . objects [ ' Book_chap-spell ' ] . color = color_doc_chap
scene . objects [ ' Book_close ' ] . color = color_doc_chap
scene . objects [ ' Book ' ] . setVisible ( True , True )
# Tout effacer
for i in range ( 1 , 13 ) : # Tout effacer
if i < 10 :
name_fct = " Book_fct-0 " + str ( i )
else :
name_fct = " Book_fct- " + str ( i )
scene . objects [ name_fct ] . setVisible ( False , False )
scene . objects [ name_fct ] . color = color_doc_fct
scene . objects [ ' Book_text_title ' ] . setVisible ( False , True )
scene . objects [ ' Book_text ' ] . setVisible ( False , True )
# Activer la page screen
scene . objects [ " Book " ] [ ' page_chap ' ] = " Book_chap-screen "
scene . objects [ " Book_chap-screen " ] . color = color_doc_activate
scene . objects [ ' Book_page_screen ' ] . worldPosition = scene . objects [ ' Book ' ] . worldPosition
scene . objects [ ' Book_page_screen ' ] . setVisible ( True , True )
2022-04-22 16:54:49 +02:00
# Fermer le livre
def doc_close ( ) :
sound_play ( sndbuff_book_close )
scene . objects [ ' Terrain ' ] [ ' manip_mode ' ] = 0 # Fenêtre modale
scene . objects [ ' Book_page_screen ' ] . setVisible ( False , True )
scene . objects [ ' Book ' ] . setVisible ( False , True )
scene . objects [ ' Book ' ] . worldPosition = [ 28 , 0.84549 , 1.53626 ] # Position dans Blender [28, 0.84549, 1.53626]
scene . objects [ ' Book_page_screen ' ] . worldPosition = scene . objects [ ' Book ' ] . worldPosition
# Click pour fermer le livre
def doc_close_click ( cont ) :
2022-04-22 02:04:48 +02:00
if cont . sensors [ ' Click ' ] . status == JUST_ACTIVATED and cont . sensors [ ' MO ' ] . positive :
2022-04-22 16:54:49 +02:00
doc_close ( )
2022-04-22 02:04:48 +02:00
2022-04-22 16:54:49 +02:00
# Highlight du livre
2022-04-22 02:04:48 +02:00
def doc_hl ( cont ) :
if cont . sensors [ ' MO ' ] . status == JUST_ACTIVATED :
obj = cont . owner
name = obj . name [ : - 7 ]
scene . objects [ name ] . color = color_doc_hl
if name == scene . objects [ ' Book ' ] [ ' page_chap ' ] or name == scene . objects [ ' Book ' ] [ ' page_fct ' ] :
scene . objects [ name ] . color = color_doc_activate
else :
scene . objects [ name ] . color = color_doc_hl
if cont . sensors [ ' MO ' ] . status == JUST_RELEASED :
obj = cont . owner
name = obj . name [ : - 7 ]
if name == scene . objects [ ' Book ' ] [ ' page_chap ' ] or name == scene . objects [ ' Book ' ] [ ' page_fct ' ] :
scene . objects [ name ] . color = color_doc_activate
else :
if name [ 5 ] == " c " :
scene . objects [ name ] . color = color_doc_chap
else :
scene . objects [ name ] . color = color_doc_fct
2022-03-10 01:08:08 +01:00
###############################################################################
2022-04-03 06:21:42 +02:00
# About
2022-03-10 01:08:08 +01:00
###############################################################################
2022-04-22 16:54:49 +02:00
color_link = ( 0.799 , 0.617 , 0.021 , 1 ) # Jaune
color_link_hl = ( 0.8 , 0.8 , 0.8 , 1 ) # Blanc
# Ouvrir le about
2022-04-03 06:21:42 +02:00
def about_open ( ) :
2022-04-11 22:42:09 +02:00
scene . objects [ ' Terrain ' ] [ ' manip_mode ' ] = 9 # Fenêtre modale
2022-04-22 16:54:49 +02:00
manip_reset ( )
2022-04-03 21:15:43 +02:00
scene . objects [ ' About_title ' ] . color = color_black
scene . objects [ ' About_text ' ] . color = color_black
scene . objects [ ' About_copyright ' ] . color = color_black
2022-04-03 06:21:42 +02:00
scene . objects [ ' About_link_git ' ] . color = color_link
scene . objects [ ' About_link_gpl ' ] . color = color_link
2022-04-22 02:04:48 +02:00
# scene.objects['About_link_blender'].color= color_link
2022-04-03 06:21:42 +02:00
scene . objects [ ' About_link_upbge ' ] . color = color_link
scene . objects [ ' About_link_kay ' ] . color = color_link
scene . objects [ ' About_link_kenney ' ] . color = color_link
2022-04-22 02:04:48 +02:00
scene . objects [ ' About_ok ' ] . color = color_link
2022-04-03 06:21:42 +02:00
scene . objects [ ' About ' ] . setVisible ( True , True )
2022-04-11 22:42:09 +02:00
scene . objects [ ' About ' ] . worldPosition = [ 0 , 1.53623 , - 1.8 ] # old [0, 1.53623, -0.892838]
2022-04-03 06:21:42 +02:00
scene . objects [ ' About ' ] [ ' timer ' ] = 0
scene . objects [ ' About ' ] [ ' anim ' ] = True
2022-04-22 16:54:49 +02:00
# Animation du about
2022-04-03 06:21:42 +02:00
def about_open_anim ( ) :
pas = 0.5
2022-04-11 22:42:09 +02:00
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
2022-04-03 06:21:42 +02:00
scene . objects [ ' About ' ] [ ' timer ' ] + = 1
if scene . objects [ ' About ' ] [ ' timer ' ] == 40 :
scene . objects [ ' About ' ] [ ' anim ' ] = False
2022-04-22 16:54:49 +02:00
# Highlight du about
2022-04-03 06:21:42 +02:00
def about_hl ( cont ) :
2022-04-11 22:42:09 +02:00
# decal = 18
# size_scale = 0.2
2022-04-03 06:21:42 +02:00
if cont . sensors [ ' MO ' ] . status == JUST_ACTIVATED :
2022-04-12 01:45:23 +02:00
if scene . objects [ ' Mouse_main ' ] [ ' mouse_graphic ' ] :
mouse_up ( )
2022-04-11 22:42:09 +02:00
# # 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
2022-04-03 06:21:42 +02:00
scene . objects [ ' Aboutbanner ' ] . color = color_white
2022-04-03 21:15:43 +02:00
scene . objects [ ' About_ok ' ] . color = color_white
scene . objects [ ' About_title ' ] . color = color_white
2022-04-11 22:42:09 +02:00
# scene.objects['Mouse_main'].worldScale=[8, 8, 8]
# print("about_hl A après", scene.objects['Mouse_main'].worldPosition)
2022-04-03 06:21:42 +02:00
if cont . sensors [ ' MO ' ] . status == JUST_RELEASED :
2022-04-11 22:42:09 +02:00
# print("about_hl R avant", scene.objects['Mouse_main'].worldPosition)
2022-04-12 01:45:23 +02:00
if scene . objects [ ' Mouse_main ' ] [ ' mouse_graphic ' ] :
mouse_down ( )
2022-04-11 22:42:09 +02:00
# 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
2022-04-03 06:21:42 +02:00
scene . objects [ ' Aboutbanner ' ] . color = color_endbanner_bluelight
2022-04-22 02:04:48 +02:00
scene . objects [ ' About_ok ' ] . color = color_link
2022-04-03 21:15:43 +02:00
scene . objects [ ' About_title ' ] . color = color_black
2022-04-11 22:42:09 +02:00
# scene.objects['Mouse_main'].worldScale=[30, 30, 30]
# print("about_hl R après", scene.objects['Mouse_main'].worldPosition)
2022-04-03 06:21:42 +02:00
2022-04-22 16:54:49 +02:00
# 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 ) :
2022-04-03 06:21:42 +02:00
if cont . sensors [ ' Click ' ] . status == JUST_ACTIVATED and cont . sensors [ ' MO ' ] . positive :
2022-04-22 16:54:49 +02:00
about_close ( )
2022-04-03 06:21:42 +02:00
2022-04-22 16:54:49 +02:00
# Liens du about
2022-04-03 06:21:42 +02:00
def about_link ( cont ) :
if cont . sensors [ ' Click ' ] . status == JUST_ACTIVATED and cont . sensors [ ' MO ' ] . positive :
obj = cont . owner
2022-04-22 02:04:48 +02:00
name = obj . name [ : - 7 ]
link = {
' About_link_git ' : ' https://gitlab.com/phroy/codetower ' ,
' 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 ] )
2022-04-03 06:21:42 +02:00
2022-04-11 22:42:09 +02:00
# FIXME: souris graphique trop compliqué
2022-04-03 06:21:42 +02:00
def about_link_hl ( cont ) :
2022-04-11 22:42:09 +02:00
decal = 15
2022-04-03 06:21:42 +02:00
if cont . sensors [ ' MO ' ] . status == JUST_ACTIVATED :
obj = cont . owner
name = obj . name [ : - 7 ]
scene . objects [ name ] . color = color_link_hl
2022-04-12 01:45:23 +02:00
if scene . objects [ ' Mouse_main ' ] [ ' mouse_graphic ' ] :
mouse_up ( )
2022-04-11 22:42:09 +02:00
2022-04-03 06:21:42 +02:00
if cont . sensors [ ' MO ' ] . status == JUST_RELEASED :
obj = cont . owner
name = obj . name [ : - 7 ]
scene . objects [ name ] . color = color_link
2022-04-12 01:45:23 +02:00
if scene . objects [ ' Mouse_main ' ] [ ' mouse_graphic ' ] :
mouse_down ( )