LogFAQs > #961291474

LurkerFAQs, Active DB, DB1, DB2, DB3, DB4, DB5, DB6, DB7, DB8, Database 9 ( 09.28.2021-02-17-2022 ), DB10, DB11, DB12, Clear
Topic List
Page List: 1
TopicProgrammingGeneral++
1337toothbrush
12/28/21 8:53:43 PM
#8:


ChocoboMog123 posted...
That's a good idea. Make an array of the needed size, reference each actor by some unique ID and fill it appropriately. The problem, ultimately, is just iterating through x elements of somewhat different types and it will probably come back when I get to damage and actions.

Eventually I'll upload all my scripts from my Unix class to github and post them here. If anyone is interested in learning awk, perl, or some bash scripts for parsing text I can share the vids. The instructor actually had a ton of really niche gaming references in the material.

Yeah, the problem is that when you assign the Character array to an array of a different type, the limitations of that array apply to it. So you assigned an array of EnemyClass to actors, thus it will act like one (and so can't hold PlayerCharacter), you should do this instead:

Character[] actors = new Character[enemy.length + 1];
for (int i = 0; i < enemy.length; ++i) actors[i] = enemy[i];
actors[actors.length-1] = mainCharacter;

Now if you want to put them all in the same array, you'll need a common method (e.g. Character has a method turn() which handles how the character carries out their turn). If it makes sense, you could always keep enemies and player characters separate and then simply iterate the two arrays based on speed. For example:

Arrays.sort(enemy, (Character a, Character b) -> a.getTurnOrder() - b.getTurnOrder());
Arrays.sort(player, (Character a, Character b) -> a.getTurnOrder() - b.getTurnOrder());
int enemyIndex = 0, playerIndex = 0;
while (enemyIndex < enemy.length || playerIndex < player.length) {
boolean playerTurn;
if (enemyIndex == enemy.length) playerTurn = true;
else if (playerIndex == player.length) playerTurn = false;
else if (player[playerIndex].getTurnOrder() <= enemy[enemyIndex].getTurnOrder()) playerTurn = true;
else playerTurn = false;

if (playerTurn) player[playerIndex++].turn();
else enemy[enemyIndex++].turn();
}

You sort both arrays by turn order and then advance the index based on which one is faster (or just advance the other array if one has been iterated through entirely already).

---
https://imgur.com/a/FU9H8 - http://imgur.com/ZkQRDsR.png - http://imgur.com/2x2gtgP.jpg
... Copied to Clipboard!
Topic List
Page List: 1