1. Sami Loone
  2. Fat x Fast

Source

Fat x Fast / FatxFast / gui / hud / buffframe.py

# This file is part of FatxFast.                                                
#                                                                               
#    FatxFast is free software: you can redistribute it and/or modify           
#    it under the terms of the GNU General Public License as published by       
#    the Free Software Foundation, either version 3 of the License, or          
#    (at your option) any later version.                                        
#                                                                               
#    FatxFast is distributed in the hope that it will be useful,                
#    but WITHOUT ANY WARRANTY; without even the implied warranty of             
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              
#    GNU General Public License for more details.                               
#                                                                               
#    You should have received a copy of the GNU General Public License          
#    along with FatxFast.  If not, see <http://www.gnu.org/licenses/>.          
                                                                                
# Versioning based on:                                                          
# http://en.wikipedia.org/wiki/Versioning#Designating_development_stage         
__author__ = "dryatu (c) 2013"                                                  
__version__ = "1.2.5" 

from FatxFast.gui.menuwidget import MenuWidget
from FatxFast.gui.boxwidget import BoxWidget
from FatxFast.gui.textwidget import TextWidget
from FatxFast.gui.hud.frame import Frame
import time

class BuffFrame(Frame):
    # Part of GameFrame
    def __init__(self, game, player, **kwargs):
        super(BuffFrame, self).__init__(game, **kwargs)
        self.player = player
        self._last_update = 0
        self._update_interval = 0.33
        self._buff_container = MenuWidget(container=self, **kwargs)
        self.add_widget("buff-container", self._buff_container)
        self._id_to_bufflist = {}

    def _add_buff(self, buff):
        if buff.entity_id not in self._id_to_bufflist:
            self._id_to_bufflist[buff.entity_id] = [] 
            self._create_buff_widget(buff)
        if buff not in self._id_to_bufflist[buff.entity_id]:
            self._id_to_bufflist[buff.entity_id].append(buff) 

    def _create_buff_widget(self, buff):
        attr = self.fetch_template("buffwidget")
        attr["name"] = "buff-"+str(buff.entity_id)
        attr["container"] = self._buff_container
        attr["colorkey"] = buff.image.get_colorkey()
        buffwidget = BoxWidget(**attr)
        buffwidget.image = buff.image
        self._buff_container.add_widget(attr["name"], buffwidget)
        self._create_buff_timer_widget(buffwidget, buff.entity_id)

    def _create_buff_timer_widget(self, buffwidget, entity_id):
        """creates the (timer)text for the buffwidget"""
        attr = self.fetch_template("bufftext")
        attr["name"] += "-"+str(entity_id)
        attr["container"] = buffwidget
        bufftext = TextWidget(**attr)
        buffwidget.add_widget(attr["name"], bufftext)

    def _create_buff_stack_widget(self, buffwidget, entity_id):
        # Buff stack size text
        attr = self.fetch_template("buffstacksizetext")
        attr["container"] = buffwidget
        attr["name"] += "-"+str(entity_id)
        stacksize = TextWidget(**attr)
        buffwidget.add_widget(attr["name"], stacksize)

    def _remove_expired_buffs(self, entity_id, buff_list):
        if len(buff_list) == 1:
            self.delete_widget("buffstacksizetext-"+str(entity_id))
        for buff in list(buff_list):
            if buff not in self.player.buffs:
                self._id_to_bufflist[entity_id].remove(buff)

    def _remove_buff(self, entity_id):
        self._buff_container.delete_widget("buff-"+str(entity_id))
        self._buff_container.fix_children_y_pos()
        del self._id_to_bufflist[entity_id]

    def update(self, dt):
        if time.time()-self._last_update > self._update_interval:
            for buff in self.player.buffs:
                if buff.invisible:
                    continue
                self._add_buff(buff)
            for entity_id, buff_list in self._id_to_bufflist.items():
                self._remove_expired_buffs(entity_id, buff_list)
                if not len(buff_list):
                    self._remove_buff(entity_id)
                else:
                    self._update_buff_widget(entity_id, buff_list)
            self._last_update = time.time()

    def _update_buff_widget(self, entity_id, buff_list):
        if len(buff_list) > 1:
            widget = self.get_widget(
                "buffstacksizetext-"+str(entity_id))
            if widget is None:
                self._create_buff_stack_widget(
                    self.get_widget(
                        "buff-"+str(entity_id)), entity_id)
            widget = self.get_widget(
                "buffstacksizetext-"+str(entity_id)).text \
                = str(len(buff_list))
        max_duration = max([buff.duration for buff in buff_list])
        self.get_widget(
            "bufftext"+"-"+str(entity_id)).text \
                = str(max_duration/1000)