forked from vanZeben/2D-Game-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Original upload. Updated to Episode 15
- Loading branch information
Ryan van Zeben
authored and
Ryan van Zeben
committed
Nov 6, 2012
0 parents
commit 287935d
Showing
17 changed files
with
838 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package ca.vanzeben.game; | ||
|
||
import java.awt.BorderLayout; | ||
import java.awt.Canvas; | ||
import java.awt.Dimension; | ||
import java.awt.Graphics; | ||
import java.awt.image.BufferStrategy; | ||
import java.awt.image.BufferedImage; | ||
import java.awt.image.DataBufferInt; | ||
|
||
import javax.swing.JFrame; | ||
import javax.swing.JOptionPane; | ||
|
||
import ca.vanzeben.game.entities.Player; | ||
import ca.vanzeben.game.gfx.Screen; | ||
import ca.vanzeben.game.gfx.SpriteSheet; | ||
import ca.vanzeben.game.level.Level; | ||
|
||
public class Game extends Canvas implements Runnable { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
public static final int WIDTH = 160; | ||
public static final int HEIGHT = WIDTH / 12 * 9; | ||
public static final int SCALE = 3; | ||
public static final String NAME = "Game"; | ||
|
||
private JFrame frame; | ||
|
||
public boolean running = false; | ||
public int tickCount = 0; | ||
|
||
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); | ||
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); | ||
private int[] colours = new int[6 * 6 * 6]; | ||
|
||
private Screen screen; | ||
public InputHandler input; | ||
public Level level; | ||
public Player player; | ||
|
||
public Game() { | ||
setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); | ||
setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); | ||
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); | ||
|
||
frame = new JFrame(NAME); | ||
|
||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
frame.setLayout(new BorderLayout()); | ||
|
||
frame.add(this, BorderLayout.CENTER); | ||
frame.pack(); | ||
|
||
frame.setResizable(false); | ||
frame.setLocationRelativeTo(null); | ||
frame.setVisible(true); | ||
} | ||
|
||
public void init() { | ||
int index = 0; | ||
for (int r = 0; r < 6; r++) { | ||
for (int g = 0; g < 6; g++) { | ||
for (int b = 0; b < 6; b++) { | ||
int rr = (r * 255 / 5); | ||
int gg = (g * 255 / 5); | ||
int bb = (b * 255 / 5); | ||
|
||
colours[index++] = rr << 16 | gg << 8 | bb; | ||
} | ||
} | ||
} | ||
screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png")); | ||
input = new InputHandler(this); | ||
level = new Level("/levels/water_test_level.png"); | ||
player = new Player(level, 0, 0, input, JOptionPane.showInputDialog(this, "Please enter a username")); | ||
level.addEntity(player); | ||
} | ||
|
||
public synchronized void start() { | ||
running = true; | ||
new Thread(this).start(); | ||
} | ||
|
||
public synchronized void stop() { | ||
running = false; | ||
} | ||
|
||
public void run() { | ||
long lastTime = System.nanoTime(); | ||
double nsPerTick = 1000000000D / 60D; | ||
|
||
int ticks = 0; | ||
int frames = 0; | ||
|
||
long lastTimer = System.currentTimeMillis(); | ||
double delta = 0; | ||
|
||
init(); | ||
|
||
while (running) { | ||
long now = System.nanoTime(); | ||
delta += (now - lastTime) / nsPerTick; | ||
lastTime = now; | ||
boolean shouldRender = true; | ||
|
||
while (delta >= 1) { | ||
ticks++; | ||
tick(); | ||
delta -= 1; | ||
shouldRender = true; | ||
} | ||
|
||
try { | ||
Thread.sleep(2); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
if (shouldRender) { | ||
frames++; | ||
render(); | ||
} | ||
|
||
if (System.currentTimeMillis() - lastTimer >= 1000) { | ||
lastTimer += 1000; | ||
System.out.println(ticks + " ticks, " + frames + " frames"); | ||
frames = 0; | ||
ticks = 0; | ||
} | ||
} | ||
} | ||
|
||
public void tick() { | ||
tickCount++; | ||
level.tick(); | ||
} | ||
|
||
public void render() { | ||
BufferStrategy bs = getBufferStrategy(); | ||
if (bs == null) { | ||
createBufferStrategy(3); | ||
return; | ||
} | ||
|
||
int xOffset = player.x - (screen.width / 2); | ||
int yOffset = player.y - (screen.height / 2); | ||
|
||
level.renderTiles(screen, xOffset, yOffset); | ||
level.renderEntities(screen); | ||
|
||
for (int y = 0; y < screen.height; y++) { | ||
for (int x = 0; x < screen.width; x++) { | ||
int colourCode = screen.pixels[x + y * screen.width]; | ||
if (colourCode < 255) | ||
pixels[x + y * WIDTH] = colours[colourCode]; | ||
} | ||
} | ||
|
||
Graphics g = bs.getDrawGraphics(); | ||
g.drawImage(image, 0, 0, getWidth(), getHeight(), null); | ||
g.dispose(); | ||
bs.show(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
new Game().start(); | ||
} | ||
|
||
public static long fact(int n) { | ||
if (n <= 1) { | ||
return 1; | ||
} else { | ||
return n * fact(n - 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package ca.vanzeben.game; | ||
|
||
import java.awt.event.KeyEvent; | ||
import java.awt.event.KeyListener; | ||
|
||
public class InputHandler implements KeyListener { | ||
|
||
public InputHandler(Game game) { | ||
game.addKeyListener(this); | ||
} | ||
|
||
public class Key { | ||
private int numTimesPressed = 0; | ||
private boolean pressed = false; | ||
|
||
public int getNumTimesPressed() { | ||
return numTimesPressed; | ||
} | ||
|
||
public boolean isPressed() { | ||
return pressed; | ||
} | ||
|
||
public void toggle(boolean isPressed) { | ||
pressed = isPressed; | ||
if (isPressed) numTimesPressed++; | ||
} | ||
} | ||
|
||
public Key up = new Key(); | ||
public Key down = new Key(); | ||
public Key left = new Key(); | ||
public Key right = new Key(); | ||
|
||
public void keyPressed(KeyEvent e) { | ||
toggleKey(e.getKeyCode(), true); | ||
} | ||
|
||
public void keyReleased(KeyEvent e) { | ||
toggleKey(e.getKeyCode(), false); | ||
} | ||
|
||
public void keyTyped(KeyEvent e) { | ||
} | ||
|
||
public void toggleKey(int keyCode, boolean isPressed) { | ||
if (keyCode == KeyEvent.VK_W || keyCode == KeyEvent.VK_UP) { | ||
up.toggle(isPressed); | ||
} | ||
if (keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_DOWN) { | ||
down.toggle(isPressed); | ||
} | ||
if (keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_LEFT) { | ||
left.toggle(isPressed); | ||
} | ||
if (keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT) { | ||
right.toggle(isPressed); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ca.vanzeben.game.entities; | ||
|
||
import ca.vanzeben.game.gfx.Screen; | ||
import ca.vanzeben.game.level.Level; | ||
|
||
public abstract class Entity { | ||
|
||
public int x, y; | ||
protected Level level; | ||
|
||
public Entity(Level level) { | ||
init(level); | ||
} | ||
|
||
public final void init(Level level) { | ||
this.level = level; | ||
} | ||
|
||
public abstract void tick(); | ||
|
||
public abstract void render(Screen screen); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package ca.vanzeben.game.entities; | ||
|
||
import ca.vanzeben.game.level.Level; | ||
import ca.vanzeben.game.level.tiles.Tile; | ||
|
||
public abstract class Mob extends Entity { | ||
|
||
protected String name; | ||
protected int speed; | ||
protected int numSteps = 0; | ||
protected boolean isMoving; | ||
protected int movingDir = 1; | ||
protected int scale = 1; | ||
|
||
public Mob(Level level, String name, int x, int y, int speed) { | ||
super(level); | ||
this.name = name; | ||
this.x = x; | ||
this.y = y; | ||
this.speed = speed; | ||
} | ||
|
||
public void move(int xa, int ya) { | ||
if (xa != 0 && ya != 0) { | ||
move(xa, 0); | ||
move(0, ya); | ||
numSteps--; | ||
return; | ||
} | ||
numSteps++; | ||
if (!hasCollided(xa, ya)) { | ||
if (ya < 0) | ||
movingDir = 0; | ||
if (ya > 0) | ||
movingDir = 1; | ||
if (xa < 0) | ||
movingDir = 2; | ||
if (xa > 0) | ||
movingDir = 3; | ||
x += xa * speed; | ||
y += ya * speed; | ||
} | ||
} | ||
|
||
public abstract boolean hasCollided(int xa, int ya); | ||
|
||
protected boolean isSolidTile(int xa, int ya, int x, int y) { | ||
if (level == null) { | ||
return false; | ||
} | ||
Tile lastTile = level.getTile((this.x + x) >> 3, (this.y + y) >> 3); | ||
Tile newTile = level.getTile((this.x + x + xa) >> 3, (this.y + y + ya) >> 3); | ||
if (!lastTile.equals(newTile) && newTile.isSolid()) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
} |
Oops, something went wrong.