diff --git a/core/assets/characters.png b/core/assets/characters.png new file mode 100644 index 0000000..8db79d0 Binary files /dev/null and b/core/assets/characters.png differ diff --git a/core/src/netwerkprog/game/client/MainGame.java b/core/src/netwerkprog/game/client/MainGame.java index 8f2e484..bb89bf2 100644 --- a/core/src/netwerkprog/game/client/MainGame.java +++ b/core/src/netwerkprog/game/client/MainGame.java @@ -6,13 +6,20 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.audio.Music; import com.badlogic.gdx.graphics.GL20; 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.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.MapRenderer; import netwerkprog.game.client.game.map.GameInputProcessor; +import netwerkprog.game.util.game.GameCharacter; import netwerkprog.game.util.graphics.FrameRate; +import netwerkprog.game.util.tree.BST; -public class MainGame extends ApplicationAdapter{ +public class MainGame extends ApplicationAdapter { SpriteBatch batch; float screenWidth; float screenHeight; @@ -20,10 +27,25 @@ public class MainGame extends ApplicationAdapter{ private Thread client; private OrthographicCamera camera; private GameInputProcessor gameInputProcessor; + private GameCharacter selectedCharacter; private Map map; public MapRenderer mapRenderer; + private BST tree; + public GameCharacter testCharacter; + + private static MainGame INSTANCE; + + private MainGame() { + } + + public static MainGame getInstance() { + if (INSTANCE == null) { + INSTANCE = new MainGame(); + } + return INSTANCE; + } @Override @@ -51,13 +73,16 @@ public class MainGame extends ApplicationAdapter{ "#########################" }; map = new Map(strings); - gameInputProcessor = new GameInputProcessor(camera, this); + gameInputProcessor = new GameInputProcessor(camera); Gdx.input.setInputProcessor(gameInputProcessor); 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.viewportHeight = screenHeight / 2; camera.update(); + this.tree = new BST<>(); + initCharacters(); +// this.tree.insert(new Hacker(,new BodySwap())); // playSong(); @@ -66,6 +91,20 @@ public class MainGame extends ApplicationAdapter{ // 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() { // play music @@ -144,5 +183,20 @@ public class MainGame extends ApplicationAdapter{ return map.getWidth(); } + public BST 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; + } } diff --git a/core/src/netwerkprog/game/client/game/characters/Agent.java b/core/src/netwerkprog/game/client/game/characters/Agent.java new file mode 100644 index 0000000..3fe9c68 --- /dev/null +++ b/core/src/netwerkprog/game/client/game/characters/Agent.java @@ -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); + } +} diff --git a/core/src/netwerkprog/game/client/game/characters/Hacker.java b/core/src/netwerkprog/game/client/game/characters/Hacker.java new file mode 100644 index 0000000..629b4b8 --- /dev/null +++ b/core/src/netwerkprog/game/client/game/characters/Hacker.java @@ -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); + } +} diff --git a/core/src/netwerkprog/game/client/game/characters/abilities/BodySwap.java b/core/src/netwerkprog/game/client/game/characters/abilities/BodySwap.java index 97514b0..7244e8a 100644 --- a/core/src/netwerkprog/game/client/game/characters/abilities/BodySwap.java +++ b/core/src/netwerkprog/game/client/game/characters/abilities/BodySwap.java @@ -1,4 +1,14 @@ 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; + } } diff --git a/core/src/netwerkprog/game/client/game/characters/abilities/Implant.java b/core/src/netwerkprog/game/client/game/characters/abilities/Implant.java index 1f86e28..f6d3b39 100644 --- a/core/src/netwerkprog/game/client/game/characters/abilities/Implant.java +++ b/core/src/netwerkprog/game/client/game/characters/abilities/Implant.java @@ -1,4 +1,14 @@ 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; + } } diff --git a/core/src/netwerkprog/game/client/game/map/GameInputProcessor.java b/core/src/netwerkprog/game/client/game/map/GameInputProcessor.java index 061d867..07ddc6c 100644 --- a/core/src/netwerkprog/game/client/game/map/GameInputProcessor.java +++ b/core/src/netwerkprog/game/client/game/map/GameInputProcessor.java @@ -8,12 +8,13 @@ import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.utils.TimeUtils; import netwerkprog.game.client.MainGame; +import netwerkprog.game.util.game.GameCharacter; import java.util.ArrayList; public class GameInputProcessor implements InputProcessor { private final OrthographicCamera camera; - private MainGame game; + private MainGame mainGame; private ArrayList keysList; private boolean isWPressed = false; private boolean isAPressed = false; @@ -29,11 +30,10 @@ public class GameInputProcessor implements InputProcessor { * makes a new game input processor * * @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.game = game; + this.mainGame = MainGame.getInstance(); keysList = new ArrayList<>(); lastTimeCounted = TimeUtils.millis(); @@ -123,19 +123,44 @@ public class GameInputProcessor implements InputProcessor { Vector3 touchPoint = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); camera.unproject(touchPoint); - for (int row = 0; row < game.mapRenderer.getGameTiles().length; row++) { - for (int col = 0; col < game.mapRenderer.getGameTiles()[0].length; col++) { - GameTile gameTile = game.mapRenderer.getGameTiles()[row][col]; + 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.contains(touchPoint.x, touchPoint.y)) { - System.out.println(gameTile + " row: " + row + ", col: " + col); - //TODO make stuff happen with the tile - return true; + if (button == Input.Buttons.LEFT) { +// 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 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 public boolean touchUp(int screenX, int screenY, int pointer, int button) { return false; @@ -164,21 +189,21 @@ public class GameInputProcessor implements InputProcessor { public void update() { long delta = TimeUtils.timeSinceMillis(lastTimeCounted); lastTimeCounted = TimeUtils.millis(); - if (camera.position.x > 5 * game.getHorizontalTileAmount()) + if (camera.position.x > 5 * mainGame.getHorizontalTileAmount()) if (isAPressed()) { 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()) { 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()) { 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()) { camera.position.add(CAMERA_MOVE_SPEED * delta, 0, 0); } diff --git a/core/src/netwerkprog/game/client/game/map/GameTile.java b/core/src/netwerkprog/game/client/game/map/GameTile.java index 964fe9f..d224a12 100644 --- a/core/src/netwerkprog/game/client/game/map/GameTile.java +++ b/core/src/netwerkprog/game/client/game/map/GameTile.java @@ -2,12 +2,14 @@ package netwerkprog.game.client.game.map; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.math.Rectangle; +import netwerkprog.game.util.game.GameCharacter; import java.util.Objects; public class GameTile extends Rectangle { private TextureRegion textureRegion; private char symbol; + private GameCharacter character; public GameTile(TextureRegion textureRegion, int xPos, int yPos, char symbol) { this.textureRegion = textureRegion; @@ -18,10 +20,37 @@ public class GameTile extends Rectangle { 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() { return textureRegion; } + public void setTextureRegion(TextureRegion textureRegion) { + this.textureRegion = textureRegion; + } + public char getSymbol() { return symbol; } diff --git a/core/src/netwerkprog/game/client/game/map/MapRenderer.java b/core/src/netwerkprog/game/client/game/map/MapRenderer.java index 86f91d4..5baa40d 100644 --- a/core/src/netwerkprog/game/client/game/map/MapRenderer.java +++ b/core/src/netwerkprog/game/client/game/map/MapRenderer.java @@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import netwerkprog.game.client.MainGame; import netwerkprog.game.util.graphics.Renderable; import com.badlogic.gdx.graphics.g2d.TextureRegion; @@ -17,6 +18,8 @@ public class MapRenderer implements Renderable { private static int x = 0; private static int y = 0; + private MainGame mainGame; + public static TextureRegion FLOOR_TILE; public static TextureRegion WALL_TILE; @@ -25,13 +28,13 @@ public class MapRenderer implements Renderable { private GameTile[][] gameTiles; - /** * makea a new mapRenderer object - * @param map the map object + * + * @param map the map object * @param tileWidth the width of the tile - * @param batch the batch object so no new ones have to be made - * @param camera the camera object + * @param batch the batch object so no new ones have to be made + * @param camera the camera object */ public MapRenderer(Map map, int tileWidth, SpriteBatch batch, OrthographicCamera camera) { this.map = map; @@ -39,6 +42,7 @@ public class MapRenderer implements Renderable { this.batch = batch; cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); this.camera = camera; + this.mainGame = MainGame.getInstance(); makeTiles(); } @@ -60,11 +64,11 @@ public class MapRenderer implements Renderable { x = 0; for (int col = 0; col < map.getWidth(); 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) == '#') { - 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') { - gameTiles[row][col] = new GameTile(PATH_TILE,x,y, 'x'); + gameTiles[row][col] = new GameTile(PATH_TILE, x, y, 'x'); } x += 32; } @@ -96,6 +100,10 @@ public class MapRenderer implements Renderable { for (int col = 0; col < gameTiles[0].length; col++) { GameTile cur = gameTileRow[col]; 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); + } } } diff --git a/core/src/netwerkprog/game/util/game/GameCharacter.java b/core/src/netwerkprog/game/util/game/GameCharacter.java index 4d8ce69..3a740ee 100644 --- a/core/src/netwerkprog/game/util/game/GameCharacter.java +++ b/core/src/netwerkprog/game/util/game/GameCharacter.java @@ -1,5 +1,6 @@ package netwerkprog.game.util.game; + import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; 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.HashSet; -public abstract class GameCharacter extends Actor { +import java.util.Objects; + +public abstract class GameCharacter extends Actor implements Comparable { protected String name; protected Faction faction; protected HashSet abilities; - protected TextureRegion[][] sprites; 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.faction = faction; this.abilities = new HashSet<>(Arrays.asList(abilities)); this.override = false; - this.sprites = TextureRegion.split(new Texture(spriteSheet),32,32); - super.setX(0); - super.setY(0); + this.textureRegion = textureRegion; } public void addAbilities(Ability ability) { @@ -37,10 +38,41 @@ public abstract class GameCharacter extends Actor { this.override = !this.override; } - public void draw(SpriteBatch batch) { - batch.begin(); - batch.draw(this.sprites[0][0],super.getX(),super.getY()); - batch.end(); + public TextureRegion getTextureRegion() { + return textureRegion; } + 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 + + '}'; + } } diff --git a/core/src/netwerkprog/game/util/tree/AbstractTree.java b/core/src/netwerkprog/game/util/tree/AbstractTree.java new file mode 100644 index 0000000..910ab3c --- /dev/null +++ b/core/src/netwerkprog/game/util/tree/AbstractTree.java @@ -0,0 +1,20 @@ +package netwerkprog.game.util.tree; + +public abstract class AbstractTree implements Tree { + @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; + } +} diff --git a/core/src/netwerkprog/game/util/tree/BST.java b/core/src/netwerkprog/game/util/tree/BST.java new file mode 100644 index 0000000..7e8980b --- /dev/null +++ b/core/src/netwerkprog/game/util/tree/BST.java @@ -0,0 +1,341 @@ +package netwerkprog.game.util.tree; + +import java.util.*; + +public class BST> extends AbstractTree { + protected TreeNode 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 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 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 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 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 createNewNode(E e) { + return new TreeNode(e); + } + + @Override /** Inorder traversal from the root*/ + public void inorder() { + inorder(root); + } + + /** Inorder traversal from a subtree */ + protected void inorder(TreeNode 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 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 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> { + protected E element; + protected TreeNode left; + protected TreeNode 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 getRoot() { + return root; + } + + /** Returns a path from the root leading to the specified element */ + public java.util.ArrayList> path(E e) { + java.util.ArrayList> list = + new java.util.ArrayList>(); + TreeNode 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 parent = null; + TreeNode 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 parentOfRightMost = current; + TreeNode 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 iterator() { + return new InorderIterator(); + } + + // Inner class InorderIterator + private class InorderIterator implements java.util.Iterator { + // Store the elements in a list + private java.util.ArrayList list = + new java.util.ArrayList(); + 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 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; + // } + // +} diff --git a/core/src/netwerkprog/game/util/tree/Tree.java b/core/src/netwerkprog/game/util/tree/Tree.java new file mode 100644 index 0000000..60c982d --- /dev/null +++ b/core/src/netwerkprog/game/util/tree/Tree.java @@ -0,0 +1,29 @@ +package netwerkprog.game.util.tree; + +public interface Tree extends Iterable { + /** 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(); +} diff --git a/core/src/netwerkprog/game/util/tree/TreeNode.java b/core/src/netwerkprog/game/util/tree/TreeNode.java new file mode 100644 index 0000000..71a5372 --- /dev/null +++ b/core/src/netwerkprog/game/util/tree/TreeNode.java @@ -0,0 +1,16 @@ +package netwerkprog.game.util.tree; + +public class TreeNode { + + protected E element; + protected TreeNode left; + protected TreeNode right; + + public TreeNode(E e) { + this.element = e; + } + + public E getElement() { + return element; + } +} diff --git a/desktop/src/netwerkprog/game/desktop/DesktopLauncher.java b/desktop/src/netwerkprog/game/desktop/DesktopLauncher.java index 3406951..f268f83 100644 --- a/desktop/src/netwerkprog/game/desktop/DesktopLauncher.java +++ b/desktop/src/netwerkprog/game/desktop/DesktopLauncher.java @@ -7,6 +7,6 @@ import netwerkprog.game.util.application.GameApplicationConfiguration; public class DesktopLauncher { public static void main (String[] arg) { GameApplicationConfiguration config = new GameApplicationConfiguration("Netwerk Game",1200,800); - new LwjglApplication(new MainGame(), config); + new LwjglApplication(MainGame.getInstance(), config); } }