information sur tour, tour erreur

This commit is contained in:
Philippe Roy 2022-03-23 00:27:11 +01:00
parent be79653f4e
commit 0195710cf8
7 changed files with 374 additions and 131 deletions

BIN
codetower-11.blend Normal file

Binary file not shown.

217
ct.py
View File

@ -3,8 +3,10 @@ import bge # Bibliothèque Blender Game Engine (UPBGE)
import math
import time
import sys
import os
import webbrowser
import threading # Multithreading
import xml.etree.ElementTree as ET # Creating/parsing XML file
import ct_map
@ -30,7 +32,11 @@ ct_cmd = importlib.import_module('ct_cmd')
# Récupérer la scène UPBGE
scene = bge.logic.getCurrentScene()
# scene_non_contruct = [] # Liste des tuiles non constructibles
# Récupérer le fichier de config
# print (os.getcwd())
ct_config = ET.parse('ct_config.xml')
ct_config_tree = ct_config.getroot()
# Couleurs
@ -39,6 +45,9 @@ 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_texte = [0, 0, 0, 1] # Noir
couleur_texte_rouge = [0.799, 0.031, 0.038, 1]
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
@ -52,11 +61,11 @@ ACTIVATE = bge.logic.KX_INPUT_ACTIVE
# JUST_DEACTIVATED = bge.logic.KX_SENSOR_JUST_DEACTIVATED
###############################################################################
# Construction
# Tour
###############################################################################
# Commande positionnement tour
def cmd_construct_tour(cont):
# Commande pour afficher la position de la tour de construction
def cmd_tower_construct(cont):
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:
@ -68,7 +77,7 @@ def cmd_construct_tour(cont):
obj_Hl.color = couleur_cmd
scene.objects['Tower_construc_mode'].setVisible(False,False)
scene.objects['Tower_construc_mode'].color = couleur_cmd
scene.objects['Text_info'].setVisible(False,False)
text_info ("")
else:
scene.objects['Terrain']['construct_mode']=True
obj.worldScale=[1.25, 1.25, 1.25]
@ -77,42 +86,79 @@ def cmd_construct_tour(cont):
obj_Hl.color = couleur_cmd_hl
scene.objects['Tower_construc_mode'].setVisible(True,False)
scene.objects['Tower_construc_mode'].color = couleur_cmd
scene.objects['Text_info'].setVisible(True,False)
scene.objects['Text_info']['Text']="Tower position : "
# Affiche position
def construct_tour_pos(cont):
obj = cont.owner
if scene.objects['Terrain']['construct_mode']==True:
hitObject = cont.sensors['MO'].hitObject
hitPosition = cont.sensors['MO'].hitPosition
if hitObject is not None :
if [round(hitPosition.x),round(hitPosition.y)] in scene.objects['Terrain']['scene_non_contruct']:
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
scene.objects['Text_info']['Text']="Tower position : "+str(round(hitPosition.x))+","+str(round(hitPosition.y))
text_info ("Tower position : ")
###############################################################################
# Terrain
# User interface : texte info et compteurs
###############################################################################
# Affichage sur la boite de texte sur 6 lignes
def text_info (texte):
if texte=="":
scene.objects['Text_info-1'].setVisible(False,False)
scene.objects['Text_info-2'].setVisible(False,False)
else:
lignes_txt=texte.split("\n", 6)
for i in range (len(lignes_txt),6):
lignes_txt.append("")
scene.objects['Text_info-1'].setVisible(True,False)
scene.objects['Text_info-2'].setVisible(True,False)
scene.objects['Text_info-1']['Text']=lignes_txt[0]+"\n"+lignes_txt[1]+"\n"+lignes_txt[2]
scene.objects['Text_info-2']['Text']=lignes_txt[3]+"\n"+lignes_txt[4]+"\n"+lignes_txt[5]
# Mise à jour de l'affichage des compteurs
def points_maj (cont):
# Affichage
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'])
# Level trop élevé
if scene.objects['Points']['level'] > scene.objects['Points']['level_max'] :
scene.objects['Level_text'].color = couleur_texte_rouge
if scene.objects['Level_text'].color == couleur_texte_rouge and scene.objects['Points']['level'] <= scene.objects['Points']['level_max']:
scene.objects['Level_text'].color = couleur_texte
# Fin de la vague
if scene.objects['Terrain']['thread_wave']==False:
if scene.objects['Terrain']['thread_wave']==False:
if scene.objects['Points']['minions']==0:
terrain_fin ()
###############################################################################
# Terrain
###############################################################################
# Mouse over du terrain
def scn_terrain_mo(cont):
# 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 :
if round(hitPosition.x) >= scene.objects['Terrain']['taille'][0] and round(hitPosition.x) <= scene.objects['Terrain']['taille'][1] and round(hitPosition.y) >= scene.objects['Terrain']['taille'][2] and round(hitPosition.y) <= scene.objects['Terrain']['taille'][3] :
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)))
# Affiche les informations sur la tour
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 ("")
# Initialisation lors du chargement du terrain
def terrain_init (cont):
@ -123,18 +169,19 @@ def terrain_init (cont):
scene.objects['Points']['level']=0
scene.objects['Points']['level_max']=1
scene.objects['Points']['minions']=0
scene.objects['Level_text'].color = couleur_texte
# Recherche les tuiles non constructibles (chemin)
scene.objects['Terrain']['scene_non_contruct'] = []
for i in range (len(scene.objects)):
if "tile_straight" in scene.objects[i].name:
scene.objects['Terrain']['scene_non_contruct'].append([scene.objects[i].worldPosition.x, scene.objects[i].worldPosition.y])
scene.objects[i].collisionGroup=2
if "tile_cornerRound" in scene.objects[i].name:
scene.objects['Terrain']['scene_non_contruct'].append([scene.objects[i].worldPosition.x, scene.objects[i].worldPosition.y])
scene.objects[i].collisionGroup=2
if "tile_hill" in scene.objects[i].name:
scene.objects[i].collisionGroup=2
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
# Init de la carte 1
ct_map.map1_init()
@ -149,9 +196,9 @@ def terrain_run (cont):
scene.objects['Pause'].setVisible(False,False)
scene.objects['Pause-Hl'].setVisible(False,False)
scene.objects['Run-Hl'].setVisible(True,False)
for i in range (len(scene.objects)): # Pause des Steerings
if "type_minion" in scene.objects[i].getPropertyNames():
scene.objects[i].actuators['Steering'].velocity=0
for obj_i in scene.objects: # Pause des Steerings
if "type_minion" in obj_i.getPropertyNames():
obj_i.actuators['Steering'].velocity=0
else:
# Run
@ -160,11 +207,13 @@ def terrain_run (cont):
scene.objects['Run-Hl'].setVisible(False,False)
scene.objects['Pause-Hl'].setVisible(True,False)
# Demarrage de la map
# Démarrage de la map
if scene.objects['Terrain']['thread_run']==False:
for i in range (len(scene.objects)): # Supprimer les tours
if scene.objects[i].name=="Tower":
scene.objects[i].endObject()
for obj_i in scene.objects: # Supprimer les tours
if "type_tower" in obj_i.getPropertyNames():
obj_i.endObject()
scene.objects['Points']['level']= scene.objects['Points']['level'] - 1
scene.objects['Terrain']['scene_tile_tower']= []
scene.objects['Stop'].setVisible(True,False)
scene.objects['Terrain']['thread_run']=True
# importlib.reload(ct_cmd) # Lecture dynamique du script python (risque de Segfault de Blender)
@ -174,9 +223,38 @@ def terrain_run (cont):
# Arrêt de la pause
else:
for i in range (len(scene.objects)): # Relance des Steerings
if "type_minion" in scene.objects[i].getPropertyNames():
scene.objects[i].actuators['Steering'].velocity=3
for obj_i in scene.objects: # Relance des Steerings
if "type_minion" in obj_i.getPropertyNames():
obj_i.actuators['Steering'].velocity=obj_i.components['Minion'].args['speed']*scene.objects['Terrain']['speed']
# Vitesse du jeu
def terrain_runspeed (cont):
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive and scene.objects['Terrain']['manip_mode']==0:
obj = cont.owner
speed_mode=[0.25, 0.5, 1,2,4,10]
speed_mode_txt=["1/4", "1/2", "1", "2","4","10"]
i=speed_mode.index(scene.objects['Terrain']['speed'])
# Affichage
if obj.name=="Speed_up" and i<5:
scene.objects['Terrain']['speed']=speed_mode[i+1]
scene.objects['Text_speed']['Text']=speed_mode_txt[i+1]
if obj.name=="Speed_down" and i>0:
scene.objects['Terrain']['speed']=speed_mode[i-1]
scene.objects['Text_speed']['Text']=speed_mode_txt[i-1]
# Maj 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 :
obj_i.sensors['Near'].skippedTicks =round(1/(obj_i.components['Tower'].args['speed']*scene.objects['Terrain']['speed']))
if "type_minion" in obj_i.getPropertyNames():
obj_i.actuators['Steering'].velocity=obj_i.components['Minion'].args['speed']*scene.objects['Terrain']['speed']
# 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)
# Arrêt et réinitialisation du cycle
def terrain_stop (cont):
@ -189,9 +267,9 @@ def terrain_stop (cont):
ct_vg.stop() # Stop des vagues
# Supprimer les enemis
for i in range (len(scene.objects)):
if "type_minion" in scene.objects[i].getPropertyNames():
scene.objects[i].endObject()
for obj_i in scene.objects:
if "type_minion" in obj_i.getPropertyNames():
obj_i.endObject()
scene.objects['Points']['minions']=0
# Commandes
@ -203,7 +281,6 @@ def terrain_stop (cont):
# Fin naturelle du cycle
def terrain_fin ():
scene.objects['Terrain']['run']=False
scene.objects['Terrain']['thread_run']=False
@ -230,11 +307,19 @@ def cmd_init():
scene.objects['Map_text'].setVisible(False,False)
scene.objects['Tower_construc_mode'].setVisible(False,False)
scene.objects['Text_info'].setVisible(False,False)
scene.objects['Text_info-1'].setVisible(False,False)
scene.objects['Text_info-2'].setVisible(False,False)
# scene.objects['Raz-vue-Hl'].setVisible(False,False)
# scene.objects['Aide-cmd-Hl'].setVisible(False,False)
# Récupérer la config (vitesse du jeu : data/config/speed)
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]
# Le highlight des commandes
def cmd_hl(cont):
obj = cont.owner
@ -321,26 +406,42 @@ def mode(cont):
# Touche F5 -> Run et Pause
if JUST_ACTIVATED in keyboard.inputs[bge.events.F5KEY].queue:
# Pause
if scene.objects['Terrain']['run'] == True:
scene.objects['Terrain']['run']=False
scene.objects['Pause'].setVisible(False,False)
scene.objects['Run'].setVisible(True,False)
for obj_i in scene.objects: # Pause des Steerings
if "type_minion" in obj_i.getPropertyNames():
obj_i.actuators['Steering'].velocity=0
# Run
else:
scene.objects['Terrain']['run']=True
scene.objects['Run'].setVisible(False,False)
scene.objects['Pause'].setVisible(True,False)
# Demarrage de la map
# Démarrage de la map
if scene.objects['Terrain']['thread_run']==False:
for i in range (len(scene.objects)): # Supprimer les tours
if scene.objects[i].name=="Tower":
scene.objects[i].endObject()
for obj_i in scene.objects: # Supprimer les tours
if "type_tower" in obj_i.getPropertyNames():
obj_i.endObject()
scene.objects['Points']['level']= scene.objects['Points']['level'] - 1
scene.objects['Terrain']['scene_tile_tower']= []
scene.objects['Stop'].setVisible(True,False)
scene.objects['Terrain']['thread_run']=True
# importlib.reload(ct_cmd) # Lecture dynamique du script python (risque de Segfault de Blender)
# importlib.reload(ct_vg) # Lecture dynamique du script python (risque de Segfault de Blender)
ct_cmd.start() # Execution des commandes
ct_vg.start() # Lancement des vagues
# Arrêt de la pause
else:
for obj_i in scene.objects: # Relance des Steerings
if "type_minion" in obj_i.getPropertyNames():
obj_i.actuators['Steering'].velocity=obj_i.components['Minion'].args['speed']*scene.objects['Terrain']['speed']
# Touche F6 -> Stop / Init
if JUST_ACTIVATED in keyboard.inputs[bge.events.F6KEY].queue:
@ -353,9 +454,9 @@ def mode(cont):
ct_vg.stop() # Stop des vagues
# Supprimer les enemis
for i in range (len(scene.objects)):
if "type_minion" in scene.objects[i].getPropertyNames():
scene.objects[i].endObject()
for obj_i in scene.objects:
if "type_minion" in obj_i.getPropertyNames():
obj_i.endObject()
scene.objects['Points']['minions']=0
# Commandes

View File

@ -1,6 +1,5 @@
import bge # Bibliothèque Blender Game Engine (UPBGE)
from ct_lib import * # Bibliothèque CodeTower
import time
###############################################################################
# ct_cmd.py
@ -11,13 +10,22 @@ import time
# @copyright: Copyright (C) 2022 Philippe Roy
# @license: GNU GPL
#
# Ce simulateur est un jeu du type tower defense où les tours sont à piloter par la programmation Python.
# This game is a tower defense coding game. The towers are driven with Python code.
# En: This game is a tower defense coding game. The towers are driven with Python code.
# Fr: Ce simulateur est un jeu du type tower defense où les tours sont à piloter par la programmation Python.
#
###############################################################################
purple = [0.202, 0.114, 0.521,1]
turquoise = [0.051, 0.270, 0.279,1]
magenta = [0.799, 0.005, 0.314,1]
orange = [0.799, 0.130, 0.063,1]
yellow = [0.799, 0.617, 0.021, 1]
green = [0.246, 0.687, 0.078, 1]
red = [0.799, 0.031, 0.038, 1]
###############################################################################
# Gestion des tâches (threads) << NE PAS MODIFIER CETTE SECTION >>
# En: Threads management << DONT CHANGE THIS SECTION >>
# Fr: Gestion des tâches (threads) << NE PAS MODIFIER CETTE SECTION >>
###############################################################################
threads=[]
@ -25,22 +33,48 @@ scene = bge.logic.getCurrentScene()
def start():
scene.objects['Terrain']['thread_cmd']=True
thread_start(threads, "commands", commandes)
thread_start(threads, "commands", commands)
def stop():
thread_stop(threads, "commands")
###############################################################################
# Instructions élémentaires
# En: Tower commands
# Fr: Commandes des tours
#
# Build a tower : ct_build(x, y, name, color)
# - x position (integer)
# - y position (integer)
# - name (string)
# - color (RGB model) : purple, turquoise, magenta, orange, yellow, green, red or [R, G, B, 1]
# - Return boolean flag
# - True : builded
# - False : not builded
#
# Remove a tower : ct_remove(x, y)
# - x position (integer)
# - y position (integer)
#
# Time management (temporization) : ct_tempo(delay)
# - delay : delay in seconds (integer)
#
###############################################################################
def commandes():
# Coder vos commandes ici ...
def commands():
#ct_tower(12,2)
ct_tower(4,5)
# Code your commands here ...
tempo(2)
print ("Threads commands #", len(threads)-1, "are arrived -> close them.") # Tâche close (thread) << NE PAS MODIFIER CETTE LIGNE >>
scene.objects['Terrain']['thread_cmd']=False # Fin du cycle << NE PAS MODIFIER CETTE LIGNE >>
ct_build(4,5, "Tower #1")
ct_build(5,5, "Tower #2", green)
ct_build(5,4, "Tower #3")
ct_build(4,4, "Tower #4", [0.3, 0.5, 0.5, 1])
ct_build(4,5, "Tower #5")
ct_tempo(20)
ct_remove(4,4)
ct_print("ok")
ct_tempo(2)
print ("Threads commands #", len(threads)-1, "are arrived -> close them.") # Thread closed << DONT CHANGE THIS LINE >>
scene.objects['Terrain']['thread_cmd']=False # End of cycle << DONT CHANGE THIS LINE >>

7
ct_config.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0"?>
<data>
<config>
<speed>1</speed>
</config>
</data>

153
ct_lib.py
View File

@ -7,7 +7,7 @@ import math
import mathutils
import random
import ct # Bibliothèque CodeTower
# from ct import * # Bibliothèque CodeTower
###############################################################################
# ct_lib.py
@ -28,6 +28,16 @@ import ct # Bibliothèque CodeTower
scene = bge.logic.getCurrentScene()
# Colors
tower_purple = [0.202, 0.114, 0.521,1]
tower_turquoise = [0.051, 0.270, 0.279,1]
tower_magenta = [0.799, 0.005, 0.314,1]
tower_orange = [0.799, 0.130, 0.063,1]
tower_yellow = [0.799, 0.617, 0.021, 1]
tower_green = [0.246, 0.687, 0.078, 1]
tower_red = [0.799, 0.031, 0.038, 1]
###############################################################################
# Méthode kill pour les tâches (threads)
###############################################################################
@ -95,10 +105,9 @@ def thread_stop(threads, type_txt):
###############################################################################
# Vagues (minions)
#
###############################################################################
# Minion category : base, level, hp, speed, armor, bounty, lifes_damage
# Minion caracteristics : category (class), level, hp, speed, armor, bounty, lifes_damage
carac_minion={
'Knight-lv1' : ["Knight", 1 , 2.0, 1.0, 0.0, 5,1],
@ -107,9 +116,8 @@ carac_minion={
# Création d'un minion
def ct_minion(x,y,cat,level):
# while scene.objects['Terrain']['run'] == False: # Pause
# pass
while scene.objects['Terrain']['run'] == False: # Pause
time.sleep(0)
# Objet 3D
minion= scene.addObject(cat, scene.objects['Terrain'])
@ -129,12 +137,11 @@ def ct_minion(x,y,cat,level):
minion.components['Minion'].args['lifes_damage']=carac_minion[cat+"-lv"+str(level)][6]
minion['hp'] =minion.components['Minion'].args['hp']
# Steering
minion.actuators['Steering'].navmesh=scene.objects['Navmesh.004']
minion.actuators['Steering'].target=scene.objects['tile_straight.036']
# Actionneur Steering
minion.actuators['Steering'].navmesh=scene.objects[scene.objects['Terrain']['navmesh']]
minion.actuators['Steering'].target=scene.objects[scene.objects['Terrain']['endtile']]
minion.actuators['Steering'].distance=0.5
minion.actuators['Steering'].velocity=2
# minion.actuators['Steering'].velocity=minion.components['Minion'].args['speed']
minion.actuators['Steering'].velocity=minion.components['Minion'].args['speed']*scene.objects['Terrain']['speed']
# Tete
# if class_minion=="Knight":
@ -149,31 +156,69 @@ def ct_minion(x,y,cat,level):
# Destruction d'un minion
def scn_minion_dead(cont):
obj = cont.owner
# if obj['dead']==True:
scene.objects['Points']['minions']= scene.objects['Points']['minions']-1
scene.objects['Points']['coins']= scene.objects['Points']['coins']+obj.components['Minion'].args['bounty']
obj.endObject()
###############################################################################
# Tours
#
# Caractéristiques :
# - Tour basique : dégats = 1.0 ; portée = 3.0 ; vitesse =0,2, portée = 3.0
#
###############################################################################
# Tower caracteristics : category (class), damage, speed, range
carac_tower={
'Base' : ["Base", 1.0 , 0.02, 3.0]}
# Création d'une tour
def ct_tower(x,y):
def ct_build(x,y,tower_name="Tower",color=tower_purple, cat='Base'):
# Vérification de la place
if [x,y] in scene.objects['Terrain']['scene_tile_noncontruct'] or [x,y] in scene.objects['Terrain']['scene_tile_tower']:
return False
# Vérification du niveau
scene.objects['Points']['level']= scene.objects['Points']['level'] + 1
if scene.objects['Points']['level'] > scene.objects['Points']['level_max'] :
tour= scene.addObject("Tower_error", scene.objects['Terrain'])
tour.worldPosition=mathutils.Vector((x,y,0.2))
tour.worldScale=mathutils.Vector((1,1,1))
scene.objects['Terrain']['scene_tile_tower'].append([x,y])
return False
# Objet 3D
tour= scene.addObject("Tower", scene.objects['Terrain'])
tour.color = color
tour.worldPosition=mathutils.Vector((x,y,0.2))
tour.worldScale=mathutils.Vector((1,1,1))
scene.objects['Terrain']['scene_non_contruct'].append([x,y])
scene.objects['Terrain']['scene_tile_tower'].append([x,y])
# Caractéristiques (composant python et propriétés de l'objet 3D)
tour.components['Tower'].args['cat']=carac_tower[cat][0]
tour.components['Tower'].args['lvl']=1
tour.components['Tower'].args['tower_name']=tower_name
tour.components['Tower'].args['damage']=carac_tower[cat][1]
tour.components['Tower'].args['speed']=carac_tower[cat][2]
tour.components['Tower'].args['range']=carac_tower[cat][3]
tour['cat']=carac_tower[cat][0]
tour['lvl']=1
tour['tower_name']=tower_name
tour['damage']=carac_tower[cat][1]
tour['speed']=carac_tower[cat][2]
tour['range']=carac_tower[cat][3]
# Capteur Near
tour.sensors['Near'].distance=tour.components['Tower'].args['range']
tour.sensors['Near'].skippedTicks =round(1/(tour.components['Tower'].args['speed']*scene.objects['Terrain']['speed']))
return True
# Supression d'une tour
def ct_remove(x,y):
for obj_i in scene.objects: # Supprimer les tours
if "type_tower" in obj_i.getPropertyNames():
if x == obj_i.worldPosition.x and y == obj_i.worldPosition.y:
obj_i.endObject()
scene.objects['Points']['level']= scene.objects['Points']['level'] - 1
# Caractéristiques
tour.components['Tower'].args['damage']=1.0
tour.components['Tower'].args['speed']=0.2
tour.components['Tower'].args['range']=3.0
# Réaction d'une tour
def scn_tower_near(cont):
obj = cont.owner
@ -182,11 +227,12 @@ def scn_tower_near(cont):
# Tir
target=sensor.hitObjectList[0]
target.actuators['Steering'].velocity=target.components['Minion'].args['speed']/2
target.actuators['Steering'].velocity=(target.components['Minion'].args['speed']*scene.objects['Terrain']['speed'])/2
bullet= scene.addObject("Bullet", scene.objects['Terrain'])
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=[0.5,0.5,0.5]
bullet.worldScale=[0.75,0.75,0.75]
# bullet.worldScale=[0.5,0.5,0.5]
bullet.worldLinearVelocity.x = (target.worldPosition.x-bullet.worldPosition.x)*bullet['velocity']
bullet.worldLinearVelocity.y= (target.worldPosition.y-bullet.worldPosition.y)*bullet['velocity']
bullet.worldLinearVelocity.z = (target.worldPosition.z+0.1-bullet.worldPosition.z)*bullet['velocity']
@ -195,8 +241,6 @@ def scn_tower_near(cont):
target['hp'] = target['hp'] - obj.components['Tower'].args['damage']
if target['hp']<=0:
target['dead']=True
scene.objects['Points']['minions']= scene.objects['Points']['minions']-1
scene.objects['Points']['coins']= scene.objects['Points']['coins']+target.components['Minion'].args['bounty']
###############################################################################
# Carte
@ -212,8 +256,8 @@ def ct_map_end(x,y):
def scn_map_end_near(cont):
obj = cont.owner
sensor = obj.sensors['Near']
if len(sensor.hitObjectList)>0:
print (sensor.hitObjectList)
# if len(sensor.hitObjectList)>0:
# print ("Arrive à la fin", sensor.hitObjectList)
for i in range (len(sensor.hitObjectList)) :
sensor.hitObjectList[i].endObject()
scene.objects['Points']['lifes']= scene.objects['Points']['lifes']-sensor.hitObjectList[0].components['Minion'].args['lifes_damage']
@ -226,30 +270,47 @@ def ct_map_endflag(x,y):
endflag.worldScale=[0.3,0.3,0.3]
if round(x) == x :
if round(y) == y :
print (x,y)
scene.objects['Terrain']['scene_non_contruct'].append([x,y])
scene.objects['Terrain']['scene_tile_noncontruct'].append([x,y])
else:
scene.objects['Terrain']['scene_non_contruct'].append([x,math.floor(y)])
scene.objects['Terrain']['scene_non_contruct'].append([x,math.ceil(y)])
scene.objects['Terrain']['scene_tile_noncontruct'].append([x,math.floor(y)])
scene.objects['Terrain']['scene_tile_noncontruct'].append([x,math.ceil(y)])
else:
if round(y) == y :
scene.objects['Terrain']['scene_non_contruct'].append([math.floor(x),y])
scene.objects['Terrain']['scene_non_contruct'].append([math.ceil(x),y])
scene.objects['Terrain']['scene_tile_noncontruct'].append([math.floor(x),y])
scene.objects['Terrain']['scene_tile_noncontruct'].append([math.ceil(x),y])
else:
scene.objects['Terrain']['scene_non_contruct'].append([math.floor(x),math.floor(y)])
scene.objects['Terrain']['scene_non_contruct'].append([math.floor(x),math.ceil(y)])
scene.objects['Terrain']['scene_non_contruct'].append([math.ceil(x),math.floor(y)])
scene.objects['Terrain']['scene_non_contruct'].append([math.ceil(x),math.ceil(y)])
# Texte de carte
def ct_map_text(text):
scene.objects['Map_text']['Text']=text
scene.objects['Map_text'].setVisible(True,False)
scene.objects['Terrain']['scene_tile_noncontruct'].append([math.floor(x),math.floor(y)])
scene.objects['Terrain']['scene_tile_noncontruct'].append([math.floor(x),math.ceil(y)])
scene.objects['Terrain']['scene_tile_noncontruct'].append([math.ceil(x),math.floor(y)])
scene.objects['Terrain']['scene_tile_noncontruct'].append([math.ceil(x),math.ceil(y)])
###############################################################################
# Temporisation
###############################################################################
def tempo (duree):
time.sleep(duree)
def ct_tempo (duree):
time.sleep(duree*(1/scene.objects['Terrain']['speed']))
###############################################################################
# Affichage
###############################################################################
# Texte du panel d'information
def ct_print (texte):
# text_info (texte)
if texte=="":
scene.objects['Text_info-1'].setVisible(False,False)
scene.objects['Text_info-2'].setVisible(False,False)
else:
lignes_txt=texte.split("\n", 6)
for i in range (len(lignes_txt),6):
lignes_txt.append("")
scene.objects['Text_info-1'].setVisible(True,False)
scene.objects['Text_info-2'].setVisible(True,False)
scene.objects['Text_info-1']['Text']=lignes_txt[0]+"\n"+lignes_txt[1]+"\n"+lignes_txt[2]
scene.objects['Text_info-2']['Text']=lignes_txt[3]+"\n"+lignes_txt[4]+"\n"+lignes_txt[5]
# Texte de carte
def ct_map_text(text):
scene.objects['Map_text']['Text']=text
scene.objects['Map_text'].setVisible(True,False)

View File

@ -22,8 +22,14 @@ from ct_lib import * # Bibliothèque CodeTower
def map1_init():
# Taille de la carte
scene.objects['Terrain']['taille'] = [-15,15,-10,10]
# Navmesh
scene.objects['Terrain']['navmesh'] = "Navmesh.004"
# Ajout des sorties
scene.objects['Terrain']['endtile'] = "tile_straight.036"
ct_map_end(1,-10)
ct_map_endflag(0.5,-10)
ct_map_endflag(1.5,-10)

View File

@ -17,7 +17,8 @@ from ct_lib import * # Bibliothèque CodeTower
###############################################################################
###############################################################################
# Gestion des tâches (threads) << NE PAS MODIFIER CETTE SECTION >>
# En: Threads management << DONT CHANGE THIS SECTION >>
# Fr: Gestion des tâches (threads) << NE PAS MODIFIER CETTE SECTION >>
###############################################################################
threads=[]
@ -25,23 +26,56 @@ scene = bge.logic.getCurrentScene()
def start():
scene.objects['Terrain']['thread_wave']=True
thread_start(threads, "waves", vagues)
thread_start(threads, "waves", waves)
def stop():
thread_stop(threads, "waves")
###############################################################################
# Instructions élémentaires
# En: Waves commands
# Fr: Commandes des vagues
#
# Spawn a minion : ct_minion (x, y, cat, level)
# - x spwan position (integer)
# - y spwan position (integer)
# - cat : minion class (string) :
# - Knight
# - Orc
# - Squelette
# - level (1, 2 or 3)
#
# Time management (temporization) : ct_tempo(delay)
# - delay : delay in seconds (integer)
#
# UI management : ct_map_text(text)
# - text for the wave label
#
#
###############################################################################
def vagues():
# Minion caracteristics : category (class), level, hp, speed, armor, bounty, lifes_damage
# carac_minion={
# 'Knight-lv1' : ["Knight", 1 , 2.0, 1.0, 0.0, 5,1],
# 'Knight-lv2' : ["Knight", 2, 4.0, 1.0, 1.0, 20,1],
# 'Knight-lv3' : ["Knight", 3, 8.0, 1.0, 2.0, 80,1]}
def waves():
# Wave 1
ct_map_text("Wave 1")
for i in range (4):
ct_minion(14,3,"Knight",2)
tempo(1)
for i in range (15):
ct_minion(14,3,"Knight",1)
ct_tempo(1)
tempo(2)
print ("Threads waves #", len(threads)-1, "are arrived -> close them.") # Tâche close (thread) << NE PAS MODIFIER CETTE LIGNE >>
scene.objects['Terrain']['thread_wave']=False # Fin du cycle << NE PAS MODIFIER CETTE LIGNE >>
ct_tempo(60)
# Wave 2
ct_map_text("Wave 2")
for i in range (20):
ct_minion(14,3,"Knight",1)
ct_tempo(1)
ct_tempo(2)
print ("Threads waves #", len(threads)-1, "are arrived -> close them.") # Thread closed << DONT CHANGE THIS LINE >>
scene.objects['Terrain']['thread_wave']=False # End of cycle << DONT CHANGE THIS LINE >>