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