Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (290)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  RPG Combat Engine  (Read 1198 times)
0 Members and 1 Guest are viewing this topic.
Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Posted 2012-02-13 09:41:18 »

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

Offline Regenuluz
« Reply #1 - Posted 2012-02-13 11:53:49 »

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. Smiley
Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Reply #2 - Posted 2012-02-13 12:44:17 »

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.  Cool

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
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;
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
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) {}
      }
   }
}

Games published by our own members! Check 'em out!
Try the Free Demo of Revenge of the Titans
Offline theagentd
« Reply #3 - Posted 2012-02-13 13:20:58 »

Level 2 compareTo(...):
1  
2  
3  
4  
   public int compareTo(Object o) {
      Monkeys tmp = (Monkeys)o;
      return this.initiative - tmp.initiative;
   }

Myomyomyo.
Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Reply #4 - Posted 2012-02-13 14:06:50 »

Level 2 compareTo(...):
1  
2  
3  
4  
   public int compareTo(Object o) {
      Monkeys tmp = (Monkeys)o;
      return this.initiative - tmp.initiative;
   }


My lazy fingers are praising you  Wink

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (66 views)
2013-05-17 21:29:12

alaslipknot (75 views)
2013-05-16 21:24:48

gouessej (106 views)
2013-05-16 00:53:38

gouessej (103 views)
2013-05-16 00:17:58

theagentd (113 views)
2013-05-15 15:01:13

theagentd (103 views)
2013-05-15 15:00:54

StreetDoggy (148 views)
2013-05-14 15:56:26

kutucuk (170 views)
2013-05-12 17:10:36

kutucuk (169 views)
2013-05-12 15:36:09

UnluckyDevil (178 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.169 seconds with 21 queries.