mirror of
https://forge.apps.education.fr/phroy/codetower.git
synced 2024-01-27 11:35:17 +01:00
162 lines
5.5 KiB
Python
162 lines
5.5 KiB
Python
import bge # Bibliothèque Blender Game Engine (UPBGE)
|
|
import ct # Bibliothèque CodeTower
|
|
import threading # Multithreading
|
|
import trace
|
|
import sys
|
|
import time
|
|
import mathutils
|
|
|
|
###############################################################################
|
|
# ct_lib.py
|
|
# @title: Bibliothèque utilisateur
|
|
# @project: CodeTower
|
|
# @lang: fr
|
|
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
|
|
# @copyright: Copyright (C) 2022 Philippe Roy
|
|
# @license: GNU GPL
|
|
#
|
|
# Commandes déclenchées par les joueurs : ct_*
|
|
# Commandes déclenchées par la scene 3D : scn_*
|
|
#
|
|
###############################################################################
|
|
|
|
# # Couleurs
|
|
|
|
# couleur_magenta = [0.800, 0.005, 0.315,1] # bouton non activable : magenta
|
|
# couleur_orange = [0.799, 0.130, 0.063,1] # bouton activable : orange
|
|
# couleur_blanc = [0.8, 0.8, 0.8, 1] # bouton focus : blanc
|
|
# couleur_jaune = [0.8, 0.619, 0.021, 1] # bouton activé : jaune
|
|
|
|
# couleur_cmd = [0.8, 0.8, 0.8, 1] # blanc
|
|
# couleur_cmd_hl = [0.8, 0.619, 0.021, 1] # jaune
|
|
# couleur_lien = [0.024, 0.006, 0.8, 1] # bleu
|
|
# couleur_lien_hl = [0.8, 0.005, 0.315, 1] # majenta
|
|
|
|
# Récupérer les objets 3D
|
|
scene = bge.logic.getCurrentScene()
|
|
# print("Objets de la scene : ", scene.objects)
|
|
|
|
###############################################################################
|
|
# 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()
|
|
print ("Thread",type_txt, "#", len(threads)-1, "ouvert.")
|
|
|
|
def thread_stop(threads, type_txt):
|
|
i=0
|
|
zombie_flag=False
|
|
for t in threads:
|
|
if not t.is_alive():
|
|
print ("Thread",type_txt, "#",i,"fermé.")
|
|
else:
|
|
print ("Thread",type_txt, "#",i,"encore ouvert ...")
|
|
t.kill()
|
|
t.join()
|
|
if not t.is_alive():
|
|
print ("Thread",type_txt, "#",i,"tué.")
|
|
else:
|
|
print ("Thread",type_txt, "#",i,"zombie ...")
|
|
zombie_flag=True
|
|
i +=1
|
|
if zombie_flag==False:
|
|
print ("Tous les threads",type_txt, "sont fermés.")
|
|
else:
|
|
print ("Il reste des threads",type_txt, "zombies.")
|
|
|
|
|
|
###############################################################################
|
|
# Vagues
|
|
###############################################################################
|
|
|
|
# Création d'un enemi
|
|
def ct_ufo(x,y):
|
|
ufo= scene.addObject("Ufo", scene.objects['Terrain'])
|
|
ufo.worldPosition=mathutils.Vector((x,y,0.37))
|
|
ufo.worldScale=mathutils.Vector((0.25,0.25,0.25))
|
|
ufo.actuators['Steering'].velocity=ufo['vitesse']
|
|
|
|
###############################################################################
|
|
# Tours
|
|
###############################################################################
|
|
|
|
# Création d'une tour
|
|
def ct_tour(x,y):
|
|
tour= scene.addObject("Tour", scene.objects['Terrain'])
|
|
tour.worldPosition=mathutils.Vector((x,y,0.2))
|
|
tour.worldScale=mathutils.Vector((1,1,1))
|
|
|
|
# Réaction d'une tour
|
|
def scn_tour_near(cont):
|
|
obj = cont.owner
|
|
sensor = obj.sensors['Near']
|
|
|
|
# Supression enemi
|
|
if len(sensor.hitObjectList)>0:
|
|
bullet= scene.addObject("Bullet", scene.objects['Terrain'], 60*2, False)
|
|
bullet.mass=0.001 # bullet.applyForce=((0,0,9.81),True)
|
|
bullet.worldPosition=mathutils.Vector((obj.worldPosition.x,obj.worldPosition.y,1.5))
|
|
# bullet.worldScale=mathutils.Vector((0.5,0.5,0.5))
|
|
bullet.worldScale=[0.5,0.5,0.5]
|
|
bullet.worldLinearVelocity.x = (sensor.hitObjectList[0].worldPosition.x-bullet.worldPosition.x)*bullet['vitesse']
|
|
bullet.worldLinearVelocity.y= (sensor.hitObjectList[0].worldPosition.y-bullet.worldPosition.y)*bullet['vitesse']
|
|
bullet.worldLinearVelocity.z = (sensor.hitObjectList[0].worldPosition.z+0.1-bullet.worldPosition.z)*bullet['vitesse']
|
|
|
|
# Fin de la vague
|
|
if len(sensor.hitObjectList)==0:
|
|
print ("fin vague")
|
|
if scene.objects['Terrain']['thread_vague']==False:
|
|
print ("fin vague2")
|
|
ct.cycle_fin ()
|
|
|
|
# # Positionnement tour
|
|
# def scn_tour_construct(cont):
|
|
# obj = cont.owner
|
|
# obj.worldScale=[1.25, 1.25, 1.25]
|
|
# obj.color = couleur_cmd_hl
|
|
|
|
|
|
###############################################################################
|
|
# Temporisation
|
|
###############################################################################
|
|
|
|
def tempo (duree):
|
|
time.sleep(duree)
|
|
|