Jump to content

How to make simple turn battle function?


Mizukage
 Share

Recommended Posts

My battle function after objects collision

        function fightEngine(gracz, enemy) {            //Trying to freze player during fight            playerSprite.body.immovable = true;            if (this.enemies.hp > 0) {                this.enemies.hp -= playerDmg;                console.log('Enemy recieved ' + playerDmg + ' dmg');                                if (this.enemies.hp <= 0) {                                        var enemyGold = Math.floor(Math.random() * this.enemies.goldMax) + this.enemies.goldMin;                    //get grind stuff                    player.gold += enemyGold;                    player.exp += this.enemies.exp;                    //delete the enemy object                    enemy.kill();                    this.shatter.play();                    console.log('"' + this.enemies.name + '"' + ' is dead.');                    console.log('Enemy drop ' + enemyGold + ' gold.');                    console.log('You gain ' + this.enemies.exp + ' experience.');                    //Restore life for next enemies clone                     this.enemies.hp = 50;                                    } else {                    var enemyDmg = Math.floor(Math.random() * this.enemies.attackMax) + this.enemies.attackMin;                    player.hp -= enemyDmg;                    console.log('You lost ' + enemyDmg + ' life');                    if (player.hp <= 0) {                        console.log('You are dead');                    }                }            }

I spend few hours and still have no idea how to change this battle to turn.

For ex.

 

function battle() {
player attack
//time for sound and animation
enemy attack
//time for sound and animation
}

 

Add to this only checking object who recieved dmg is alive, if is than fine, if not then can start another new function like a enemyDie(enemy) or something like that. 
 

Link to comment
Share on other sites

You want to use timers. If you want battle to only happen every 1 second, you need code that looks like this:

game.time.events.add(1000, fightBattle, null, gracz, enemy);

That will call this function once every second, passing in "gracz" and "enemy" as its two arguments.

 

If you want first the enemy to attack, then wait for animation, then player to attack, then wait for animation, you'll end up having multiple timers. Or, better yet, you'll start the enemy attacking and, when it's done, start a timer to start the player attack.

 

Regardless, the solution won't look like a single function that you call. It's going to end up as multiple functions that get called one after another all controlled by events (onAnimationComplete) and timers.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...