Exemple #1
0
class Main(AVGApp):
    
    multitouch = True
    
    def __init__(self, parent):
        
        self.configReader = ConfigReader()
        self.mainMenu = MainMenu(start_cap_mode=self.startCapCallback, 
                                 start_city_defense_mode=self.startCityDefenseCallback,
                                 start_versus_2_mode=self.startVersus2Callback, 
                                 start_versus_3_mode=self.startVersus3Callback,
                                 pos=(0,0), size=(1280,800))
        parent.appendChild(self.mainMenu)
        self.rootNode = parent
        self._theme = themes.StandardTheme()
                
    def endGameAndShowMenuAgain(self, fade=True, oldNode=None):
        
        if fade:
            self.rootNode.appendChild(self.mainMenu)
            self.mainMenu.opacity = 0
            avg.LinearAnim(self.gameNode, "opacity", 2000, 1, 0, False, None, self.gameNode.delete).start()
            avg.LinearAnim(self.mainMenu, "opacity", 3000, 0, 1).start()
        else:
            self.gameNode.delete()
            self.rootNode.insertChildBefore(self.mainMenu, oldNode)
            self.mainMenu.opacity = 1
           
    def startCapCallback(self, event):
        self.mainMenu.coop_chooser.slideOutToLeft()
        self.configReader.refresh()
        currentDifculty =  self.configReader.get_current_difficulty(GameModes.ComeAndPlay)
        settings, items = self.configReader.get_game_info(GameModes.ComeAndPlay,
                                                                        currentDifculty)
        infos = self.configReader.get_infos()
        self.mainMenu.unlink(False)      
        self.gameNode = ComeAndPlayGameNode(settings, items, self.endGameAndShowMenuAgain,
                                    size=util.WINDOW_SIZE, parent = self.rootNode,
                                    infos=infos)
      
    def startCityDefenseCallback(self, event):
        self.mainMenu.coop_chooser.slideOutToLeft()
       
        self.mainMenu.unlink(False)
        self.configReader.refresh()
        currentDifculty =  self.configReader.get_current_difficulty(GameModes.CityDefense)
        settings, items = self.configReader.get_game_info(GameModes.CityDefense,
                                                          currentDifculty)
        infos = self.configReader.get_infos()
        
        self.gameNode = CityDefenderGameNode(settings, items,
                                             self.endGameAndShowMenuAgain,
                                             size=util.WINDOW_SIZE,
                                             parent = self.rootNode,
                                             infos=infos,
                                             )
       
    def startVersus2Callback(self, event):
        self.mainMenu.coop_chooser.slideOutToLeft()
        self.mainMenu.unlink(False)
        self.configReader.refresh()
        currentDifculty =  self.configReader.get_current_difficulty(GameModes.Versus2P)
      
        settings, items = self.configReader.get_game_info(GameModes.Versus2P,
                                                          currentDifculty)
        infos = self.configReader.get_infos()
        self.gameNode = VersusModeNode(2, settings, items,
                                       self.endGameAndShowMenuAgain,
                                       size=util.WINDOW_SIZE,
                                       parent = self.rootNode,
                                       infos=infos)

    def startVersus3Callback(self, event):
        self.mainMenu.coop_chooser.slideOutToLeft()
        self.mainMenu.unlink(False)
        self.configReader.refresh()
        currentDifculty =  self.configReader.get_current_difficulty(GameModes.Versus3P)
      
        settings, items = self.configReader.get_game_info(GameModes.Versus3P,
                                                         currentDifculty)
        infos = self.configReader.get_infos()
        self.gameNode = VersusModeNode(3, settings, items,
                                       self.endGameAndShowMenuAgain,
                                       size=util.WINDOW_SIZE,
                                       parent = self.rootNode,
                                       infos=infos)

    def onKeyDown(self, event):
        if event.keystring == "b":
            node = self.crystalGenerator.getNextCrystalConstructor(self)(1, self.gameNode.space, pos=(400,500), parent=self.gameNode)
            node._body.apply_impulse((0,-825))
        elif event.keystring == "x":
            node = nodes.ShieldNode(self.gameNode.space, pos1=(200,200), pos2=(600, 200), strokewidth=20, color="FF0000", parent=self.gameNode)
        elif event.keystring == "r":
            self.gameNode.toggleRotations()
class SettingsBox(SubBox):
    
    def __init__(self, **kwargs):
        
        SubBox.__init__(self, **kwargs)

        self.config = ConfigReader()
        
        self.slots = []
        self.itemButton = None
        
        game_mode_size = (self.descr_div.size[0]*0.5,self.descr_div.size[1]*0.15)
        self.game_mode_img = avg.ImageNode(href=self.theme.getStaticImage(self.game_mode), 
                                       size=game_mode_size,
                                       pos=(self.descr_div.size[0]/2-game_mode_size[0]/2,-self.descr_div.size[0]*0.01),
                                       parent=self.descr_div
                                       )
        
        self._appendSlot(pos=0, image="difficulty", startVal=0, endVal=3, difficulty=True, append=False)
        
        if self.game_mode == "come_n_play":
            self._appendSlot(pos=1, image="rotation_speed")
            self._appendSlot(pos=2, image="growing_speed")
            self._appendSlot(pos=3, image="crystals", startVal=4, endVal=8)
            self._appendItemButton()
        
        elif self.game_mode == "city_defense":
            self._appendSlot(pos=1, image="rotation_speed")
            self._appendSlot(pos=2, image="growing_speed")
            self._appendSlot(pos=3, image="shooting_speed")
            self._appendSlot(pos=4, image="crystals", startVal=4, endVal=8)
            self._appendItemButton()
        
        elif self.game_mode == "versus_2":
            self._appendSlot(pos=1, image="growing_speed")
            self._appendSlot(pos=2, image="crystals", startVal=4, endVal=8)
            self._appendItemButton()
        
        elif self.game_mode == "versus_3":
            self._appendSlot(pos=1, image="growing_speed")
            self._appendSlot(pos=2, image="crystals", startVal=4, endVal=8)
            self._appendItemButton()
        
        if not self.config.get_current_difficulty(self.game_mode) == "custom":
                self.deactivateSlots()
        
    def _appendSlot(self, pos, image, startVal=0, endVal=100, difficulty=False, append=True):
        
        settings_size_left = (self.descr_div.size[0]*0.5, self.descr_div.size[1]*0.13)
        settings_size_right = (self.descr_div.size[0]*0.35, self.descr_div.size[1]*0.13)
        
        left = avg.ImageNode(href=self.theme.getStaticImage(image), 
                            size=settings_size_left,
                            pos=(0,self.descr_div.size[1]*(pos+1)*0.14),
                            parent=self.descr_div
                            )
            
        right = avg.DivNode(
                            size=settings_size_right,
                            pos=(self.descr_div.size[0]*0.6,self.descr_div.size[1]*(pos+1)*0.14),
                            parent=self.descr_div
                            )

        difficulties = ["easy", "medium", "hard", "custom"]
        
        if difficulty==True:
            curr_diff = self.config.get_current_difficulty(self.game_mode)
            initVal = difficulties.index(curr_diff)
        else:
            infos = self.config.get_game_info(self.game_mode, "custom")[0]
            initVal = int(infos[image])
      
        self.Selector(settingsBox=self, setting_name=image, initVal=initVal, parent=right, startVal=startVal, endVal=endVal, difficulty=difficulty)
        
        if append:
            self.slots.append((left,right))
            
    def _appendItemButton(self):
         
        self.itemButton = MenuButton(
                                 upImg="ItemsUp",
                                 downImg="ItemsDown", 
                                 pos=(self.descr_div.size[0]/2,self.descr_div.size[1]-self.descr_div.size[1]*0.1),
                                 size=(self.descr_div.size[0]*0.5, self.descr_div.size[1]*0.13),
                                 clickHandler=lambda event: self._showItemSettings(),
                                 parent=self.descr_div
                                 )
        
         
    def _showItemSettings(self):
        
        self.item_settings = []
        
        new_bg = avg.RectNode(fillcolor="1D1100",fillopacity=1, strokewidth=0, size=self.descr_div.size, parent=self.descr_div)
        self.item_settings.append(new_bg)
        
        back = MenuButton(
                          upImg="BackUp",
                          downImg="BackDown", 
                          pos=(self.descr_div.size[0]/2,self.descr_div.size[1]-self.descr_div.size[1]*0.02),
                          size=(self.descr_div.size[0]*0.5, self.descr_div.size[1]*0.13),
                          clickHandler=lambda event: self._hideItemSettings(),
                          parent=self.descr_div
                          )
        
        self.item_settings.append(back)
        
        items = self.config.get_game_info(self.game_mode, "custom")[1]
        
        cannon = avg.ImageNode(
                               href=self.theme.getStaticImage("Structure"),
                               size=(self.descr_div.size[0]*0.45, self.descr_div.size[1]*0.1),
                               pos=(0,0),
                               parent=self.descr_div
                               ) 
        
        structure = avg.ImageNode(
                               href=self.theme.getStaticImage("Cannon"),
                               size=(self.descr_div.size[0]*0.45, self.descr_div.size[1]*0.1),
                               pos=(self.descr_div.size[0]*0.5,0),
                               parent=self.descr_div
                               ) 
        
        self.item_settings.append(cannon)
        self.item_settings.append(structure)

        
        i=0
        j=7  
        for item in items.keys():
            if item.endswith("Crystal"):
                i = i+1
                self.item_settings.append(self.ItemSelector(item, int(items[item]), i, self).selector_div)
            elif item.endswith("Element"):
                j = j+1
                self.item_settings.append(self.ItemSelector(item, int(items[item]), j, self).selector_div)

        
    def _hideItemSettings(self):
        for element in self.item_settings:
            element.unlink()
                
    def deactivateSlots(self):
        for (left,right) in self.slots:
            left.active = False
            right.active = False
        if not self.itemButton == None:
            self.itemButton.active = False
            
    def activateSlots(self):
        for (left,right) in self.slots:
            left.active = True
            right.active = True
        if not self.itemButton == None:
            self.itemButton.active = True
            
            
    class ItemSelector():
        
        def __init__(self, item, initVal, pos, settingsBox):
            
            self.item = item
            self.current_value = initVal
            self.settingsBox = settingsBox
            
            
            self.__upTimer = None
            self.__downTimer = None
                
            def startContInc():
                self.next(val)
                self.__upTimer = avg.Player.get().setTimeout(1, startContInc)
                
            def stopContInc():
                if self.__upTimer is not None:
                    avg.Player.get().clearInterval(self.__upTimer)
                        
            def startContDec():
                self.prev(val)
                self.__downTimer = avg.Player.get().setTimeout(1, startContDec)
                
            def stopContDec():
                if self.__downTimer is not None:
                    avg.Player.get().clearInterval(self.__downTimer)    
        
            
            theme = settingsBox.theme
            parent = settingsBox.descr_div
            
            if pos < 7:
                self.selector_div = avg.DivNode(
                                           size=(parent.size[0]*0.45, parent.size[1]*0.08),
                                           pos=(0,parent.size[1]*(pos)*0.12),
                                           parent=parent
                                           ) 
            else:
                self.selector_div = avg.DivNode(
                                           size=(parent.size[0]*0.45, parent.size[1]*0.08),
                                           pos=(parent.size[0]*0.5,parent.size[1]*(pos-7)*0.12),
                                           parent=parent
                                           )
                
            button_size = (self.selector_div.size[0]*0.2, self.selector_div.size[1])
            
            avg.ImageNode(
                          href=theme.getStaticImage(item[0:len(item)-7]),
                          size=(self.selector_div.size[0]*0.5, self.selector_div.size[1]),
                          parent=self.selector_div
                          )
            
            dec = MenuButton(
                            upImg="LessUp",
                            downImg="LessDown", 
                            pos=(self.selector_div.size[0]*0.5 + button_size[0]/2, button_size[1]/2),
                            size=button_size,
                            clickHandler=lambda event: self.prev(val),
                            parent=self.selector_div
                            )
            
            inc = MenuButton(
                            upImg="GreaterUp",
                            downImg="GreaterDown", 
                            pos=(self.selector_div.size[0]*0.8 + button_size[0]/2, button_size[1]/2),
                            size=button_size,
                            clickHandler=lambda event: self.next(val),
                            parent=self.selector_div
                            )
            
            val = avg.WordsNode(
                                text=str(self.current_value)+"%",
                                pos=(self.selector_div.size[0]*0.65 + button_size[0]/2, self.selector_div.size[1]*0.1),
                                size=button_size,
                                alignment="center",
                                fontsize=30,
                                color="FF9600",
                                parent=self.selector_div,
                                sensitive=False
                                )
            
            ui.HoldRecognizer(node=inc,
                              detectedHandler=lambda event: startContInc(),
                              stopHandler=lambda event: stopContInc()
                              )
            
            ui.HoldRecognizer(node=dec,
                              detectedHandler=lambda event: startContDec(),
                              stopHandler=lambda event: stopContDec()
                              )
            
        def next(self, val):
                self.current_value = (self.current_value + 1) % 101
                val.text = str(self.current_value) + "%"
                self.settingsBox.config.set_item_probability(self.settingsBox.game_mode, self.item, str(self.current_value))
                
        def prev(self, val):
                self.current_value = (self.current_value - 1) % 101
                val.text = str(self.current_value) + "%"
                self.settingsBox.config.set_item_probability(self.settingsBox.game_mode, self.item, str(self.current_value))

            
    class Selector():
        
        def __init__(self, settingsBox, setting_name, parent, initVal, startVal, endVal, difficulty):
            
            self.setting_name = setting_name
            self.parent = parent
            self.startVal = startVal
            self.endVal = endVal
            self.current_value = initVal
            self.settingsBox = settingsBox
            
            self.difficulty = difficulty
            self.difficulties = ["easy", "medium", "hard", "custom"]
            
            self.__upTimer = None
            self.__downTimer = None
            
            def startContInc():
                self.next(val)
                self.__upTimer = avg.Player.get().setTimeout(1, startContInc)
            
            def stopContInc():
                if self.__upTimer is not None:
                    avg.Player.get().clearInterval(self.__upTimer)
                    
            def startContDec():
                self.prev(val)
                self.__downTimer = avg.Player.get().setTimeout(1, startContDec)
            
            def stopContDec():
                if self.__downTimer is not None:
                    avg.Player.get().clearInterval(self.__downTimer)    

        
            dec = MenuButton(
                            upImg="LessUp",
                            downImg="LessDown", 
                            pos=(self.parent.size[0]*0.2*0.5, self.parent.size[1]/2),
                            size=(self.parent.size[0]*0.2, self.parent.size[1]),
                            pressHandler=lambda event: self.prev(val),
                            parent=self.parent
                            )
            
            inc = MenuButton(
                            upImg="GreaterUp",
                            downImg="GreaterDown", 
                            pos=(self.parent.size[0]-self.parent.size[0]*0.2*0.5,self.parent.size[1]/2),
                            size=(self.parent.size[0]*0.2, self.parent.size[1]),
                            pressHandler=lambda event: self.next(val),
                            parent=self.parent
                            )
            
            val = avg.WordsNode(
                                text=self._getText(),
                                pos=(self.parent.size[0]/2,self.parent.size[1]*0.1),
                                size=(self.parent.size[0]*0.6, self.parent.size[1]),
                                alignment="center",
                                fontsize=30,
                                color="FF9600",
                                parent=self.parent,
                                sensitive=False
                                )
            
            ui.HoldRecognizer(node=inc,
                              detectedHandler=lambda event: startContInc(),
                              stopHandler=lambda event: stopContInc()
                              )
            
            ui.HoldRecognizer(node=dec,
                              detectedHandler=lambda event: startContDec(),
                              stopHandler=lambda event: stopContDec()
                              )
  
            
        def next(self, val, stepSize=1):
                self.current_value = ((self.current_value - self.startVal + stepSize) % (self.endVal - self.startVal + 1)) + self.startVal
                
                val.text = self._getText()
                    
                self.settingsBox.config.set_setting(game_mode=self.settingsBox.game_mode,
                                                   id=self.setting_name,
                                                   new_val=self._getText())
                
                if not (val.text == "custom") and self.setting_name == "difficulty":
                    self.settingsBox.deactivateSlots()
                else:
                    self.settingsBox.activateSlots()
                
                
        def prev(self, val, stepSize=1):
                self.current_value = ((self.current_value - self.startVal - stepSize) % (self.endVal - self.startVal + 1)) + self.startVal
                
                val.text = self._getText()
                    
                self.settingsBox.config.set_setting(game_mode=self.settingsBox.game_mode,
                                                   id=self.setting_name,
                                                   new_val=self._getText())
                
                if not (val.text == "custom") and self.setting_name == "difficulty":
                    self.settingsBox.deactivateSlots()
                else:
                    self.settingsBox.activateSlots()
                    
                    
        def _getText(self):
            if self.difficulty:
                return self.difficulties[self.current_value]
            else: 
                return str(self.current_value)