I am hoping this will help you to understand my library. This is where the games heart and soul lies. It goes through the objects and should check what commands were given from other objects, such as a key press mouse click, if one is found run the action. I can seem to grasp how to preform actions that need to run longer than an event. Most events have actions that only need to happen on one iteration of the loop, while others need to happen on multiple iterations of the loop. The multiple iteration thing is where I am kinda stumped.
So, when I loop through each game object, how would I know that moveToY is attached to that game object and it still needs to move to the position?
package JGame.Room;
import JGame.Game.Game;
import JGame.GameObject.GameObject;
import JGame.Util.KeyboardMap;
import JGame.Util.Mapping;
import JGame.Util.MouseMap;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Room extends JPanel implements Runnable{
protected Image bg;
ArrayList<GameObject> gameObjects = new ArrayList<>();
protected int width = Game.width, height = Game.height;
public static int CENTERX = 0, CENTERY = 0;
protected int roomSpeed = 5;
/*
* This runs the main thread for the current room.
*
* run() tests for key presses and Mouse Events, it then runs its set method
* if a key press or mouse click is activated.
*/
@Override
public void run(){
try{
while(true){
// Check for key press events
Iterator actions = KeyboardMap.map.entrySet().iterator();
while(actions.hasNext()){
Map.Entry ap = (Map.Entry)actions.next();
Mapping mp = (Mapping)ap.getValue();
if(mp.pressed){
mp.run();
}
}
// Check for mouse events
Iterator actions2 = MouseMap.map.entrySet().iterator();
while(actions2.hasNext()){
Map.Entry ap = (Map.Entry)actions2.next();
Mapping mp = (Mapping)ap.getValue();
if(mp.pressed){
mp.run();
}
}
for(GameObject go : gameObjects){
// If An object isn't allowed to leave the screen don't let it leave
if(!go.getLeaveScreen()){
int goWidth = go.getWidth();
int goHeight = go.getHeight();
int goX = go.getX();
int goY = go.getY();
int gameWidth = Game.width;
int gameHeight = Game.height;
if(goX + goWidth >= gameWidth){
go.setX(gameWidth - goWidth);
}
if(goX <= 0){
go.setX(0);
}
if(goY + goHeight >= gameHeight){
go.setY(gameHeight - goHeight);
}
if(goY <= 0){
go.setY(0);
}
}
}
this.repaint();
Thread.sleep(roomSpeed);
}
}catch(Exception e){
}
}
public Room(){
Thread thread = new Thread(this);
thread.start();
}
/**
*
* @param speed
*
* Sets the room speed; this tells the thread how long it should sleep for.
*
* The higher the number the slower the room becomes.
*/
public void setRoomSpeed(int speed){
this.roomSpeed = speed;
}
/**
*
* @param width
* @param height
*
* Sets the size of a room.
*/
public void setRoomSize(int width, int height){
this.width = width;
this.height = height;
Room.CENTERX = (int)(this.width / 2.0);
Room.CENTERY = (int)(this.height / 2.0);
}
/**
*
* @param color
*
* Sets a color to be the background a room
*/
@Override
public void setBackground(Color color){
super.setBackground(color);
}
/**
*
* @param filename
*
* Sets an image to be the background of a room
*/
public void setBackground(String filename){
URL loc = this.getClass().getResource(filename);
ImageIcon imgIcon = new ImageIcon(loc);
bg = imgIcon.getImage();
}
/**
*
* @param gameObject
* @param x
* @param y
*
* Adds a GameObject to the room at a particular location
*/
public void addGameObjectAt(GameObject gameObject, int x, int y){
gameObject.setX(x);
gameObject.setY(y);
gameObjects.add(gameObject);
}
/**
*
* @param go
*
* Removes a known GameObject from the room
*/
public void removeGameObject(GameObject go){
gameObjects.remove(go);
System.gc();
}
/**
*
* @param g
*
* Draws out all of the GameObjects in the room
*/
@Override
public void paintComponent(Graphics g){
try{
g.drawImage(bg, 0, 0, this);
for(int i = 0; i < gameObjects.size(); i++){
GameObject go = gameObjects.get(i);
g.drawImage(go.getSprite(), go.getX(), go.getY(), this);
}
}catch(Exception e){
}
}
/**
*
* @return
*
* Gets the room's width
*/
@Override
public int getWidth(){
return this.width;
}
/**
*
* @return
*
* Gets the room's height
*/
@Override
public int getHeight(){
return this.height;
}
}
Here is then how I attach actions to an object (very basic Moves the ship +X to move it to the right):
keyboard.keyPress("RIGHT", new Runnable(){
@Override
public void run(){
move.moveX(Ship.this, shipSpeed);
}
});
Something a little more complex, this listens for the space bar, if pressed: create a laser image, and move it vertically to the top of the screen, once it gets to it's designation destroy it:
keyboard.keyPress("SPACE", new Runnable(){
TimeRegulator tr = new TimeRegulator(200);
@Override
public void run(){
if(!tr.checkTime()){
return;
}
MoveAction move = new MoveAction();
CollisionEvent colide = new CollisionEvent();
Sprite sprite = new Sprite("/media/images/laser.png");
final GameObject laser = create.create(
sprite,
(Room)Ship.this.room,
Ship.this.getX() + (Ship.this.getWidth() / 2) - 3,
Ship.this.getY());
move.moveToY(laser, 0 - sprite.getHeight(), 1, new AbstractAction(){
@Override
public void actionPerformed(ActionEvent evt){
DestroyAction destroy = new DestroyAction();
destroy.destroyGameObject((Room)Ship.this.room, laser);
}
});
}
});