import bge # Bibliothèque Blender Game Engine (UPBGE) import aud # Sounds ############################################################################### # ct_doc.py # @title: Documentation # @project: CodeTower # @lang: fr,en # @authors: Philippe Roy # @copyright: Copyright (C) 2022 Philippe Roy # @license: GNU GPL # # 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() # Colors color_doc_chap = (0.153, 0.116, 0.105, 1) # WoodDark color_doc_fct = (0.326, 0.101, 0.0592, 1) # BrownDark # color_doc_fct = (0.577, 0.233, 0.115, 1) # Brown # color_doc_hl = (0.799, 0.617, 0.021, 1) # Yellow color_doc_hl = (0.13, 0.254, 0.407, 1) # BlueDark color_doc_activate = (1.0, 0.025, 0.116, 1) # Red # Sounds audiodev = aud.Device() 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) snd_book_flip = aud.Sound('asset/sounds/book_flip.ogg') sndbuff_book_flip = aud.Sound.cache(snd_book_flip) # UPBGE constants JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED JUST_RELEASED = bge.logic.KX_INPUT_JUST_RELEASED ACTIVATE = bge.logic.KX_INPUT_ACTIVE # JUST_DEACTIVATED = bge.logic.KX_SENSOR_JUST_DEACTIVATED # Functions description fct_description ={} ############################################################################### # Map ############################################################################### map_card=["Level-card", "Sleep-card"] # ct_level () ct_level_title = "\n Get your level" ct_level_text = "\n \n ct_level ()\n \v - Return your level (integer)\n \n" ct_level_text = ct_level_text + " For each new tower, new upgrade, \n new tech or new spell, you spend one \n level-point.\n" fct_description.update({"Level-card" : [ct_level_title, ct_level_text]}) # ct_sleep (delay) ct_sleep_title="\n Time management" ct_sleep_text="\n \n ct_sleep (delay)\n \v - delay : duration in seconds (float)" fct_description.update({"Sleep-card" : [ct_sleep_title, ct_sleep_text]}) ############################################################################### # Tower ############################################################################### tower_card=["Build-card", "Remove-card", "Upgrade-card", "Activate-card", "Tune-card", "Targets-card"] # ct_build (x, y, category, name, color, style) ct_build_title="Build a tower" ct_build_text="ct_build (x,y, category, name, color, style)\n \v- x tower location (integer)\n \v- y tower location (integer)\n" ct_build_text = ct_build_text +" \v- category (string) : \n \v \v- \"Archer tower\" (default value)\n \v \v- \"Mage tower\"\n" ct_build_text = ct_build_text +" \v- name (string)\n \v- color (RGB tuple, default=purple)\n" ct_build_text = ct_build_text +" \v- style (string) : \n \v \v- \"square\" (default value) or \"round\"\n \v \v- version : A (default value), B or C\n \v \v- exemple : 'round-B' \n" ct_build_text = ct_build_text +" \v- Return boolean flag (builded -> True)\n\n" ct_build_text = ct_build_text +"Predefined colors : blue, green, magenta,\n orange, purple, red, turquoise, yellow.\n\n" ct_build_text = ct_build_text +"Exemple : ct_build (1, 1, \"Archer tower\", \n \"Tower #1\", yellow, \"round-A\")\n" fct_description.update({"Build-card" : [ct_build_title, ct_build_text]}) # ct_remove (x, y) ct_remove_title="\n Remove a tower" ct_remove_text="\n \n ct_remove (x, y) \n \v - x tower location (integer)\n \v - y tower location (integer)\n \n" ct_remove_text = ct_remove_text + " When you destroy a tower you regain \n the level-points spent by the tower." fct_description.update({"Remove-card" : [ct_remove_title, ct_remove_text]}) # ct_upgrade (x, y) ct_upgrade_title="\n Upgrade / downgrade a tower" ct_upgrade_text="\n \n ct_upgrade (x, y, value) \n \v - x tower location (integer)\n \v - y tower location (integer)\n \v - value (integer)\n \n" ct_upgrade_text = ct_upgrade_text + " You change the tower level by adding \n the value. \n \n" ct_upgrade_text = ct_upgrade_text + " For downgrading, the value have to \n be negative." fct_description.update({"Upgrade-card" : [ct_upgrade_title, ct_upgrade_text]}) # ct_activate (x, y) ct_activate_title="\n Activate/desactivate a tower" ct_activate_text="\n \n ct_activate (x, y, state) \n \v - x tower location (integer)\n \v - y tower location (integer)\n" ct_activate_text = ct_activate_text +" \v - state (bool) : \n \v \v - True (default value) : set \n \v \v - False : unset\n \n" ct_activate_text = ct_activate_text + " When you desactivate a tower you \n regain the level-points spent by the \n tower.\n \n" ct_activate_text = ct_activate_text + " When you reactivate a tower, you must \n to have the amount of level-points \n spent by the tower." fct_description.update({"Activate-card" : [ct_activate_title, ct_activate_text]}) # ct_tune (x, y) ct_tune_title="Tune a tower" ct_tune_text=" ct_tune (x, y, damage, speed, range) \n \v - x tower location (integer)\n \v - y tower location (integer)\n" ct_tune_text = ct_tune_text + " \v - damage (float)\n \v - speed (float)\n \v - range (float)\n \n" ct_tune_text = ct_tune_text + " You change the tower characteristics. \n" ct_tune_text = ct_tune_text +" The base value for each characteristic \n is 1." ct_tune_text = ct_tune_text +" The tower power is the sum : \n damage + speed + range. \n \n" ct_tune_text = ct_tune_text +" The power depend on the level : \n" ct_tune_text = ct_tune_text +" \v - level = 2^(power-3)\n" ct_tune_text = ct_tune_text +" \v - power = ln(level)/ln(2) + 3\n \n" ct_tune_text = ct_tune_text +" Power : 3 | 4 | 5 | 6 | 7 | 8 | ... \n" ct_tune_text = ct_tune_text +" Level : 1 | 2 | 4 | 8 | 16 | 32 | ... \n" fct_description.update({"Tune-card" : [ct_tune_title, ct_tune_text]}) # ct_targets (x, y) ct_targets_title = "\n Get the targets list" ct_targets_text=" \n \n ct_targets (x, y) \n \v - x tower location (integer)\n \v - y tower location (integer)\n " ct_targets_text = ct_targets_text +" \v- Return the list of minions within \n range of the tower (list of \n targets) \n \n" ct_targets_text = ct_targets_text +" Target (list) : [ name (string), class \n (string), level (integer), distance \n travelled (float)," ct_targets_text = ct_targets_text +" hp (integer), speed \n (float), armor (float)," ct_targets_text = ct_targets_text +" bounty (integer), \n lifes_damage (integer) ]" fct_description.update({"Targets-card" : [ct_targets_title, ct_targets_text]}) ############################################################################### # Tech ############################################################################### tech_card=["Accurate-card", "Volley-card", "Slow_tech-card", "Fire-card", "Bolt-card", "Ice-card", "Acid-card"] # ct_accurate ct_accurate_title="\n Accurate" ct_accurate_text="\n \n ct_accurate (x, y, state) \n \v - x tower location (integer)\n \v - y tower location (integer)\n" ct_accurate_text = ct_accurate_text +" \v - state (bool) : \n \v \v - True : set \n \v \v - False : unset\n \n" ct_accurate_text = ct_accurate_text +" Increases attack damage by 4. \n \n" ct_accurate_text = ct_accurate_text +" It can't be combined with \'volley\' \n or \'slow\'." # ct_accurate_text = ct_accurate_text +" With the accurate mode, the damage \n is multiplied by 4 but the tower speed \n is divided by 2." fct_description.update({"Accurate-card" : [ct_accurate_title, ct_accurate_text]}) # ct_volley ct_volley_title="\n Volley of arrows " ct_volley_text="\n \n ct_volley (x, y, state) \n \v - x tower location (integer)\n \v - y tower location (integer)\n" ct_volley_text = ct_volley_text +" \v - state (bool) : \n \v \v - True : set \n \v \v - False : unset\n \n" ct_volley_text = ct_volley_text +" Fires an volley of 4 arrows.\n \n" ct_volley_text = ct_volley_text +" It can't be combined with \'accurate\' \n or \'slow\'." # ct_volley_text = ct_volley_text +" With the volley mode, the tower fires \n volley of arrows (4 arrows), but the \n damage is divided by 2." fct_description.update({"Volley-card" : [ct_volley_title, ct_volley_text]}) # ct_slow_tech ct_slow_tech_title="\n Slow down the target" ct_slow_tech_text="\n \n ct_slow (x, y, state) \n \v - x tower location (integer)\n \v - y tower location (integer)\n" ct_slow_tech_text = ct_slow_tech_text +" \v - state (bool) : \n \v \v - True : set \n \v \v - False : unset\n \n" ct_slow_tech_text = ct_slow_tech_text +" Finds the vulnerability for slow down \n the target" ct_slow_tech_text = ct_slow_tech_text +" (speed divided by 4).\n \n" ct_slow_tech_text = ct_slow_tech_text +" It can't be combined with \'accurate\' \n or \'volley\'." # ct_slow_tech_text = ct_slow_tech_text +" (minion speed divided by 2)." fct_description.update({"Slow_tech-card" : [ct_slow_tech_title, ct_slow_tech_text]}) # ct_fire ct_fire_title="\n Flaming arrow" ct_fire_text="\n \n ct_fire (x, y, state) \n \v - x tower location (integer)\n \v - y tower location (integer)\n" ct_fire_text = ct_fire_text +" \v - state (bool) : \n \v \v - True : set \n \v \v - False : unset\n \n" ct_fire_text = ct_fire_text +" Deals fire damage.\n \n" ct_fire_text = ct_fire_text +" The tower can't cumulate two arrow \n types (fire, bolt, ice, acid) at once." fct_description.update({"Fire-card" : [ct_fire_title, ct_fire_text]}) # ct_bolt ct_bolt_title="\n Charged arrow" ct_bolt_text="\n \n ct_bolt (x, y, state) \n \v - x tower location (integer)\n \v - y tower location (integer)\n" ct_bolt_text = ct_bolt_text +" \v - state (bool) : \n \v \v - True : set \n \v \v - False : unset\n \n" ct_bolt_text = ct_bolt_text +" Deals lighting damage.\n \n" ct_bolt_text = ct_bolt_text +" The tower can't cumulate two arrow \n types (fire, bolt, ice, acid) at once." fct_description.update({"Bolt-card" : [ct_bolt_title, ct_bolt_text]}) # ct_ice ct_ice_title="\n Ice arrow" ct_ice_text="\n \n ct_ice (x, y, state) \n \v - x tower location (integer)\n \v - y tower location (integer)\n" ct_ice_text = ct_ice_text +" \v - state (bool) : \n \v \v - True : set \n \v \v - False : unset\n \n" ct_ice_text = ct_ice_text +" Deals ice damage.\n \n" ct_ice_text = ct_ice_text +" The tower can't cumulate two arrow \n types (fire, bolt, ice, acid) at once." fct_description.update({"Ice-card" : [ct_ice_title, ct_ice_text]}) # ct_acid ct_acid_title="\n Acid arrow" ct_acid_text="\n \n ct_acid (x, y, state) \n \v - x tower location (integer)\n \v - y tower location (integer)\n" ct_acid_text = ct_acid_text +" \v - state (bool) : \n \v \v - True : set \n \v \v - False : unset\n \n" ct_acid_text = ct_acid_text +" Deals acid damage.\n \n" ct_acid_text = ct_acid_text +" The tower can't cumulate two arrow \n types (fire, bolt, ice, acid) at once." fct_description.update({"Acid-card" : [ct_acid_title, ct_acid_text]}) ############################################################################### # Spell ############################################################################### spell_card=["Slow_spell-card", "Detect-card", "Fear-card", "Aura-card"] # ct_slow_tech ct_slow_tech_title="\n Slow down the target" ct_slow_tech_text="\n \n ct_slow (x, y, state) \n \v - x tower location (integer)\n \v - y tower location (integer)\n" ct_slow_tech_text = ct_slow_tech_text +" \v - state (bool) : \n \v \v - True : set \n \v \v - False : unset\n \n" ct_slow_tech_text = ct_slow_tech_text +" Finds the vulnerability for slow down \n the target" ct_slow_tech_text = ct_slow_tech_text +" (speed divided by 4).\n \n" ct_slow_tech_text = ct_slow_tech_text +" It can't be combined with \'accurate\' \n or \'volley\'." # ct_slow_tech_text = ct_slow_tech_text +" (minion speed divided by 2)." fct_description.update({"Slow_tech-card" : [ct_slow_tech_title, ct_slow_tech_text]}) # ct_slow_spell ct_slow_spell_title="FIXME" ct_slow_spell_text="FIXME\n" fct_description.update({"Slow_spell-card" : [ct_slow_spell_title, ct_slow_spell_text]}) # ct_detect ct_detect_title="FIXME" ct_detect_text="FIXME\n" fct_description.update({"Detect-card" : [ct_detect_title, ct_detect_text]}) # ct_fear ct_fear_title="FIXME" ct_fear_text="FIXME\n" fct_description.update({"Fear-card" : [ct_fear_title, ct_fear_text]}) # ct_aura ct_aura_title="FIXME" ct_aura_text="FIXME\n" fct_description.update({"Aura-card" : [ct_aura_title, ct_aura_text]}) ############################################################################### # Interface ############################################################################### ## # Initialisation du livre ## def init(): # Mettre les pages avec la couleurs par defaut 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 # Mettre les cartes avec la couleurs par defaut for i in range(len(map_card)): scene.objects[map_card[i]].color = color_doc_fct scene.objects[map_card[i]+"-colbox"].suspendPhysics() for i in range(len(tower_card)): scene.objects[tower_card[i]].color = color_doc_fct scene.objects[tower_card[i]+"-colbox"].suspendPhysics() for i in range(len(tech_card)): scene.objects[tech_card[i]].color = color_doc_fct scene.objects[tech_card[i]+"-colbox"].suspendPhysics() for i in range(len(spell_card)): scene.objects[spell_card[i]].color = color_doc_fct scene.objects[spell_card[i]+"-colbox"].suspendPhysics() ## # Ouverture du livre ## def open(): # Afficher le livre 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) scene.objects['Book_text_title'].setVisible(False,True) scene.objects['Book_text'].setVisible(False,True) sound_play (sndbuff_book_open) # Activer la page screen (page par defaut) scene.objects['Book']['page_chap'] = "Book_chap-screen" scene.objects['Book']['page_fct'] = "" 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) # # Mettre les cartes avec la couleurs par defaut # for i in range(len(map_card)): # scene.objects[map_card[i]].color = color_doc_fct # scene.objects[map_card[i]+"-colbox"].suspendPhysics() # for i in range(len(tower_card)): # scene.objects[tower_card[i]].color = color_doc_fct # scene.objects[tower_card[i]+"-colbox"].suspendPhysics() # for i in range(len(tech_card)): # scene.objects[tech_card[i]].color = color_doc_fct # scene.objects[tech_card[i]+"-colbox"].suspendPhysics() # for i in range(len(spell_card)): # scene.objects[spell_card[i]].color = color_doc_fct # scene.objects[spell_card[i]+"-colbox"].suspendPhysics() ## # Fermeture du livre ## def close(): sound_play (sndbuff_book_close) # Cacher les cartes de fonction scene.objects['Book_page_screen'].setVisible(False,True) scene.objects['Book_page_map'].setVisible(False,True) scene.objects['Book_page_tower'].setVisible(False,True) scene.objects['Book_page_tech'].setVisible(False,True) scene.objects['Book_page_spell'].setVisible(False,True) # Mettre les cartes avec la couleurs par defaut for i in range(len(map_card)): scene.objects[map_card[i]+"-colbox"].suspendPhysics() for i in range(len(tower_card)): scene.objects[tower_card[i]+"-colbox"].suspendPhysics() for i in range(len(tech_card)): scene.objects[tech_card[i]+"-colbox"].suspendPhysics() for i in range(len(spell_card)): scene.objects[spell_card[i]+"-colbox"].suspendPhysics() # Cacher le livre 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'].worldPosition = [28, 15, 14] # Position dans Blender [28, 0.84549, 1.53626] scene.objects['Book_page_screen'].worldPosition = scene.objects['Book'].worldPosition ## # Highlight du livre ## def 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:10]=="chap-": scene.objects[name].color = color_doc_chap else: scene.objects[name].color = color_doc_fct ## # Afficher le chapitre ## def chapter(cont): if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive and cont.sensors['Click'].positive: sound_play (sndbuff_book_flip) obj = cont.owner last_name_chap= scene.objects['Book']['page_chap'] name_chap= obj.name[:-7] scene.objects[last_name_chap].color = color_doc_chap scene.objects[name_chap].color = color_doc_activate scene.objects['Book']['page_chap'] = name_chap last_name_fct= scene.objects['Book']['page_fct'] if last_name_fct != "": scene.objects[last_name_fct].color = color_doc_fct scene.objects['Book']['page_fct'] = "" scene.objects['Book_text_title'].setVisible(False,True) scene.objects['Book_text'].setVisible(False,True) # Effacer les fonctions cartes scene.objects['Book_page_screen'].setVisible(False,True) scene.objects['Book_page_map'].setVisible(False,True) scene.objects['Book_page_tower'].setVisible(False,True) scene.objects['Book_page_tech'].setVisible(False,True) scene.objects['Book_page_spell'].setVisible(False,True) # Désactiver les cartes for i in range(len(map_card)): scene.objects[map_card[i]+"-colbox"].suspendPhysics() for i in range(len(tower_card)): scene.objects[tower_card[i]+"-colbox"].suspendPhysics() for i in range(len(tech_card)): scene.objects[tech_card[i]+"-colbox"].suspendPhysics() for i in range(len(spell_card)): scene.objects[spell_card[i]+"-colbox"].suspendPhysics() # Afficher la page Screen if name_chap == "Book_chap-screen": scene.objects['Book_page_screen'].worldPosition = scene.objects['Book'].worldPosition scene.objects['Book_page_screen'].setVisible(True,True) # Afficher la page Map if name_chap == "Book_chap-map": scene.objects['Book_page_map'].worldPosition = scene.objects['Book'].worldPosition scene.objects['Book_page_map'].setVisible(True,True) for i in range(len(map_card)): scene.objects[map_card[i]+"-colbox"].restorePhysics() # Afficher la page Tower if name_chap == "Book_chap-tower": scene.objects['Book_page_tower'].worldPosition = scene.objects['Book'].worldPosition scene.objects['Book_page_tower'].setVisible(True,True) for i in range(len(tower_card)): scene.objects[tower_card[i]+"-colbox"].restorePhysics() # Afficher la page Tech if name_chap == "Book_chap-tech": scene.objects['Book_page_tech'].worldPosition = scene.objects['Book'].worldPosition scene.objects['Book_page_tech'].setVisible(True,True) for i in range(len(tech_card)): scene.objects[tech_card[i]+"-colbox"].restorePhysics() # Afficher la page Speel if name_chap == "Book_chap-spell": scene.objects['Book_page_spell'].worldPosition = scene.objects['Book'].worldPosition scene.objects['Book_page_spell'].setVisible(True,True) for i in range(len(spell_card)): scene.objects[spell_card[i]+"-colbox"].restorePhysics() ## # Afficher les details de la fonction à partir d'une carte ## def card (cont): if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive : sound_play (sndbuff_book_flip) obj = cont.owner # Highlight des cartes last_name_fct= scene.objects['Book']['page_fct'] name_fct= obj.name[:-7] if last_name_fct != "": scene.objects[last_name_fct].color = color_doc_fct scene.objects['Book']['page_fct'] = name_fct scene.objects[name_fct].color = color_doc_activate # Afficher le texte de la fonction # print ("name_fct : ", name_fct) # print ("fct_description : ", fct_description) scene.objects['Book_text_title']['Text'] = fct_description[name_fct][0] scene.objects['Book_text']['Text'] = fct_description[name_fct][1] scene.objects['Book_text_title'].setVisible(True, False) scene.objects['Book_text'].setVisible(True, False) ## # Sounds ## def sound_play (sound): if scene.objects['Commands']['sound']: audiodev.play(sound)