Initial dirty commit of old code into git

This commit is contained in:
Paul Schaub 2019-12-12 15:20:41 +01:00
commit 5546f26619
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
1985 changed files with 255900 additions and 0 deletions

8
.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
.metadata/
*/.metadata/
*/.project
*/.classpath
*/.setting/
*/bin/

View file

@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7

90
Game/src/Ammunition.java Normal file
View file

@ -0,0 +1,90 @@
/**
* Projectile with ballistic attributes and physics
* @author vanitas,lil
*
*/
public class Ammunition extends MovingObject
{
private float durability, //Lifetime in seconds
lifetime; //"Age" of the projectile in seconds
/**
* Constructor
* @param direction
* @param x location
* @param y
* @param pearcing can this penetrate other Objects?
*/
public Ammunition(float rotation, float x, float y, boolean pearcing)
{
//this.direction = (float) Math.toRadians(direction+90); //Set the direction of the projectile
this.setRotation(rotation);
this.x = x; this.y = y; this.durability = 0; //Set variables
this.setShape(new Shape(Shape.S_BULLET));
this.getShape().setSize(1f);
this.durability = 2; //Lifetime in seconds
}
/**
* moves the object one frame
*/
@Override
public void move(float delta, boolean keepOnScreen)
{
float xNeu = (float) (x+Math.cos((float) Math.toRadians(90-this.getRotation()))*0.35f*delta);
float yNeu = (float) (y+Math.sin((float) Math.toRadians(90-this.getRotation()))*0.35f*delta);
this.lifetime += (delta/1000); //Let the projectile "age"
if(this.lifetime>this.durability) //If "too old" do something
{
SpaceSpiel.getMunition().remove(this);
}
x = xNeu;
y = yNeu;
if(keepOnScreen) keepOnScreen();
}
/**
* modifies the speedvector (In this case it doesnt, cause a bullet in space doesn't
* change direction and speed
*/
@Override
public void accelerate()
{
//Keep this empty, Munition doesn't accelerate (Except for rockets maybe)
}
//Getters and setters
public float getDurability()
{
return durability;
}
public void setDurability(float durability)
{
this.durability = durability;
}
public float getX()
{
return x;
}
public void setX(float x)
{
this.x = x;
}
public float getY()
{
return y;
}
public void setY(float y)
{
this.y = y;
}
public float getLifetime()
{
return lifetime;
}
public void setLifetime(float lifetime)
{
this.lifetime = lifetime;
}
}

50
Game/src/Button.java Normal file
View file

@ -0,0 +1,50 @@
import org.lwjgl.util.Rectangle;
import java.awt.Font;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;
public class Button{
private boolean isClicked;
private String name;
Rectangle bounds = new Rectangle();
private TrueTypeFont font;
public Button(float loc, String name) {
super();
this.name = name;
this.isClicked = false;
bounds.setX(50);
bounds.setY((int) ((loc * 50) - 15));
bounds.setHeight(30);
bounds.setWidth(100);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isClicked() {
return isClicked;
}
public void setClicked(boolean isClicked) {
this.isClicked = isClicked;
}
public Rectangle getBounds() {
return bounds;
}
public void setBounds(Rectangle bounds) {
this.bounds = bounds;
}
public TrueTypeFont getFont() {
return font;
}
public void setFont(TrueTypeFont font) {
this.font = font;
}
public void init(String name) {
}
}

View file

@ -0,0 +1,19 @@
/**
* Implementation of a ship controlled by a human
* @author vanitas
*
*/
public class ControlledShip extends Ship
{
public ControlledShip()
{
super();
this.setX(SpaceSpiel.frameSizeX/2); //Place the ship in the middle of the screen
this.setY(SpaceSpiel.frameSizeY/2);
this.setRotation(0f); //Initialize rotation
this.setThrust(0.1f); //Set thrust and maxSpeed
this.setMaxSpeed(6f);
}
}

279
Game/src/Game.java Normal file
View file

@ -0,0 +1,279 @@
import java.util.ArrayList;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.input.Mouse;
/**
* This is the main class of the game. This renders the game, handles input and output etc.
* Important methods: update, renderGL
* @author vanitas
*
*/
public class Game{
long lastFrame, lastFPS; //Values to calibrate the framerate etc.
int fps;
public static final int frameSizeX = 800, frameSizeY = 600; //Resolution of the Window
@SuppressWarnings("rawtypes")
private ArrayList<ArrayList> objects; //A ArrayList containing th following Lists
private ArrayList<Metroid> metroids; //A List Containing all the metroids
private ArrayList<MovingObject> ships; //A List Containing all the ships etc
private static ArrayList<Ammunition> munition; //A List Containing all the active fired projectiles
private ControlledShip playerShip; //The players ship
private Menu menu; //The Menu-Display
private boolean menuIsActive; //if menu is active this variable is true
private boolean closeRequested = false;
/**
* Constructor
*/
@SuppressWarnings("rawtypes")
public Game()
{
//
//Initialize ship and Objects (Add Objects here and dont forget to add them to the ArrayLists as well;)
playerShip = new ControlledShip();
Turret geschuetz = new Turret(playerShip.getX(),playerShip.getY());
geschuetz.setParent(playerShip);
//Initialize ArrayLists
objects = new ArrayList<ArrayList>();
metroids = new ArrayList<Metroid>();
ships = new ArrayList<MovingObject>();
munition = new ArrayList<Ammunition>();
//Add Objects to Lists
ships.add(playerShip);
ships.add(geschuetz);
//Pack Lists together
objects.add(metroids);
objects.add(ships);
objects.add(munition);
}
/**
* This method updates all the objects (moves them, captures keyboard and mouse input etc)
* @param delta
*/
public void update(int delta)
{
//Handle Keyboard KeyPresses
//Gas geben ;)
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) //Arrow up
{
playerShip.setVelocity(1f); //Set the velocity to 1 (Power on)
}
//rotation (Arrow right)
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
{
playerShip.turnRight(delta);
}
//Arrow left
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
{
playerShip.turnLeft(delta);
}
//When Arrow up is released, turn down the power.
if(!Keyboard.isKeyDown(Keyboard.KEY_UP))
{
playerShip.setVelocity(0f);
}
//If Spacebar is pressed and shotburnout is over, fire a projectile
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE))
{
playerShip.fire(delta);
}
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
{
menuIsActive = true;
}
//Call an update (movements etc.) for every Object in the game
updateObjects(delta);
updateFPS(); // update FPS Counter
}
/**
* Update (accelerate and move) all the Objects in the ArrayLists
* @param delta
*/
public void updateObjects(float delta)
{
for(int i = 0; i<objects.size(); i++)
{
ArrayList<?> l = objects.get(i);
for(int j = 0; j<l.size(); j++)
{
((MovingObject) l.get(j)).accelerate();
((MovingObject) l.get(j)).move(delta, true);
}
}
}
/**
* Main Rendering method. This calls the subrendermethods for the ship, metroids etc.
* This renderes all the Objects in the Lists.
* Remember: Whatever gets rendered last gets rendered on top of everything else (Stack)
*/
public void renderGL() {
// Clear The Screen And The Depth Buffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
//Load ArrayLists and render every Object inside
for(int i = 0; i<objects.size(); i++)
{
ArrayList<?> l = objects.get(i);
for(int j = 0; j<l.size(); j++)
{
//Render
renderObject((MovingObject) l.get(j));
}
}
}
/**
* This Method should be able to render any Shape with vertices given by Shape
* @param o
*/
public void renderObject(MovingObject o)
{
//Get the shape of the object o
Shape s = o.getShape();
//get the color
float[] c = s.getColor();
//Beginn a matrix
GL11.glPushMatrix();
//Set color
GL11.glColor3f(c[0], c[1], c[2]);
//Move it
GL11.glTranslatef(o.getX(), o.getY(), 0f);
//Rotate and scale
GL11.glRotatef(-o.getRotation(), 0f, 0f, 1f);
GL11.glScalef(s.getSize(), s.getSize(), 1f);
//Translate it back
GL11.glTranslatef(-o.getX(), -o.getY(), 0f);
//Load all the Vertices
GL11.glBegin(GL11.GL_POLYGON);
for(int i = 0; i<s.getLength(); i++)
{
GL11.glVertex2f(o.getX()+s.getX(i),o.getY()+s.getY(i));
}
GL11.glEnd();
GL11.glPopMatrix();
}
/**
* Initializes the OpenGL System. Ignore this.
*/
public void initGL()
{
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, frameSizeX, 0, frameSizeY, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
/**
* This method starts the game
*/
public void start()
{
try //Create the Display
{
Display.setDisplayMode(new DisplayMode(frameSizeX, frameSizeY));
Display.create();
}
catch (LWJGLException e) //When something goes wrong, evacuate the dancefloor!!!
{
e.printStackTrace();
System.exit(0);
}
initGL(); //initialize OpenGL
getDelta();
lastFPS = getTime(); //initialize timer (just for calibrating framerate, ignore this!)
//This loop runs as long as the game is running. Every run creates a frame
while (!Display.isCloseRequested() && !this.closeRequested)
{
int delta = getDelta(); //Value to sync fps and movements (lower fps != lower moves)
update(delta);
renderGL(); //Render the frame
if(!menuIsActive) Display.update(); //Update the display (I have no idea ;))
Display.sync(60); //cap fps to 60fps
}
Display.destroy(); //Kill the display, when the window is closed
}
/**
* Returns a value used to sync fps and movements
* @return
*/
public int getDelta()
{
long time = getTime();
int delta = 0;
if(!menuIsActive)
{
delta = (int) (time - lastFrame); //If the menu is active, there should be no more movements etc
}
lastFrame = time;
return delta;
}
/**
* Returns the time of the system in (i think) milliseconds
* @return
*/
public long getTime()
{
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
/**
* Updates the title of the screen with the current fps
*/
public void updateFPS()
{
if (getTime() - lastFPS > 1000)
{
Display.setTitle("The best game in the world. FPS: " + fps);
fps = 0;
lastFPS += 1000;
}
fps++;
}
public ArrayList<ArrayList> getObjects()
{
return objects;
}
public void setObjects(ArrayList<ArrayList> objects)
{
this.objects = objects;
}
public ArrayList<Metroid> getMetroids()
{
return metroids;
}
public void setMetroids(ArrayList<Metroid> metroids)
{
this.metroids = metroids;
}
public ArrayList<MovingObject> getShips()
{
return ships;
}
public void setShips(ArrayList<MovingObject> ships)
{
this.ships = ships;
}
public static ArrayList<Ammunition> getMunition()
{
return munition;
}
public void setMunition(ArrayList<Ammunition> munition)
{
this.munition = munition;
}
}

28
Game/src/Item.java Normal file
View file

@ -0,0 +1,28 @@
/**
* TODO: Item wird gedropt und fliegt kurz weiter, verlangsamt aber.
* Wenn der Spieler sich nähert gleitet das Item mit wachsender
* Geschwindigkeit auf den Spieler zu, wird quasi "angezogen"
* @author vanitas,
*
*/
public class Item extends MovingObject
{
private float friction = 0.95f;
public Item()
{
this.setShape(new Shape(Shape.S_ITEM));
}
/**
* This doesn't accelerate the Item, but it slows it down till it stops.
*/
@Override
public void accelerate() //This Method is overwritten to let the Item
{ //glide with decreasing speed, untill it stops
if(this.getSpeedX()>0.001) this.speedX = this.getSpeedX()*friction;
else this.setSpeedX(0f);
if(this.getSpeedY()>0.001) this.speedY = this.getSpeedY()*friction;
else this.setSpeedY(0f);
}
}

29
Game/src/MathShit.java Normal file
View file

@ -0,0 +1,29 @@
public class MathShit
{
/**
* Calculates the x-component of a vector, rotated around 0|0 by angle
* @param angle angle to rotate, in radians
* @param x old x-component of the vector or point
* @param y old y-component of the vector or point
* @return the new x component
*/
public static float rotateX(double angle, float x, float y)
{
double beta = (2*Math.PI)-angle;
return (float) (Math.cos(beta)*x-Math.sin(beta)*y);
}
/**
* Calculates the y-component of a vector, rotated around 0|0 by angle
* @param angle angle to rotate, in radians
* @param x old x-component of the vector or point
* @param y old y-component of the vector or point
* @return the new y-component
*/
public static float rotateY(double angle, float x, float y)
{
double beta = (2*Math.PI)-angle;
return (float) (Math.sin(beta)*x+Math.cos(beta)*y);
}
}

7
Game/src/Menu.java Normal file
View file

@ -0,0 +1,7 @@
public class Menu{
public Menu(){
}
}

108
Game/src/Metroid.java Normal file
View file

@ -0,0 +1,108 @@
import java.util.ArrayList;
/**
* A floating, gliding metroid TODO: Maybe add lifepoints, so that multiple hits are needed to destroy this
* @author vanitas
*
*/
public class Metroid extends MovingObject
{
private int level, //Size of the Metroid (default 3-1)
countBabies; //How many new Metroids spawn, when THIS gets killed?
private float spin;
public Metroid(int level)
{
this.level = level;
this.spin = (float) Math.random();
this.shape = new Shape(Shape.S_METROID);
this.getShape().setSize(level);
this.x = 400f;
this.y = 150f;
this.speedX = (float) (Math.random()*2f);
this.speedY = (float) (Math.random()*2f);
}
/**
* Creates a Metroid with
* @param level
* @param x
* @param y
* @param speedX
* @param speedY
*/
public Metroid(int level, float x, float y, float speedX, float speedY, float spin)
{
this.level = level;
this.spin = (float) Math.random();
this.shape = new Shape(Shape.S_METROID);
this.getShape().setSize(level*1.5f);
this.x = x;
this.y = y;
this.speedX = speedX;
this.speedY = speedY;
}
/**
* This method moves the Metroid according to its speedVector
* @param delta
* @param keepOnScreen if true the method calls keepOnScreen()
*/
@Override
public void move(float delta, boolean keepOnScreen)
{
this.x += delta*0.1f*this.speedX;
this.y += delta*0.1f*this.speedY;
this.rotation += delta*this.spin*0.2f;
if(keepOnScreen) this.keepOnScreen();
}
/**
* this method handles the case that THIS Metroid gets hit by an Object (Normally Ammunition)
* @param o The object that hit the Metroid
*/
@SuppressWarnings("unchecked")
@Override
public void processCollision(MovingObject o)
{
ArrayList<Metroid> m = SpaceSpiel.getMetroids();
if(this.level>1)
{
for(int i = 0; i<4; i++)
{
m.add(new Metroid(this.level-1, this.getX(),this.getY(),(float) (Math.random()*2f),(float) (Math.random()*2f),(float) Math.random()));
}
}
else
{
//TODO: By chance of 5% or so add some random Items or PowerUps to the game.
//
}
m.remove(this);
}
/**
* An empty method (A Metroid shouldn't accelerate)
* Only here to overwrite the parents method
*/
@Override
public void accelerate()
{
//Has to be empty
}
//Getters and setters
public int getLevel()
{
return this.level;
}
public void setLevel(int level)
{
this.level = level;
}
public float getSpin()
{
return this.spin;
}
public void setSpin(float spin)
{
this.spin = spin;
}
}

251
Game/src/MovingObject.java Normal file
View file

@ -0,0 +1,251 @@
/**
* An Object that moves following physical rules
* @author vanitas
*
*/
public class MovingObject
{
//Attributes
protected float velocity, //whats the velocity of the object? 0 or 1
x, //x position of the object
y, //y position of the object
speedX, //x component of the speedvector
speedY, //y component (eg. x=2,y=2 => object is flying to the right corner with a speed of 2.7
maxSpeed, //Maximal speed of the object
rotation, //totation of the object (0° <=> object points towards the top of the screen)
thrust; //Power of acceleration
protected int team; //Which team is the object in? 0 = nature
protected Shape shape; //Contains the vertices for the shape
//Constructors (Lol there are none)
/**
* moves the Object one frame
* @param delta
* @param keepOnScreen
*/
public void move(float delta, boolean keepOnScreen)
{
//Set new x and y locations calculated from old location and
//Speedvector
this.setX(this.getX()+this.getSpeedX()*delta*0.2f);
this.setY(this.getY()+this.getSpeedY()*delta*0.2f);
//Eventually keep the object on the screen if it leaves at the borders
if(keepOnScreen) this.keepOnScreen();
}
/**
* Rotates the Object to the right
* @param delta
*/
public void turnRight(int delta)
{
this.setRotation(this.getRotation() + 0.35f * delta);
}
/**
* Rotates the Object to the left
* @param delta
*/
public void turnLeft(int delta)
{
this.setRotation(this.getRotation() - 0.35f * delta);
}
/**
* Let this Object follow another Object
* @param x
*/
public void follow(MovingObject x) //TODO Test this
{
float xOther = x.getX();
float yOther = x.getY();
float hypot = (float) Math.hypot(Math.abs(this.x-xOther), Math.abs(this.y-yOther));
//Set Rotation
float rot;
if(this.x<xOther) rot = (float) -(Math.toDegrees((Math.PI/2)-Math.asin((yOther-this.getY())/hypot)));
else rot = (float) (Math.toDegrees((Math.PI/2)-Math.asin((yOther-this.getY())/hypot)));
//Catch NaN (if rot is Infinity)
if(!Float.isNaN(rot)) this.setRotation(rot);
else this.setRotation(0);
this.setVelocity(1);
}
/**
* Calculate the SpeedVector for the Object.
*/
public void accelerate()
{
this.speedX = (float) (Math.sin(Math.toRadians(this.getRotation()))*this.getThrust()*this.getVelocity()+this.getSpeedX());
this.speedY = (float) (Math.cos(Math.toRadians(this.getRotation()))*this.getThrust()*this.getVelocity()+this.getSpeedY());
double actualSpeed = Math.sqrt(Math.pow(this.getSpeedX(),2)+Math.pow(this.getSpeedY(),2));
if(actualSpeed>this.getMaxSpeed())
{
double fac = actualSpeed/this.getMaxSpeed();
this.speedX = (float) (this.speedX/fac);
this.speedY = (float) (this.speedY/fac);
}
}
/**
* Keep the Object on the screen if it leaves to the borders
*/
public void keepOnScreen()
{
this.x = this.x%SpaceSpiel.frameSizeX;
this.y = this.y%SpaceSpiel.frameSizeY;
if(this.x<0) this.x = SpaceSpiel.frameSizeX+this.x;
if(this.y<0) this.y = SpaceSpiel.frameSizeY+this.y;
}
/**
* This Method fires (In this class its only there, to be overwritten)
* @param delta
*/
public void fire(float delta)
{
//Let me empty
}
/**
* Checks, if this Object collides with another TODO: This is not accurate, maybe there's a fix for this
* @param otherObject The Object, this Object should check for collision with
* @return the Object if collided, else null
* @author lil, Oooh, vanitas
*/
public MovingObject intersect(MovingObject otherObject)
{
//Extract the shapes of the objects
Shape one = this.getShape();
Shape other = otherObject.getShape();
//Go through all the edges of the shape of THIS object
for(int i = 0; i<one.getLength(); i++)
{
int iinc = (i+1)%one.getLength();
//Go through all the edges of otherObject's shape
for(int j = 0; j< other.getLength(); j++)
{
int jinc = (j+1)%other.getLength();
//Calculate all the start and end-points of the edges
//(1-2=>current edge of THIS shape, 3-4 => current edge of otherObject's shape)
double x1 = this.getX()+MathShit.rotateX(Math.toRadians(this.getRotation()), one.getX(i), one.getY(i))*one.getSize();
double x2 = this.getX()+MathShit.rotateX(Math.toRadians(this.getRotation()), one.getX(iinc), one.getY(iinc))*one.getSize();
double x3 = otherObject.getX()+MathShit.rotateX(Math.toRadians(otherObject.getRotation()), other.getX(j), other.getY(j))*other.getSize();
double x4 = otherObject.getX()+MathShit.rotateX(Math.toRadians(otherObject.getRotation()), other.getX(jinc), other.getY(jinc))*other.getSize();
double y1 = this.getY()+MathShit.rotateY(Math.toRadians(this.getRotation()), one.getX(i), one.getY(i))*one.getSize();
double y2 = this.getY()+MathShit.rotateY(Math.toRadians(this.getRotation()), one.getX(iinc), one.getY(iinc))*one.getSize();
double y3 = otherObject.getY()+MathShit.rotateY(Math.toRadians(otherObject.getRotation()), other.getX(j), other.getY(j))*other.getSize();
double y4 = otherObject.getY()+MathShit.rotateY(Math.toRadians(otherObject.getRotation()), other.getX(jinc), other.getY(jinc))*other.getSize();
// Zaehler
double zx = (x1 * y2 - y1 * x2)*(x3-x4) - (x1 - x2) * (x3 * y4 - y3 * x4);
double zy = (x1 * y2 - y1 * x2)*(y3-y4) - (y1 - y2) * (x3 * y4 - y3 * x4);
// Nenner
double n = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
// Koordinaten des Schnittpunktes
double x = zx/n;
double y = zy/n;
// Vielleicht ist bei der Division durch n etwas schief gelaufen
if (Double.isNaN(x)& Double.isNaN(y))
{
System.out.println("NaN");
}
// Test ob der Schnittpunkt auf den angebenen Strecken liegt oder außerhalb.
if (Math.abs((x - x1) / (x2 - x1)) > 1 || Math.abs((x - x3) / (x4 - x3)) > 1 || Math.abs((y - y1) / (y2 - y1)) > 1 || Math.abs((y - y3) / (y4 - y3)) > 1 )
{
//System.out.println("Alles ok");
}
else return otherObject;
}
}
return null;
}
/**
* Placeholder for children. Children should overwrite this method to handle the case of a collision
* @param o The Object THIS collided with
*/
public void processCollision(MovingObject o)
{
}
//Getters and setters
public float getVelocity()
{
return this.velocity;
}
public void setVelocity(float v)
{
this.velocity = v;
}
public float getX()
{
return this.x;
}
public void setX(float x)
{
this.x = x;
}
public float getY()
{
return y;
}
public void setY(float y)
{
this.y = y;
}
public float getSpeedX()
{
return speedX;
}
public void setSpeedX(float speedX)
{
this.speedX = speedX;
}
public float getSpeedY()
{
return speedY;
}
public void setSpeedY(float speedY)
{
this.speedY = speedY;
}
public float getMaxSpeed()
{
return maxSpeed;
}
public void setMaxSpeed(float maxSpeed)
{
this.maxSpeed = maxSpeed;
}
public float getRotation()
{
return rotation;
}
public void setRotation(float rotation)
{
this.rotation = rotation%360;
}
public float getThrust()
{
return thrust;
}
public void setThrust(float thrust)
{
this.thrust = thrust;
}
public int getTeam()
{
return team;
}
public void setTeam(int team)
{
this.team = team;
}
public Shape getShape()
{
return shape;
}
public void setShape(Shape shape)
{
this.shape = shape;
}
}

202
Game/src/Shape.java Normal file
View file

@ -0,0 +1,202 @@
import java.util.ArrayList;
/**
* This defines the renderable shape of objects such as triangles, squares, etc.
* The Shape is defined via a list of vertices (x,y coordinates -> x,y)
* SpaceSpiel can read these vertices in the renderObject method and render the object.
* @author vanitas
*
*/
public class Shape
{
//Lists containing the vertices' x and y coordinates
private ArrayList<Float> x;
private ArrayList<Float> y;
private float size;
//the color of the shape
private float[] color;
//Template IDs
public static final int S_SHIP = 0001;
public static final int S_TURRET = 0002;
public static final int S_ITEM = 0003;
public static final int S_METROID = 0004;
public static final int S_BULLET = 0005;
public static final int S_BUTTON = 0006;
public static final int S_MENU = 0007;
/**
* Constructor
*/
public Shape()
{
//Initialize
x = new ArrayList<Float>();
y = new ArrayList<Float>();
color = new float[]{1.0f,0.2f,0.2f};
}
/**
* Second constructor (Creates an Object from a template)
* @param blueprint id of the template to create
*/
public Shape(int blueprint)
{
//Initialize
x = new ArrayList<Float>();
y = new ArrayList<Float>();
//Fill with data
this.createFromBlueprint(blueprint);
}
/**
* Create a shape from a template
* @param b id of the template
*/
public void createFromBlueprint(int b)
{
switch(b)
{
case S_SHIP:
{ //Create the shape of a ship
x.add(0f); y.add(20f); //Spitze
x.add(10f); y.add(-10f); //rechts hinten
x.add(-10f); y.add(-10f); //links hinten
this.setColor(1f, 0.2f, 0.2f);
break;
}
case S_TURRET:
{ //Create the shape of a Turret
x.add(-1f); y.add(10f); //Spitze
x.add(1f); y.add(10f);
x.add(5f); y.add(-5f); //rechts hinten
x.add(-5f); y.add(-5f); //links hinten
this.setColor(0.2f, 0.2f, 0.2f);
break;
}
case S_BULLET:
{
//Create a bullet
this.add(2.5f, 7.5f);
this.add(-2.5f, 7.5f);
this.add(-2.5f, -7.5f);
this.add(2.5f, -7.5f);
this.setColor(1f, 1f, 0f);
break;
}
case S_MENU:
{
this.add(SpaceSpiel.frameSizeX/2, SpaceSpiel.frameSizeY/2);
this.add(-SpaceSpiel.frameSizeX/2, SpaceSpiel.frameSizeY/2);
this.add(-SpaceSpiel.frameSizeX/2, -SpaceSpiel.frameSizeY/2);
this.add(SpaceSpiel.frameSizeX/2, -SpaceSpiel.frameSizeY/2);
this.setColor(0f, 0f, 0f);
break;
}
case S_BUTTON: //TODO: Mit dem Uhrzeigersinn
{
//create a Button
this.add(100f, 15f);
this.add(-0f, 15f);
this.add(-0f, -15f);
this.add(100f, -15f);
this.setColor(1f, 1f, 1f);
break;
}
case S_METROID:
{
this.add(4f, 7f);
this.add(8f,0f);
this.add(4f,-7f);
this.add(-4f,-7f);
this.add(-8f,0f);
this.add(-4f,7f);
this.setColor(0.15f, 0.15f, 0.15f);
break;
}
default:
this.setColor(0.2f, 0.2f, 0.2f);
//TODO: Add shape of multiple ammunition types and items
}
}
/**
* Add a new vertex to the shape
* @param nx
* @param ny
*/
public void add(float nx, float ny)
{
this.x.add(nx);
this.y.add(ny);
}
/**
* Modify the vertex on position pos
* @param pos position of the vertex
* @param nx new x value
* @param ny new y value
*/
public void modify(int pos, float nx, float ny)
{
this.x.set(pos, nx);
this.y.set(pos, ny);
}
/**
* return the x component of a specific vertex
* @param pos position of the vertex in the list
* @return x component
*/
public float getX(int pos)
{
return x.get(pos);
}
/**
* return the y component of a specific vertex
* @param pos position of the vertex in the list
* @return y component
*/
public float getY(int pos)
{
return y.get(pos);
}
/**
* returns the Length of the x list representative for how many vertices are in the list.
* x.size() and y.size() should be equal all the time.
* @return size of the arrays
*/
public int getLength()
{
return x.size();
}
/**
* set the color of the shape
* @param r red
* @param g green
* @param b blue
*/
public void setColor(float r, float g, float b)
{
color = new float[3];
color[0]=r;
color[1]=g;
color[2]=b;
}
/**
* returns the color of the shape
* @return color
*/
public float[] getColor()
{
//System.out.println(""+color[0]+""+color[1]+""+color[2]);
return color;
}
public void setSize(float s)
{
this.size = s;
}
public float getSize()
{
return this.size;
}
}

47
Game/src/Ship.java Normal file
View file

@ -0,0 +1,47 @@
/**
* A Spaceship that can fly and shoot
* @author vanitas
*
*/
public class Ship extends MovingObject
{
float firerate;
float burnout = 1000;
public Ship()
{
super();
this.setShape(new Shape(Shape.S_SHIP));
this.getShape().setSize(1f);
this.setMaxSpeed(1);
this.setFirerate(10);
}
public void fire(float delta)
{
while(burnout >= firerate){
float xOfBullet = (float) (this.getX()+Math.sin(Math.toRadians(this.getRotation()))*(this.getShape().getY(0)+7.5f));
float yOfBullet = (float) (this.getY()+Math.cos(Math.toRadians(this.getRotation()))*(this.getShape().getY(0)+7.5f));
Ammunition shot = new Ammunition(this.getRotation(), xOfBullet, yOfBullet, false); //TODO: Place bullet in front of the ship when fired
SpaceSpiel.getMunition().add(shot);
burnout = 0;
}
}
public void setFirerate(float firerateSec){
this.firerate = 60/firerateSec;
}
@Override
public void accelerate()
{
super.accelerate();
burnout++;
}
@Override
public void processCollision(MovingObject o)
{
System.out.println("Verloren du Lappen");
}
}

380
Game/src/SpaceSpiel.java Normal file
View file

@ -0,0 +1,380 @@
import java.util.ArrayList;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.input.Mouse;
/**
* This is the main class of the game. This renders the game, handles input and output etc.
* Important methods: update, renderGL
* @author vanitas
*
*/
public class SpaceSpiel
{
long lastFrame, lastFPS; //Values to calibrate the framerate etc.
int fps;
public static final int frameSizeX = 800, frameSizeY = 600; //Resolution of the Window
@SuppressWarnings("rawtypes")
private ArrayList<ArrayList> objects; //A ArrayList containing th following Lists
private static ArrayList<Metroid> metroids; //A List Containing all the metroids
private ArrayList<MovingObject> ships; //A List Containing all the ships etc
private static ArrayList<Ammunition> munition; //A List Containing all the active fired projectiles
private ControlledShip playerShip; //The players ship
//private Menu menu; //The Menu-Display
//private boolean menuIsActive; //if menu is active this variable is true
private boolean closeRequested = false;
//private Button startbutton;
//private Button exitbutton;
/**
* Constructor
*/
@SuppressWarnings("rawtypes")
public SpaceSpiel()
{
//Initialize the menu and the for this needed variables
//menu = new Menu();
//menuIsActive = false;
//startbutton = new Button(1 , "START");
//exitbutton = new Button(2, "EXIT");
//Initialize ship and Objects (Add Objects here and dont forget to add them to the ArrayLists as well;)
playerShip = new ControlledShip();
Turret geschuetz = new Turret(playerShip.getX(),playerShip.getY());
Metroid m1 = new Metroid(3);
Metroid m2 = new Metroid(3);
Metroid m3 = new Metroid(3);
geschuetz.setParent(playerShip);
//Initialize ArrayLists
objects = new ArrayList<ArrayList>();
metroids = new ArrayList<Metroid>();
ships = new ArrayList<MovingObject>();
munition = new ArrayList<Ammunition>();
//Add Objects to Lists
ships.add(playerShip);
ships.add(geschuetz);
metroids.add(m1);
metroids.add(m2);
metroids.add(m3);
//Pack Lists together
objects.add(metroids);
objects.add(ships);
objects.add(munition);
}
/**
* This method updates all the objects (moves them, captures keyboard and mouse input etc)
* @param delta
*/
public void update(int delta)
{
//Handle Keyboard KeyPresses
//Gas geben ;)
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) //Arrow up
{
playerShip.setVelocity(1f); //Set the velocity to 1 (Power on)
}
//rotation (Arrow right)
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
{
playerShip.turnRight(delta);
}
//Arrow left
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
{
playerShip.turnLeft(delta);
}
//When Arrow up is released, turn down the power.
if(!Keyboard.isKeyDown(Keyboard.KEY_UP))
{
playerShip.setVelocity(0f);
}
if(Keyboard.isKeyDown(Keyboard.KEY_DOWN))
{
playerShip.setSpeedX(0);
playerShip.setSpeedY(0);
}
if(Keyboard.isKeyDown(Keyboard.KEY_W))
{
playerShip.setY(playerShip.getY()+1);
}
if(Keyboard.isKeyDown(Keyboard.KEY_A))
{
playerShip.setX(playerShip.getX()-1);
}
if(Keyboard.isKeyDown(Keyboard.KEY_D))
{
playerShip.setX(playerShip.getX()+1);
}
if(Keyboard.isKeyDown(Keyboard.KEY_S))
{
playerShip.setY(playerShip.getY()-1);
}
//If Spacebar is pressed and shotburnout is over, fire a projectile
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE))
{
playerShip.fire(delta);
}
//if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
//{
// menuIsActive = true