Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
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 1934 times)
0 Members and 1 Guest are viewing this topic.
Offline Christopher

Senior Member


Medals: 2
Projects: 1



« Posted 2012-02-13 08: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 10: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 11: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!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2012-02-13 12: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 13: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.

 

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

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!