Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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  
  Spawning multiple enemies.  (Read 2749 times)
0 Members and 1 Guest are viewing this topic.
Offline SquidNig

Senior Newbie





« Posted 2012-06-24 01:24:22 »

Alright, so i have this program that all it does so far is a path finding AI that just follows you based on your coordinates. Now, what i want to do is be able to have MULTIPLE AI's on the screen. How would i go about doing that? I heard that i have to use an ArrayList, then what? I just shove them in there and have them drawn? If someone could provide a detailed discription of what to do that would be awesome. :c

My code for my Player and main game:
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  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
package src;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;

public class Game extends JFrame implements Runnable{
   
   // Global Variables
 
    static Rectangle player = new Rectangle(30, 50, 15, 15);
    static Rectangle object = new Rectangle(275, 175, 15, 15);
   
    private Image dbImage;
    private Graphics dbg;
   
    static AI AI = new AI(object, player);
   
    Color Player = new Color(255,202,102);
   
    int xDirection, yDirection;
   
    // Setting up the Game Window
   
    public Game(){
        setSize(700,400);
        setResizable(false);
        setLocationRelativeTo(null);
        setTitle("ZOMBIE BLOCK");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        addKeyListener(new AL());
       
    }
   
    // Double-Buffer Graphics
   
    @Override
    public void paint(Graphics g){
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);
    }
   
    // Drawing the Player and AI
   
    private void paintComponent (Graphics g)  {
        g.setColor(Player);
        g.fillRect(player.x, player.y, player.width, player.height);
        AI.draw(g);
     
        repaint();
    }
   
    // Collision Detection
   
    public void detectEdges(){
           if(player.x <= 0)
               setXDirection(1);
           if(player.x >= 700-player.width)
               setXDirection(-1);
           if(player.y <= 25)
               setYDirection(1);
           if(player.y > 400-player.height)
               setYDirection(-1);
       }
   
   
    // Letting the player move
   
    public void move(){
        player.x += xDirection;
        player.y += yDirection;
    }
   
    public void setYDirection(int ydir){
        yDirection = ydir;
    }
    public void setXDirection(int xdir){
        xDirection = xdir;
    }
   
    // Key Inputs
   
    public class AL extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e){
            int keyCode = e.getKeyCode();
            if(keyCode == e.VK_A){
               setXDirection(-1);
            }
            if(keyCode == e.VK_D){
                setXDirection(1);
            }
            if(keyCode == e.VK_W){
                setYDirection(-1);
            }
            if(keyCode == e.VK_S){
               setYDirection(1);
            }
               
            }
       

      @Override
        public void keyReleased(KeyEvent e){
            int keyCode = e.getKeyCode();
            if(keyCode == e.VK_A){
                setXDirection(0);
            }
            if(keyCode == e.VK_D){
                setXDirection(0);
            }
            if(keyCode == e.VK_W){
                setYDirection(0);
            }
            if(keyCode == e.VK_S){
                setYDirection(0);
            }
     
       }
    }

   
    // Starting the threads
   
    public static void main(String[] args) {
        Game Game = new Game();
        Thread t = new Thread(Game);
        t.start();
        Thread t1 = new Thread(AI);
        t1.start();
    }
   
    // Letting the threads run.
   
    @Override
    public void run(){
       
        try{
            while(true){
                move();
                detectEdges();
                Thread.sleep(5);
            }
        }catch(Exception e){
            System.err.println(e.getMessage());
        }
    }
}


And this is the code for my AI or my Zombie:
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  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
package src;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

public class AI implements Runnable{
   
   Rectangle AI, target;
   
   boolean resting;
   boolean shouldSetRandDir = true;
   
   static int xDirection, yDirection;
   
   Color Zombie = new Color(0,149,0);

   public AI (Rectangle r, Rectangle t) {
     
      AI = r;
      target = t;
     
   }
   
   public void draw(Graphics g){
     
      g.setColor(Zombie);
      if (AI != null) {
         
         // Draw Rectangle
        g.fillRect(AI.x, AI.y, AI.width, AI.height);
         
         
      }
     
   }
   
   public void findPathToTarget(){
     
      if (AI.x < target.x) {
         setxDir(1);
      }
      if (AI.x > target.x) {
         setxDir(-1);
      }
      if (AI.y < target.y) {
         setyDir(1);
      }
      if (AI.y > target.y) {
         setyDir(-1);
      }
     
     
   }
   
   
    public void detectEdges(){
           if(AI.x <= 0)
               setxDir(1);
           if(AI.x >= 700-AI.width)
               setxDir(-1);
           if(AI.y <= 25)
               setyDir(1);
           if(AI.y > 400-AI.height)
               setyDir(-1);
       }
   
   public void setxDir(int xdir){
     
      xDirection = xdir;
     
   }
   public void setyDir(int ydir){
     
      yDirection = ydir;
     
   }
   public void move(){
     
      AI.x += xDirection;
      AI.y += yDirection;
     
   }
   
   @Override
   public void run() {
     
      try {
         
         while(true){
           
            if (!resting) {
               
               
               long start = System.currentTimeMillis();
               long end = start + 1*1000;
               while(System.currentTimeMillis() < end ) {  
                  findPathToTarget();
                  move();
                  detectEdges();
                  // Speed in which it moves
                 Thread.sleep(15);
               }
               
               resting = true;
               
            } else {
               
               Thread.sleep(50);
               shouldSetRandDir = true;
               resting = false;
               
            }
         }
         
      }catch(Exception e) {
         
         System.out.println("ERROR");
         
      }
     
     
   }

}
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #1 - Posted 2012-06-24 01:35:08 »

Here's some pseudo-code.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
List<Entity> entities = new ArrayList<Entity>();
   
   public void init() {
      for (int i = 0; i < ENTITIES_TO_BE_SPAWNED; i++) {
         entities.add(new Mob());
      }
     
      entities.add(new Player());
   }
   
   public void tick() {
      for (Entity entity : entities) entity.tick();
   }
   
   public void render() {
      for (Entity entity : entities) entity.render();
   }


Then just have Player and Mob extend Entity.

Hope this helps. If you have any other questions, please ask.

-Nathan

Check out my website!
Offline SquidNig

Senior Newbie





« Reply #2 - Posted 2012-06-24 01:45:37 »

Thank you for responding so quickly! (Love your profiles avatar by the way) now try to add another AI instance into the ArrayList im needing to enter two Rectangles as parameters. The only two rectangles i have that i have declared is my AI and my Player? But that wouldnt make sense having to pass through my player for a zombie? How would i go about doing that?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #3 - Posted 2012-06-24 01:52:14 »

Are the rectangles used by the player and the mob the same every time? If so, just put them in their respective classes.

If the rectangles are created differently based on coordinates or something, then make a constructor in the class and define the rectangle then.

1  
2  
3  
4  
5  
public class Mob extends Entity {
     public Mob(int x, int y, int width, int height) {
          super(int x, int y, int width, int height);
     }
}


1  
2  
3  
4  
5  
6  
7  
public class Entity {
     public Rectangle rectangle;

     public Entity(int x, int y, int width, int height) {
          this.rectangle = new Rectangle(x, y, width, height);
     }
}


Or something like that.

-Nathan

Check out my website!
Offline SquidNig

Senior Newbie





« Reply #4 - Posted 2012-06-24 02:32:06 »

Alright, so i entered the code and fixed it to my projects liking. When i run it i still have only 1 AI on the screen. Why is that? By the way the code below has the Output() method which i used to debug what was wrong. Apparently there's 10 zombies in the list.

Here's the new Main Code:
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  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
package src;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;

public class Game extends JFrame implements Runnable{
   
   // Global Variables
 
    static Rectangle player = new Rectangle(30, 50, 15, 15);
    static Rectangle object = new Rectangle(275, 175, 15, 15);
   
    List<AI> zombies = new ArrayList<AI>();
   
    private Image dbImage;
    private Graphics dbg;
   
    static AI AI = new AI(object, player);
   
    Color Player = new Color(255,202,102);
   
    int xDirection, yDirection;
   
   
    // Setting up the Game Window
   
    public Game(){
        setSize(700,400);
        setResizable(false);
        setLocationRelativeTo(null);
        setTitle("zombies BLOCK");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        addKeyListener(new AL());
       
        for (int i = 0; i < 10; i++) {
            zombies.add(new AI(object, player));
         }
       
    }
   
    public void output () {
       
       int len = zombies.size();
       System.out.println(len);
       
    }
   
    // Double-Buffer Graphics
   
    @Override
    public void paint(Graphics g){
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);
    }
   
    // Drawing the Player and AI
   
    private void paintComponent (Graphics g)  {
        g.setColor(Player);
        g.fillRect(player.x, player.y, player.width, player.height);
        for (AI AI : zombies) AI.draw(g);
        repaint();
    }
   
    // Collision Detection
   
    public void detectEdges(){
           if(player.x <= 0)
               setXDirection(1);
           if(player.x >= 700-player.width)
               setXDirection(-1);
           if(player.y <= 25)
               setYDirection(1);
           if(player.y > 400-player.height)
               setYDirection(-1);
       }
   
   
    // Letting the player move
   
    public void move(){
        player.x += xDirection;
        player.y += yDirection;
    }
   
    public void setYDirection(int ydir){
        yDirection = ydir;
    }
    public void setXDirection(int xdir){
        xDirection = xdir;
    }
   
    // Key Inputs
   
    public class AL extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e){
            int keyCode = e.getKeyCode();
            if(keyCode == e.VK_A){
               setXDirection(-1);
            }
            if(keyCode == e.VK_D){
                setXDirection(1);
            }
            if(keyCode == e.VK_W){
                setYDirection(-1);
            }
            if(keyCode == e.VK_S){
               setYDirection(1);
            }
               
            }
       

      @Override
        public void keyReleased(KeyEvent e){
            int keyCode = e.getKeyCode();
            if(keyCode == e.VK_A){
                setXDirection(0);
            }
            if(keyCode == e.VK_D){
                setXDirection(0);
            }
            if(keyCode == e.VK_W){
                setYDirection(0);
            }
            if(keyCode == e.VK_S){
                setYDirection(0);
            }
     
       }
    }

   
    // Starting the threads
   
    public static void main(String[] args) {
        Game Game = new Game();
        Thread t = new Thread(Game);
        t.start();
        Thread t1 = new Thread(AI);
        t1.start();
    }
   
    // Letting the threads run.
   
    @Override
    public void run(){
       
        try{
            while(true){
               output();
                move();
                detectEdges();
                Thread.sleep(5);
            }
        }catch(Exception e){
            System.err.println(e.getMessage());
        }
    }
}


and this is the AI's code:

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  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
package src;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

public class AI implements Runnable{
   
   Rectangle AI, target;

   boolean resting;
   boolean shouldSetRandDir = true;
   
   static int xDirection, yDirection;
   
   Color Zombie = new Color(0,149,0);
   
   public AI (Rectangle r, Rectangle t) {
     
      AI = r;
      target = t;
     
   }


   public void draw(Graphics g){
     
      g.setColor(Zombie);
      if (AI != null) {
         
         // Draw Rectangle
        g.fillRect(AI.x, AI.y, AI.width, AI.height);
         
         
      }
     
   }
   
   public void findPathToTarget(){
     
      if (AI.x < target.x) {
         setxDir(1);
      }
      if (AI.x > target.x) {
         setxDir(-1);
      }
      if (AI.y < target.y) {
         setyDir(1);
      }
      if (AI.y > target.y) {
         setyDir(-1);
      }
     
     
   }
   
   
    public void detectEdges(){
           if(AI.x <= 0)
               setxDir(1);
           if(AI.x >= 700-AI.width)
               setxDir(-1);
           if(AI.y <= 25)
               setyDir(1);
           if(AI.y > 400-AI.height)
               setyDir(-1);
       }
   
   public void setxDir(int xdir){
     
      xDirection = xdir;
     
   }
   public void setyDir(int ydir){
     
      yDirection = ydir;
     
   }
   public void move(){
     
      AI.x += xDirection;
      AI.y += yDirection;
     
   }
   
   @Override
   public void run() {
     
      try {
         
         while(true){
           
            if (!resting) {
               
               
               long start = System.currentTimeMillis();
               long end = start + 1*1000;
               while(System.currentTimeMillis() < end ) {  
                  findPathToTarget();
                  move();
                  detectEdges();
                  // Speed in which it moves
                 Thread.sleep(15);
               }
               
               resting = true;
               
            } else {
               
               Thread.sleep(50);
               shouldSetRandDir = true;
               resting = false;
               
            }
         }
         
      }catch(Exception e) {
         
         System.out.println("ERROR");
         
      }
     
     
   }

}
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #5 - Posted 2012-06-24 02:41:25 »

You ARE drawing all 10 of them, but you are drawing them at the exact same coordinates.

Try this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
Rectangle object;
   Random random = new Random();
   private int WIDTH = 700, HEIGHT = 400, objectWidth = 15, objectHeight = 15;
   
      for (int i = 0; i < 10; i++) {
         object = new Rectangle(random.nextInt(WIDTH - objectWidth), random.nextInt(HEIGHT - objectHeight), objectWidth, objectHeight);
         zombies.add(new AI(object, player));
      }
   }


Also.. why are you passing the player instance to every zombie?

-Nathan

EDIT: Actually, try removing this line, too:

1  
static AI AI = new AI(object, player);

Check out my website!
Offline SquidNig

Senior Newbie





« Reply #6 - Posted 2012-06-24 03:06:37 »

Since the AI object is gone i have nothing to point the t1 Thread too. Sad Im sorry for being such a noob, bro.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #7 - Posted 2012-06-24 03:28:54 »

In that case, simply rename this

1  
for (AI AI : zombies) AI.draw(g);


to this

1  
for (AI zombie : zombies) zombie.draw(g);


See if that works. Otherwise, I'll ask ra4king to take a look.

-Nathan

Check out my website!
Offline Ultroman

JGO Knight


Medals: 24
Projects: 1


Snappin' at snizzes since '83


« Reply #8 - Posted 2012-06-24 05:07:59 »

You never start the threads of the new AI-objects you create in the for-loop. There's probably a smarter way to implement this, but I'll show you something that should work.
Assuming you've already implemented the fix posted by StonePickaxes, do this.

Change your entire Main to this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public static void main(String[] args) {
// Create a helper to control the threads. It can execute any Runnable class.
ExecutorService threadExecutor = Executors.newCachedThreadPool();

// Instanciate your Game-class, which creates the AI-instances in it's constructor
Game Game = new Game();

// Starts your zombies
for(AI zombie : game.getZombies()) {
   threadExecutor.execute(zombie);
}

// Execute your runnable game-class
threadExecutor.execute(t);

// When all threads are done running, ThreadExecutor cleans up
threadExecutor.shutdown();
}

- Jonas
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #9 - Posted 2012-06-24 05:12:49 »

OH GOD.....NEVER....EVER.....EVER....use threads for each entity.....EVER persecutioncomplex

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ultroman

JGO Knight


Medals: 24
Projects: 1


Snappin' at snizzes since '83


« Reply #10 - Posted 2012-06-24 05:18:40 »

OH GOD.....NEVER....EVER.....EVER....use threads for each entity.....EVER persecutioncomplex
True that! But its a nice way to learn about threads. I'm hoping this isn't for something bigger, though  Wink

If you want this to end up being a game or any sort of bigger program than it already is, you should really take a look at the threads on game-loops around this forum. Take a days chill and read. It'll be good for you. I spent 3 weeks chugging at that log, hehe. ra4king really helped me on that one.

Here's an example:
Game-class with a few balls bouncing
Ball-class
I've commented it a bit.

Here's a great post on game-loops

- Jonas
Online theagentd
« Reply #11 - Posted 2012-06-24 05:53:05 »

Using threads: +1
Using threads like that: -1

People need to start threading games soon or we'll be in deep shit in the near future.

Myomyomyo.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 114
Projects: 4
Exp: 3 years



« Reply #12 - Posted 2012-06-24 05:58:04 »

OH GOD.....NEVER....EVER.....EVER....use threads for each entity.....EVER persecutioncomplex
Absolutely right. I remember my first game, I did that by accident. RAM usage went up and up and up...
like 1300 threads on the profiler...
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #13 - Posted 2012-06-24 06:32:30 »

Using threads properly: +1
FTFY Grin

Offline SquidNig

Senior Newbie





« Reply #14 - Posted 2012-06-24 16:42:11 »

Thank you StonePickaxe and Ultroman! Cheesy I used Ultromans last little code for my main. But the getZombies() you had wasn't a method. So i just used my ArrayList and that worked. Even though that worked is that still considered 'bad practice'?

WHEW. I did not expect something that seems as simple as this to get so complicated. xD
Offline Ultroman

JGO Knight


Medals: 24
Projects: 1


Snappin' at snizzes since '83


« Reply #15 - Posted 2012-06-25 14:21:36 »

Thank you StonePickaxe and Ultroman! Cheesy I used Ultromans last little code for my main. But the getZombies() you had wasn't a method. So i just used my ArrayList and that worked.
I assumed you had a getter for that ArrayList, or that you'd catch the drift Wink
I'm happy it worked for you. But no, it is a very intricate way you've gone about this. There's no need to have a separate thread for each of them. If you take a look at the example posted in my last post, and consider the Balls as your zombies, you can get an idea of how to build it better.

The method-calls in your run() in your AI-class should be moved to an update(int deltaTime) method in AI, which you call just like I call the update-method in my Ball-class. Then you can add a delay-timer (adding a new global variable int timer; as such (pseudo-code!):

public void update(int deltaTime){
    if(timer>=150){
       run the methods
       timer=0;
    }
    else timer += deltaTime;
}

This will make your zombies update/move every 150ms (or whatever you wish). You should also move them with a double velocity like I move the balls around.

- Jonas
Offline _Al3x

JGO Coder


Medals: 7
Projects: 1


Indie Games FTW!


« Reply #16 - Posted 2012-07-19 11:39:26 »

I've seen "static AI AI = new AI(object, player);" somewhere (just copied it and moved downwards). It just felt wrong somehow.
If you said that you get only ONE AI, that may be the problem?

Offline Grunnt

JGO Wizard


Medals: 55
Projects: 9
Exp: 5 years


Complex != complicated


« Reply #17 - Posted 2012-07-19 15:09:19 »

Maybe you could rename the "AI" class to "Zombie" class? It would sure make the code easier to read. But that may be a personal thing.

On using threads: I'd avoid using them like the plague before finishing your first game. They're a source of much complexity and unhappiness, and little gain in most situations.

Good luck with the zombies Smiley

Offline Mads

JGO Ninja


Medals: 24
Projects: 3


One for all!


« Reply #18 - Posted 2012-07-19 17:04:49 »

Excuse me if this has been said before.

Usually, I just make a List (either LinkedList, or ArrayList depending on the situation) and roll through it for updating and drawing.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
init() {
   arrayList<Entity> = new ArrayList<Entity>();
   // fill it!
}

update(delta i) {
   for (Entity t : arrayList) {
      // update one entity in a uniform manner
  }
}

render(Graphics g) {
   for (Entity t : arrayList) {
      // render one entity in a uniform manner.
     // you can grab the fields from the specific entity using the t-object
  }
}


You can't add/delete entities this way though - that's a different problem.

Offline Sickan

Senior Member


Medals: 8



« Reply #19 - Posted 2012-07-19 17:25:28 »

I personally like the symmetry of having 'tick' matching 'draw' or 'render' matching 'update'. That's just me though.

I'm just here to learn.
Online matheus23

JGO Wizard


Medals: 97
Projects: 3


You think about my Avatar right now!


« Reply #20 - Posted 2012-07-22 10:12:07 »

for me 'tick' matches update and 'render' matches 'draw'... Feel the logic with draw and render? Smiley *personal-opinions*

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
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.

xsi3rr4x (30 views)
2014-04-15 18:08:23

BurntPizza (27 views)
2014-04-15 03:46:01

UprightPath (42 views)
2014-04-14 17:39:50

UprightPath (24 views)
2014-04-14 17:35:47

Porlus (41 views)
2014-04-14 15:48:38

tom_mai78101 (63 views)
2014-04-10 04:04:31

BurntPizza (123 views)
2014-04-08 23:06:04

tom_mai78101 (222 views)
2014-04-05 13:34:39

trollwarrior1 (189 views)
2014-04-04 12:06:45

CJLetsGame (198 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!