فهرست منبع

Initial commit

frans 8 سال پیش
کامیت
9192079988

+ 27 - 0
.classpath

@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" output="target/classes" path="src/main/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="src" path="res"/>
+	<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="src" output="target/test-classes" path="src/test/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="output" path="target/classes"/>
+</classpath>

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+/bin/
+/target/
+/res/
+res

+ 23 - 0
.project

@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>Breakout</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.m2e.core.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.m2e.core.maven2Nature</nature>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

+ 12 - 0
.settings/org.eclipse.jdt.core.prefs

@@ -0,0 +1,12 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.8
+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.problem.forbiddenReference=warning
+org.eclipse.jdt.core.compiler.source=1.8

+ 4 - 0
.settings/org.eclipse.m2e.core.prefs

@@ -0,0 +1,4 @@
+activeProfiles=
+eclipse.preferences.version=1
+resolveWorkspaceProjects=true
+version=1

+ 52 - 0
pom.xml

@@ -0,0 +1,52 @@
+<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.breakout</groupId>
+	<artifactId>breakout</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+	<name>Breakout</name>
+	<description>A simple Breakout-clone.</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>
+		<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>

+ 107 - 0
src/main/java/eu/tankernn/breakout/Ball.java

@@ -0,0 +1,107 @@
+package eu.tankernn.breakout;
+
+import java.util.List;
+
+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 Ball extends Entity2D {
+
+	private static final float SPEED = 0.03f;
+
+	private Pad pad;
+	private List<Block> blocks;
+	private Block lastHit;
+	private boolean locked;
+	private int unlockKey;
+
+	public Ball(Texture texture, Pad p1, List<Block> blocks, int unlockKey) {
+		super(texture, new Vector2f(0, 0), new Vector2f(0.03f, 0.03f));
+		this.pad = p1;
+		this.blocks = blocks;
+		locked = true;
+		this.unlockKey = unlockKey;
+	}
+
+	@Override
+	public void update() {
+		if (locked) {
+			this.position = Vector2f.add(pad.getPosition(), new Vector2f(0, this.scale.y), null);
+			if (Keyboard.isKeyDown(unlockKey))
+				this.locked = false;
+			return;
+		}
+
+		if (position.y + scale.y > 1) {
+			velocity.y *= -1;
+			lastHit = null;
+			position.y = Math.max(position.y, -1 + scale.y);
+			position.y = Math.min(position.y, 1 - scale.y);
+		} else if (position.y - scale.y < -1) {
+			this.alive = false;
+		}
+
+		if (position.x + scale.x > 1 || position.x - scale.x < -1) {
+			velocity.x *= -1;
+			lastHit = null;
+			position.x = Math.max(position.x, -1 + scale.x);
+			position.x = Math.min(position.x, 1 - scale.x);
+		}
+
+		if (this.collides(pad)) {
+			lastHit = null;
+			Vector2f padPos = pad.getPosition();
+			Vector2f delta = Vector2f.sub(position, padPos, null);
+			double angle = Math.atan(delta.x / Math.abs(delta.y));
+			this.setAngle(angle);
+		}
+
+		for (Block b : blocks) {
+			if (this.collides(b) && !b.equals(lastHit)) {
+
+				// Under and over
+				float w = (this.scale.x + b.getScale().x);
+				float h = (this.scale.y + b.getScale().y);
+				float dx = this.position.x - b.getPosition().x;
+				float dy = this.position.y - b.getPosition().y;
+
+				if (Math.abs(dx) <= w && Math.abs(dy) <= h && !b.equals(lastHit)) {
+					/* collision! */
+					this.lastHit = b;
+					b.hit();
+					float wy = w * dy;
+					float hx = h * dx;
+
+					if (wy > hx)
+						if (wy > -hx) {
+							// Top
+							velocity.y *= -1;
+							position.y = b.getPosition().y + h;
+						} else {
+							// Left
+							velocity.x *= -1;
+							position.x = b.getPosition().x - w;
+						}
+					else if (wy > -hx) {
+						// Right
+						velocity.x *= -1;
+						position.x = b.getPosition().x + w;
+					} else {
+						// Bottom
+						velocity.y *= -1;
+						position.y = b.getPosition().y - h;
+					}
+				}
+			}
+		}
+
+		super.update();
+	}
+
+	private void setAngle(double angle) {
+		this.setVelocity(new Vector2f((float) (SPEED * Math.sin(angle)), (float) (SPEED * Math.cos(angle))));
+	}
+}

+ 30 - 0
src/main/java/eu/tankernn/breakout/Block.java

@@ -0,0 +1,30 @@
+package eu.tankernn.breakout;
+
+import org.lwjgl.util.vector.Vector2f;
+
+import eu.tankernn.gameEngine.entities.Entity2D;
+import eu.tankernn.gameEngine.loader.textures.Texture;
+
+public class Block extends Entity2D {
+
+	public static final int ROWS = 8, COLUMNS = 14;
+
+	int health = 1;
+
+	public Block(Texture texture, int gridX, int gridY) {
+		super(texture, new Vector2f(0, 0), new Vector2f(1.0f / (COLUMNS), 0.5f / (ROWS)));
+
+		this.position.x = this.scale.x * 2f * (gridX) + this.scale.x - 1;
+		this.position.y = this.scale.y * 2f * (gridY) + this.scale.y;
+	}
+	
+	public Block setHealth(int health) {
+		this.health = health;
+		return this;
+	}
+
+	public void hit() {
+		this.alive = --health > 0;
+	}
+
+}

+ 109 - 0
src/main/java/eu/tankernn/breakout/Breakout.java

@@ -0,0 +1,109 @@
+package eu.tankernn.breakout;
+
+import static org.lwjgl.opengl.GL11.glClear;
+import static org.lwjgl.opengl.GL11.glClearColor;
+
+import java.io.FileNotFoundException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+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.loader.textures.Texture;
+import eu.tankernn.gameEngine.renderEngine.gui.GuiRenderer;
+import eu.tankernn.gameEngine.renderEngine.gui.GuiTexture;
+
+public class Breakout extends TankernnGame {
+
+	public static final String GAME_NAME = "Breakout";
+
+	private GuiRenderer renderer;
+
+	private Texture[] colors;
+	private Texture white;
+	private Texture steel;
+
+	private List<Block> blocks = new ArrayList<Block>();
+
+	private Ball ball;
+	private Pad pad;
+
+	public Breakout() {
+		super(GAME_NAME);
+
+		renderer = new GuiRenderer(loader);
+
+		try {
+			white = loader.loadTexture("white.png");
+			steel = loader.loadTexture("steel.png");
+			String[] colorNames = { "blue", "brown", "cyan", "green", "pink", "red", "yellow" };
+			colors = Arrays.stream(colorNames).map(s -> {
+				try {
+					return loader.loadTexture(s + ".png");
+				} catch (FileNotFoundException e) {
+					e.printStackTrace();
+					return null;
+				}
+			}).toArray(size -> new Texture[size]);
+		} catch (FileNotFoundException e) {
+			e.printStackTrace();
+			return;
+		}
+
+		// Setup blocks
+		// TODO Load custom maps from files
+		int i = 0;
+		for (int x = 0; x < Block.COLUMNS; x++)
+			for (int y = 0; y < Block.ROWS; y++) {
+				blocks.add(new Block(colors[i++], x, y));
+				i %= colors.length;
+			}
+
+		// Ball + pad
+
+		pad = new Pad(white, new Vector2f(0, -0.8f), Keyboard.KEY_LEFT, Keyboard.KEY_RIGHT);
+		ball = new Ball(white, pad, blocks, Keyboard.KEY_SPACE);
+
+	}
+
+	public void update() {
+		blocks.removeIf(b -> !b.isAlive());
+
+		pad.update();
+		ball.update();
+
+	}
+
+	public void render() {
+		glClearColor(0, 0, 0, 1);
+		glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
+
+		List<GuiTexture> entities = new ArrayList<GuiTexture>();
+
+		for (GuiTexture texture : blocks)
+			entities.add(texture);
+
+		entities.add(ball);
+		entities.add(pad);
+
+		renderer.render(entities);
+
+		super.render();
+	}
+
+	public void cleanUp() {
+		renderer.cleanUp();
+		super.cleanUp();
+	}
+
+	public static void main(String[] args) {
+		GameLauncher.init(GAME_NAME, 800, 800);
+		GameLauncher.launch(new Breakout());
+	}
+
+}

+ 33 - 0
src/main/java/eu/tankernn/breakout/Pad.java

@@ -0,0 +1,33 @@
+package eu.tankernn.breakout;
+
+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 leftKey, rightKey;
+
+	public Pad(Texture texture, Vector2f position, int leftKey, int rightKey) {
+		super(texture, position, new Vector2f(0.3f, 0.02f));
+		this.leftKey = leftKey;
+		this.rightKey = rightKey;
+	}
+
+	@Override
+	public void update() {
+		if (Keyboard.isKeyDown(leftKey) && position.x - scale.x > -1) {
+			this.setVelocity(new Vector2f(-SPEED, 0));
+		} else if (Keyboard.isKeyDown(rightKey) && position.x + scale.x < 1) {
+			this.setVelocity(new Vector2f(SPEED, 0));
+		} else {
+			this.setVelocity(new Vector2f(0, 0));
+		}
+
+		super.update();
+	}
+}