Lets the player set a name

Better comments
This commit is contained in:
Kristian Knarvik 2018-03-13 10:50:50 +01:00
parent 7d0f0dc4f8
commit 3c1eca4d39
11 changed files with 162 additions and 52 deletions

View File

@ -73,7 +73,8 @@ c)
# Del C # Del C
## Oversikt over designvalg og hva du har gjort ## Oversikt over designvalg og hva du har gjort
* * Oversikt over taster: WASD og piltaster kan brukes til bevegelse. Q for å legge ned ting. E for å plukke opp ting. 1-5 for å velge hvilke ting du ønsker å legge ned eller plukke opp.
* Main og Game har blitt endret til å tillate spilleren å selv velge når den har utført en tur. Dette har blitt gjort for å kunne ignorere uønskede tastetrykk, og for å tilby spilleren valg.
### Tredjepartsfiler ### Tredjepartsfiler
* Bow Fire Arrow Sound fra http://soundbible.com av Stephan Schutze * Bow Fire Arrow Sound fra http://soundbible.com av Stephan Schutze

View File

@ -8,6 +8,8 @@ import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.util.Objects;
public class TextFontAdjuster extends Application { public class TextFontAdjuster extends Application {
// private static final String FONT_NAME = "PetMe64.ttf"; // private static final String FONT_NAME = "PetMe64.ttf";
// new TextFont(FONT_NAME, 22.2, TextModes.CHAR_BOX_SIZE, 0.0, 0.0, 1.0, 1.0); // new TextFont(FONT_NAME, 22.2, TextModes.CHAR_BOX_SIZE, 0.0, 0.0, 1.0, 1.0);
@ -219,7 +221,7 @@ public class TextFontAdjuster extends Application {
return false; return false;
}); });
screen.setKeyTypedHandler((KeyEvent event) -> { screen.setKeyTypedHandler((KeyEvent event) -> {
if (event.getCharacter() != KeyEvent.CHAR_UNDEFINED) { if (!Objects.equals(event.getCharacter(), KeyEvent.CHAR_UNDEFINED)) {
printer.print(event.getCharacter()); printer.print(event.getCharacter());
return true; return true;
} }

View File

@ -53,7 +53,7 @@ public class Main extends Application {
private void setup() { private void setup() {
// //
game = new Game(screen, painter, printer); game = new Game(this, screen, painter, printer);
game.draw(); game.draw();
// //
@ -123,18 +123,18 @@ public class Main extends Application {
printer.redrawTextPage(); printer.redrawTextPage();
return true; return true;
} }
} else if (code == KeyCode.ENTER) { /*} else if (code == KeyCode.ENTER) {
try { try {
doTurn(); doTurn();
} catch (Exception e) { } catch (Exception e) {
printer.printAt(1, 25, "Exception: " + e.getMessage(), Color.RED); printer.printAt(1, 25, "Exception: " + e.getMessage(), Color.RED);
e.printStackTrace(); e.printStackTrace();
} }
return true; return true;*/ // This interferes with other code
} else { } else {
try { try {
game.keyPressed(code); game.keyPressed(code);
doTurn(); //doTurn();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
try { try {

View File

@ -62,12 +62,14 @@ public class Game implements IGame {
private final ITurtle painter; private final ITurtle painter;
private final Printer printer; private final Printer printer;
private int numPlayers = 0; private int numPlayers = 0;
private Main main;
public Game(Screen screen, ITurtle painter, Printer printer) { public Game(Main main, Screen screen, ITurtle painter, Printer printer) {
this.painter = painter; this.painter = painter;
this.printer = printer; this.printer = printer;
addFactory(); addFactory();
this.main = main;
// NOTE: in a more realistic situation, we will have multiple levels (one map // NOTE: in a more realistic situation, we will have multiple levels (one map
// per level), and (at least for a Roguelike game) the levels should be // per level), and (at least for a Roguelike game) the levels should be
@ -307,7 +309,6 @@ public class Game implements IGame {
printer.clearLine(Main.LINE_MSG1); printer.clearLine(Main.LINE_MSG1);
printer.clearLine(Main.LINE_MSG2); printer.clearLine(Main.LINE_MSG2);
printer.clearLine(Main.LINE_MSG3); printer.clearLine(Main.LINE_MSG3);
System.out.println(lastMessages.size());
if (lastMessages.size() > 0) { if (lastMessages.size() > 0) {
printer.printAt(1, Main.LINE_MSG1, lastMessages.get(0)); printer.printAt(1, Main.LINE_MSG1, lastMessages.get(0));
} }
@ -427,9 +428,12 @@ public class Game implements IGame {
// only an IPlayer/human can handle keypresses, and only if it's the human's // only an IPlayer/human can handle keypresses, and only if it's the human's
// turn // turn
if (currentActor instanceof IPlayer) { if (currentActor instanceof IPlayer) {
((IPlayer) currentActor).keyPressed(this, code); // do your thing /*((IPlayer) currentActor).keyPressed(this, code); // do your thing
if (movePoints <= 0) if (movePoints <= 0)
doTurn(); // proceed with turn if we're out of moves doTurn(); // proceed with turn if we're out of moves*/
if (((IPlayer) currentActor).keyPressed(this, code)) {
main.doTurn();
}
} }
} }

View File

@ -114,11 +114,7 @@ public class Backpack<T extends IItem> implements IContainer {
@Override @Override
public boolean isFull() { public boolean isFull() {
if (content.size() >= MAX_SIZE) { return content.size() >= MAX_SIZE;
return true;
} else {
return false;
}
} }
//This shows as an error, but is necessary to compile. //This shows as an error, but is necessary to compile.

View File

@ -20,6 +20,7 @@ public interface IPlayer extends IActor {
* *
* @param game * @param game
* Game, for interacting with the world * Game, for interacting with the world
* @return True if the player has done anything consuming a turn. False otherwise
*/ */
void keyPressed(IGame game, KeyCode key); boolean keyPressed(IGame game, KeyCode key);
} }

View File

@ -12,66 +12,149 @@ import java.util.List;
public class Player implements IPlayer { public class Player implements IPlayer {
private int hp = getMaxHealth(); private int hp = getMaxHealth();
private final Backpack<IItem> equipped = new Backpack<>(); private final Backpack<IItem> equipped = new Backpack<>();
// True if the player wants to pick up something using the digit keys
private boolean dropping = false;
// True if the player wants to drop something using the digit keys
private boolean picking = false;
private boolean writing = false;
private String text = "";
private String name = "Player";
@Override @Override
public void keyPressed(IGame game, KeyCode key) { public boolean keyPressed(IGame game, KeyCode key) {
boolean turnConsumed = false;
if (writing) {
if (key != KeyCode.ENTER) {
text += key.getName();
game.displayMessage("Please enter your name: " + text);
return false;
} else {
name = text;
text = "";
game.displayMessage("Name set.");
writing = false;
}
}
if (key == KeyCode.LEFT || key == KeyCode.A) { if (key == KeyCode.LEFT || key == KeyCode.A) {
tryToMove(game, GridDirection.WEST); tryToMove(game, GridDirection.WEST);
turnConsumed = true;
} else if (key == KeyCode.RIGHT || key == KeyCode.D) { } else if (key == KeyCode.RIGHT || key == KeyCode.D) {
tryToMove(game, GridDirection.EAST); tryToMove(game, GridDirection.EAST);
turnConsumed = true;
} else if (key == KeyCode.UP || key == KeyCode.W) { } else if (key == KeyCode.UP || key == KeyCode.W) {
tryToMove(game, GridDirection.NORTH); tryToMove(game, GridDirection.NORTH);
turnConsumed = true;
} else if (key == KeyCode.DOWN || key == KeyCode.S) { } else if (key == KeyCode.DOWN || key == KeyCode.S) {
tryToMove(game, GridDirection.SOUTH); tryToMove(game, GridDirection.SOUTH);
turnConsumed = true;
} else if (key == KeyCode.E) { } else if (key == KeyCode.E) {
pickUp(game); turnConsumed = pickUpInit(game);
} else if (key == KeyCode.Q) { } else if (key == KeyCode.Q) {
drop(game); turnConsumed = dropInit(game);
} } else if (key.isDigitKey()) {
showStatus(game); //Takes care of all digit keys, but we only use the first 5 for picking up and dropping.
} int keyValue = Integer.parseInt(key.getName());
if (keyValue <= 5) {
private void pickUp(IGame game) { if (dropping) {
//TODO: If there are several items on a tile, let player choose what to pick up. drop(game, keyValue - 1);
if (equipped.size() < 5) { dropping = false;
List<IItem> items = game.getLocalItems(); turnConsumed = true;
for (IItem item : items) { } else if (picking) {
IItem pickedUp = game.pickUp(item); pickUp(game, keyValue - 1);
if (pickedUp != null) { picking = false;
equipped.add(pickedUp); turnConsumed = true;
game.displayMessage("Picked up " + pickedUp.getName());
return;
} }
} }
game.displayMessage("There is nothing to pick up."); } else if (key == KeyCode.N) {
} else { game.displayMessage("Please enter your name: ");
game.displayMessage("Your inventory is full."); writing = true;
} }
showStatus(game);
return turnConsumed;
} }
/*private void pickUpV2(IGame game) { /**
* Initializes the picking up of an item, and does what is needed.
*
* @param game An IGame object
* @return True if a turn was consumed. False otherwise
*/
private boolean pickUpInit(IGame game) {
List<IItem> items = game.getLocalItems(); List<IItem> items = game.getLocalItems();
if (items.size() < 1) { if (items.size() < 1) {
game.displayMessage("There is nothing to pick up."); game.displayMessage("There is nothing to pick up.");
return; } else {
if (items.size() == 1) {
pickUp(game, 0);
return true;
} else {
StringBuilder msg = new StringBuilder("Items on this tile:");
for (int i = 0; i < Math.min(items.size(), 5); i++) {
msg.append(" [").append(i + 1).append("] ").append(firstCharToUpper(items.get(i).getName()));
}
game.displayMessage(msg.toString());
picking = true;
}
} }
StringBuilder msg = new StringBuilder("Items on this tile:"); return false;
for (int i = 0; i < items.size(); i++) { }
msg.append(" [").append(i).append("] ").append(items.get(i).getName());
/**
* Initialized the dropping of an item, and does what is needed.
*
* @param game An IGame object
* @return True if a turn was consumed. False otherwise
*/
private boolean dropInit(IGame game) {
if (equipped.size() == 1) {
drop(game, 0);
return true;
} else {
StringBuilder msg = new StringBuilder("Items to drop:");
for (int i = 0; i < equipped.size(); i++) {
msg.append(" [").append(i + 1).append("] ").append(firstCharToUpper(equipped.get(i).getName()));
}
game.displayMessage(msg.toString());
dropping = true;
} }
game.displayMessage(msg.toString()); return false;
}
/**
* Picks up an item at index i.
*
* @param game An IGame object
* @param i The wanted index
*/
private void pickUp(IGame game, int i) {
if (equipped.size() < 5) { if (equipped.size() < 5) {
//TODO: Let user choose 1-5. List<IItem> items = game.getLocalItems();
if (items.size() >= i) {
IItem pickedUp = game.pickUp(items.get(i));
if (pickedUp != null) {
equipped.add(pickedUp);
game.displayMessage("Picked up " + pickedUp.getName());
} else {
game.displayMessage("The item could not be picked up.");
}
} else {
game.displayMessage("That item does not exist.");
}
} else { } else {
game.displayMessage("Your inventory is full."); game.displayMessage("Your inventory is full.");
} }
}*/ }
private void drop(IGame game) { /**
//TODO: Find a way to implement V2. * Drops an item at index i.
if (!equipped.isEmpty()) { *
if (game.drop(equipped.get(0))) { * @param game An IGame object
equipped.remove(0); * @param i The wanted index
*/
private void drop(IGame game, int i) {
if (!equipped.isEmpty() && equipped.size() > i) {
if (game.drop(equipped.get(i))) {
equipped.remove(i);
game.displayMessage("Item dropped."); game.displayMessage("Item dropped.");
} else { } else {
game.displayMessage("The ground rejected the item."); game.displayMessage("The ground rejected the item.");
@ -81,15 +164,30 @@ public class Player implements IPlayer {
} }
} }
/**
* Updates the status bar with the Player's current stats.
*
* @param game An IGame object
*/
private void showStatus(IGame game) { private void showStatus(IGame game) {
List<String> items = new ArrayList<>(); List<String> items = new ArrayList<>();
for (IItem item : equipped.getContent()) { for (IItem item : equipped.getContent()) {
String name = item.getName(); String name = item.getName();
items.add(Character.toUpperCase(name.charAt(0)) + name.substring(1)); items.add(firstCharToUpper(name));
} }
game.formatStatus("HP: %d/%d ATK: %d DEF: %s INV: %s", hp, getMaxHealth(), getAttack(), getDefence(), String.join(",", items)); game.formatStatus("HP: %d/%d ATK: %d DEF: %s INV: %s", hp, getMaxHealth(), getAttack(), getDefence(), String.join(",", items));
} }
/**
* Changes the first character in a string to uppercase.
*
* @param input The input string
* @return The input string with the first character uppercased
*/
private String firstCharToUpper(String input) {
return Character.toUpperCase(input.charAt(0)) + input.substring(1);
}
private void tryToMove(IGame game, GridDirection dir) { private void tryToMove(IGame game, GridDirection dir) {
ILocation loc = game.getLocation(); ILocation loc = game.getLocation();
if (game.canGo(dir)) { if (game.canGo(dir)) {
@ -129,7 +227,7 @@ public class Player implements IPlayer {
@Override @Override
public String getName() { public String getName() {
return "Person"; return name;
} }
@Override @Override

View File

@ -3,7 +3,6 @@ package inf101.v18.rogue101.shared;
import inf101.v18.grid.GridDirection; import inf101.v18.grid.GridDirection;
import inf101.v18.grid.ILocation; import inf101.v18.grid.ILocation;
import inf101.v18.rogue101.enemies.Girl; import inf101.v18.rogue101.enemies.Girl;
import inf101.v18.rogue101.examples.Carrot;
import inf101.v18.rogue101.game.IGame; import inf101.v18.rogue101.game.IGame;
import inf101.v18.rogue101.objects.IActor; import inf101.v18.rogue101.objects.IActor;
import inf101.v18.rogue101.objects.IItem; import inf101.v18.rogue101.objects.IItem;

View File

@ -2,6 +2,9 @@ package inf101.v18.rogue101.states;
import java.util.Random; import java.util.Random;
/**
* An enum to distinguish different enemies of the same type.
*/
public enum Age { public enum Age {
TODDLER, CHILD, TEEN, ADULT, ELDER; TODDLER, CHILD, TEEN, ADULT, ELDER;

View File

@ -2,6 +2,9 @@ package inf101.v18.rogue101.states;
import java.util.Random; import java.util.Random;
/**
* An enum to distinguish different enemies of the same type.
*/
public enum Occupation { public enum Occupation {
KNIGHT, MAGE, BOWSMAN; KNIGHT, MAGE, BOWSMAN;

View File

@ -2,6 +2,9 @@ package inf101.v18.rogue101.states;
import java.util.Random; import java.util.Random;
/**
* An enum to distinguish different enemies of the same type.
*/
public enum Personality { public enum Personality {
AGRESSIVE, CALM, AFRAID; AGRESSIVE, CALM, AFRAID;