This commit is contained in:
MickWerf
2020-06-07 17:14:03 +02:00
6 changed files with 168 additions and 122 deletions

Binary file not shown.

View File

@@ -162,7 +162,7 @@ public class MainGame extends Game implements ClientCallback {
private void playSong() { private void playSong() {
// play music // play music
Music music = Gdx.audio.newMusic(Gdx.files.getFileHandle("core/assets/beat.mp3", Files.FileType.Internal)); Music music = Gdx.audio.newMusic(Gdx.files.getFileHandle("core/assets/sound/beat.mp3", Files.FileType.Internal));
music.setVolume(.1f); music.setVolume(.1f);
music.play(); music.play();
music.setLooping(true); music.setLooping(true);
@@ -205,7 +205,8 @@ public class MainGame extends Game implements ClientCallback {
renderTurnText(); renderTurnText();
} else if (this.gamestate == GAMESTATE.SELECTING_FACTION) { } else if (this.gamestate == GAMESTATE.SELECTING_FACTION) {
clearRender(67, 168, 186, 1); clearRender(67, 168, 186, 1);
renderString("FACTION SELECT\nYou are: " + username + "\nPress 1 for mega corporation, press 2 for hackers", Gdx.graphics.getWidth() / 2f, Gdx.graphics.getHeight() / 2f); String text = username == null ? "Connecting to server..." : "FACTION SELECT\nYou are: " + username + "\nPress 1 for mega corporation, press 2 for hackers";
renderString(text, Gdx.graphics.getWidth() / 2f, Gdx.graphics.getHeight() / 2f);
if (this.ready && this.enemyReady) { if (this.ready && this.enemyReady) {
if (this.chosenFaction == Faction.HACKER) { if (this.chosenFaction == Faction.HACKER) {
chooseHacker(); chooseHacker();
@@ -392,7 +393,7 @@ public class MainGame extends Game implements ClientCallback {
} else if (data instanceof TeamData) { } else if (data instanceof TeamData) {
// check if it is not our own message // check if it is not our own message
if (!((TeamData) data).getUsername().equals(this.username)) { if (!((TeamData) data).getUsername().equals(this.username)) {
// if we have already chosen a faction, so we were first // if we have not yet chosen a faction, select the opposing faction
TeamData teamData = (TeamData) data; TeamData teamData = (TeamData) data;
Faction enemyFaction = teamData.getFaction(); Faction enemyFaction = teamData.getFaction();
if (this.chosenFaction == null) { if (this.chosenFaction == null) {
@@ -410,7 +411,7 @@ public class MainGame extends Game implements ClientCallback {
if (!moveData.getUsername().equals(this.username)) { if (!moveData.getUsername().equals(this.username)) {
GameTile tile = mapRenderer.getGameTile(moveData.getPos()); GameTile tile = mapRenderer.getGameTile(moveData.getPos());
GameCharacter character = enemyTeam.get(moveData.getCharacterName()); GameCharacter character = enemyTeam.get(moveData.getCharacterName());
gameInputProcessor.removeCharacterFromTile(character); mapRenderer.removeCharacterFromTile(character);
tile.visit(character); tile.visit(character);
} }
} else if (data instanceof DamageData) { } else if (data instanceof DamageData) {

View File

@@ -94,7 +94,6 @@ public class GameInputProcessor implements InputProcessor {
@Override @Override
public boolean keyUp(int keycode) { public boolean keyUp(int keycode) {
// System.out.println(camera.position.x + " , " + camera.position.y);
if (mainGame.getGamestate() == GAMESTATE.PLAYING) { if (mainGame.getGamestate() == GAMESTATE.PLAYING) {
if (keysList.contains(keycode)) { if (keysList.contains(keycode)) {
@@ -156,14 +155,14 @@ public class GameInputProcessor implements InputProcessor {
if (mainGame.hasCharacterSelected() && !gameTile.containsCharacter()) { if (mainGame.hasCharacterSelected() && !gameTile.containsCharacter()) {
if (gameTile.getSymbol() != '#' && mainGame.mapRenderer.getSurroundedTilesOfCurrentCharacter().contains(gameTile)) { if (gameTile.getSymbol() != '#' && mainGame.mapRenderer.getSurroundedTilesOfCurrentCharacter().contains(gameTile)) {
removeCharacterFromTile(mainGame.getSelectedCharacter()); mainGame.mapRenderer.removeCharacterFromTile(mainGame.getSelectedCharacter());
gameTile.visit(mainGame.getSelectedCharacter()); gameTile.visit(mainGame.getSelectedCharacter());
mainGame.mapRenderer.setSurroundedTilesOfCurrentCharacter(col, row); mainGame.mapRenderer.setSurroundedTilesOfCurrentCharacter(col, row);
mainGame.increaseTurn(); mainGame.increaseTurn();
mainGame.send(new MoveData(mainGame.getUsername(), mainGame.getSelectedCharacter().getName(), mainGame.mapRenderer.getPos(gameTile))); mainGame.send(new MoveData(mainGame.getUsername(), mainGame.getSelectedCharacter().getName(), mainGame.mapRenderer.getPos(gameTile)));
} }
} }
// clicking on enemy // clicking on enemy
if (mainGame.hasCharacterSelected() && gameTile.containsCharacter() && gameTile.getCharacter().getFaction() != mainGame.getChosenFaction()) { if (mainGame.hasCharacterSelected() && gameTile.containsCharacter() && gameTile.getCharacter().getFaction() != mainGame.getChosenFaction()) {
if (mainGame.mapRenderer.getSurroundedTilesOfCurrentCharacter().contains(gameTile)) { if (mainGame.mapRenderer.getSurroundedTilesOfCurrentCharacter().contains(gameTile)) {
if (!gameTile.getCharacter().isDead()) { if (!gameTile.getCharacter().isDead()) {
@@ -202,18 +201,7 @@ public class GameInputProcessor implements InputProcessor {
return false; return false;
} }
public 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) {

View File

@@ -240,6 +240,23 @@ public class MapRenderer implements Renderable {
return null; return null;
} }
/**
* remove character from tile
* @param character the character to remove
*/
public void removeCharacterFromTile(GameCharacter character) {
rowLoop:
for (int row = 0; row < getGameTiles().length; row++) {
for (int col = 0; col < getGameTiles()[0].length; col++) {
GameTile gameTile = getGameTiles()[row][col];
if (gameTile.containsCharacter() && gameTile.getCharacter().equals(character)) {
gameTile.removeCharacter();
break rowLoop;
}
}
}
}
/** /**
* resize the screen * resize the screen
* @param screenWidth the width of the screen * @param screenWidth the width of the screen

View File

@@ -1,107 +1,112 @@
package netwerkprog.game.util.tree; package netwerkprog.game.util.tree;
import java.util.ArrayList;
public class BST<E extends Comparable<E>> extends AbstractTree<E> { public class BST<E extends Comparable<E>> extends AbstractTree<E> {
protected TreeNode<E> root; protected TreeNode<E> root;
protected int size = 0; protected int size = 0;
public int sum () {
public int sum() {
return this.sum(this.getRoot()); return this.sum(this.getRoot());
} }
public int sum( TreeNode<E> node ) { public int sum(TreeNode<E> node) {
// Schrijf hier jouw code...
if (node == null) { if (node == null) {
return 0; return 0;
} }
int nodeValue = (Integer) node.element; // Tip, omdat E nog onbekend is doen we het zo (niet helemaal netjes) int nodeValue = (Integer) node.element;
return sum(node.left) + sum(node.right); return sum(node.left) + sum(node.right);
} }
public int totalLeaves () {
public int totalLeaves() {
return this.totalLeaves(this.getRoot()); return this.totalLeaves(this.getRoot());
} }
public int totalLeaves ( TreeNode<E> node ) { public int totalLeaves(TreeNode<E> node) {
if (node == null) { if (node == null) {
return 0; return 0;
} }
if (node.left == null && node.right == null) { if (node.left == null && node.right == null) {
return 1; return 1;
} }
return totalLeaves(node.left) + totalLeaves(node.right); return totalLeaves(node.left) + totalLeaves(node.right);
} }
/** Create a default binary tree */ /**
* Create a default binary tree
*/
public BST() { public BST() {
} }
/** Create a binary tree from an array of objects */ /**
* Create a binary tree from an array of objects
*/
public BST(E[] objects) { public BST(E[] objects) {
for (int i = 0; i < objects.length; i++) for (E object : objects) insert(object);
insert(objects[i]);
} }
@Override /** Returns true if the element is in the tree */ /**
* Returns true if the element is in the tree
*/
@Override
public boolean search(E e) { public boolean search(E e) {
return search(e, root); return search(e, root);
} }
private boolean search(E e, TreeNode<E> tree) private boolean search(E e, TreeNode<E> tree) {
{ // nog niet correct
if (tree == null) if (tree == null) {
{
return false; return false;
} }
if (e.compareTo(tree.element) == 0) if (e.compareTo(tree.element) == 0) {
{
return true; return true;
} }
if (e.compareTo(tree.element) < 0) if (e.compareTo(tree.element) < 0) {
{
return search(e, tree.left); return search(e, tree.left);
} } else // (e.compareTo(tree.element) > 0)
else // (e.compareTo(tree.element) > 0)
{ {
return search(e, tree.right); return search(e, tree.right);
} }
} }
@Override /** Insert element o into the binary tree /**
* Return true if the element is inserted successfully */ * Insert element o into the binary tree
public boolean insert(E e) { * Return true if the element is inserted successfully
*/
@Override
public void insert(E e) {
if (root == null) { if (root == null) {
root = createNewNode(e); // Create a new root root = createNewNode(e); // Create a new root
size++; size++;
return true; } else {
} insert(e, root);
else {
return insert(e, root);
} }
} }
/** Insert element o into the binary tree /**
* Insert element o into the binary tree
* Return true if the element is inserted successfully * Return true if the element is inserted successfully
pre: root != null * pre: root != null
*/ */
public boolean insert(E e, TreeNode<E> tree) { public boolean insert(E e, TreeNode<E> tree) {
if (e.compareTo(tree.element) == 0) { if (e.compareTo(tree.element) == 0) {
return false; // Duplicate node not inserted return false; // Duplicate node not inserted
} } else if (e.compareTo(tree.element) < 0 && tree.left != null)
else if (e.compareTo(tree.element) < 0 && tree.left != null) return insert(e, tree.left);
return insert(e, tree.left);
else if (e.compareTo(tree.element) > 0 && tree.right != null) else if (e.compareTo(tree.element) > 0 && tree.right != null)
return insert(e, tree.right); return insert(e, tree.right);
// Create the new node and attach it to the parent node // Create the new node and attach it to the parent node
else { else {
if (e.compareTo(tree.element) < 0) { if (e.compareTo(tree.element) < 0) {
tree.left = createNewNode(e); tree.left = createNewNode(e);
} } else {
else {
tree.right = createNewNode(e); tree.right = createNewNode(e);
} }
size++; size++;
@@ -111,15 +116,20 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
protected TreeNode<E> createNewNode(E e) { protected TreeNode<E> createNewNode(E e) {
return new TreeNode<E>(e); return new TreeNode<>(e);
} }
@Override /** Inorder traversal from the root*/ /**
* Inorder traversal from the root
*/
@Override
public void inorder() { public void inorder() {
inorder(root); inorder(root);
} }
/** Inorder traversal from a subtree */ /**
* Inorder traversal from a subtree
*/
protected void inorder(TreeNode<E> root) { protected void inorder(TreeNode<E> root) {
if (root == null) return; if (root == null) return;
inorder(root.left); inorder(root.left);
@@ -127,12 +137,17 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
inorder(root.right); inorder(root.right);
} }
@Override /** Postorder traversal from the root */ /**
* Post order traversal from the root
*/
@Override
public void postorder() { public void postorder() {
postorder(root); postorder(root);
} }
/** Postorder traversal from a subtree */ /**
* Post order traversal from a subtree
*/
protected void postorder(TreeNode<E> root) { protected void postorder(TreeNode<E> root) {
if (root == null) return; if (root == null) return;
postorder(root.left); postorder(root.left);
@@ -140,12 +155,17 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
System.out.print(root.element + " "); System.out.print(root.element + " ");
} }
@Override /** Preorder traversal from the root */ /**
* Preorder traversal from the root
*/
@Override
public void preorder() { public void preorder() {
preorder(root); preorder(root);
} }
/** Preorder traversal from a subtree */ /**
* Preorder traversal from a subtree
*/
protected void preorder(TreeNode<E> root) { protected void preorder(TreeNode<E> root) {
if (root == null) return; if (root == null) return;
System.out.print(root.element + " "); System.out.print(root.element + " ");
@@ -153,8 +173,10 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
preorder(root.right); preorder(root.right);
} }
/** This inner class is static, because it does not access /**
any instance members defined in its outer class */ * 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>> { public static class TreeNode<E extends Comparable<E>> {
protected E element; protected E element;
protected TreeNode<E> left; protected TreeNode<E> left;
@@ -165,41 +187,49 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
} }
} }
@Override /** Get the number of nodes in the tree */ /**
* Get the number of nodes in the tree
*/
@Override
public int getSize() { public int getSize() {
return size; return size;
} }
/** Returns the root of the tree */ /**
* Returns the root of the tree
*/
public TreeNode<E> getRoot() { public TreeNode<E> getRoot() {
return root; return root;
} }
/** Returns a path from the root leading to the specified element */ /**
public java.util.ArrayList<TreeNode<E>> path(E e) { * Returns a path from the root leading to the specified element
java.util.ArrayList<TreeNode<E>> list = */
new java.util.ArrayList<TreeNode<E>>(); public ArrayList<TreeNode<E>> path(E e) {
ArrayList<TreeNode<E>> list =
new ArrayList<>();
TreeNode<E> current = root; // Start from the root TreeNode<E> current = root; // Start from the root
while (current != null) { while (current != null) {
list.add(current); // Add the node to the list list.add(current); // Add the node to the list
if (e.compareTo(current.element) < 0) { if (e.compareTo(current.element) < 0) {
current = current.left; current = current.left;
} } else if (e.compareTo(current.element) > 0) {
else if (e.compareTo(current.element) > 0) {
current = current.right; current = current.right;
} } else
else
break; break;
} }
return list; // Return an array list of nodes return list; // Return an array list of nodes
} }
@Override /** Delete an element from the binary tree. /**
* Delete an element from the binary tree.
* Return true if the element is deleted successfully * Return true if the element is deleted successfully
* Return false if the element is not in the tree */ * Return false if the element is not in the tree
public boolean delete(E e) { */
@Override
public void delete(E e) {
// Locate the node to be deleted and also locate its parent node // Locate the node to be deleted and also locate its parent node
TreeNode<E> parent = null; TreeNode<E> parent = null;
TreeNode<E> current = root; TreeNode<E> current = root;
@@ -207,32 +237,28 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
if (e.compareTo(current.element) < 0) { if (e.compareTo(current.element) < 0) {
parent = current; parent = current;
current = current.left; current = current.left;
} } else if (e.compareTo(current.element) > 0) {
else if (e.compareTo(current.element) > 0) {
parent = current; parent = current;
current = current.right; current = current.right;
} } else
else
break; // Element is in the tree pointed at by current break; // Element is in the tree pointed at by current
} }
if (current == null) if (current == null)
return false; // Element is not in the tree return; // Element is not in the tree
// Case 1: current has no left child // Case 1: current has no left child
if (current.left == null) { if (current.left == null) {
// Connect the parent with the right child of the current node // Connect the parent with the right child of the current node
if (parent == null) { if (parent == null) {
root = current.right; root = current.right;
} } else {
else {
if (e.compareTo(parent.element) < 0) if (e.compareTo(parent.element) < 0)
parent.left = current.right; parent.left = current.right;
else else
parent.right = current.right; parent.right = current.right;
} }
} } else {
else {
// Case 2: The current node has a left child // Case 2: The current node has a left child
// Locate the rightmost node in the left subtree of // Locate the rightmost node in the left subtree of
// the current node and also its parent // the current node and also its parent
@@ -251,15 +277,17 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
if (parentOfRightMost.right == rightMost) if (parentOfRightMost.right == rightMost)
parentOfRightMost.right = rightMost.left; parentOfRightMost.right = rightMost.left;
else else
// Special case: parentOfRightMost == current // Special case: parentOfRightMost == current
parentOfRightMost.left = rightMost.left; parentOfRightMost.left = rightMost.left;
} }
size--; size--;
return true; // Element deleted successfully
} }
@Override /** Obtain an iterator. Use inorder. */ /**
* Obtain an iterator. Use inorder.
*/
@Override
public java.util.Iterator<E> iterator() { public java.util.Iterator<E> iterator() {
return new InorderIterator(); return new InorderIterator();
} }
@@ -267,41 +295,51 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
// Inner class InorderIterator // Inner class InorderIterator
private class InorderIterator implements java.util.Iterator<E> { private class InorderIterator implements java.util.Iterator<E> {
// Store the elements in a list // Store the elements in a list
private java.util.ArrayList<E> list = private final java.util.ArrayList<E> list =
new java.util.ArrayList<E>(); new java.util.ArrayList<>();
private int current = 0; // Point to the current element in list private int current = 0; // Point to the current element in list
public InorderIterator() { public InorderIterator() {
inorder(); // Traverse binary tree and store elements in list inorder(); // Traverse binary tree and store elements in list
} }
/** Inorder traversal from the root*/ /**
* Inorder traversal from the root
*/
private void inorder() { private void inorder() {
inorder(root); inorder(root);
} }
/** Inorder traversal from a subtree */ /**
* Inorder traversal from a subtree
*/
private void inorder(TreeNode<E> root) { private void inorder(TreeNode<E> root) {
if (root == null)return; if (root == null) return;
inorder(root.left); inorder(root.left);
list.add(root.element); list.add(root.element);
inorder(root.right); inorder(root.right);
} }
@Override /** More elements for traversing? */ /**
* More elements for traversing?
*/
@Override
public boolean hasNext() { public boolean hasNext() {
if (current < list.size()) return current < list.size();
return true;
return false;
} }
@Override /** Get the current element and move to the next */ /**
* Get the current element and move to the next
*/
@Override
public E next() { public E next() {
return list.get(current++); return list.get(current++);
} }
@Override /** Remove the current element */ /**
* Remove the current element
*/
@Override
public void remove() { public void remove() {
delete(list.get(current)); // Delete the current element delete(list.get(current)); // Delete the current element
list.clear(); // Clear the list list.clear(); // Clear the list
@@ -309,7 +347,9 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
} }
} }
/** Remove all elements from the tree */ /**
* Remove all elements from the tree
*/
public void clear() { public void clear() {
root = null; root = null;
size = 0; size = 0;
@@ -317,9 +357,9 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
@Override @Override
public String toString() { public String toString() {
String res = ""; StringBuilder res = new StringBuilder();
for (E e : this) { for (E e : this) {
res += e.toString(); res.append(e.toString());
} }
return "BST{" + return "BST{" +
"root=" + root + "root=" + root +

View File

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