Permalink
Browse files

1. Make Animation objects removable; 2. add Sprite and Sprite action;…

… 3. add Me in demo
  • Loading branch information...
Jeky Cui
Jeky Cui committed Dec 7, 2017
1 parent d837c00 commit 5129ab8e054fbcab91eb1231847bb32f9dcad59b

This file was deleted.

Oops, something went wrong.
@@ -1,9 +1,17 @@
package org.jam.core.game;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import javax.swing.JFrame;

import org.jam.core.game.animations.Animation;
import org.jam.core.game.animations.Sprite;

public class Game implements Runnable {

public static final int DEFAULT_FPS = 60;
@@ -12,18 +20,18 @@
private GameCanvas canvas;
private JFrame gameWindow;
private boolean start;
private List<Animation> animations;
private List<Sprite> sprites;

public Game() {
this(DEFAULT_FPS);
}

public Game(int fps) {
this((long) 1000 / fps, new GameCanvas());
}

public Game(long timeDelta, GameCanvas canvas) {
this.timeDelta = timeDelta;
this.canvas = canvas;
animations = new LinkedList<>();
sprites = new LinkedList<>();
timeDelta = (long) 1000 / fps;
canvas = new GameCanvas(animations);

start = false;

@@ -36,10 +44,26 @@ public void windowClosing(WindowEvent e) {
stop();
}
});
gameWindow.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
sprites.forEach(s -> s.onKeyPressed(e.getKeyCode()));
}

@Override
public void keyReleased(KeyEvent e) {
sprites.forEach(s -> s.onKeyReleased(e.getKeyCode()));
}
});
}

public void addAnimation(Animation animation) {
animations.add(animation);
}

public GameCanvas getCanvas() {
return canvas;
public void addSprite(Sprite sprite) {
animations.add(sprite);
sprites.add(sprite);
}

public void start() {
@@ -57,6 +81,14 @@ public void stop() {
@Override
public void run() {
while (start) {
// update all objects
animations.forEach(a -> a.update(canvas.getSize()));
// collect all the objects that are no longer needed
List<Animation> toRemoveList = animations.stream()
.filter(Animation::toRemove)
.collect(Collectors.toList());
animations.removeAll(toRemoveList);
// render all objects
canvas.repaint();
try {
Thread.sleep(timeDelta);
@@ -4,39 +4,33 @@
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JComponent;

import org.jam.core.game.animations.Animation;

public class GameCanvas extends JComponent {

private List<Animation> animations;
private Color backgroundColor;

public GameCanvas() {
this(Color.black);
public GameCanvas(List<Animation> animations) {
this(animations, Color.black);
}

public GameCanvas(Color backgroundColor) {
public GameCanvas(List<Animation> animations, Color backgroundColor) {
this.backgroundColor = backgroundColor;
this.animations = new LinkedList<>();
this.animations = animations;
this.setPreferredSize(new Dimension(800, 600));
}

public void addAnimation(Animation animation) {
animations.add(animation);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(backgroundColor);
g2d.fillRect(0, 0, getWidth(), getHeight());

animations.forEach(a -> {
a.update();
a.draw(g2d);
});
animations.forEach(a -> a.draw(g2d));
}
}
@@ -0,0 +1,63 @@
package org.jam.core.game.animations;

/**
* A base class of animation object
*/
public abstract class AbstractAnimationObject implements Animation {

protected int x;
protected int y;
protected int width;
protected int height;
protected boolean toRemove;

public AbstractAnimationObject() {
x = 0;
y = 0;
width = 0;
height = 0;
toRemove = false;
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

public void setToRemove(boolean toRemove) {
this.toRemove = toRemove;
}

@Override
public boolean toRemove() {
return toRemove;
}

}
@@ -0,0 +1,31 @@
package org.jam.core.game.animations;

import java.awt.Dimension;
import java.awt.Graphics2D;

/**
* An animation object.
*/
public interface Animation {

/**
* Update this animation object
*
* @param canvasDimension dimension of canvas
*/
void update(Dimension canvasDimension);

/**
* Check if this animation object needs to be removed.
*
* @return true if it needs to be removed
*/
boolean toRemove();

/**
* Draw this animation object.
*
* @param g2d painter
*/
void draw(Graphics2D g2d);
}
@@ -0,0 +1,60 @@
package org.jam.core.game.animations;

import java.util.HashMap;
import java.util.Map;

/**
* Sprite is a controllable animation object
*/
public abstract class Sprite extends AbstractAnimationObject {

protected Map<Integer, SpriteAction> pressedActions;
protected Map<Integer, SpriteAction> releasedActions;

public Sprite() {
pressedActions = new HashMap<>();
releasedActions = new HashMap<>();
}

/**
* Register key to an action. This action will be invoked when key pressed.
*
* @param keyCode key code {@link java.awt.event.KeyEvent}
* @param action sprite action {@link SpriteAction}
*/
public <T extends Sprite> void registerKeyPressedEvent(int keyCode, SpriteAction<T> action) {
pressedActions.put(keyCode, action);
}

/**
* Register key to an action. This action will be invoked when key released.
*
* @param keyCode key code {@link java.awt.event.KeyEvent}
* @param action sprite action {@link SpriteAction}
*/
public <T extends Sprite> void registerKeyReleasedEvent(int keyCode, SpriteAction<T> action) {
releasedActions.put(keyCode, action);
}

/**
* Delegate key press event to sprite action
*
* @param keyCode key code {@link java.awt.event.KeyEvent}
*/
public void onKeyPressed(int keyCode) {
if (pressedActions.containsKey(keyCode)) {
pressedActions.get(keyCode).run(this);
}
}

/**
* Delegate key press event to sprite action
*
* @param keyCode key code {@link java.awt.event.KeyEvent}
*/
public void onKeyReleased(int keyCode) {
if (releasedActions.containsKey(keyCode)) {
releasedActions.get(keyCode).run(this);
}
}
}
@@ -0,0 +1,12 @@
package org.jam.core.game.animations;

/**
* Sprite action.
*
* @param <T> subclass of sprite
*/
@FunctionalInterface
public interface SpriteAction<T extends Sprite> {

void run(T sprite);
}
@@ -1,37 +1,39 @@
package org.jam.demo;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;

import org.jam.core.game.Animation;
import org.jam.core.game.animations.AbstractAnimationObject;

public class AnimatedCircle implements Animation {
public class AnimatedCircle extends AbstractAnimationObject {

private int x;
private int y;
private int r;
private int dx;
private int dy;
private Color color;

public AnimatedCircle(int x, int y, int r, int dx, int dy, Color color) {
this.x = x;
this.y = y;
this.r = r;
this.width = r;
this.height = r;
this.dx = dx;
this.dy = dy;
this.color = color;
}

@Override
public void update() {
public void update(Dimension canvasDimension) {
x += dx;
y += dy;
if (x < 0 || y < 0 || x > canvasDimension.width || y > canvasDimension.height) {
toRemove = true;
}
}

@Override
public void draw(Graphics2D g2d) {
g2d.setColor(color);
g2d.fillOval(x, y, r, r);
g2d.fillOval(x, y, width, height);
}
}
@@ -1,27 +1,12 @@
package org.jam.demo;

import java.awt.Color;
import java.util.Random;
import java.util.stream.IntStream;

import org.jam.core.game.Game;

public class Main {

public static void main(String[] args) {
Random random = new Random();

Game game = new Game();
IntStream.range(0, 10)
.forEach(i -> {
int x = random.nextInt(100) + 350;
int y = random.nextInt(100) + 250;
int dx = random.nextInt(3) - 1;
int dy = random.nextInt(3) - 1;
int r = 5;
Color c = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
game.getCanvas().addAnimation(new AnimatedCircle(x, y, r, dx, dy, c));
});
game.addSprite(new Me());
game.start();
}
}
Oops, something went wrong.

0 comments on commit 5129ab8

Please sign in to comment.