Source

Fat x Fast / FatxFast / objects / buffs.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"


import pygame
from FatxFast.objects.entity import GameEntity

# Buff IDs

COFFEE_BUFF_ID = 513
WARP_DEBUFF_ID = 514
RAGE_BUFF_ID = 515

class Buff(GameEntity):
    
    def __init__(self, duration, **kwargs):
        super(Buff, self).__init__(colorkey="blue", **kwargs)
        self.duration = duration

    def clean_up(self):
        pass

    def kill(self):
        super(Buff, self).kill()
        self.clean_up()

    def trigger(self):
        pass

    def update(self, dt): 
        self.duration -= dt

class CharacterBuff(Buff):
    
    def __init__(self, character, duration, **kwargs):
        super(CharacterBuff, self).__init__(duration, **kwargs)
        self.character = character

class WarpDebuff(CharacterBuff):
    def __init__(self, character, duration, **kwargs):
        super(WarpDebuff, self).__init__(character, duration, 
            imagename="warpdebuff.png", entity_id=WARP_DEBUFF_ID, **kwargs)

class SpeedBuff(CharacterBuff):
    
    def __init__(self, character, duration, **kwargs):
        super(SpeedBuff, self).__init__(character, duration, invisible=0,
            imagename="coffee.png", entity_id=COFFEE_BUFF_ID)
        self.speedmultiplier = 1.2
        self.capspeed = self.character.physics.get_default_maxspeed()*\
            (self.speedmultiplier**4)

    def _increase_speed(self):
        new_value = self.character.physics.maxspeed*self.speedmultiplier
        if (new_value-self.capspeed) <= 0.001:
            self.character.physics.maxspeed = new_value
            return True

    def trigger(self):
        # In case no new buff can be added, remove oldest
        if not self._increase_speed():
            buff_to_replace = min(self.character.buffs, 
                key=lambda x: x.duration)
            if buff_to_replace:
                buff_to_replace.kill()
                self._increase_speed()
            else:
                self.kill()
            
    def clean_up(self):
        self.character.physics.maxspeed /= self.speedmultiplier

class RageBuff(CharacterBuff):
    
    def __init__(self, character, duration, **kwargs):
        super(RageBuff, self).__init__(character, duration, invisible=1, 
            entity_id=RAGE_BUFF_ID)
        self.character.set_animation_frames("charrage.png")

    def clean_up(self):
        self.character.set_animation_frames("char.png")

BUFF_CLASSES = {
            WARP_DEBUFF_ID: WarpDebuff,
            COFFEE_BUFF_ID: SpeedBuff}