Tower bullet with 3d object but without physic

This commit is contained in:
Philippe Roy 2022-07-20 05:03:56 +02:00
parent c8bc8c9859
commit 3783722d19
4 changed files with 280 additions and 215 deletions

BIN
codetower-35.blend Normal file

Binary file not shown.

137
ct.py
View File

@ -1,8 +1,8 @@
# import importlib
# import imp
import bge # Blender Game Engine (UPBGE)
import bpy # Blender
import aud # Sounds
import bge # Blender Game Engine (UPBGE)
import bpy # Blender
import aud # Sounds
import math
import mathutils
import time
@ -90,11 +90,14 @@ JUST_RELEASED = bge.logic.KX_INPUT_JUST_RELEASED
ACTIVATE = bge.logic.KX_INPUT_ACTIVE
# JUST_DEACTIVATED = bge.logic.KX_SENSOR_JUST_DEACTIVATED
###############################################################################
###############################################################################
# Tour
###############################################################################
##
# 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"]
@ -120,11 +123,14 @@ def cmd_tower_construct(cont):
text_info ("Tower position : ")
scene.objects['Cmd-text']['Text']= ""
###############################################################################
###############################################################################
# User interface : texte info et compteurs
###############################################################################
##
# Affichage sur la boite de texte sur 6 lignes
##
def text_info (text):
if text=="":
scene.objects['Info-1-text'].setVisible(False,False)
@ -138,7 +144,10 @@ def text_info (text):
scene.objects['Info-1-text']['Text']=lines_txt[0]+"\n"+lines_txt[1]+"\n"+lines_txt[2]
scene.objects['Info-2-text']['Text']=lines_txt[3]+"\n"+lines_txt[4]+"\n"+lines_txt[5]
##
# Mise à jour de l'affichage des compteurs
##
def points_maj (cont):
global fps_time
@ -176,7 +185,7 @@ def points_maj (cont):
if milliseconds != fps_time:
fps = int(1000/(milliseconds-fps_time))
else:
fps= "----"
fps = "----"
print ("Durée entre deux tics (16 ms), fps (60), coins :", milliseconds-fps_time, fps, str(scene.objects['Points']['coins']))
fps_time = milliseconds
@ -224,11 +233,14 @@ def points_maj (cont):
scene.objects['Points']['wave'] +=1
ct_map.start(scene.objects['Points']['wave']) # Lancement du script de la vague
###############################################################################
###############################################################################
# Terrain
###############################################################################
##
# Mouse over du terrain
##
def scn_terrain_mo (cont):
# Affiche la position de la tour de construction
@ -261,7 +273,10 @@ def scn_terrain_mo (cont):
else:
text_info ("")
##
# Initialisation lors du chargement du terrain
##
def terrain_init ():
# Cacher les fenêtres
@ -306,7 +321,10 @@ def terrain_init ():
ct_map.map_reset()
scene.objects['Points-Level-text'].color = color_text
##
# Mise en route et pause du cycle
##
def terrain_run ():
sound_play (sndbuff_click)
@ -374,7 +392,10 @@ def terrain_run ():
if "type_minion" in obj_i.getPropertyNames():
obj_i.actuators['Steering'].velocity=obj_i['speed_base']*scene.objects['Terrain']['speed']
##
# Arrêt et réinitialisation du cycle
##
def terrain_stop ():
sound_play (sndbuff_click)
@ -403,7 +424,10 @@ def terrain_stop ():
scene.objects['Stop-Hl'].setVisible(False,False)
scene.objects['Cmd-text'].setVisible(False,False)
##
# Fin de la map
##
def terrain_end ():
scene.objects['Terrain']['run']=False
scene.objects['Terrain']['thread_run']=False
@ -460,7 +484,10 @@ def terrain_end ():
if scene.objects['End']['timer']== 40:
scene.objects['Terrain']['map_run'] = False
##
# Vitesse du jeu
##
def terrain_speed (obj):
sound_play (sndbuff_click)
speed_mode=[0.25, 0.5, 1,2,4,10]
@ -495,7 +522,10 @@ def terrain_speed (obj):
with open("ct_config.xml", "wb") as f:
f.write(buffer_xml)
##
# Highlight de la page de fin
##
def endbanner_hl(cont):
if cont.sensors['MO'].status == JUST_ACTIVATED:
if scene.objects['Mouse_main']['mouse_graphic']:
@ -512,7 +542,10 @@ def endbanner_hl(cont):
scene.objects['Endbanner_points'].color = color_black
scene.objects['Endbanner_ok'].color = color_black
##
# Fermer la page de fin
##
def endbanner_close():
sound_play (sndbuff_click)
scene.objects['Terrain']['manip_mode']=0
@ -528,7 +561,7 @@ def endbanner_close_click(cont):
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive :
endbanner_close()
###############################################################################
###############################################################################
# Sons
###############################################################################
@ -568,14 +601,17 @@ def sound_unset ():
with open("ct_config.xml", "wb") as f:
f.write(buffer_xml)
###############################################################################
###############################################################################
# Commandes
###############################################################################
color_cmd = (0.8, 0.8, 0.8, 1) # Blanc
color_cmd_hl = (0.8, 0.619, 0.021, 1) # Jaune
##
# Init
##
def cmd_init():
# UI : Commands
@ -632,7 +668,10 @@ def cmd_init():
i=speed_mode.index(scene.objects['Terrain']['speed'])
scene.objects['Text_speed']['Text']=speed_mode_txt[i]
##
# Highlight des commandes
##
def cmd_hl(cont):
obj = cont.owner
@ -720,7 +759,10 @@ def cmd_hl(cont):
scene.objects[obj.name].setVisible(False,True)
scene.objects['Cmd-text'].setVisible(False,False)
##
# 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:
@ -740,15 +782,18 @@ def cmd_click (cont):
about_open ()
###############################################################################
###############################################################################
# Gestion du clavier
###############################################################################
# Mode :
##
# Mode
#
# 0 : rien (par défaut)
# 1 : Pan avec Shift
# 2 : Zoom avec Ctrl
# 9 : Fenêtre modale
# 9 : Fenêtre modal
##
def mode(cont):
obj = cont.owner
@ -821,11 +866,14 @@ def mode(cont):
if JUST_ACTIVATED in keyboard.inputs[bge.events.PADMINUS].queue:
terrain_speed (scene.objects['Speed_down'])
###############################################################################
###############################################################################
# Manipulation 3D de la scène
###############################################################################
##
# 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
@ -843,7 +891,10 @@ def manip_init(cont):
scene.objects['Points-Map-text']['init_relativ_ly']=scene.objects['Points-Map-text'].worldPosition.y-scene.objects['Points'].worldPosition.y
scene.objects['Points-Map-text']['init_relativ_lz']=scene.objects['Points-Map-text'].worldPosition.z-scene.objects['Points'].worldPosition.z
##
# Atteindre une orientation (bas niveau)
##
def applyRotationTo(obj, rx=None, ry=None, rz=None, Local=True):
rres=0.001 # resolution rotation
@ -874,7 +925,10 @@ def applyRotationTo(obj, rx=None, ry=None, rz=None, Local=True):
obj.applyRotation((0, 0, rres), Local)
# print ("delta z ",rz-obj.worldOrientation.to_euler().z)
##
# Reset de la manipulation de la vue
##
def manip_reset():
scene.objects['Camera'].worldPosition.x = scene.objects['Camera']['init_lx']
scene.objects['Camera'].worldPosition.y = scene.objects['Camera']['init_ly']
@ -882,19 +936,28 @@ def manip_reset():
applyRotationTo(scene.objects['Terrain'], 0, 0, 0)
scene.objects['Cmd-text']['Text']= ""
##
# Position de départ pour la manipulation de la vue
##
def manip_start(cont):
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]
##
# 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
@ -929,7 +992,10 @@ def manip(cont):
scene.objects['Camera']['past_ly']=scene.objects['Camera'].worldPosition.y
scene.objects['Camera']['past_lz']=scene.objects['Camera'].worldPosition.z
##
# Manipulation du modèle ou de la caméra
##
def manip_wheel(cont):
if scene.objects['Terrain']['manip_mode']!=9: # Fenêtre modale
obj = cont.owner
@ -958,7 +1024,10 @@ def manip_wheel(cont):
else:
return
##
# Icone de la souris
##
def mouse(cont):
if scene.objects['Mouse_main']['mouse_graphic']==False:
return
@ -1001,7 +1070,10 @@ def mouse(cont):
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:
@ -1015,7 +1087,10 @@ def mouse_up():
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:
@ -1038,7 +1113,10 @@ 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
##
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
@ -1086,7 +1164,10 @@ def doc (cont):
scene.objects['Book_page_screen'].worldPosition = scene.objects['Book'].worldPosition
scene.objects['Book_page_screen'].setVisible(True,True)
##
# Fermer le livre
##
def doc_close ():
sound_play (sndbuff_book_close)
scene.objects['Terrain']['manip_mode']=0 # Fenêtre modale
@ -1107,12 +1188,18 @@ def doc_close ():
scene.objects['Book-cmd-underlay'].setVisible(False,False)
scene.objects['Construc-underlay'].setVisible(False,False)
##
# Click pour fermer le livre
def doc_close_click (cont):
##
def doc_close_click(cont):
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive :
doc_close()
##
# Highlight du livre
##
def doc_hl (cont):
if cont.sensors['MO'].status == JUST_ACTIVATED :
obj = cont.owner
@ -1133,14 +1220,17 @@ def doc_hl (cont):
else:
scene.objects[name].color = color_doc_fct
###############################################################################
###############################################################################
# About
###############################################################################
color_link = (0.799, 0.617, 0.021, 1) # Jaune
color_link_hl = (0.8, 0.8, 0.8, 1) # Blanc
##
# Ouvrir le about
##
def about_open():
scene.objects['Terrain']['manip_mode']=9 # Fenêtre modale
manip_reset()
@ -1158,7 +1248,10 @@ def about_open():
scene.objects['About']['timer'] = 0
scene.objects['About']['anim'] = True
##
# Animation du about
##
def about_open_anim():
pas=0.5
scene.objects['About'].localPosition.y -= pas
@ -1169,7 +1262,10 @@ def about_open_anim():
if scene.objects['About']['timer']== 40:
scene.objects['About']['anim'] = False
##
# Highlight du about
##
def about_hl(cont):
# decal = 18
# size_scale = 0.2
@ -1204,7 +1300,10 @@ def about_hl(cont):
# scene.objects['Mouse_main'].worldScale=[30, 30, 30]
# print("about_hl R après", scene.objects['Mouse_main'].worldPosition)
##
# Fermer le about
##
def about_close():
sound_play (sndbuff_click)
scene.objects['Terrain']['manip_mode']=0
@ -1213,12 +1312,18 @@ def about_close():
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):
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive :
about_close()
##
# Liens du about
##
def about_link(cont):
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive :
obj = cont.owner
@ -1232,7 +1337,7 @@ def about_link(cont):
'About_link-kenney' : 'https://www.kenney.nl'}
webbrowser.open(link [name])
# FIXME: souris graphique trop compliqué
# FIXME: souris graphique trop compliqué
def about_link_hl(cont):
decal = 15
if cont.sensors['MO'].status == JUST_ACTIVATED :

347
ct_lib.py
View File

@ -1,5 +1,5 @@
import bge # Blender Game Engine (UPBGE)
import aud # Sounds
import aud # Sounds
import threading # Multithreading
import trace
import sys
@ -24,7 +24,7 @@ import random
# Commands trigged by 3D scene objects : scn_*
# Commands trigged by user (student or map designer) : ct_*
# 3D scene manipulation : manip_*
#
#
###############################################################################
scene = bge.logic.getCurrentScene()
@ -71,7 +71,7 @@ JUST_RELEASED = bge.logic.KX_INPUT_JUST_RELEASED
ACTIVATE = bge.logic.KX_INPUT_ACTIVE
# JUST_DEACTIVATED = bge.logic.KX_SENSOR_JUST_DEACTIVATED
###############################################################################
###############################################################################
# Données générales
###############################################################################
@ -81,7 +81,7 @@ def ct_level_current():
def ct_level():
return scene.objects['Points']['level_max']
###############################################################################
###############################################################################
# Méthode kill pour les tâches (threads)
###############################################################################
@ -89,33 +89,33 @@ 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
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)
###############################################################################
@ -159,7 +159,7 @@ def thread_cmd_start(fct):
def thread_cmd_stop():
thread_stop(threads_cmd, "commands")
###############################################################################
###############################################################################
# Sounds
###############################################################################
@ -168,14 +168,17 @@ def sound_play (sound):
if scene.objects['Commands']['sound'] and sys.platform!="win32":
audiodev.play(sound)
###############################################################################
###############################################################################
# Waves (minions)
###############################################################################
##
# Création d'un minion
#
# Minion caracteristics : category (class), level, hp, speed, armor, bounty, lifes_damage
# Minion 3d body : body (male,female,old, ...), variante (A,B,C,D, ...), level
##
# Création d'un minion
def ct_minion_create(x,y,cat,level):
category=cat+"-lv"+str(level)
minion_3d= scene.objects['Terrain']['minion_3d']
@ -241,13 +244,19 @@ def ct_minion_create_details(x,y,cat,level,body="Knight_m_A_common"):
scene.objects['Terrain']['thread_cmd_lock'] = False
return minion.name
##
# Activation du minion (steering)
##
def ct_minion_go(minion_name):
minion=scene.objects[minion_name]
minion.restorePhysics()
minion.setVisible(True)
##
# Destruction d'un minion
##
def scn_minion_dead(cont):
obj = cont.owner
scene.objects['Points']['minions'] -=1
@ -256,11 +265,14 @@ def scn_minion_dead(cont):
scene.objects['Points']['coins']= scene.objects['Points']['coins']+obj['bounty']
obj.endObject()
###############################################################################
###############################################################################
# Spells / Casts
###############################################################################
##
# Buff/debuff Minion
##
def scn_minion_affect(cont):
if scene.objects['Terrain']['run'] == False: # Pause
return
@ -276,7 +288,7 @@ def scn_minion_affect(cont):
# Lod
# print(obj.currentLodLevel)
# Etats actif
# Etats actif
for debuff_i in obj['buff']:
if debuff_i[1] <= 0:
obj['buff'].remove(debuff_i)
@ -292,13 +304,16 @@ def scn_minion_affect(cont):
else:
obj.actuators['Steering'].velocity = obj['speed_base']*scene.objects['Terrain']['speed']
###############################################################################
###############################################################################
# Towers
###############################################################################
# Tower caracteristics : category (class), damage, speed, range
##
# Création d'une tour
#
# Tower caracteristics : category (class), damage, speed, range
##
def ct_build(x,y, cat='Archer tower', tower_name="Tower", color=tower_purple, building="square-A"):
tower_minion_3d= scene.objects['Terrain']['tower_minion_3d']
if cat=='Archer tower': # Archer
@ -308,13 +323,16 @@ def ct_build(x,y, cat='Archer tower', tower_name="Tower", color=tower_purple, bu
body = random.choice(tower_minion_3d[category][0])+"_"+random.choice(tower_minion_3d[category][1])+"_"+random.choice(tower_minion_3d[category][2])
return (ct_build_details(x,y, cat, tower_name, color, building, body))
##
# Création d'une tour détaillée
##
def ct_build_details(x,y, cat='Archer tower', tower_name="Tower", color=tower_purple, building="square-A", body="Archer_m_A_common"):
# 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'] :
@ -324,7 +342,7 @@ def ct_build_details(x,y, cat='Archer tower', tower_name="Tower", color=tower_pu
scene.objects['Terrain']['scene_tile_tower'].append([x,y])
return False
# Blocage des autres threads pendant la construction
# Blocage des autres threads pendant la construction
scene.objects['Terrain']['thread_cmd_lock'] = True
# Objets 3D
@ -345,12 +363,12 @@ def ct_build_details(x,y, cat='Archer tower', tower_name="Tower", color=tower_pu
# Draw3d
if cat=='Archer tower':
for i in range (10):
for i in range (3):
ct_add_bullet(x,y,i)
# Sounds
sound_play(sndbuff_build)
# Caracteristics
tower_carac= scene.objects['Terrain']['tower_carac']
tour['cat']=tower_carac[cat][0]
@ -372,12 +390,15 @@ def ct_build_details(x,y, cat='Archer tower', tower_name="Tower", color=tower_pu
tour.sensors['Near'].distance=tour['range']
tour.sensors['Near'].skippedTicks =round(1/(tour['speed']*scene.objects['Terrain']['speed']))
# Déblocage des autres threads après la construction
# Déblocage des autres threads après la construction
scene.objects['Terrain']['thread_cmd_lock'] = False
# print (scene.objects)
return True
##
# Suppression d'une tour
##
def ct_remove(x,y):
for obj_i in scene.objects:
if "type_tower" in obj_i.getPropertyNames():
@ -386,23 +407,24 @@ def ct_remove(x,y):
obj_i.endObject()
scene.objects['Points']['level']= scene.objects['Points']['level'] - 1
##
# Création d'un projectile
def ct_add_bullet(x,y,num):
bullet1= scene.addObject("Bullet", scene.objects['Terrain'])
bullet1.name="tower("+str(x)+','+str(y)+")-bullet"+str(num)
bullet1.mass=0.001
bullet1.worldPosition=[x,y,1.5]
bullet1.worldScale=[0.75,0.75,0.75]
bullet1.suspendPhysics (True)
bullet1.setVisible(False)
bullet1['init_lx']=x
bullet1['init_ly']=y
bullet1['init_lz']=1.5
bullet1['activated']=False
bullet1['velocity']=10
# print ("Création : "+bullet1.name)
##
def ct_add_bullet(x,y,num):
bullet= scene.addObject("Bullet", scene.objects['Terrain'])
bullet.name="tower("+str(x)+','+str(y)+")-bullet"+str(num)
bullet.worldPosition=[x,y,1.5]
bullet.worldScale=[0.75,0.75,0.75]
bullet.suspendPhysics (True)
bullet.setVisible(False)
bullet['activated']=False
# print ("Création : "+bullet.name)
##
# Réaction d'une tour
##
def scn_tower_near(cont):
obj = cont.owner
sensor = obj.sensors['Near']
@ -458,7 +480,7 @@ def scn_tower_near(cont):
# obj['target_past']=[]
# for obj_i in sensor.hitObjectList:
# obj['target_past'].append([obj_i.name, obj.getDistanceTo(obj_i)])
# Orientation du tower minion
towerminion="tm("+str(round(obj.worldPosition.x))+','+str(round(obj.worldPosition.y))+")"
angle =math.atan((target.worldPosition.y-obj.worldPosition.y)/(target.worldPosition.x-obj.worldPosition.x))
@ -470,61 +492,6 @@ def scn_tower_near(cont):
angle3=math.pi+angle
scene.objects[towerminion].applyRotation((0, 0, angle2), False)
# Bullet (3d object) (vitesse lente) # -> tendance au plantage
# if scene.objects['Terrain']['speed']<1:
# bullet= scene.addObject("Bullet", scene.objects['Terrain'])
# bullet.mass=0.001 # bullet.applyForce=((0,0,9.81),True)
# bullet.worldPosition=[obj.worldPosition.x,obj.worldPosition.y,1.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']
# print (obj.name)
# bullet1_name=obj.name+"-bullet1" # Tower bullet 1 (tb)
for i in range (10):
bullet = scene.objects[obj.name+"-bullet"+str(i)]
if bullet['activated']==False:
print ("Activate bullet : ", bullet.name, "-> minion : ", target.name)
bullet['activated']=True
bullet.setVisible(True)
bullet.mass=0.001
bullet['velocity']=20
bullet.restorePhysics()
# bullet.worldPosition=[target.worldPosition.x,target.worldPosition.y,target.worldPosition.z+0.5]
bullet.worldPosition=[bullet['init_lx'],bullet['init_ly'],1.5]
# bullet.worldPosition=[round(obj.worldPosition.x),round(obj.worldPosition.y),1.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 = 0
# bullet.worldLinearVelocity.z = (target.worldPosition.z+0.5-bullet.worldPosition.z)*bullet['velocity']
# bullet.worldLinearVelocity.z = (target.worldPosition.z-0.1-bullet.worldPosition.z)*bullet['velocity']
bullet.worldLinearVelocity=[(target.worldPosition.x-bullet.worldPosition.x)*bullet['velocity'],
(target.worldPosition.y-bullet.worldPosition.y)*bullet['velocity'],
(target.worldPosition.z+0.5-1.5)*bullet['velocity']]
print ("bullet.worldPosition.x : "+str(bullet.worldPosition.x)+" -> target.worldPosition.x : "+str(target.worldPosition.x)+" - bullet.worldLinearVelocity.x :"+str(bullet.worldLinearVelocity.x))
print ("bullet.worldPosition.y : "+str(bullet.worldPosition.y)+" -> target.worldPosition.y : "+str(target.worldPosition.y)+" - bullet.worldLinearVelocity.y :"+str(bullet.worldLinearVelocity.y))
print ("bullet.worldPosition.z : "+str(bullet.worldPosition.z)+" -> target.worldPosition.z : "+str(target.worldPosition.z)+" - bullet.worldLinearVelocity.z :"+str(bullet.worldLinearVelocity.z))
# print ("bullet.worldLinearVelocity.x : "+str(bullet.worldLinearVelocity.x))
# print ("bullet.worldPosition.y : "+str(bullet.worldPosition.y)+" -> target.worldPosition.y : ", str(target.worldPosition.y))
# print ("bullet.worldPosition.z : "+str(bullet.worldPosition.z)+" -> target.worldPosition.z : ", str(target.worldPosition.z))
# bullet.worldLinearVelocity.z = (target.worldPosition.z+0.1-bullet.worldPosition.z)*bullet['velocity']
# bullet.worldLinearVelocity.z = 0
break
if i ==9 :
print ("Plus de bullet de disponible !!")
# Sounds
if obj['cat']=="Archer tower":
sound_play(sndbuff_archer)
@ -538,17 +505,28 @@ def scn_tower_near(cont):
# Archer (tir de flêche)
if obj['cat']=="Archer tower":
if target.name in scene.objects:
pass
# scene.objects['Terrain']['draw2d_process']=True
# scene.objects['Terrain']['draw2d_list'].append([5, "arrow", [obj.worldPosition.x, obj.worldPosition.y, obj.worldPosition.z+0.8],target.name, angle3, ray_black, 5])
# scene.objects['Terrain']['draw3d_process']=True
# scene.objects['Terrain']['draw3d_list'].append([5, "arrow", [obj.worldPosition.x, obj.worldPosition.y, obj.worldPosition.z+0.8], obj.name, target.name, angle3, ray_black, 5])
# scene.objects['Terrain']['draw2d_list'].append([5, "arrow", [obj.worldPosition.x, obj.worldPosition.y, obj.worldPosition.z+0.8], target.name, angle3, ray_black, 5])
for i in range (3):
bullet = scene.objects[obj.name+"-bullet"+str(i)]
if bullet['activated']==False:
bullet.worldPosition=[obj.worldPosition.x, obj.worldPosition.y, obj.worldPosition.z+0.8]
bullet['activated']=True
bullet.setVisible(True)
scene.objects['Terrain']['draw3d_process']=True
scene.objects['Terrain']['draw3d_list'].append([20, "arrow", obj.name, bullet.name, target.name, angle3, ray_black, 20])
break
if i ==3 :
print ("Plus de bullet de disponible pour la tour : "+obj.name)
# Cast zone
if obj['cat']=="Mage tower": # Mage (cast)
scene.objects['Terrain']['draw2d_process']=True
scene.objects['Terrain']['draw2d_list'].append([30, "cast", [obj.worldPosition.x, obj.worldPosition.y, obj.worldPosition.z+0.8],ray_blue,30])
# 'Archer tower' : ["Archer tower", 1.0 , 0.02, 2.5],
# 'Mage tower' : ["Mage tower", 0.0 , 0.005, 2.5],
# Rayon
if obj['cat']=="Test":
if target.name in scene.objects:
@ -565,21 +543,24 @@ def scn_tower_near(cont):
for target_i in sensor.hitObjectList:
target_i['buff'].append([obj['cast'], obj['cast_duration']])
# Cible touchée par un projectile
def scn_bullet_collision(cont):
obj = cont.owner
# obj['activated']=False
# # obj.suspendPhysics (True)
# obj.setVisible(False)
# # obj.worldPosition=[obj['init_lx'],obj['init_ly'],obj['init_lz']]
# print ("dead bullet : ", obj.name)
# # Cible touchée par un projectile
# def scn_bullet_collision(cont):
# obj = cont.owner
# # obj['activated']=False
# # # obj.suspendPhysics (True)
# # obj.setVisible(False)
# # # obj.worldPosition=[obj['init_lx'],obj['init_ly'],obj['init_lz']]
# # print ("dead bullet : ", obj.name)
###############################################################################
# Carte
###############################################################################
# Carte
###############################################################################
##
# Texte de carte
##
def ct_map_text_wave(wave):
scene.objects['Points-Map-text']['Text']=("Wave " + str(wave))
scene.objects['Points-Map-text'].setVisible(True,False)
@ -587,17 +568,26 @@ def ct_map_text_wave(wave):
scene.objects['Points-Map-text']['timer']=0
scene.objects['Points-Map-text']['anim']=True
##
# Texte de carte
##
def ct_map_text(text):
scene.objects['Points-Map-text']['Text']=text
scene.objects['Points-Map-text'].setVisible(True,False)
##
# Fin
##
def ct_map_end(x,y):
scene.objects['Map_end'].worldPosition=[x,y,0.2]
scene.objects['Map_end'].worldScale=[0.25,0.25,0.25]
##
# Minion arrivé à la fin
##
def scn_map_end_near(cont):
obj = cont.owner
sensor = obj.sensors['Near']
@ -610,26 +600,10 @@ def scn_map_end_near(cont):
for obj_i in sensor.hitObjectList :
obj_i.endObject()
# def scn_map_end_near(cont):
# obj = cont.owner
# print(obj)
# sensor = obj.sensors['Near']
# if sensor.positive and len(sensor.hitObjectList)>0 :
# # print ("end, len(sensor.hitObjectList) : ", len(sensor.hitObjectList))
# for obj_i in sensor.hitObjectList :
# # print ("obj['idm_last'], obj_i ['id'] : ", obj['idm_last'], obj_i ['id'])
# if obj['idm_last'] != obj_i ['id']:
# obj['idm_last'] = obj_i ['id']
# sound_play(sndbuff_life)
# if scene.objects['Points']['lifes']>0:
# scene.objects['Points']['lifes']= scene.objects['Points']['lifes']-obj_i['lifes_damage']
# scene.objects['Points']['minions_run'] -=1
# obj['idm_last'] = obj_i ['id']
# for obj_i in sensor.hitObjectList :
# obj_i.endObject()
##
# Drapeau de fin
##
# Drapeau de fin
def ct_map_endflag(x,y):
endflag= scene.addObject("Map_endflag", scene.objects['Terrain'])
endflag.worldPosition=[x,y,0.3]
@ -650,7 +624,7 @@ def ct_map_endflag(x,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
###############################################################################
@ -674,11 +648,14 @@ def ct_sleep (duration):
# def ct_tempo_wave_trigger (duree):
# print ("delay wave ", scene.objects['Terrain']['delay_wave'])
###############################################################################
###############################################################################
# Affichage
###############################################################################
##
# Texte du panel d'information
##
def ct_print (text):
# text_info (texte)
if text=="":
@ -687,17 +664,17 @@ def ct_print (text):
else:
lines_txt=text.split("\n", 6)
for i in range (len(lines_txt),6):
lines_txt.append("")
lines_txt.append("")
scene.objects['Info-1-text'].setVisible(True,False)
scene.objects['Info-2-text'].setVisible(True,False)
scene.objects['Info-1-text']['Text']=lines_txt[0]+"\n"+lines_txt[1]+"\n"+lines_txt[2]
scene.objects['Info-2-text']['Text']=lines_txt[3]+"\n"+lines_txt[4]+"\n"+lines_txt[5]
###############################################################################
# Dessin 3d
###############################################################################
# Dessin 3d
###############################################################################
# scene.objects['Terrain']['draw3d_list'].append([5, "arrow", [obj.worldPosition.x, obj.worldPosition.y, obj.worldPosition.z+0.8], obj.name, target.name, angle3, ray_black, 5])
# scene.objects['Terrain']['draw3d_list'].append([5, "arrow", obj.name, bullet.name, target.name, angle3, ray_black, 5])
def scn_draw3d(cont):
obj = cont.owner
@ -706,68 +683,50 @@ def scn_draw3d(cont):
if len(scene.objects['Terrain']['draw3d_list'])==0:
scene.objects['Terrain']['draw3d_process']=False
return
# Dépilage des draws à executer
for draw_cmd in scene.objects['Terrain']['draw3d_list']:
# Archer (tir de flêche)
if draw_cmd[1]=="arrow":
if draw_cmd[3] in scene.objects:
bullet1.name=draw_cmd[3]+"-bullet1"
if scene.objects['Terrain']['speed']<1:
draw_cmd[0] = draw_cmd[0]-scene.objects['Terrain']['speed']
else:
draw_cmd[0] = draw_cmd[0]-1
if draw_cmd[1]=="arrow":
if draw_cmd[4] in scene.objects:
tower= scene.objects[draw_cmd[2]]
bullet = scene.objects[draw_cmd[3]]
target = scene.objects[draw_cmd[4]]
x0 = tower.worldPosition.x
y0 = tower.worldPosition.y
z0 = tower.worldPosition.z+0.8 # ajustement +0.8
x1 = target.worldPosition.x
y1 = target.worldPosition.y
z1 = target.worldPosition.z+0.5 # ajustement +0.5
# z1 = target.worldPosition.z-0.1 # ajustement -0.1
step_x=(x1-x0)/draw_cmd[7]
step_y=(y1-y0)/draw_cmd[7]
step_z=(z1-z0)/draw_cmd[7]
step = draw_cmd[7]-draw_cmd[0]
bullet.worldPosition=[x0+step_x*step, y0+step_y*step, z0+step_z*step]
draw_cmd[0] = draw_cmd[0]-scene.objects['Terrain']['speed']
# Dégats
if draw_cmd[0]<=0:
bullet['activated']=False
bullet.setVisible(False)
# bullet.worldPosition=[x0,y0,z0]
target['hp'] = target['hp'] - tower['damage']
if target['hp']<=0: # Mort
target['dead']=True
bullet1.name="tower("+str(x)+','+str(y)+")-bullet1" # Tower bullet 1 (tb)
# Suppression des draws finis
i=0
for draw_cmd in scene.objects['Terrain']['draw3d_list']:
if draw_cmd[0]<=0:
scene.objects['Terrain']['draw3d_list'].pop(i)
else:
i=i+1
if len(scene.objects['Terrain']['draw3d_list'])==0:
scene.objects['Terrain']['draw3d_process']=False
# bullet= scene.addObject("Bullet", scene.objects['Terrain'])
# bullet.mass=0.001 # bullet.applyForce=((0,0,9.81),True)
# bullet.worldPosition=[obj.worldPosition.x,obj.worldPosition.y,1.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']
# # x0 = draw_cmd[2][0]+0.25*(math.cos(draw_cmd[4]))
# # y0 = draw_cmd[2][1]+0.25*(math.sin(draw_cmd[4]))
# x0 = draw_cmd[2][0]
# y0 = draw_cmd[2][1]
# z0 = draw_cmd[2][2]
# x1 = scene.objects[draw_cmd[3]].worldPosition.x
# y1 = scene.objects[draw_cmd[3]].worldPosition.y
# z1 = scene.objects[draw_cmd[3]].worldPosition.z-0.1 # ajustement -0.1
# distance = math.sqrt((x1-x0)**2+(y1-y0)**2+(z1-z0)**2)
# distance_xy = math.sqrt((x1-x0)**2+(y1-y0)**2)
# distance_z = z1-z0
# angle_z =math.atan((z1-z0)/(distance_xy))
# angle_xy =math.atan((y1-y0)/(x1-x0))
# step=distance_xy/(2+draw_cmd[6])
# step_z=distance_z/(2+draw_cmd[6])
# if x1>x0:
# angle2=angle_xy
# else:
# angle2=math.pi+angle_xy
# x2=x0+(((6-draw_cmd[0])*step)*(math.cos(angle2)))
# y2=y0+(((6-draw_cmd[0])*step)*(math.sin(angle2)))
# z2=z0-(((6-draw_cmd[0])*step_z)*(math.sin(angle_z)))
# x3=x0+(((6-draw_cmd[0])*step+step)*(math.cos(angle2)))
# y3=y0+(((6-draw_cmd[0])*step+step)*(math.sin(angle2)))
# z3=z0-(((6-draw_cmd[0])*step_z+step_z)*(math.sin(angle_z)))
# bge.render.drawLine([x2,y2, z2], [x3,y3,z3], draw_cmd[5])
# draw_cmd[0] = draw_cmd[0]-scene.objects['Terrain']['speed']
# # if scene.objects['Terrain']['speed']<1:
# # draw_cmd[0] = draw_cmd[0]-scene.objects['Terrain']['speed']
# # else:
# # draw_cmd[0] = draw_cmd[0]-1
# # bge.render.drawLine([draw_cmd[2][0]+((6-draw_cmd[0])*0.25)*(math.cos(draw_cmd[4])), draw_cmd[2][1]+((6-draw_cmd[0])*0.25)*(math.sin(draw_cmd[4])),draw_cmd[2][2]],
# # [draw_cmd[2][0]+((6-draw_cmd[0])*0.25+0.25)*(math.cos(draw_cmd[4])), draw_cmd[2][1]+((6-draw_cmd[0])*0.25+0.25)*(math.sin(draw_cmd[4])),draw_cmd[2][2]],
# # draw_cmd[5])
###############################################################################
###############################################################################
# Dessin 2d (bge.render.drawLine)
###############################################################################
@ -783,13 +742,15 @@ def circle (center, radius, color):
bge.render.drawLine([x0,y0,center[2]],[x1,y1,center[2]],color)
ang += ang_step
##
# Affiche les draws 2d en cours
#
# Type de draw 2d:
# Type de draw 2d:
# arrow : [5, "arrow", [obj.worldPosition.x, obj.worldPosition.y, obj.worldPosition.z+0.8],target.name, angle3, ray_yellow,5]
# cast : [30, "cast", [obj.worldPosition.x, obj.worldPosition.y, obj.worldPosition.z+0.8], ray_blue,30]
# ray : [5, "ray", [obj.worldPosition.x, obj.worldPosition.y, obj.worldPosition.z+0.8],[target.worldPosition.x, target.worldPosition.y, target.worldPosition.z], angle3, ray_yellow,5]
#
##
def scn_draw2d(cont):
obj = cont.owner
if obj.sensors['Draw2d'].positive==False:
@ -797,12 +758,12 @@ def scn_draw2d(cont):
if len(scene.objects['Terrain']['draw2d_list'])==0:
scene.objects['Terrain']['draw2d_process']=False
return
# Dépilage des draws à executer
for draw_cmd in scene.objects['Terrain']['draw2d_list']:
# Archer (tir de flêche)
if draw_cmd[1]=="arrow":
if draw_cmd[1]=="arrow":
if draw_cmd[3] in scene.objects:
# x0 = draw_cmd[2][0]+0.25*(math.cos(draw_cmd[4]))
# y0 = draw_cmd[2][1]+0.25*(math.sin(draw_cmd[4]))

View File

@ -14,16 +14,16 @@ import os
#
# This game is a tower defense coding game. The towers are driven by Python code.
# The file is the the map and waves definition.
#
#
###############################################################################
scene = bge.logic.getCurrentScene()
###############################################################################
###############################################################################
# En: Threads management << DONT CHANGE THIS SECTION >>
# Fr: Gestion des tâches (threads) << NE PAS MODIFIER CETTE SECTION >>
###############################################################################
# waves_f={
# 1 : wave1(),
# 2 :wave2(),
@ -181,7 +181,7 @@ def tower_definition():
# Tower caracteristics : category (class), damage, speed, range
tower_carac={
'Archer tower' : ["Archer tower", 1.0 , 0.02, 2.5],
'Archer tower' : ["Archer tower", 1.0 , 0.01, 2.5],
'Mage tower' : ["Mage tower", 0.0 , 0.005, 2.5],
'Barrack' : ["Barrack", 1.0 , 0.0001, 2.5]}
scene.objects['Terrain']['tower_carac'] = tower_carac
@ -224,7 +224,7 @@ def map_init():
ct_map_end(1,-10)
ct_map_endflag(0.5,-10)
ct_map_endflag(1.5,-10)
# Reset counters
def map_reset():
scene.objects['Points']['lifes']=10
@ -235,4 +235,3 @@ def map_reset():
scene.objects['Points']['minions']=0
scene.objects['Points']['minions_run']=0
scene.objects['Points']['wave']=1