Jump to content

  • Log In with Google      Sign In   
  • Create Account

Component system, am I doing it right?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 bombshell93   Members   -  Reputation: 205

Like
0Likes
Like

Posted 05 February 2012 - 03:48 AM

Well I'm just experimenting with a bunch of stuff in all sorts of languages to get better understandings of different systems.
One of them is the workings of java, I thought I'd kill 2 birds with 1 stone and make a little RPG system in java.
So I've started writing the base systems in Java.

Bear with me on this, here is my implementation so far, I will have another class with its own thread to handle collisions, this is intended only a 2D game.
To fill in some blanks,
Flag is a type I made to make better use of byte types rather than just using bools, it now acts like 8 bools, likewise I named it Flag intending to use it as such. Flag.B0 is 0000 0000, Flag B1 - B8 are 1, 2, 4, 8, 16, 32, 64 and 128. respectively.
Things ending in able (such as updatable) are kept in the components package, simple interfaces to mark whether an object can be drawn, can be updated, etc.

EDIT: BIG EDIT! I've updated the code, now it actually starts runs and ends. I'm not sure on the input though, I tried making it end when pressing escape but it did nothing :/

import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JFrame;
import Essentials.*;
import Components.*;
public class Main extends Applet implements Runnable, KeyListener
{
private static final long serialVersionUID = 1L;

private Thread T;
private JFrame Frame;
private Flag GameState = Flag.B0;

private Vector<Integer> KeysDown;
private HashMap<Long, BaseObject> Objects;
private long ObjCount = 0;

private double lastUpdateTime = System.nanoTime();
    private double lastRenderTime = System.nanoTime();

private final double TARGET_FPS = 30;
    private final double TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS;
private final double GAME_HERTZ = 30.0;
private final double TIME_BETWEEN_UPDATES = 1000000000 / GAME_HERTZ;

private double now = System.nanoTime();
private int updateCount = 0;

// Double Buffer //
public static BufferedImage Buffer;
public static Graphics2D BufferDevice;

public static void main(String args[])
{
  System.out.print("HELLO WORLD");
  Main M = new Main();
  M.Init();
}

// Implements Runnable //
@Override
public void run()
{
  System.out.println("THREAD STARTED");
  while (GameState.is(Flag.B1))
  {
   System.out.print("LOOPSTART ");
   now = System.nanoTime();
   updateCount = 0;
  
   // Catch up on updates, max at 2 per frame //
   while( now - lastUpdateTime > TIME_BETWEEN_UPDATES && updateCount < 2 )
		 {
			 Update();
			 lastUpdateTime += TIME_BETWEEN_UPDATES;
			 updateCount++;
			 System.out.print("UPDATE ");
		 }
  
   Draw();
   lastRenderTime = now;
   System.out.print("RENDER ");
  
   // free up CPU until next frame //
   while ( now - lastRenderTime < TARGET_TIME_BETWEEN_RENDERS && now - lastUpdateTime < TIME_BETWEEN_UPDATES)
   {
			 Thread.yield();
			 try {Thread.sleep(1);} catch(Exception e) {}
			 now = System.nanoTime();
		 }
   System.out.print("LOOP END; ");
  
   if (true)
   {
    GameState.sub(Flag.B1);
    Frame.setVisible(false);
    Frame.dispose();
    System.out.println("THREAD END");
   }
  }
}

@Override
public void paint(Graphics g)
{
  g.drawImage(Buffer, 0, 0, Frame);
}

// Handle Key Press //
public void keyPressed(KeyEvent e)
{
  if (!KeysDown.contains(e.getKeyCode()))
  {
   KeysDown.add(e.getKeyCode());
  }
}

// Handle Key Release //
public void keyReleased(KeyEvent e)
{
  if (KeysDown.contains(e.getKeyCode()))
  {
   KeysDown.remove(e.getKeyCode());
  }
}

// Implements KeyListener //
@Override
public void keyTyped(KeyEvent e)
{
 
}

// Initialization //
public void Init()
{
  // First bit will be used to determine whether or not the game is running //
  GameState.add(Flag.B1);
  KeysDown = new Vector<Integer>();
  Frame = new JFrame();
  Frame.setSize(640, 480);
  Frame.setVisible(true);
  Objects = new HashMap<Long, BaseObject>();
 
  Buffer = new BufferedImage(640, 480,BufferedImage.TYPE_INT_RGB);
 
  T = new Thread(this);
  T.start();
  System.out.println("INITIALIZATION SUCCESSFUL");
}

// Update Handling //
public void Update()
{
  for (BaseObject i : Objects.values())
  {
   // pass Controllable objects a list containing pressed keys //
   if (i instanceof Controllable)
   {
    ((Controllable) i).Update(KeysDown);
   }
   // Update all Updatable objects //
   if (i instanceof Updatable)
   {
    ((Updatable) i).Update();
   }
  }
}

// Draw Handling //
public void Draw()
{
  for (BaseObject i : Objects.values())
  {
   // Draw all Drawable objects //
   if (i instanceof Drawable)
   {
    ((Drawable) i).Draw(BufferDevice);
   }
  }
  repaint();
}

// Add Object //
public long AddObject(BaseObject Obj)
{
  // Assign the object an ID so it can be easily accessed //
  ObjCount++;
  Obj.Create(ObjCount);
  Objects.put(ObjCount, Obj);
  // Return the ID incase unique handling is required //
  return ObjCount;
}

// Remove Object //
public void RemoveObject(Long key)
{
  Objects.get(key).Destroy();
  Objects.remove(key);
}


}

I mainly want to know if I'm going the right way about this,
I've not done much in java before but I'm trying it for the sake of learning it, so any tips or criticisms on me implementation would help infinitely.

Any and all help appreciated,
Thanks in advanced,
Bombshell

Sponsor:

#2 BeerNutts   Crossbones+   -  Reputation: 2945

Like
0Likes
Like

Posted 06 February 2012 - 05:16 PM

I'm no expert on Component based entity systems, but you've got very little on which I can comment. One thing I can comment on is why do you want to have multiple threads? What do you plan on gaining by using multiple threads? It sounds like you're going to make an RPG, so, any speed improvements you MIGHT gain, won't be relevant.

I would suggest not trying to handle multiple threads, you'll just make it more complex.

FWIW, I've developer a Component Base Entity System (see my Dev Journal in my sig) that uses Events to pass information through the entities and components. I used the class component = data and logic, while it seems more people use the component = datat, and use systems to process the components.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#3 bombshell93   Members   -  Reputation: 205

Like
0Likes
Like

Posted 07 February 2012 - 01:53 AM

I've pretty much got a system I'm happy with now,
I've fixxed the thread thing and a lot of other things I was doing horrificly wrong.
The entity system I'm using starts of with the base entity
BaseClass
all BaseClass derived objects are held in a hashtable with their own ID for easy access.
Then I used interfaces for components, if a class implements a component it must contain get methods for required variables and ofcoarse things like, in the case of Updatables Update. Drawables Draw. etc. etc.
I'm not sure if its most efficient, but it works.

#4 DjMaSh   Members   -  Reputation: 198

Like
0Likes
Like

Posted 07 February 2012 - 06:03 PM

Then I used interfaces for components


Your BaseObject class should just store a list of components. You then derive different component classes, in this case: drawable, updatable, and controllable.

Add instances of these components to a base object instance.

Your main update loop then just iterates over the components of each BaseObject, calling 'Update' or 'Process', which your derived class would have overridden with specific functionality.

eg:

class Component
{
void Update()
}

class BaseObject
{
void AddComponent(Component c)
void RemoveComponent(Component c)

Component components[]
}

//Main loop
foreach(BaseObject b in objects)
	 foreach(Component c in b.components)
		  c.Update()

PS I dont remember how to write java code properly so dont expect it to compile

#5 bombshell93   Members   -  Reputation: 205

Like
0Likes
Like

Posted 07 February 2012 - 08:01 PM

java doesn't support multiple inheritance, however it can have multiple implementation (inherit virtual functions from an interface)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS