• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Bombshell93

Component system, am I doing it right?

4 posts in this topic

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 :/

[code]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);
}


}
[/code]

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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote name='bombshell93' timestamp='1328601181' post='4910434']
Then I used interfaces for components
[/quote]

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:

[code]
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()
[/code]

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

Share this post


Link to post
Share on other sites
java doesn't support multiple inheritance, however it can have multiple implementation (inherit virtual functions from an interface)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0