First steps.
This commit is contained in:
23
src/globals.h
Normal file
23
src/globals.h
Normal file
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef VERSION
|
||||
# define VERSION "[version]"
|
||||
#endif
|
26
src/main.c
Normal file
26
src/main.c
Normal file
@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "globals.h"
|
||||
|
||||
int main() {
|
||||
printf("SpaceShipSim v%s\n", VERSION);
|
||||
return 0;
|
||||
}
|
@ -1,106 +0,0 @@
|
||||
package spaceshipsim;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.geom.*;
|
||||
import java.util.*;
|
||||
|
||||
import spaceshipsim.entities.*;
|
||||
|
||||
/**
|
||||
* @author Nicolás A. Ortega
|
||||
* @copyright Nicolás A. Ortega
|
||||
* @license GNU GPLv3
|
||||
* @year 2014
|
||||
*
|
||||
*/
|
||||
public class SimPanel extends JPanel implements Runnable, KeyListener {
|
||||
public final static String version = "v0.3";
|
||||
|
||||
// Graphics/Framework items
|
||||
private Thread gameloop;
|
||||
private Graphics2D g2d;
|
||||
|
||||
// The Ship
|
||||
private Ship ship;
|
||||
|
||||
public SimPanel() {
|
||||
setFocusable(true);
|
||||
ship = new Ship(400, 300);
|
||||
addKeyListener(this);
|
||||
start();
|
||||
}
|
||||
|
||||
public void start() {
|
||||
gameloop = new Thread(this);
|
||||
gameloop.start();
|
||||
}
|
||||
|
||||
public void stop() { gameloop = null; }
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g2d = (Graphics2D) g;
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.fillRect(0, 0, 800, 600);
|
||||
|
||||
drawInfo();
|
||||
drawShip();
|
||||
}
|
||||
|
||||
private void drawInfo() {
|
||||
g2d.translate(0, 10);
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.drawString("Welcome to SpaceShipSim " + version, 10, 10);
|
||||
g2d.drawString("Position: " + (int)ship.getX() + ", " + (int)ship.getY(), 10, 25);
|
||||
g2d.drawString("Velocity (px/s): " + (int)(ship.getVelX() * 50) + ", " + (int)(ship.getVelY() * 50), 10, 40);
|
||||
g2d.drawString("Acceleration (px/s/s): " + (int)(ship.getAccelX() * Math.pow(50, 2)) + ", " + (int)(ship.getAccelY() * Math.pow(50, 2)), 10, 55);
|
||||
g2d.drawString("Move Angle: " + (int)ship.getMoveAngle(), 10, 70);
|
||||
g2d.drawString("Face Angle: " + (int)(ship.getFaceAngle() - 90), 10, 85);
|
||||
}
|
||||
|
||||
private void drawShip() {
|
||||
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) {
|
||||
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 void reset() {
|
||||
ship = new Ship(400, 300);
|
||||
}
|
||||
}
|
@ -1,125 +0,0 @@
|
||||
package spaceshipsim;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
/**
|
||||
* @author Nicolás A. Ortega
|
||||
* @copyright Nicolás A. Ortega
|
||||
* @license GNU GPLv3
|
||||
* @year 2014
|
||||
*
|
||||
*/
|
||||
public class SpaceShipSim {
|
||||
private JFrame frame;
|
||||
private SimPanel panel;
|
||||
|
||||
// Menu items
|
||||
JMenuBar menuBar;
|
||||
|
||||
JMenu simulationMenu;
|
||||
JMenuItem resetItem;
|
||||
JMenuItem exitItem;
|
||||
|
||||
JMenu helpMenu;
|
||||
JMenuItem instructionsItem;
|
||||
JMenuItem licenseItem;
|
||||
JMenuItem aboutItem;
|
||||
|
||||
// Constructor:
|
||||
public SpaceShipSim() {
|
||||
frame = new JFrame("SpaceShipSim");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
panel = new SimPanel();
|
||||
frame.add(panel);
|
||||
menuSetup();
|
||||
frame.setSize(800, 600);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
public void menuSetup() {
|
||||
menuBar = new JMenuBar();
|
||||
simulationMenu = new JMenu("Simulation");
|
||||
resetItem = new JMenuItem("Reset");
|
||||
exitItem = new JMenuItem("Exit");
|
||||
helpMenu = new JMenu("Help");
|
||||
instructionsItem = new JMenuItem("Instructions");
|
||||
licenseItem = new JMenuItem("License");
|
||||
aboutItem = new JMenuItem("About");
|
||||
|
||||
resetItem.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent ae) { panel.reset(); }
|
||||
});
|
||||
exitItem.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent ae) {
|
||||
panel.stop();
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
|
||||
instructionsItem.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent ae) {
|
||||
new Window("Instructions", "Accelerate: Up Arrow\n" +
|
||||
"Turn left: Left Arrow\n" +
|
||||
"Turn right: Right Arrow\n\n");
|
||||
}
|
||||
});
|
||||
licenseItem.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent ae) {
|
||||
new Window("License Information",
|
||||
"SpaceShipSim " + panel.version + ", a simulation of a spaceship in a frictionless environment\n" +
|
||||
"Copyright (C) 2014 Nicolás A. Ortega\n\n" +
|
||||
"This program is free software: you can redistribute it and/or modify\n" +
|
||||
"it under the terms of the GNU General Public License as published by\n" +
|
||||
"the Free Software Foundation, either version 3 of the License, or\n" +
|
||||
"(at your option) any later version.\n\n" +
|
||||
"This program is distributed in the hope that it will be useful,\n" +
|
||||
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
|
||||
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" +
|
||||
"GNU General Public License for more details.\n\n" +
|
||||
"You should have received a copy of the GNU General Public License\n" +
|
||||
"along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n");
|
||||
}
|
||||
});
|
||||
aboutItem.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent ae) {
|
||||
new Window("About", "SpaceShipSim " + panel.version + "\n" +
|
||||
"Copyright (C) 2014 Nicolás A. Ortega\n" +
|
||||
"Contact: nicolas.ortega.froysa@gmail.com\n" +
|
||||
"Source-code: https://github.com/Deathsbreed/SpaceShipSim\n" +
|
||||
"Developers: Nicolás Ortega\n\n");
|
||||
}
|
||||
});
|
||||
|
||||
resetItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK));
|
||||
exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK));
|
||||
|
||||
|
||||
simulationMenu.add(resetItem);
|
||||
simulationMenu.addSeparator();
|
||||
simulationMenu.add(exitItem);
|
||||
|
||||
helpMenu.add(instructionsItem);
|
||||
helpMenu.add(licenseItem);
|
||||
helpMenu.addSeparator();
|
||||
helpMenu.add(aboutItem);
|
||||
|
||||
menuBar.add(simulationMenu);
|
||||
menuBar.add(helpMenu);
|
||||
|
||||
frame.setJMenuBar(menuBar);
|
||||
}
|
||||
|
||||
public class Window {
|
||||
public Window(String title, String msg) {
|
||||
JOptionPane.showMessageDialog(null, msg, title, JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) { new SpaceShipSim(); }
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
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 float x, y;
|
||||
private float velX, velY;
|
||||
private float moveAngle, faceAngle;
|
||||
|
||||
// Constructor:
|
||||
public BaseVectorShape() {
|
||||
setShape(null);
|
||||
setAlive(false);
|
||||
setX(0.0f);
|
||||
setY(0.0f);
|
||||
setVelX(0.0f);
|
||||
setVelY(0.0f);
|
||||
setMoveAngle(0.0f);
|
||||
setFaceAngle(0.0f);
|
||||
}
|
||||
|
||||
// Accessor methods:
|
||||
public Shape getShape() { return shape; }
|
||||
public boolean isAlive() { return alive; }
|
||||
public float getX() { return x; }
|
||||
public float getY() { return y; }
|
||||
public float getVelX() { return velX; }
|
||||
public float getVelY() { return velY; }
|
||||
public float getMoveAngle() { return moveAngle; }
|
||||
public float getFaceAngle() { return faceAngle; }
|
||||
|
||||
// Setter methods:
|
||||
public void setShape(Shape shape) { this.shape = shape; }
|
||||
public void setAlive(boolean alive) { this.alive = alive; }
|
||||
public void setX(float x) { this.x = x; }
|
||||
public void incX(float iX) { this.x += iX; }
|
||||
public void setY(float y) { this.y = y; }
|
||||
public void incY(float iY) { this.y += iY; }
|
||||
public void setVelX(float velX) { this.velX = velX; }
|
||||
public void incVelX(float iVX) { this.velX += iVX; }
|
||||
public void setVelY(float velY) { this.velY = velY; }
|
||||
public void incVelY(float iVY) { this.velY += iVY; }
|
||||
public void setMoveAngle(float nMA) { this.moveAngle = nMA; }
|
||||
public void incMoveAngle(float iMA) { this.moveAngle += iMA; }
|
||||
public void setFaceAngle(float nFA) { this.faceAngle = nFA; }
|
||||
public void incFaceAngle(float iFA) { this.faceAngle += iFA; }
|
||||
}
|
@ -1,81 +0,0 @@
|
||||
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 float accelerateX;
|
||||
private float accelerateY;
|
||||
|
||||
// Constructor:
|
||||
public Ship(float nx, float 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.1f;
|
||||
accelerateY = calcAngleMoveY(getMoveAngle()) * 0.1f;
|
||||
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 float getAccelX() { return accelerateX; }
|
||||
public float getAccelY() { return accelerateY; }
|
||||
|
||||
// Setter methods:
|
||||
public void setAccelerate(boolean accel) { this.accelerate = accel; }
|
||||
public void setAccelX(float aX) { this.accelerateX = aX; }
|
||||
public void setAccelY(float aY) { this.accelerateY = aY; }
|
||||
public void incAccelX(float iaX) { this.accelerateX += iaX; }
|
||||
public void incAccelY(float iaY) { this.accelerateY += iaY; }
|
||||
public void setTurnLeft(boolean tL) { this.turnLeft = tL; }
|
||||
public void setTurnRight(boolean tR) { this.turnRight = tR; }
|
||||
|
||||
// Methods used for calculations:
|
||||
public float calcAngleMoveX(float angle) { return (float)Math.cos(angle * Math.PI / 180); }
|
||||
public float calcAngleMoveY(float angle) { return (float)Math.sin(angle * Math.PI / 180); }
|
||||
}
|
Reference in New Issue
Block a user