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  
  Rendering multiple classes of entities by cycling through an ArrayList  (Read 1277 times)
0 Members and 1 Guest are viewing this topic.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Posted 2012-03-11 03:32:13 »

So... instead of manually writing a new render line in my render updates method for each new class I make that needs to be rendered, I thought it would be easier to just add all of the objects to be rendered to an ArrayList called entities. This seems like it would work in theory, and I can add the individual objects and cycle through it, but I don't know how to figure out which one needs to be rendered. If anyone uses a method of rendering like this, some advice on how to do it would be welcome.

-Nathan

Check out my website!
Offline ReBirth
« Reply #1 - Posted 2012-03-11 03:50:04 »

I almost understand all except this line
Quote
but I don't know how to figure out which one needs to be rendered
IMO, you put them into an AL to be rendered. You may want to simplify your code, why not paste your current one?

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #2 - Posted 2012-03-11 04:13:29 »

Sure, here goes:

Main class:
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  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
package com.natekramber.main;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import com.natekramber.entities.player.Player;
import com.natekramber.entities.player.Movement;
import com.natekramber.entities.zombie.Zombie;
import com.natekramber.menus.MenuMain;

public class Main extends Canvas implements Runnable {
   BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
   private static final long serialVersionUID = 1L;
   public static final int HEIGHT = 360;
   public static final int WIDTH = 640;
   public static final int SCALE = 1;
   public static final String title = "Momentum Test";
   
   private boolean applet = false;
   
   public double unprocessed = 0;
   public long lastTime;
   public String setState = "null";
   
   boolean running = false;
   String state = "null";
   
   int ticks = 0;
   
   public Graphics g;
   public ActionListener listener = new ActionListener(this);
   public StateManager statemanager = new StateManager(this);
   public MenuMain menumain = new MenuMain(this);
   public Player player = new Player(this);
   public Movement movement = new Movement(this);
   public SpriteSheet spritesheet = new SpriteSheet(this);
   public Zombie zombie = new Zombie(this);
   
   public synchronized void start() {
      requestFocus();
      running = true;
      new Thread(this).start();
   }
   
   public synchronized void stop() {
      running = false;
   }
   
   public void init() {
      addKeyListener(listener);
      statemanager.setState("menumain");
   }
   
   public void run() {
      lastTime = System.nanoTime();
      unprocessed = 0;
      int frames = 0;
      double nsPerTick = 1000000000.0 / 60.0;
      long lastTimer1 = System.currentTimeMillis();
     
      init();
     
      while (running)
      {
         long now = System.nanoTime();
         unprocessed += (now - lastTime) / nsPerTick;
         lastTime = now;
         
         while (unprocessed >= 1)
         {
            tick();
            frames++;
            render();
            unprocessed -= 1;
         }
         
         try {
            Thread.sleep(10);
         } catch (InterruptedException e) {
               e.printStackTrace();
         }
         
         if (System.currentTimeMillis() - lastTimer1 > 1000)
         {
            lastTimer1 += 1000;
            System.out.println(ticks + " ticks, " + frames + " fps");
            frames = 0;
            ticks = 0;
         }
      }
      System.exit(0);
   }
   
   public void tick() {
      ticks++;
     
      this.state = statemanager.state;
      if ((!setState.equals(state)) && (!setState.equals("null")))
      {
         if (setState.equals("quitting"))
         {
            if (!isApplet()) stop();
            if (isApplet()) setState = state;
         }
         
         statemanager.setState(setState);
         setState = "null";
      }
     
      logicUpdates();
   }
   
   public void logicUpdates() {
      if (state.equals("menumain")) menumain.update();
      if (state.equals("playing")) {
         player.update();
      }
   }
   
   public void renderUpdates() {
      if (state.equals("menumain")) menumain.render(g);
      if (state.equals("playing")) {
         player.render(g);
      }
   }
   
   public void render() {
      BufferStrategy bs = getBufferStrategy();
      if (bs == null)
      {
         this.createBufferStrategy(3);
         return;
      }
     
      g = image.getGraphics();
      g.setFont(new Font("Arial", 5, 18));
      g.setColor(Color.BLACK);
      g.fillRect(0, 0, WIDTH, HEIGHT);
      g.setColor(Color.WHITE);
     
      renderUpdates();
     
      ///
     
      g.drawString(state, 20, 20);
      g.drawString(title, 20, 40);
     
      ///
     
      g.dispose();
     
     
      g = bs.getDrawGraphics();
      g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
      g.dispose();
      bs.show();
   }
   
   public static void main(String args[]) {
      Main game = new Main();
      game.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
      game.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
      game.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
     
      JFrame frame = new JFrame(title);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLayout(new BorderLayout());
      frame.add(game);
      frame.pack();
      frame.setResizable(false);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      game.start();
   }

   public boolean isApplet() {
      return applet;
   }

   public void setApplet(boolean applet) {
      this.applet = applet;
   }
}


Player class:
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  
package com.natekramber.entities.player;

import java.awt.Graphics;

import com.natekramber.main.Interfaces.Screen;
import com.natekramber.main.Main;

public class Player implements Screen {
   Main main;
   private int pHeight = 64;
   private int pWidth = 64;
   
   public Player(Main main) {
      this.main = main;
   }

   public void render(Graphics g) {
      g.drawImage(main.spritesheet.sprites[0], main.movement.getX(), main.movement.getY(), 64, 64, null);
   }

   public void update() {
      main.movement.updateMovement();
   }

   public int getHeight() {
      return pHeight;
   }

   public int getWidth() {
      return pWidth;
   }
}


That's all that should be needed, I think.

-Nathan

Check out my website!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2012-03-11 04:24:23 »

You should let each state to handle the render. For example, your menumain and playing state have different things to render. Your main class should only know which state current is and update it
1  
2  
currentState.render(g);
//when you change the state: currentState = stateManager.getState("playing");

it goes same with entities too. What you need is to let them organized by implementing same interface or extending same superclass.

Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-03-11 05:55:37 »

Maybe have a base class called Entity, which has an update and render methods. All Entities extend this class and implement those methods.

Then in your main class you have an ArrayList<Entity> so when you update and render:
1  
2  
3  
4  
5  
6  
7  
8  
9  
//update
for(Entity e : entities)
    e.update();

...

//render
for(Entity e : entities)
    e.render(g);

Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #5 - Posted 2012-03-11 11:46:30 »

ON Topic:
I'd go the way ra4king said it, it's plain simple. If you have entities that don't need to be updated or drawn you could use different ArrayLists or add a boolean field in the Entity class that checks if the entity needs to be updated or drawn, so in the draw loop you simple check for that field. Or if you use different lists, you simple loop the one that needs to be updated.
I hope you don't run into more troubles with this, but also hope you post them right away so we can handle it together Smiley
ReBirth also said some useful tips, try to implement them!

OFF Topic:
I like your code, very clean, but why this:
1  
if (state.equals("menumain")) menumain.update();

to do right down to it this instead?:
1  
2  
3  
if (state.equals("playing")) {
   player.update();
}


Just found it funny Smiley
1  
2  
if (state.equals("menumain")) menumain.update();
if (state.equals("playing")) player.update();

That way you keep your touch everywhere Smiley

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!