merge Character tile into master #7

Merged
SemvdH merged 10 commits from character-tile into master 2020-05-27 21:15:51 +00:00
15 changed files with 635 additions and 37 deletions

BIN
core/assets/characters.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

View File

@@ -6,13 +6,20 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music; import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import netwerkprog.game.client.game.characters.Hacker;
import netwerkprog.game.client.game.characters.abilities.BodySwap;
import netwerkprog.game.client.game.map.Map; import netwerkprog.game.client.game.map.Map;
import netwerkprog.game.client.game.map.MapRenderer; import netwerkprog.game.client.game.map.MapRenderer;
import netwerkprog.game.client.game.map.GameInputProcessor; import netwerkprog.game.client.game.map.GameInputProcessor;
import netwerkprog.game.util.game.GameCharacter;
import netwerkprog.game.util.graphics.FrameRate; import netwerkprog.game.util.graphics.FrameRate;
import netwerkprog.game.util.tree.BST;
public class MainGame extends ApplicationAdapter{ public class MainGame extends ApplicationAdapter {
SpriteBatch batch; SpriteBatch batch;
float screenWidth; float screenWidth;
float screenHeight; float screenHeight;
@@ -20,10 +27,25 @@ public class MainGame extends ApplicationAdapter{
private Thread client; private Thread client;
private OrthographicCamera camera; private OrthographicCamera camera;
private GameInputProcessor gameInputProcessor; private GameInputProcessor gameInputProcessor;
private GameCharacter selectedCharacter;
private Map map; private Map map;
public MapRenderer mapRenderer; public MapRenderer mapRenderer;
private BST<GameCharacter> tree;
public GameCharacter testCharacter;
private static MainGame INSTANCE;
private MainGame() {
}
public static MainGame getInstance() {
if (INSTANCE == null) {
INSTANCE = new MainGame();
}
return INSTANCE;
}
@Override @Override
@@ -51,13 +73,16 @@ public class MainGame extends ApplicationAdapter{
"#########################" "#########################"
}; };
map = new Map(strings); map = new Map(strings);
gameInputProcessor = new GameInputProcessor(camera, this); gameInputProcessor = new GameInputProcessor(camera);
Gdx.input.setInputProcessor(gameInputProcessor); Gdx.input.setInputProcessor(gameInputProcessor);
mapRenderer = new MapRenderer(map, 32, batch, camera); mapRenderer = new MapRenderer(map, 32, batch, camera);
camera.position.set(screenWidth/2,screenHeight/2,0); camera.position.set(screenWidth / 2, screenHeight / 2, 0);
camera.viewportWidth = screenWidth / 2; camera.viewportWidth = screenWidth / 2;
camera.viewportHeight = screenHeight / 2; camera.viewportHeight = screenHeight / 2;
camera.update(); camera.update();
this.tree = new BST<>();
initCharacters();
// this.tree.insert(new Hacker(,new BodySwap()));
// playSong(); // playSong();
@@ -66,6 +91,20 @@ public class MainGame extends ApplicationAdapter{
// connectToServer(); // connectToServer();
} }
private void initCharacters() {
Texture texture = new Texture(Gdx.files.internal("core/assets/characters.png"));
TextureRegion[][] characters = TextureRegion.split(texture, 32, 32);
this.testCharacter = new Hacker("harry",characters[1][0], new BodySwap("test"));
GameCharacter character2 = new Hacker("test2",characters[2][0], new BodySwap("test"));
// this.tree.insert(testCharacter);
// this.tree.insert(character2);
// this.tree.insert(new Agent(characters[2][0], new Implant("test")));
this.setSelectedCharacter(testCharacter);
mapRenderer.getGameTiles()[0][1].visit(testCharacter);
mapRenderer.getGameTiles()[0][2].visit(character2);
}
private void playSong() { private void playSong() {
// play music // play music
@@ -144,5 +183,20 @@ public class MainGame extends ApplicationAdapter{
return map.getWidth(); return map.getWidth();
} }
public BST<GameCharacter> getTree() {
return tree;
}
public void setSelectedCharacter(GameCharacter character) {
this.selectedCharacter = character;
System.out.println("selected character set to : " + character);
}
public GameCharacter getSelectedCharacter() {
return selectedCharacter;
}
public boolean hasCharacterSelected() {
return selectedCharacter != null;
}
} }

View File

@@ -0,0 +1,12 @@
package netwerkprog.game.client.game.characters;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import netwerkprog.game.util.game.Ability;
import netwerkprog.game.util.game.Faction;
import netwerkprog.game.util.game.GameCharacter;
public class Agent extends GameCharacter {
public Agent(TextureRegion textureRegion, Ability... abilities) {
super("Agent", Faction.MEGACORPORATION, textureRegion, abilities);
}
}

View File

@@ -0,0 +1,12 @@
package netwerkprog.game.client.game.characters;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import netwerkprog.game.util.game.Ability;
import netwerkprog.game.util.game.Faction;
import netwerkprog.game.util.game.GameCharacter;
public class Hacker extends GameCharacter {
public Hacker(String name, TextureRegion textureRegion, Ability... abilities) {
super(name, Faction.HACKER, textureRegion, abilities);
}
}

View File

@@ -1,4 +1,14 @@
package netwerkprog.game.client.game.characters.abilities; package netwerkprog.game.client.game.characters.abilities;
public class BodySwap { import netwerkprog.game.util.game.Ability;
public class BodySwap extends Ability {
public BodySwap(String name) {
super(name);
}
@Override
public String getCommand() {
return null;
}
} }

View File

@@ -1,4 +1,14 @@
package netwerkprog.game.client.game.characters.abilities; package netwerkprog.game.client.game.characters.abilities;
public class Implant { import netwerkprog.game.util.game.Ability;
public class Implant extends Ability {
public Implant(String name) {
super(name);
}
@Override
public String getCommand() {
return null;
}
} }

View File

@@ -8,12 +8,13 @@ import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.TimeUtils; import com.badlogic.gdx.utils.TimeUtils;
import netwerkprog.game.client.MainGame; import netwerkprog.game.client.MainGame;
import netwerkprog.game.util.game.GameCharacter;
import java.util.ArrayList; import java.util.ArrayList;
public class GameInputProcessor implements InputProcessor { public class GameInputProcessor implements InputProcessor {
private final OrthographicCamera camera; private final OrthographicCamera camera;
private MainGame game; private MainGame mainGame;
private ArrayList<Integer> keysList; private ArrayList<Integer> keysList;
private boolean isWPressed = false; private boolean isWPressed = false;
private boolean isAPressed = false; private boolean isAPressed = false;
@@ -29,11 +30,10 @@ public class GameInputProcessor implements InputProcessor {
* makes a new game input processor * makes a new game input processor
* *
* @param camera the camera object to use * @param camera the camera object to use
* @param game the game object to get objects from
*/ */
public GameInputProcessor(OrthographicCamera camera, MainGame game) { public GameInputProcessor(OrthographicCamera camera) {
this.camera = camera; this.camera = camera;
this.game = game; this.mainGame = MainGame.getInstance();
keysList = new ArrayList<>(); keysList = new ArrayList<>();
lastTimeCounted = TimeUtils.millis(); lastTimeCounted = TimeUtils.millis();
@@ -123,19 +123,44 @@ public class GameInputProcessor implements InputProcessor {
Vector3 touchPoint = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); Vector3 touchPoint = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPoint); camera.unproject(touchPoint);
for (int row = 0; row < game.mapRenderer.getGameTiles().length; row++) { for (int row = 0; row < mainGame.mapRenderer.getGameTiles().length; row++) {
for (int col = 0; col < game.mapRenderer.getGameTiles()[0].length; col++) { for (int col = 0; col < mainGame.mapRenderer.getGameTiles()[0].length; col++) {
GameTile gameTile = game.mapRenderer.getGameTiles()[row][col]; GameTile gameTile = mainGame.mapRenderer.getGameTiles()[row][col];
if (gameTile.contains(touchPoint.x, touchPoint.y)) { if (gameTile.contains(touchPoint.x, touchPoint.y)) {
System.out.println(gameTile + " row: " + row + ", col: " + col); if (button == Input.Buttons.LEFT) {
//TODO make stuff happen with the tile // System.out.println(gameTile + " row: " + row + ", col: " + col);
if (mainGame.hasCharacterSelected() && !gameTile.containsCharacter()) {
// System.out.println(mainGame.getSelectedCharacter());
removeCharacterFromTile(mainGame.getSelectedCharacter());
gameTile.visit(mainGame.getSelectedCharacter());
}
if (!mainGame.hasCharacterSelected() && gameTile.containsCharacter()) {
mainGame.setSelectedCharacter(gameTile.getCharacter());
}
if (gameTile.containsCharacter() && !mainGame.getSelectedCharacter().equals(gameTile.getCharacter())) {
mainGame.setSelectedCharacter(gameTile.getCharacter());
}
return true; return true;
} }
} }
} }
}
return false; return false;
} }
private void removeCharacterFromTile(GameCharacter character) {
rowLoop:
for (int row = 0; row < mainGame.mapRenderer.getGameTiles().length; row++) {
for (int col = 0; col < mainGame.mapRenderer.getGameTiles()[0].length; col++) {
GameTile gameTile = mainGame.mapRenderer.getGameTiles()[row][col];
if (gameTile.containsCharacter() && gameTile.getCharacter().equals(character)) {
gameTile.removeCharacter();
break rowLoop;
}
}
}
}
@Override @Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) { public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false; return false;
@@ -164,21 +189,21 @@ public class GameInputProcessor implements InputProcessor {
public void update() { public void update() {
long delta = TimeUtils.timeSinceMillis(lastTimeCounted); long delta = TimeUtils.timeSinceMillis(lastTimeCounted);
lastTimeCounted = TimeUtils.millis(); lastTimeCounted = TimeUtils.millis();
if (camera.position.x > 5 * game.getHorizontalTileAmount()) if (camera.position.x > 5 * mainGame.getHorizontalTileAmount())
if (isAPressed()) { if (isAPressed()) {
camera.position.add(-CAMERA_MOVE_SPEED * delta, 0, 0); camera.position.add(-CAMERA_MOVE_SPEED * delta, 0, 0);
} }
if (camera.position.y < 30 * game.getVerticalTileAmount()) if (camera.position.y < 30 * mainGame.getVerticalTileAmount())
if (isWPressed()) { if (isWPressed()) {
camera.position.add(0, CAMERA_MOVE_SPEED * delta, 0); camera.position.add(0, CAMERA_MOVE_SPEED * delta, 0);
} }
if (camera.position.y > 5 * game.getVerticalTileAmount()) if (camera.position.y > 5 * mainGame.getVerticalTileAmount())
if (isSPressed()) { if (isSPressed()) {
camera.position.add(0, -CAMERA_MOVE_SPEED * delta, 0); camera.position.add(0, -CAMERA_MOVE_SPEED * delta, 0);
} }
if (camera.position.x < game.getScreenWidth() / 2 + 5 * game.getHorizontalTileAmount()) if (camera.position.x < mainGame.getScreenWidth() / 2 + 5 * mainGame.getHorizontalTileAmount())
if (isDPressed()) { if (isDPressed()) {
camera.position.add(CAMERA_MOVE_SPEED * delta, 0, 0); camera.position.add(CAMERA_MOVE_SPEED * delta, 0, 0);
} }

View File

@@ -2,12 +2,14 @@ package netwerkprog.game.client.game.map;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Rectangle;
import netwerkprog.game.util.game.GameCharacter;
import java.util.Objects; import java.util.Objects;
public class GameTile extends Rectangle { public class GameTile extends Rectangle {
private TextureRegion textureRegion; private TextureRegion textureRegion;
private char symbol; private char symbol;
private GameCharacter character;
public GameTile(TextureRegion textureRegion, int xPos, int yPos, char symbol) { public GameTile(TextureRegion textureRegion, int xPos, int yPos, char symbol) {
this.textureRegion = textureRegion; this.textureRegion = textureRegion;
@@ -18,10 +20,37 @@ public class GameTile extends Rectangle {
super.height = textureRegion.getRegionHeight(); super.height = textureRegion.getRegionHeight();
} }
public GameCharacter getCharacter() {
return character;
}
public boolean containsCharacter() {
return character != null;
}
/**
* sets the character on this tile
* @param character the character to visit this tile
* @return false if this tile already had a character on it.
*/
public boolean visit(GameCharacter character) {
if (this.character != null) return false;
this.character = character;
return true;
}
public void removeCharacter() {
this.character = null;
}
public TextureRegion getTextureRegion() { public TextureRegion getTextureRegion() {
return textureRegion; return textureRegion;
} }
public void setTextureRegion(TextureRegion textureRegion) {
this.textureRegion = textureRegion;
}
public char getSymbol() { public char getSymbol() {
return symbol; return symbol;
} }

View File

@@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import netwerkprog.game.client.MainGame;
import netwerkprog.game.util.graphics.Renderable; import netwerkprog.game.util.graphics.Renderable;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
@@ -17,6 +18,8 @@ public class MapRenderer implements Renderable {
private static int x = 0; private static int x = 0;
private static int y = 0; private static int y = 0;
private MainGame mainGame;
public static TextureRegion FLOOR_TILE; public static TextureRegion FLOOR_TILE;
public static TextureRegion WALL_TILE; public static TextureRegion WALL_TILE;
@@ -25,9 +28,9 @@ public class MapRenderer implements Renderable {
private GameTile[][] gameTiles; private GameTile[][] gameTiles;
/** /**
* makea a new mapRenderer object * makea a new mapRenderer object
*
* @param map the map object * @param map the map object
* @param tileWidth the width of the tile * @param tileWidth the width of the tile
* @param batch the batch object so no new ones have to be made * @param batch the batch object so no new ones have to be made
@@ -39,6 +42,7 @@ public class MapRenderer implements Renderable {
this.batch = batch; this.batch = batch;
cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
this.camera = camera; this.camera = camera;
this.mainGame = MainGame.getInstance();
makeTiles(); makeTiles();
} }
@@ -60,11 +64,11 @@ public class MapRenderer implements Renderable {
x = 0; x = 0;
for (int col = 0; col < map.getWidth(); col++) { for (int col = 0; col < map.getWidth(); col++) {
if (map.get(row, col) == ' ') { if (map.get(row, col) == ' ') {
gameTiles[row][col] = new GameTile(FLOOR_TILE,x,y, ' '); gameTiles[row][col] = new GameTile(FLOOR_TILE, x, y, ' ');
} else if (map.get(row, col) == '#') { } else if (map.get(row, col) == '#') {
gameTiles[row][col] = new GameTile(WALL_TILE,x,y, '#'); gameTiles[row][col] = new GameTile(WALL_TILE, x, y, '#');
} else if (map.get(row, col) == 'x') { } else if (map.get(row, col) == 'x') {
gameTiles[row][col] = new GameTile(PATH_TILE,x,y, 'x'); gameTiles[row][col] = new GameTile(PATH_TILE, x, y, 'x');
} }
x += 32; x += 32;
} }
@@ -96,6 +100,10 @@ public class MapRenderer implements Renderable {
for (int col = 0; col < gameTiles[0].length; col++) { for (int col = 0; col < gameTiles[0].length; col++) {
GameTile cur = gameTileRow[col]; GameTile cur = gameTileRow[col];
batch.draw(cur.getTextureRegion(), cur.x, cur.y); batch.draw(cur.getTextureRegion(), cur.x, cur.y);
if (cur.containsCharacter()) {
batch.draw(cur.getCharacter().getTextureRegion(), cur.x, cur.y);
// System.out.println("drawing character at " + cur.x + " " + cur.y);
}
} }
} }

View File

@@ -1,5 +1,6 @@
package netwerkprog.game.util.game; package netwerkprog.game.util.game;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
@@ -8,21 +9,21 @@ import com.badlogic.gdx.scenes.scene2d.Actor;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
public abstract class GameCharacter extends Actor { import java.util.Objects;
public abstract class GameCharacter extends Actor implements Comparable<GameCharacter> {
protected String name; protected String name;
protected Faction faction; protected Faction faction;
protected HashSet<Ability> abilities; protected HashSet<Ability> abilities;
protected TextureRegion[][] sprites;
protected boolean override; protected boolean override;
protected TextureRegion textureRegion;
public GameCharacter(String name, Faction faction, String spriteSheet, Ability... abilities) { public GameCharacter(String name, Faction faction, TextureRegion textureRegion, Ability... abilities) {
this.name = name; this.name = name;
this.faction = faction; this.faction = faction;
this.abilities = new HashSet<>(Arrays.asList(abilities)); this.abilities = new HashSet<>(Arrays.asList(abilities));
this.override = false; this.override = false;
this.sprites = TextureRegion.split(new Texture(spriteSheet),32,32); this.textureRegion = textureRegion;
super.setX(0);
super.setY(0);
} }
public void addAbilities(Ability ability) { public void addAbilities(Ability ability) {
@@ -37,10 +38,41 @@ public abstract class GameCharacter extends Actor {
this.override = !this.override; this.override = !this.override;
} }
public void draw(SpriteBatch batch) { public TextureRegion getTextureRegion() {
batch.begin(); return textureRegion;
batch.draw(this.sprites[0][0],super.getX(),super.getY());
batch.end();
} }
public void setTextureRegion(TextureRegion textureRegion) {
this.textureRegion = textureRegion;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
if (!(o instanceof GameCharacter)) return false;
GameCharacter character = (GameCharacter) o;
return override == character.override &&
Objects.equals(name, character.name) &&
faction == character.faction &&
Objects.equals(abilities, character.abilities);
}
@Override
public int hashCode() {
return Objects.hash(name, faction, abilities, override);
}
@Override
public int compareTo(GameCharacter o) {
return this.name.compareTo(o.name);
}
@Override
public String toString() {
return "GameCharacter{" +
"name='" + name + '\'' +
", faction=" + faction +
'}';
}
} }

View File

@@ -0,0 +1,20 @@
package netwerkprog.game.util.tree;
public abstract class AbstractTree<E> implements Tree<E> {
@Override /** Inorder traversal from the root*/
public void inorder() {
}
@Override /** Postorder traversal from the root */
public void postorder() {
}
@Override /** Preorder traversal from the root */
public void preorder() {
}
@Override /** Return true if the tree is empty */
public boolean isEmpty() {
return getSize() == 0;
}
}

View File

@@ -0,0 +1,341 @@
package netwerkprog.game.util.tree;
import java.util.*;
public class BST<E extends Comparable<E>> extends AbstractTree<E> {
protected TreeNode<E> root;
protected int size = 0;
// Helper methode
public int sum () {
return this.sum(this.getRoot());
}
// Opgave 1b (10 punten): Maak de recursieve methode sum af in de klasse bst.BST. Deze methode telt de getallen
// van alle elementen van de binaire zoekboom bij elkaar op. De methode geeft de totale som terug van alle getallen
// in de boom.
public int sum( TreeNode<E> node ) {
// Schrijf hier jouw code...
if (node == null) {
return 0;
}
int nodeValue = (Integer) node.element; // Tip, omdat E nog onbekend is doen we het zo (niet helemaal netjes)
return sum(node.left) + sum(node.right);
}
// Helper methode
public int totalLeaves () {
return this.totalLeaves(this.getRoot());
}
// Opgave 1c (10 punten): Maak de methode totalLeaves af om de klasse bst.BST. Deze methode telt het aantal
// bladeren (leaves) van de gegeven binaire zoekboom en geeft deze terug. Je hoeft deze methode niet recursief te
// implementeren. Het mag wel.
public int totalLeaves ( TreeNode<E> node ) {
if (node == null) {
return 0;
}
if (node.left == null && node.right == null) {
return 1;
}
return totalLeaves(node.left) + totalLeaves(node.right);
}
/** Create a default binary tree */
public BST() {
}
/** Create a binary tree from an array of objects */
public BST(E[] objects) {
for (int i = 0; i < objects.length; i++)
insert(objects[i]);
}
@Override /** Returns true if the element is in the tree */
public boolean search(E e) {
return search(e, root);
}
private boolean search(E e, TreeNode<E> tree)
{
// nog niet correct
if (tree == null)
{
return false;
}
if (e.compareTo(tree.element) == 0)
{
return true;
}
if (e.compareTo(tree.element) < 0)
{
return search(e, tree.left);
}
else // (e.compareTo(tree.element) > 0)
{
return search(e, tree.right);
}
}
@Override /** Insert element o into the binary tree
* Return true if the element is inserted successfully */
public boolean insert(E e) {
if (root == null) {
root = createNewNode(e); // Create a new root
size++;
return true;
}
else {
return insert(e, root);
}
}
/** Insert element o into the binary tree
* Return true if the element is inserted successfully
pre: root != null
*/
public boolean insert(E e, TreeNode<E> tree) {
if (e.compareTo(tree.element) == 0) {
return false; // Duplicate node not inserted
}
else if (e.compareTo(tree.element) < 0 && tree.left != null)
return insert(e, tree.left);
else if (e.compareTo(tree.element) > 0 && tree.right != null)
return insert(e, tree.right);
// Create the new node and attach it to the parent node
else {
if (e.compareTo(tree.element) < 0) {
tree.left = createNewNode(e);
}
else {
tree.right = createNewNode(e);
}
size++;
return true;
}
}
protected TreeNode<E> createNewNode(E e) {
return new TreeNode<E>(e);
}
@Override /** Inorder traversal from the root*/
public void inorder() {
inorder(root);
}
/** Inorder traversal from a subtree */
protected void inorder(TreeNode<E> root) {
if (root == null) return;
inorder(root.left);
System.out.print(root.element + " ");
inorder(root.right);
}
@Override /** Postorder traversal from the root */
public void postorder() {
postorder(root);
}
/** Postorder traversal from a subtree */
protected void postorder(TreeNode<E> root) {
if (root == null) return;
postorder(root.left);
postorder(root.right);
System.out.print(root.element + " ");
}
@Override /** Preorder traversal from the root */
public void preorder() {
preorder(root);
}
/** Preorder traversal from a subtree */
protected void preorder(TreeNode<E> root) {
if (root == null) return;
System.out.print(root.element + " ");
preorder(root.left);
preorder(root.right);
}
/** This inner class is static, because it does not access
any instance members defined in its outer class */
public static class TreeNode<E extends Comparable<E>> {
protected E element;
protected TreeNode<E> left;
protected TreeNode<E> right;
public TreeNode(E e) {
element = e;
}
}
@Override /** Get the number of nodes in the tree */
public int getSize() {
return size;
}
/** Returns the root of the tree */
public TreeNode<E> getRoot() {
return root;
}
/** Returns a path from the root leading to the specified element */
public java.util.ArrayList<TreeNode<E>> path(E e) {
java.util.ArrayList<TreeNode<E>> list =
new java.util.ArrayList<TreeNode<E>>();
TreeNode<E> current = root; // Start from the root
while (current != null) {
list.add(current); // Add the node to the list
if (e.compareTo(current.element) < 0) {
current = current.left;
}
else if (e.compareTo(current.element) > 0) {
current = current.right;
}
else
break;
}
return list; // Return an array list of nodes
}
@Override /** Delete an element from the binary tree.
* Return true if the element is deleted successfully
* Return false if the element is not in the tree */
public boolean delete(E e) {
// Locate the node to be deleted and also locate its parent node
TreeNode<E> parent = null;
TreeNode<E> current = root;
while (current != null) {
if (e.compareTo(current.element) < 0) {
parent = current;
current = current.left;
}
else if (e.compareTo(current.element) > 0) {
parent = current;
current = current.right;
}
else
break; // Element is in the tree pointed at by current
}
if (current == null)
return false; // Element is not in the tree
// Case 1: current has no left child
if (current.left == null) {
// Connect the parent with the right child of the current node
if (parent == null) {
root = current.right;
}
else {
if (e.compareTo(parent.element) < 0)
parent.left = current.right;
else
parent.right = current.right;
}
}
else {
// Case 2: The current node has a left child
// Locate the rightmost node in the left subtree of
// the current node and also its parent
TreeNode<E> parentOfRightMost = current;
TreeNode<E> rightMost = current.left;
while (rightMost.right != null) {
parentOfRightMost = rightMost;
rightMost = rightMost.right; // Keep going to the right
}
// Replace the element in current by the element in rightMost
current.element = rightMost.element;
// Eliminate rightmost node
if (parentOfRightMost.right == rightMost)
parentOfRightMost.right = rightMost.left;
else
// Special case: parentOfRightMost == current
parentOfRightMost.left = rightMost.left;
}
size--;
return true; // Element deleted successfully
}
@Override /** Obtain an iterator. Use inorder. */
public java.util.Iterator<E> iterator() {
return new InorderIterator();
}
// Inner class InorderIterator
private class InorderIterator implements java.util.Iterator<E> {
// Store the elements in a list
private java.util.ArrayList<E> list =
new java.util.ArrayList<E>();
private int current = 0; // Point to the current element in list
public InorderIterator() {
inorder(); // Traverse binary tree and store elements in list
}
/** Inorder traversal from the root*/
private void inorder() {
inorder(root);
}
/** Inorder traversal from a subtree */
private void inorder(TreeNode<E> root) {
if (root == null)return;
inorder(root.left);
list.add(root.element);
inorder(root.right);
}
@Override /** More elements for traversing? */
public boolean hasNext() {
if (current < list.size())
return true;
return false;
}
@Override /** Get the current element and move to the next */
public E next() {
return list.get(current++);
}
@Override /** Remove the current element */
public void remove() {
delete(list.get(current)); // Delete the current element
list.clear(); // Clear the list
inorder(); // Rebuild the list
}
}
/** Remove all elements from the tree */
public void clear() {
root = null;
size = 0;
}
// if (tree == null) {
// return false;
// }
// else if (e.compareTo(tree.element) > 0) {
// return search(e, tree.right);
// }
// else if (e.compareTo(tree.element) < 0) {
// return search(e, tree.left);
// }
// else {
// return true;
// }
//
}

View File

@@ -0,0 +1,29 @@
package netwerkprog.game.util.tree;
public interface Tree<E> extends Iterable<E> {
/** Return true if the element is in the tree */
public boolean search(E e);
/** Insert element o into the binary tree
* Return true if the element is inserted successfully */
public boolean insert(E e);
/** Delete the specified element from the tree
* Return true if the element is deleted successfully */
public boolean delete(E e);
/** Inorder traversal from the root*/
public void inorder();
/** Postorder traversal from the root */
public void postorder();
/** Preorder traversal from the root */
public void preorder();
/** Get the number of nodes in the tree */
public int getSize();
/** Return true if the tree is empty */
public boolean isEmpty();
}

View File

@@ -0,0 +1,16 @@
package netwerkprog.game.util.tree;
public class TreeNode<E> {
protected E element;
protected TreeNode<E> left;
protected TreeNode<E> right;
public TreeNode(E e) {
this.element = e;
}
public E getElement() {
return element;
}
}

View File

@@ -7,6 +7,6 @@ import netwerkprog.game.util.application.GameApplicationConfiguration;
public class DesktopLauncher { public class DesktopLauncher {
public static void main (String[] arg) { public static void main (String[] arg) {
GameApplicationConfiguration config = new GameApplicationConfiguration("Netwerk Game",1200,800); GameApplicationConfiguration config = new GameApplicationConfiguration("Netwerk Game",1200,800);
new LwjglApplication(new MainGame(), config); new LwjglApplication(MainGame.getInstance(), config);
} }
} }