Added source files.

This commit is contained in:
Deathsbreed 2014-05-26 16:04:54 -05:00
parent af17f7863d
commit a5459143f6
3 changed files with 247 additions and 0 deletions

View File

@ -0,0 +1,115 @@
package spaceshipsim;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.util.*;
import spaceshipsim.entities.*;
/**
* @author Nicolás A. Ortega
* @copyright Nicolás A. Ortega
* @license GNU GPLv3
* @year 2014
*
*/
public class SpaceShipSim extends JFrame implements Runnable, KeyListener {
private final String version = "v0.1";
private Thread gameloop;
private BufferedImage backbuffer;
private Graphics2D g2d;
private AffineTransform identity = new AffineTransform();
private Ship ship;
// Constructor:
public SpaceShipSim() {
super("Space Ship Simulator");
setSize(800, 600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
backbuffer = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
g2d = backbuffer.createGraphics();
ship = new Ship(400, 300);
addKeyListener(this);
start();
}
public void start() {
gameloop = new Thread(this);
gameloop.start();
}
public void stop() { gameloop = null; }
public void paint(Graphics g) {
g2d.setTransform(identity);
g2d.setPaint(Color.BLACK);
g2d.fillRect(0, 0, getSize().width, getSize().height);
drawInfo();
drawShip();
g.drawImage(backbuffer, 0, 0, this);
}
private void drawInfo() {
g2d.setPaint(Color.WHITE);
g2d.drawString("Welcome to SpaceShipSim " + version, 10, 40);
g2d.drawString("Position: " + (int)ship.getX() + ", " + (int)ship.getY(), 10, 55);
g2d.drawString("Velocity (px/s): " + (int)(ship.getVelX() * 50) + ", " + (int)(ship.getVelY() * 50), 10, 70);
g2d.drawString("Acceleration (px/s/s): " + (int)(ship.getAccelX() * Math.pow(50, 2)) + ", " + (int)(ship.getAccelY() * Math.pow(50, 2)), 10, 85);
g2d.drawString("Move Angle: " + (int)ship.getMoveAngle(), 10, 100);
g2d.drawString("Face Angle: " + (int)(ship.getFaceAngle() - 90), 10, 115);
}
private void drawShip() {
g2d.setTransform(identity);
g2d.translate(ship.getX(), ship.getY());
g2d.rotate(Math.toRadians(ship.getFaceAngle()));
g2d.setColor(Color.RED);
g2d.fill(ship.getShape());
}
public void run() {
Thread t = Thread.currentThread();
while(t == gameloop) {
try {
ship.update();
Thread.sleep(20);
} catch(InterruptedException ie) {
ie.printStackTrace();
}
repaint();
}
}
public void keyPressed(KeyEvent ke) {
//keys[ke.getKeyCode()] = true;
int keyCode = ke.getKeyCode();
if(keyCode == KeyEvent.VK_UP) { ship.setAccelerate(true); }
if(keyCode == KeyEvent.VK_LEFT) { ship.setTurnLeft(true); }
if(keyCode == KeyEvent.VK_RIGHT) { ship.setTurnRight(true); }
}
public void keyReleased(KeyEvent ke) {
int keyCode = ke.getKeyCode();
if(keyCode == KeyEvent.VK_UP) { ship.setAccelerate(false); }
if(keyCode == KeyEvent.VK_LEFT) { ship.setTurnLeft(false); }
if(keyCode == KeyEvent.VK_RIGHT) { ship.setTurnRight(false); }
}
public void keyTyped(KeyEvent ke) {}
public static void main(String[] args) { new SpaceShipSim(); }
}

View File

@ -0,0 +1,56 @@
package spaceshipsim.entities;
import java.awt.Shape;
/**
* @author Nicolás A. Ortega
* @copyright Nicolás A. Ortega
* @license GNU GPLv3
* @year 2014
*
*/
public class BaseVectorShape {
private Shape shape;
private boolean alive;
private double x, y;
private double velX, velY;
private double moveAngle, faceAngle;
// Constructor:
public BaseVectorShape() {
setShape(null);
setAlive(false);
setX(0.0);
setY(0.0);
setVelX(0.0);
setVelY(0.0);
setMoveAngle(0.0);
setFaceAngle(0.0);
}
// Accessor methods:
public Shape getShape() { return shape; }
public boolean isAlive() { return alive; }
public double getX() { return x; }
public double getY() { return y; }
public double getVelX() { return velX; }
public double getVelY() { return velY; }
public double getMoveAngle() { return moveAngle; }
public double getFaceAngle() { return faceAngle; }
// Setter methods:
public void setShape(Shape shape) { this.shape = shape; }
public void setAlive(boolean alive) { this.alive = alive; }
public void setX(double x) { this.x = x; }
public void incX(double iX) { this.x += iX; }
public void setY(double y) { this.y = y; }
public void incY(double iY) { this.y += iY; }
public void setVelX(double velX) { this.velX = velX; }
public void incVelX(double iVX) { this.velX += iVX; }
public void setVelY(double velY) { this.velY = velY; }
public void incVelY(double iVY) { this.velY += iVY; }
public void setMoveAngle(double nMA) { this.moveAngle = nMA; }
public void incMoveAngle(double iMA) { this.moveAngle += iMA; }
public void setFaceAngle(double nFA) { this.faceAngle = nFA; }
public void incFaceAngle(double iFA) { this.faceAngle += iFA; }
}

View File

@ -0,0 +1,76 @@
package spaceshipsim.entities;
import java.awt.*;
/**
* @author Nicolás A. Ortega
* @copyright Nicolás A. Ortega
* @license GNU GPLv3
* @year 2014
*
*/
public class Ship extends BaseVectorShape {
// Ship shape points for vector image
private int[] shipx = { -6, -3, 0, 3, 6, 0 };
private int[] shipy = { 6, 7, 7, 7, 6, -7 };
// Booleans for movement
private boolean accelerate = false;
private boolean turnLeft = false;
private boolean turnRight = false;
// Acceleration variables
private double accelerateX;
private double accelerateY;
// Constructor:
public Ship(double nx, double ny) {
setX(nx);
setY(ny);
setShape(new Polygon(shipx, shipy, shipx.length));
setAlive(true);
}
// Update Method:
public void update() {
if(accelerate) {
setMoveAngle(getFaceAngle() - 90);
accelerateX = calcAngleMoveX(getMoveAngle()) * 0.1;
accelerateY = calcAngleMoveY(getMoveAngle()) * 0.1;
incVelX(accelerateX);
incVelY(accelerateY);
} else {
accelerateX = 0;
accelerateY = 0;
}
if(turnLeft) {
incFaceAngle(-5);
if(getFaceAngle() < 0) { setFaceAngle(355); } // 355 = 360 - 5
}
if(turnRight) {
incFaceAngle(5);
if(getFaceAngle() > 360) { setFaceAngle(5); }
}
incX(getVelX());
incY(getVelY());
}
// Accessor methods:
public Rectangle getBounds() {
Rectangle r;
r = new Rectangle((int)getX() - 6, (int)getY() - 6, 12, 12);
return r;
}
public double getAccelX() { return accelerateX; }
public double getAccelY() { return accelerateY; }
// Setter methods:
public void setAccelerate(boolean accel) { this.accelerate = accel; }
public void setTurnLeft(boolean tL) { this.turnLeft = tL; }
public void setTurnRight(boolean tR) { this.turnRight = tR; }
// Methods used for calculations:
public double calcAngleMoveX(double angle) { return (double) (Math.cos(angle * Math.PI / 180)); }
public double calcAngleMoveY(double angle) { return (double) (Math.sin(angle * Math.PI / 180)); }
}