Saturday, April 6 2013
I apologize for this being late, as I totally dropped the ball on posting yesterday.
So, this week Matt and I wanted to focus heavily on getting our battling system up and running with some dummy data. And lo and behold, we succeeded. Below you will find a code snippet from the battle system we are using:
do{
if(playerTurn == true)
{
while(playerTurn == true){
KeyListener();
Display.update();
Display.sync(60);
if(chosenAttack != null)
{
enemies.get(0).setHealth(enemies.get(0).getHealth() - calcDamage(chosenAttack));
//check to see if enemy is dead
if(enemies.get(0).getHealth() <= 0)
{
enemies.remove(0);
}
playerTurn = false;
}
/*if(chosenItem != null)
{
playerTurn = false;
}*/
}
enemyTurn = true;
}
if(enemyTurn == true)
{
Attack a = enemies.get(0).getAttack(Vector.random(0, 3));
player.setHealth(player.getHealth() - calcDamage(a));
System.out.println("The enemy did "+ calcDamage(a) + " points of damage.");
playerTurn = true;
enemyTurn = false;
}
Display.update();
Display.sync(60);
}
while(player.getHealth() > 0 || enemies.size() != 0);
If that looks a little ugly, both the Blogger formatting and the actual code, I know. I tried for hours to get something onto Blogger that would look like a scrolling code box, but nothing wanted to work (and I am not a web programmer).
So, what you see there is the meat of our battle system. There are a few portions missing, as I worked on it on Matt's laptop towards the end of our meeting yesterday and forgot to grab a copy of it, but you get the gist. The first portion of the code, which you cannot see here, checks to see which attacks the player has enabled out of their repertoire of attacks. This forces the player to only have 4 attacks available to them during any battle. They can change these attacks outside of battle, but not inside. The other part you do not see is the code where we determine who goes first, the player or the enemy, based on their speeds. That part is fairly straightforward, so I will not go into detail about it.
What you see above is how the actual battle system works. It is encased in a do, while loop in order to keep the battle going until either the player dies or the enemy dies. The chosenAttack variable you see stores the attack the player chose from the menu that shows up during battle. We needed a way for the UI to interact with the game, and that was the solution we came up with. The piece of code you may not understand is this portion:
Attack a = enemies.get(0).getAttack(Vector.random(0, 3));
This piece of code sets an Attack object a to a random attack pulled from the attack ArrayList that every monster has. The Vector.random is a function Matt built in order for us to be able to generate a random number between a minimum and maximum value. The enemies.get(0) is a hacky piece of code we are using at the moment until we get actual data to work with. It pulls the first object from the enemies ArrayList that is local to the battle system. When we have actual data to work with (which means, more enemies to fight at any given time in a single battle), this portion of the code will change to reflect whose turn it is in the battle.
Hope you all enjoyed a look into the code of our project in its current stage, and if you have any questions, leave a comment below.
No comments:
Post a Comment