Browse Source

Initial commit

Tankernn 8 years ago
commit
5ba540d0e2
6 changed files with 289 additions and 0 deletions
  1. 21 0
      .classpath
  2. 7 0
      .gitignore
  3. 53 0
      pom.xml
  4. 68 0
      src/eu/tankernn/pong/Ball.java
  5. 34 0
      src/eu/tankernn/pong/Pad.java
  6. 106 0
      src/eu/tankernn/pong/Pong.java

+ 21 - 0
.classpath

@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" output="target/classes" path="src">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="lib" path="target/tankernn-pong-0.0.1.jar"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="output" path="target/classes"/>
+</classpath>

+ 7 - 0
.gitignore

@@ -0,0 +1,7 @@
+/bin/
+/target/
+/res/
+res
+.project
+.settings
+.DS_Store

+ 53 - 0
pom.xml

@@ -0,0 +1,53 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>eu.tankernn.pong</groupId>
+	<artifactId>tankernn-pong</artifactId>
+	<version>0.0.1</version>
+	<name>Pong</name>
+	<description>Pong. What more can I say?</description>
+
+	<repositories>
+		<repository>
+			<id>tankernn</id>
+			<name>Tankernn Maven Repository</name>
+			<url>http://repo.maven.tankernn.eu</url>
+		</repository>
+	</repositories>
+
+	<dependencies>
+		<dependency>
+			<groupId>eu.tankernn.gameEngine</groupId>
+			<artifactId>tankernn-game-engine</artifactId>
+			<version>1.2</version>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<sourceDirectory>src</sourceDirectory>
+		<plugins>
+			<plugin>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>3.5.1</version>
+				<configuration>
+					<source>1.8</source>
+					<target>1.8</target>
+				</configuration>
+			</plugin>
+			<plugin>
+				<groupId>com.googlecode.mavennatives</groupId>
+				<artifactId>maven-nativedependencies-plugin</artifactId>
+				<version>0.0.6</version>
+				<executions>
+					<execution>
+						<id>unpacknatives</id>
+						<phase>package</phase>
+						<goals>
+							<goal>copy</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
+	</build>
+</project>

+ 68 - 0
src/eu/tankernn/pong/Ball.java

@@ -0,0 +1,68 @@
+package eu.tankernn.pong;
+
+import org.lwjgl.util.vector.Vector2f;
+
+import eu.tankernn.gameEngine.entities.Entity2D;
+import eu.tankernn.gameEngine.loader.textures.Texture;
+
+public class Ball extends Entity2D {
+
+	private static final float SPEED = 0.03f;
+
+	private Pad p1, p2;
+	private Pad winner = null;
+
+	public Ball(Texture texture, Pad p1, Pad p2) {
+		super(texture, new Vector2f(0, 0), new Vector2f(0.03f, 0.03f));
+		this.p1 = p1;
+		this.p2 = p2;
+
+		double rads = Math.toRadians(Math.random() * 360);
+		this.setAngle(rads);
+	}
+
+	@Override
+	public void update() {
+		if (position.y - scale.y < -1 || position.y + scale.y > 1) {
+			velocity.y *= -1;
+			position.y = Math.max(position.y, -1 + scale.y);
+			position.y = Math.min(position.y, 1 - scale.y);
+		}
+
+		Vector2f padPos = null;
+
+		if (this.collides(p1)) {
+			padPos = p1.getPosition();
+		} else if (this.collides(p2)) {
+			padPos = p2.getPosition();
+		}
+
+		if (padPos != null) {
+			Vector2f delta = Vector2f.sub(position, padPos, null);
+			System.out.println(delta);
+			double angle;
+			if (delta.y >= 0)
+				angle = Math.atan(delta.x / delta.y);
+			else
+				angle = Math.PI - Math.atan(delta.x / Math.abs(delta.y));
+			System.out.println(Math.toDegrees(angle));
+			this.setAngle(angle);
+		}
+
+		if (position.x - scale.x < -1) {
+			winner = p2;
+		} else if (position.x + scale.x > 1) {
+			winner = p1;
+		}
+
+		super.update();
+	}
+
+	private void setAngle(double angle) {
+		this.setVelocity(new Vector2f((float) (SPEED * Math.sin(angle)), (float) (SPEED * Math.cos(angle))));
+	}
+
+	public Pad getWinner() {
+		return winner;
+	}
+}

+ 34 - 0
src/eu/tankernn/pong/Pad.java

@@ -0,0 +1,34 @@
+package eu.tankernn.pong;
+
+import org.lwjgl.input.Keyboard;
+import org.lwjgl.util.vector.Vector2f;
+
+import eu.tankernn.gameEngine.entities.Entity2D;
+import eu.tankernn.gameEngine.loader.textures.Texture;
+
+public class Pad extends Entity2D {
+
+	private static final float SPEED = 0.05f;
+
+	private int upKey, downKey;
+
+	public Pad(Texture texture, Vector2f position, int upKey, int downKey) {
+		super(texture, position, new Vector2f(0.02f, 0.3f));
+		this.upKey = upKey;
+		this.downKey = downKey;
+		this.position = position;
+	}
+
+	@Override
+	public void update() {
+		if (Keyboard.isKeyDown(upKey) && position.y + scale.y < 1) {
+			this.setVelocity(new Vector2f(0, SPEED));
+		} else if (Keyboard.isKeyDown(downKey) && position.y - scale.y > -1) {
+			this.setVelocity(new Vector2f(0, -SPEED));
+		} else {
+			this.setVelocity(new Vector2f(0, 0));
+		}
+
+		super.update();
+	}
+}

+ 106 - 0
src/eu/tankernn/pong/Pong.java

@@ -0,0 +1,106 @@
+package eu.tankernn.pong;
+
+import static org.lwjgl.opengl.GL11.glClear;
+import static org.lwjgl.opengl.GL11.glClearColor;
+
+import java.io.FileNotFoundException;
+
+import org.lwjgl.input.Keyboard;
+import org.lwjgl.opengl.GL11;
+import org.lwjgl.util.vector.Vector2f;
+
+import eu.tankernn.gameEngine.GameLauncher;
+import eu.tankernn.gameEngine.TankernnGame;
+import eu.tankernn.gameEngine.entities.Entity2D;
+import eu.tankernn.gameEngine.loader.font.FontType;
+import eu.tankernn.gameEngine.loader.font.GUIText;
+import eu.tankernn.gameEngine.loader.textures.Texture;
+import eu.tankernn.gameEngine.renderEngine.gui.GuiTexture;
+import eu.tankernn.gameEngine.util.InternalFile;
+
+public class Pong extends TankernnGame {
+	private Pad leftPlayer, rightPlayer;
+	private Ball ball;
+	private Entity2D[] entities;
+	private GUIText scoreText, messageText;
+	private Texture white;
+
+	private int leftScore = 0, rightScore = 0;
+	private boolean running = true;
+
+	public Pong() {
+		super("Pong");
+		try {
+			FontType font = new FontType(loader.loadTexture("arial.png"), new InternalFile("arial.fnt"));
+			scoreText = new GUIText("0  0", 2, font, new Vector2f(-0.5f, 0), 2, true).setColor(1, 1, 1);
+			messageText = new GUIText("", 2, font, new Vector2f(-0.5f, 0.5f), 2, true).setColor(1, 1, 1);
+			white = loader.loadTexture("white.png");
+		} catch (FileNotFoundException e1) {
+			e1.printStackTrace();
+		}
+
+		leftPlayer = new Pad(white, new Vector2f(-0.9f, 0.1f), Keyboard.KEY_A, Keyboard.KEY_Z);
+		rightPlayer = new Pad(white, new Vector2f(0.9f, 0.1f), Keyboard.KEY_K, Keyboard.KEY_M);
+		ball = new Ball(white, leftPlayer, rightPlayer);
+		entities = new Entity2D[] { leftPlayer, rightPlayer, ball };
+
+		for (Entity2D e : entities)
+			guiMaster.loadGui(e);
+
+		guiMaster.loadGui(new GuiTexture(white, new Vector2f(0.0f, 0.0f), new Vector2f(0.005f, 1.0f)));
+
+		textMaster.loadText(scoreText);
+	}
+
+	public void update() {
+		if (!running) {
+			if (Keyboard.isKeyDown(Keyboard.KEY_R)) {
+				ball = new Ball(white, leftPlayer, rightPlayer);
+				running = true;
+				entities = new Entity2D[] { leftPlayer, rightPlayer, ball };
+				textMaster.removeText(messageText);
+			} else {
+				return;
+			}
+		}
+
+		for (Entity2D e : entities)
+			e.update();
+
+		if (ball.getWinner() != null) {
+			if (ball.getWinner().equals(leftPlayer)) {
+				System.out.println("Player 1 wins.");
+				messageText.setText("Player 1 wins.");
+				leftScore++;
+			} else {
+				System.out.println("Player 2 wins.");
+				messageText.setText("Player 2 wins.");
+				rightScore++;
+			}
+			textMaster.loadText(messageText);
+			running = false;
+		}
+
+		scoreText.setText(String.format("%d  %d", leftScore, rightScore));
+	}
+
+	public void render() {
+		glClearColor(0, 0, 0, 1);
+		glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
+
+		guiMaster.render();
+		textMaster.render();
+		super.render();
+	}
+
+	public void cleanUp() {
+		guiMaster.cleanUp();
+		textMaster.cleanUp();
+		super.cleanUp();
+	}
+
+	public static void main(String[] args) {
+		GameLauncher.init("Pong", 800, 800);
+		GameLauncher.launch(new Pong());
+	}
+}