Example #1
0
    def __init__(self, combatants):

        self.engine = annchienta.getEngine()
        self.videoManager = annchienta.getVideoManager()
        self.inputManager = annchienta.getInputManager()
        self.mapManager = annchienta.getMapManager()
        self.audioManager = annchienta.getAudioManager()
        self.cacheManager = annchienta.getCacheManager()
        self.mathManager = annchienta.getMathManager()
        self.sceneManager = scene.getSceneManager()
        self.battleManager = getBattleManager()

        self.activeCombatants = self.combatants = combatants
        self.running = True

        self.enemies = filter(lambda c: c.hostile, self.activeCombatants)
        self.allies = filter(lambda c: not c.hostile, self.activeCombatants)

        # Set positions for combatants
        for i in range(len(self.allies)):
            a = self.allies[i]
            w, h = a.getSize()
            a.setPosition(
                self.videoManager.getScreenWidth() / 2 - 30 - i * 10 - w,
                100 + i * 40 - h)

        for i in range(len(self.enemies)):
            e = self.enemies[i]
            w, h = e.getSize()
            e.setPosition(self.videoManager.getScreenWidth() / 2 + 30 + i * 10,
                          100 + i * 40 - h)

        self.updateCombatantArrays()

        self.background = None
Example #2
0
    def __init__( self ):

        self.engine = annchienta.getEngine()
        self.videoManager = annchienta.getVideoManager()
        self.inputManager = annchienta.getInputManager()
        self.cacheManager = annchienta.getCacheManager()
        self.mapManager   = annchienta.getMapManager()
        self.mathManager  = annchienta.getMathManager()
        self.sceneManager = SceneManager.getSceneManager()

        # Background spriteace
        self.background = annchienta.Surface( "images/backgrounds/sky.png" )
        self.backgroundY = 0.0

        # Create a ship for the player
        self.ship = GameObject( annchienta.Vector( videoManager.getScreenWidth()/2, videoManager.getScreenHeight()/2 ),
                                annchienta.Surface( "sprites/ship_small.png" ) )

        # Load sprites into cache
        self.enemySprite = annchienta.Surface("sprites/ship_pirate.png")

        # All enemies
        self.enemies = []

        # The final enemy
        self.captain = None

        # Number of miliseconds we need to fly to gain victory
        self.victoryTime = 60000
Example #3
0
    def __init__(self):

        self.audioManager = annchienta.getAudioManager()
        self.mathManager = annchienta.getMathManager()
        self.logManager = annchienta.getLogManager()
        self.engine = annchienta.getEngine()

        self.enemiesInMap = []
        self.battleBackground = None
        self.randomBattleDelay = self.mathManager.randInt(300, 1000)
Example #4
0
    def __init__(self, m_battle, m_combatant):

        self.cacheManager = annchienta.getCacheManager()
        self.audioManager = annchienta.getAudioManager()
        self.mathManager = annchienta.getMathManager()
        self.sceneManager = scene.getSceneManager()

        self.m_combatant = m_combatant
        self.m_battle = m_battle
        self.turns = 0
Example #5
0
    def __init__(self):

        self.engine = annchienta.getEngine()
        self.mathManager = annchienta.getMathManager()
        self.videoManager = annchienta.getVideoManager()
        self.inputManager = annchienta.getInputManager()
        self.logManager = annchienta.getLogManager()
        self.audioManager = annchienta.getAudioManager()
        self.partyManager = PartyManager.getPartyManager()

        self.enemiesLocation = "battle/enemies.xml"
        self.enemiesFile = xml.dom.minidom.parse(self.enemiesLocation)

        self.randomBattleDelay = self.mathManager.randInt(300, 400)
        self.background = None

        # Drum on battle start
        self.drum = annchienta.Sound("sounds/battle.ogg")

        self.randomBattleEnemies = []
Example #6
0
    def __init__(self, combatants, background, canFlee):

        # Set variables
        self.combatants = combatants
        self.running = True
        self.background = background
        self.canFlee = canFlee
        self.won = False
        self.actionLock = False

        # Get references
        self.engine = annchienta.getEngine()
        self.videoManager = annchienta.getVideoManager()
        self.inputManager = annchienta.getInputManager()
        self.cacheManager = annchienta.getCacheManager()
        self.mathManager = annchienta.getMathManager()
        self.mapManager = annchienta.getMapManager()
        self.audioManager = annchienta.getAudioManager()
        self.sceneManager = SceneManager.getSceneManager()
        self.partyManager = PartyManager.getPartyManager()

        # Lines for the 'console' window
        self.lines = []
Example #7
0
    def __init__( self ):

        # Get references
        self.engine = annchienta.getEngine()
        self.videoManager = annchienta.getVideoManager()
        self.inputManager = annchienta.getInputManager()
        self.cacheManager = annchienta.getCacheManager()
        self.mapManager   = annchienta.getMapManager()
        self.mathManager  = annchienta.getMathManager()
        self.sceneManager = SceneManager.getSceneManager()

        # Load images
        self.background = annchienta.Surface("images/backgrounds/water.png")
        self.cacheManager.getSurface("sprites/rock1.png")
        self.cacheManager.getSurface("sprites/rock2.png")

        # Initial positions
        self.backgroundY = 0.0
        self.speed = 0.1
        self.nextSpawn = 500
        self.rocks = []

        self.raft = RaftObject( annchienta.Surface("sprites/raft.png"), annchienta.Vector( self.videoManager.getScreenWidth()/2, self.videoManager.getScreenHeight()/2 ) )
Example #8
0
    def __init__(self):

        # Get some references
        self.engine = annchienta.getEngine()
        self.inputManager = annchienta.getInputManager()
        self.mapManager = annchienta.getMapManager()
        self.cacheManager = annchienta.getCacheManager()
        self.mathManager = annchienta.getMathManager()
        self.sceneManager = SceneManager.getSceneManager()

        # Set variables
        self.player = 0
        self.records = []
        self.inventory = 0
        self.lastMaps = []
        self.currentMap = 0
        self.startTime = 0

        # Create a map loader
        self.mapLoader = MapLoader()

        # Battle variables
        self.randomBattleDelay = self.mathManager.randInt(300, 400)
Example #9
0
class SceneManager:

    margin = 6
    defaultFont, italicsFont = None, None
    boxTextures = []

    engine = annchienta.getEngine()
    videoManager = annchienta.getVideoManager()
    inputManager = annchienta.getInputManager()
    audioManager = annchienta.getAudioManager()
    cacheManager = annchienta.getCacheManager()
    mathManager = annchienta.getMathManager()
    mapManager = annchienta.getMapManager()

    def waitForClick(self):
        self.videoManager.storeBuffer(7)
        self.inputManager.update()
        while self.inputManager.running(
        ) and not self.inputManager.buttonTicked(0):
            self.inputManager.update()
            self.videoManager.clear()
            self.videoManager.restoreBuffer(7)
            self.videoManager.flip()

        self.mapManager.resync()

    def defaultColor(self):
        self.videoManager.setColor(255, 255, 255)

    def activeColor(self):
        self.videoManager.setColor(255, 255, 0)

    def inactiveColor(self):
        self.videoManager.setColor(170, 170, 170)

    ## \brief Draw a box.
    #
    #  A box is the main GUI element. You can customize the box layout
    #  by changing bitmaps.
    def drawBox(self, x1, y1, x2, y2):

        #self.videoManager.push()
        #self.videoManager.identity()
        self.videoManager.boxBlur(x1, y1, x2, y2)
        #self.videoManager.pop()

        # If there are not enough textures, just draw a stupid simple box.
        if (len(self.boxTextures) <= 8):
            self.videoManager.setColor(100, 100, 150, 200)
            self.videoManager.drawRectangle(x1, y1, x2, y2)
            self.videoManager.setColor()
        else:
            # Draw the corner textures
            self.videoManager.drawSurface(self.boxTextures[0], x1, y1)
            self.videoManager.drawSurface(self.boxTextures[2],
                                          x2 - self.boxTextures[2].getWidth(),
                                          y1)
            self.videoManager.drawSurface(self.boxTextures[6], x1,
                                          y2 - self.boxTextures[6].getHeight())
            self.videoManager.drawSurface(self.boxTextures[8],
                                          x2 - self.boxTextures[2].getWidth(),
                                          y2 - self.boxTextures[6].getHeight())

            # Draw the side textures as patterns.
            self.videoManager.drawPattern(self.boxTextures[1],
                                          x1 + self.boxTextures[0].getWidth(),
                                          y1,
                                          x2 - self.boxTextures[2].getWidth(),
                                          y1 + self.boxTextures[1].getHeight())
            self.videoManager.drawPattern(self.boxTextures[7],
                                          x1 + self.boxTextures[7].getWidth(),
                                          y2 - self.boxTextures[7].getHeight(),
                                          x2 - self.boxTextures[8].getWidth(),
                                          y2)
            self.videoManager.drawPattern(self.boxTextures[3], x1,
                                          y1 + self.boxTextures[0].getHeight(),
                                          x1 + self.boxTextures[3].getWidth(),
                                          y2 - self.boxTextures[6].getHeight())
            self.videoManager.drawPattern(self.boxTextures[5],
                                          x2 - self.boxTextures[5].getWidth(),
                                          y1 + self.boxTextures[2].getHeight(),
                                          x2,
                                          y2 - self.boxTextures[8].getHeight())

            # Draw the main texture as pattern.
            self.videoManager.drawPattern(self.boxTextures[4],
                                          x1 + self.boxTextures[0].getWidth(),
                                          y1 + self.boxTextures[0].getHeight(),
                                          x2 - self.boxTextures[8].getWidth(),
                                          y2 - self.boxTextures[8].getHeight())

    ## \brief Renders justified text.
    #
    #  \return The height of the rendered text.
    def renderTextInArea(self, text, x1, y1, x2, font):
        paragraphs = text.split('\n')
        spaceWidth = font.getStringWidth(' ')
        maxWidth = x2 - x1
        oy1 = y1
        for paragraph in paragraphs:
            words = paragraph.split()
            lineWords = []
            totalWidth = 0
            for word in words:
                if totalWidth + font.getStringWidth(
                        word) + spaceWidth < maxWidth:
                    totalWidth += font.getStringWidth(word) + spaceWidth
                    lineWords.append(word)
                else:
                    if len(lineWords) <= 0:
                        break
                    totalWidth = -spaceWidth
                    for w in lineWords:
                        totalWidth += font.getStringWidth(w)
                    actualSpaceWidth = (maxWidth -
                                        totalWidth) / (len(lineWords))
                    x = x1
                    for w in lineWords:
                        self.videoManager.drawString(font, w, x, y1)
                        x += font.getStringWidth(w) + actualSpaceWidth
                    y1 += font.getLineHeight()
                    totalWidth = font.getStringWidth(word)
                    lineWords = [word]
            x = x1
            for w in lineWords:
                self.videoManager.drawString(font, w, x, y1)
                x += font.getStringWidth(w) + spaceWidth
            y1 += font.getLineHeight()
        # Return the height elapsed.
        return y1 - oy1

    ## \brief Display some text.
    #
    #  \param text The text to be displayed.
    def text(self, text, font=None):

        font = self.defaultFont if font is None else font

        text = str(text)

        self.inputManager.update()
        self.videoManager.storeBuffer(7)

        while self.inputManager.running(
        ) and not self.inputManager.buttonTicked(0):

            self.inputManager.update()

            self.videoManager.clear()
            self.videoManager.restoreBuffer(7)
            self.drawBox(self.margin, self.margin,
                         self.videoManager.getScreenWidth() - self.margin, 110)
            self.videoManager.setClippingRectangle(
                2 * self.margin, 2 * self.margin,
                self.videoManager.getScreenWidth() - 3 * self.margin,
                110 - self.margin)
            self.defaultColor()
            height = self.renderTextInArea(
                text, 2 * self.margin, 2 * self.margin,
                self.videoManager.getScreenWidth() - 3 * self.margin, font)
            height -= 110 - font.getLineHeight()
            self.videoManager.disableClipping()
            self.videoManager.flip()

        self.videoManager.restoreBuffer(7)
        self.mapManager.resync()

    def thoughts(self, text):

        text = str(text)
        scroll = 0
        self.inputManager.update()
        self.videoManager.storeBuffer(7)
        while self.inputManager.running(
        ) and not self.inputManager.buttonTicked(0):

            self.inputManager.update()

            self.videoManager.clear()
            self.videoManager.setColor(0, 0, 0)
            self.videoManager.drawRectangle(
                0, 0, self.videoManager.getScreenWidth(),
                self.videoManager.getScreenHeight())
            self.defaultColor()
            self.renderTextInArea(
                text, 2 * self.margin, 100,
                self.videoManager.getScreenWidth() - 2 * self.margin,
                self.italicsFont)
            self.videoManager.flip()

        self.videoManager.restoreBuffer(7)
        self.mapManager.resync()

    ## \brief Display some info.
    #
    #  \param text The text to be displayed.
    def info(self, text, timeOut=800):

        text = str(text)
        self.inputManager.update()
        self.videoManager.storeBuffer(7)

        start = self.engine.getTicks()

        done = False

        while not done:

            self.inputManager.update()

            self.videoManager.clear()
            self.videoManager.restoreBuffer(7)
            self.drawBox(
                self.margin,
                self.videoManager.getScreenHeight() - self.margin * 3 -
                self.defaultFont.getLineHeight(),
                self.videoManager.getScreenWidth() - self.margin,
                self.videoManager.getScreenHeight() - self.margin)
            self.defaultColor()
            self.videoManager.drawString(
                self.defaultFont, text, 2 * self.margin,
                self.videoManager.getScreenHeight() - self.margin * 2 -
                self.defaultFont.getLineHeight())
            self.videoManager.flip()

            if not self.inputManager.running(
            ) or self.inputManager.buttonTicked(0):
                done = True

            if timeOut is not None:
                if self.engine.getTicks() > start + timeOut:
                    done = True

        self.videoManager.restoreBuffer(7)
        self.mapManager.resync()

    ## \brief lets someone say something.
    #
    def speak(self, object, text):

        self.mapManager.cameraPeekAt(object, True)
        self.videoManager.clear()
        self.mapManager.renderFrame()
        self.text(object.getName().capitalize() + ":\n" + text,
                  self.defaultFont)

    def chat(self, speaker, intro, answers):

        if speaker is not None:
            self.mapManager.cameraPeekAt(speaker, True)
        self.videoManager.clear()
        self.mapManager.renderFrame()

        selected = 0
        self.inputManager.update()
        self.videoManager.storeBuffer(7)

        intro = (speaker.getName().capitalize() + ":\n" +
                 intro) if speaker is not None else intro

        done = False

        while self.inputManager.running() and not done:

            self.inputManager.update()

            y = 0

            self.videoManager.clear()
            self.videoManager.restoreBuffer(7)
            self.drawBox(self.margin, self.margin,
                         self.videoManager.getScreenWidth() - self.margin, 110)
            # Make sure everything goes in the box.
            self.videoManager.setClippingRectangle(
                2 * self.margin, 2 * self.margin,
                self.videoManager.getScreenWidth() - 2 * self.margin,
                110 - self.margin)
            self.defaultColor()
            height = self.renderTextInArea(
                intro, 2 * self.margin, 2 * self.margin + y,
                self.videoManager.getScreenWidth() - 2 * self.margin,
                self.defaultFont)
            y += height + self.margin

            for i in range(len(answers)):

                if self.inputManager.hover(
                        2 * self.margin, 2 * self.margin + y,
                        self.videoManager.getScreenWidth() - 2 * self.margin,
                        2 * self.margin + y +
                        self.defaultFont.getLineHeight()):
                    self.activeColor()
                    if self.inputManager.buttonTicked(0):
                        selected = i
                        done = True
                else:
                    self.inactiveColor()

                height = self.renderTextInArea(
                    answers[i], 2 * self.margin, 2 * self.margin + y,
                    self.videoManager.getScreenWidth() - 2 * self.margin,
                    self.defaultFont)
                y += height

            self.videoManager.disableClipping()
            self.videoManager.flip()

        self.videoManager.setColor()
        self.videoManager.restoreBuffer(7)
        self.mapManager.resync()
        return selected

    def choose(self, title, answers):

        selected = 0
        done = False
        self.inputManager.update()
        self.videoManager.storeBuffer(7)

        while self.inputManager.running() and not done:

            self.inputManager.update()

            self.videoManager.clear()
            self.videoManager.restoreBuffer(7)
            self.drawBox(self.margin, self.margin,
                         self.videoManager.getScreenWidth() - self.margin, 110)
            self.defaultColor()
            y = self.margin * 2
            self.videoManager.drawString(self.defaultFont, title,
                                         self.margin * 2, y)
            y += self.defaultFont.getLineHeight()
            for i in range(len(answers)):

                if self.inputManager.hover(
                        self.margin * 2, y,
                        self.videoManager.getScreenWidth() - self.margin * 2,
                        y + self.defaultFont.getLineHeight()):
                    self.activeColor()
                    if self.inputManager.buttonTicked(0):
                        selected = i
                        done = True
                else:
                    self.inactiveColor()

                self.videoManager.drawString(self.defaultFont, answers[i],
                                             self.margin * 2, y)
                y += self.defaultFont.getLineHeight()
            self.videoManager.flip()

        self.videoManager.setColor()
        self.videoManager.restoreBuffer(7)
        self.mapManager.resync()
        return answers[selected]

    ## \moves someone.
    #
    def move(self, objects, points):

        # Convert objects and points to lists if they aren't.
        if type(objects) is not type([]):
            objects = [objects]
            points = [points]

        self.inputManager.update()
        self.inputManager.setInputMode(annchienta.CinematicMode)

        going = 1

        while self.inputManager.running() and going:

            going = sum(
                map(lambda i: int(objects[i].stepTo(points[i])),
                    range(len(objects))))

            self.mapManager.update(False)
            self.inputManager.update()

            self.mapManager.cameraPeekAt(objects[0], True)

            self.videoManager.clear()
            self.mapManager.renderFrame()
            self.videoManager.flip()

        self.inputManager.setInputMode(annchienta.InteractiveMode)
        self.mapManager.resync()

    ## \brief Inits a dialog.
    #
    def initDialog(self, objects):
        for o in objects:
            o.freeze(True)
            o.setStandAnimation()
        for i in range(len(objects)):
            objects[i].lookAt(objects[(i + 1) % len(objects)])
        self.objectsInDialog = objects

    ## \brief Ends a dialog.
    #
    def quitDialog(self):
        for object in self.objectsInDialog:
            object.freeze(False)

    ## \fades
    #
    def fadeOut(self, r=0, g=0, b=0, ms=500):
        start = self.engine.getTicks()
        self.videoManager.storeBuffer(7)
        while self.engine.getTicks(
        ) < start + ms and self.inputManager.running():
            self.inputManager.update()
            alpha = float(self.engine.getTicks() - start) / float(ms) * 255.0
            self.videoManager.clear()
            self.videoManager.restoreBuffer(7)
            self.videoManager.setColor(r, g, b, int(alpha))
            self.videoManager.drawRectangle(
                0, 0, self.videoManager.getScreenWidth(),
                self.videoManager.getScreenHeight())
            self.videoManager.flip()
        self.mapManager.resync()

    def rotateEffect(self, duration=1500):
        start = self.engine.getTicks()
        self.videoManager.storeBuffer(7)
        while self.engine.getTicks(
        ) < start + duration and self.inputManager.running():
            factor = float(self.engine.getTicks() - start) / float(duration)
            self.videoManager.clear()
            self.videoManager.translate(
                self.videoManager.getScreenWidth() / 2,
                self.videoManager.getScreenHeight() / 2)
            self.videoManager.rotate((factor + 1.) * (factor + 1.) * 360.)
            self.videoManager.scale(factor * 50., factor * 50.)
            self.videoManager.translate(
                -self.videoManager.getScreenWidth() / 2,
                -self.videoManager.getScreenHeight() / 2)
            self.videoManager.restoreBuffer(7)
            self.videoManager.flip()
        self.mapManager.resync()

    def noise(self, duration=1000):
        start = self.engine.getTicks()
        self.videoManager.storeBuffer(7)
        s = self.cacheManager.getSound("sounds/noise.ogg")
        self.audioManager.playSound(s)
        while self.engine.getTicks(
        ) < start + duration and self.inputManager.running():
            self.inputManager.update()
            self.videoManager.clear()
            r = self.mathManager.randInt(0, 256)
            self.videoManager.setColor(r, r, r)
            self.videoManager.drawRectangle(
                0, 0, self.videoManager.getScreenWidth(),
                self.videoManager.getScreenHeight())
            self.videoManager.flip()
        self.videoManager.setColor()
        self.videoManager.restoreBuffer(7)
        self.mapManager.resync()
Example #10
0
import annchienta, SceneManager, PartyManager
partyManager, sceneManager = PartyManager.getPartyManager(
), SceneManager.getSceneManager()
mathManager = annchienta.getMathManager()

messages = [
    "He doesn't look to healthy.", "Spooky.", "I wonder what happened to him?",
    "I have a bad feeling about this...", "This is getting scary...",
    "I wonder how he died?", "I hope I don't end up like that..."
]

sceneManager.initDialog([partyManager.player])
sceneManager.speak(partyManager.player,
                   messages[mathManager.randInt(len(messages))])
sceneManager.quitDialog()
Example #11
0
class Combatant:

    videoManager = annchienta.getVideoManager()
    inputManager = annchienta.getInputManager()
    mathManager = annchienta.getMathManager()

    name = "Name"
    delay = 6
    hostile = False
    status = None
    m_strategy = None
    m_battle = None

    def __init__(self, element):

        self.battleManager = battle.getBattleManager()
        self.sceneManager = scene.getSceneManager()

        self.name = element.getAttribute("name")

        spriteElement = element.getElementsByTagName("sprite")[0]
        if spriteElement.hasAttribute("x1"):
            self.setSprite(str(spriteElement.getAttribute("filename")),
                           int(spriteElement.getAttribute("x1")),
                           int(spriteElement.getAttribute("y1")),
                           int(spriteElement.getAttribute("x2")),
                           int(spriteElement.getAttribute("y2")))
        else:
            self.setSprite(str(spriteElement.getAttribute("filename")))

        statusElement = element.getElementsByTagName("status")[0]
        self.status = Status(statusElement)

        strategiesElement = element.getElementsByTagName("strategies")[0]
        self.strategies = map(lambda s: s.lower(),
                              strategiesElement.firstChild.data.split())

        experienceElements = element.getElementsByTagName("experience")
        if len(experienceElements):
            self.experience = Status(experienceElements[0])
        else:
            self.experience = Status()

        self.reset()

    def reset(self):
        self.ailments = []
        self.buffers = []
        self.delay = 6
        self.m_strategy = strategy.Strategy(None, self)

    def draw(self):

        # Fade a little when death.
        if self.status.get("health") > 0:
            self.videoManager.setColor()
        else:
            self.videoManager.setColor(255, 255, 255, 127)

        if self.sx1 is None:
            self.videoManager.drawSurface(self.sprite, self.x, self.y)
        else:
            self.videoManager.drawSurface(self.sprite, self.x, self.y,
                                          self.sx1, self.sy1, self.sx2,
                                          self.sy2)

    def setSprite(self, fname, x1=None, y1=None, x2=None, y2=None):
        self.spriteFileName = fname
        self.sprite = annchienta.Surface(self.spriteFileName)
        self.sx1, self.sy1 = x1, y1
        self.sx2, self.sy2 = x2, y2

    def getSize(self):
        if self.sx1 is None:
            return self.sprite.getWidth(), self.sprite.getHeight()
        else:
            return (self.sx2 - self.sx1), (self.sy2 - self.sy1)

    def setPosition(self, x, y):
        self.posX, self.posY = x, y
        self.x, self.y = x, y

    def takeTurn(self):

        if self.m_strategy.turns <= 0:
            self.m_strategy = self.createStrategy()

        if self.inputManager.running():
            self.m_strategy.control()

        if "poison" in self.ailments:
            self.addHealth(-self.status.get("maxhealth") / 12)

    def createStrategy(self):

        # Select a random one.
        n = self.strategies[self.mathManager.randInt(0,
                                                     len(self.strategies) - 1)]
        s = strategy.getStrategy(n)
        return s(self.m_battle, self)

    def addHealth(self, health):
        h = self.status.get("health")
        h += health
        h = 0 if h < 0 else (self.status.get("maxhealth")
                             if h > self.status.get("maxhealth") else h)
        self.status.set("health", h)

    def physicalAttack(self, target, attackPower=20, chanceOnHit=0.8):

        # Miss a lot when you're blinded.
        if "blind" in target.buffers:
            chanceOnHit /= 2.0

        # Check if we hit first.
        r = self.mathManager.randFloat()
        if r > chanceOnHit:
            self.sceneManager.info(self.name.capitalize() + " misses " +
                                   target.name.capitalize() + "!")
            return 0

        # Now calculate damage.
        dmg = float(attackPower) * float(self.status.get("strength")) / float(
            target.status.get("defense"))

        # Apply a small random factor and covert to int.
        dmg *= self.mathManager.randFloat(0.9, 1.1)
        dmg = int(dmg)

        # Half damage if we have protect.
        if "protect" in target.buffers:
            dmg /= 2

        # Subtract damage from target's health.
        target.addHealth(-dmg)
        return dmg

    def magicalAttack(self, target, attackPower=20, chanceOnHit=0.8):

        # Check if we hit first.
        r = self.mathManager.randFloat()
        if r > chanceOnHit:
            self.sceneManager.info(target.name.capitalize() +
                                   " resists the spell cast by " +
                                   self.name.capitalize() + "!")
            return 0

        # Now calculate damage.
        dmg = float(attackPower) * float(self.status.get("magic")) / float(
            target.status.get("resistance"))

        # Apply a small random factor and covert to int.
        dmg *= self.mathManager.randFloat(0.9, 1.1)
        dmg = int(dmg)

        if "barrier" in target.buffers:
            dmg /= 2

        # Subtract damage from target's health.
        target.addHealth(-dmg)
        return dmg
Example #12
0
    def __init__(self, xmlElement):

        # Call super constructor
        BattleEntity.BattleEntity.__init__(self, xmlElement)

        # We need to log stuff
        self.logManager = annchienta.getLogManager()

        # Get references
        self.videoManager = annchienta.getVideoManager()
        self.cacheManager = annchienta.getCacheManager()
        self.mathManager = annchienta.getMathManager()
        self.sceneManager = SceneManager.getSceneManager()

        # Create a dictionary describing the level stuff
        self.level = {}
        levelElement = xmlElement.getElementsByTagName("level")[0]
        for k in levelElement.attributes.keys():
            self.level[k] = int(levelElement.attributes[k].value)

        # Create a dictionary describing the health stats
        self.healthStats = {}
        healthStatsElement = xmlElement.getElementsByTagName("healthstats")[0]
        for k in healthStatsElement.attributes.keys():
            self.healthStats[k] = int(healthStatsElement.attributes[k].value)

        # Get all possible actions. The actual actions are in the first child
        # of the element, hence the code. <actions> action1 action2 </actions>
        actionsElement = xmlElement.getElementsByTagName("actions")[0]
        actionNames = str(actionsElement.firstChild.data).split()
        # Prepare to get the from the xml data
        self.actions = []
        # Get them
        for a in actionNames:
            self.addAction(a)

        # Create a dictionary describing the elemental properties
        # Only enemies have them, usually
        self.primaryElemental = {}
        elementalElements = xmlElement.getElementsByTagName("elemental")
        if len(elementalElements):
            for k in elementalElements[0].attributes.keys():
                self.primaryElemental[k] = float(
                    elementalElements[0].attributes[k].value)

        # Load sprite
        spriteElement = xmlElement.getElementsByTagName("sprite")[0]
        # Keep the filename so we can save it later on
        self.spriteFilename = str(spriteElement.getAttribute("filename"))
        self.sprite = annchienta.Surface(self.spriteFilename)
        if spriteElement.hasAttribute("x1"):
            self.sx1 = int(spriteElement.getAttribute("x1"))
            self.sy1 = int(spriteElement.getAttribute("y1"))
            self.sx2 = int(spriteElement.getAttribute("x2"))
            self.sy2 = int(spriteElement.getAttribute("y2"))
        else:
            self.sx1, self.sy1 = 0, 0
            self.sx2 = self.sprite.getWidth()
            self.sy2 = self.sprite.getHeight()

        # Get width and height from those facts.
        self.width = self.sx2 - self.sx1
        self.height = self.sy2 - self.sy1

        self.position = annchienta.Vector(0, 0)

        # We will draw a mark upon ourselves sometimes
        self.active = False
        self.selected = False

        # Damage done by an attack
        self.damage = 0
        self.damageTimer = 0.0

        self.reset()