RPG Combat Engine

Hi all,

My RPG is coming along really well. I have been working on the layout and am more or less at the state of making buttons work etc.

Conceptually I am stuck on the idea of turn based combat.

I imagine each party member to be an object and and each enemy to be an object. These are held in an array which cycles through the rollInitiative method for each. How do i then re-order my array so that i can then just cycle through takeAction method for each participant in the correct order?

Is this the best approach anyone can think of?

Thank you

You could make your own sort function and once each object has rolled for initiative just sort the list.

So something along the lines of implementing Comparable on your party member class. I’m not sure if this is the most effecient way of doing it, but it’ll work. :slight_smile:

Thanks for the reply Regenuluz this is the path I ended up taking after a little more research.

For those looking to implement a similar system here is a test program called “Three Monkeys” that I made to get my head around the comparable method.

The program will generate 3 Monkey objects and place them in an ArayList, then randomly generate an initiative roll for each. The ArrayList is then re-ordered based on highest roll and each monkey takes it in turn to say what they have to say. Hope this helps someone. 8)



public class Monkeys implements Comparable{
	
	public int initiative;
	int speed;
	String action;
	
	public Monkeys(String action) {
		super();
		this.action = action;
		speed = 5;
	}
	
	public int genInit(int roll) {
		this.initiative = roll + speed;
		return this.initiative;
	}

	public int compareTo(Object o) {
		Monkeys tmp = (Monkeys)o;
		if(this.initiative < tmp.initiative) {
			return -1;
		}else if(this.initiative > tmp.initiative) {
			return 1;
		}return 0;
	}
}



import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;


public class MonkeyMagic {
	
	public static void main(String[] args) {
		
		ArrayList<Monkeys> m = new ArrayList(); 
		m.add(new Monkeys("I am a bezerker monkey"));
		m.add(new Monkeys("I just wants the bananas"));
		m.add(new Monkeys("I am a lovely monkey"));
		
		Random rand = new Random();
		
		for(Monkeys i: m) {
			int r = rand.nextInt(100);
			i.genInit(r);
		}
		
		Collections.sort(m);
		
		for(Monkeys i: m) {
			System.out.println(i.initiative + " " + i.action);
			try {
				Thread.sleep(1000);
			} catch (Exception e) {}
		}
	}
}

Level 2 compareTo(…):


   public int compareTo(Object o) {
      Monkeys tmp = (Monkeys)o;
      return this.initiative - tmp.initiative;
   }

My lazy fingers are praising you :wink: