Friday, April 26, 2013

a rpg combat system draws near!

Here is the coded from my prototype game. When I have time I will go back and create a way to have encounters for multiple monsters. create a monster prototype make all monsters instances etc.

<html>

<head>

         <script>

var hp = 15;

var mp = 10;

var slimeHp= 30;

function initialize(){

document.getElementById("hp").innerHTML = hp;

document.getElementById("mp").innerHTML = mp;

}

function gameOver(heroMessage,slimeMessage){

    var fight = document.getElementById("fight");

    var magic = document.getElementById("magic");

    var run = document.getElementById("run");

    (run.parentNode).removeChild(run);

    (magic.parentNode).removeChild(magic);

    (fight.parentNode).removeChild(fight);

    document.getElementById("startup").innerHTML = heroMessage;

    document.getElementById("slimeAct").innerHTML = slimeMessage; 

}

function slimeAttack(){

    var slimeHit = Math.floor(Math.random() * 2);

    if(slimeHit){

        hp -= Math.floor(Math.random() * 5 + 1);

        document.getElementById("slimeAct").innerHTML = "The slime smacks you good with its icky self.";

        initialize();

    }

    else{

        document.getElementById("slimeAct").innerHTML = "The slime misses!";

    }

}

function checkSlime(){

    if (slimeHp <= 0){  

        gameOver("The Slime has been Defeated!","The slime explodes into pus and goo.");

    }

    else{

        slimeAttack();

    }
}

function checkHero(){

    if (hp <= 0){

        gameOver("You have been Defeated!","The slime laughs over your corpse.");  

    }

}

function fight(){

    var youHit = Math.floor(Math.random() * 2);

    if(youHit){

        slimeHp--;

        document.getElementById("startup").innerHTML = "You Hit the Slime!";

    }

    else{

        document.getElementById("startup").innerHTML = "You Missed the Slime!";

    }

    checkSlime();

    checkHero();

}

function magic(){

    mp -= 2;

    document.getElementById("startup").innerHTML = "You Cast a Powerful Fireball";

    fireHit = Math.floor(Math.random() * 3);

    if (fireHit < 1){

        slimeHp = 0;

      

    }

    else{

        document.getElementById("slimeAct").innerHTML = "The slime is unfazed";

    }

    checkSlime();

    checkHero();

}

function run(){

    var runSpeed = Math.floor(Math.random() * 10);

    if (runSpeed > 5){

        gameOver("You Flee","The slime laughs at your cowardice.");

    }

    else{

        document.getElementById("startup").innerHTML = "You Run Too Slow";

        checkSlime();

        checkHero();

    }

}

        </script>

</head>

    <body>

        <div id="startup">A Slime Draws Near!</div>
<img id="slime" onmouseover="initialize()" src="http://shadowsinthenyte.com/wp-content/uploads/2011/08/IMG_0903-300x224.jpg" />
  <div id="slimeAct">It lears at you.</div>
<table id="stats">
<tr>

        <td>HP</td><td id="hp"></td></tr>
<tr><td>MP</td><td id="mp"></td>

        </tr>
</table>
<button id="fight" onclick="fight()">Fight</button>

        <button id="magic" onclick="magic()">Magic</button>

        <button id="run" onclick="run()">Run</button>

    </body>

</html>

No comments:

Post a Comment