Lets the player set a name
Better comments
This commit is contained in:
		@@ -73,7 +73,8 @@ c)
 | 
			
		||||
 | 
			
		||||
# Del C
 | 
			
		||||
## 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
 | 
			
		||||
* Bow Fire Arrow Sound fra http://soundbible.com av Stephan Schutze
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,8 @@ import javafx.scene.input.KeyEvent;
 | 
			
		||||
import javafx.scene.paint.Color;
 | 
			
		||||
import javafx.stage.Stage;
 | 
			
		||||
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
public class TextFontAdjuster extends Application {
 | 
			
		||||
	// 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);
 | 
			
		||||
@@ -219,7 +221,7 @@ public class TextFontAdjuster extends Application {
 | 
			
		||||
			return false;
 | 
			
		||||
		});
 | 
			
		||||
		screen.setKeyTypedHandler((KeyEvent event) -> {
 | 
			
		||||
			if (event.getCharacter() != KeyEvent.CHAR_UNDEFINED) {
 | 
			
		||||
			if (!Objects.equals(event.getCharacter(), KeyEvent.CHAR_UNDEFINED)) {
 | 
			
		||||
				printer.print(event.getCharacter());
 | 
			
		||||
				return true;
 | 
			
		||||
			}
 | 
			
		||||
 
 | 
			
		||||
@@ -53,7 +53,7 @@ public class Main extends Application {
 | 
			
		||||
 | 
			
		||||
	private void setup() {
 | 
			
		||||
		//
 | 
			
		||||
		game = new Game(screen, painter, printer);
 | 
			
		||||
		game = new Game(this, screen, painter, printer);
 | 
			
		||||
		game.draw();
 | 
			
		||||
 | 
			
		||||
		//
 | 
			
		||||
@@ -123,18 +123,18 @@ public class Main extends Application {
 | 
			
		||||
					printer.redrawTextPage();
 | 
			
		||||
					return true;
 | 
			
		||||
				}
 | 
			
		||||
			} else if (code == KeyCode.ENTER) {
 | 
			
		||||
			/*} else if (code == KeyCode.ENTER) {
 | 
			
		||||
				try {
 | 
			
		||||
					doTurn();
 | 
			
		||||
				} catch (Exception e) {
 | 
			
		||||
					printer.printAt(1, 25, "Exception: " + e.getMessage(), Color.RED);
 | 
			
		||||
					e.printStackTrace();
 | 
			
		||||
				}
 | 
			
		||||
				return true;
 | 
			
		||||
				return true;*/ // This interferes with other code
 | 
			
		||||
			} else {
 | 
			
		||||
				try {
 | 
			
		||||
					game.keyPressed(code);
 | 
			
		||||
					doTurn();
 | 
			
		||||
					//doTurn();
 | 
			
		||||
				} catch (Exception e) {
 | 
			
		||||
					e.printStackTrace();
 | 
			
		||||
					try {
 | 
			
		||||
 
 | 
			
		||||
@@ -62,12 +62,14 @@ public class Game implements IGame {
 | 
			
		||||
	private final ITurtle painter;
 | 
			
		||||
	private final Printer printer;
 | 
			
		||||
	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.printer = printer;
 | 
			
		||||
 | 
			
		||||
		addFactory();
 | 
			
		||||
		this.main = main;
 | 
			
		||||
 | 
			
		||||
		// 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
 | 
			
		||||
@@ -307,7 +309,6 @@ public class Game implements IGame {
 | 
			
		||||
		printer.clearLine(Main.LINE_MSG1);
 | 
			
		||||
        printer.clearLine(Main.LINE_MSG2);
 | 
			
		||||
        printer.clearLine(Main.LINE_MSG3);
 | 
			
		||||
        System.out.println(lastMessages.size());
 | 
			
		||||
        if (lastMessages.size() > 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
 | 
			
		||||
		// turn
 | 
			
		||||
		if (currentActor instanceof IPlayer) {
 | 
			
		||||
			((IPlayer) currentActor).keyPressed(this, code); // do your thing
 | 
			
		||||
			/*((IPlayer) currentActor).keyPressed(this, code); // do your thing
 | 
			
		||||
			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();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -114,11 +114,7 @@ public class Backpack<T extends IItem> implements IContainer {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isFull() {
 | 
			
		||||
        if (content.size() >= MAX_SIZE) {
 | 
			
		||||
            return true;
 | 
			
		||||
        } else {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        return content.size() >= MAX_SIZE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //This shows as an error, but is necessary to compile.
 | 
			
		||||
 
 | 
			
		||||
@@ -20,6 +20,7 @@ public interface IPlayer extends IActor {
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param game
 | 
			
		||||
	 *            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);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -12,66 +12,149 @@ import java.util.List;
 | 
			
		||||
public class Player implements IPlayer {
 | 
			
		||||
    private int hp = getMaxHealth();
 | 
			
		||||
    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
 | 
			
		||||
    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) {
 | 
			
		||||
            tryToMove(game, GridDirection.WEST);
 | 
			
		||||
            turnConsumed = true;
 | 
			
		||||
        } else if (key == KeyCode.RIGHT || key == KeyCode.D) {
 | 
			
		||||
            tryToMove(game, GridDirection.EAST);
 | 
			
		||||
            turnConsumed = true;
 | 
			
		||||
        } else if (key == KeyCode.UP || key == KeyCode.W) {
 | 
			
		||||
            tryToMove(game, GridDirection.NORTH);
 | 
			
		||||
            turnConsumed = true;
 | 
			
		||||
        } else if (key == KeyCode.DOWN || key == KeyCode.S) {
 | 
			
		||||
            tryToMove(game, GridDirection.SOUTH);
 | 
			
		||||
            turnConsumed = true;
 | 
			
		||||
        } else if (key == KeyCode.E) {
 | 
			
		||||
            pickUp(game);
 | 
			
		||||
            turnConsumed = pickUpInit(game);
 | 
			
		||||
        } else if (key == KeyCode.Q) {
 | 
			
		||||
            drop(game);
 | 
			
		||||
        }
 | 
			
		||||
        showStatus(game);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void pickUp(IGame game) {
 | 
			
		||||
        //TODO: If there are several items on a tile, let player choose what to pick up.
 | 
			
		||||
        if (equipped.size() < 5) {
 | 
			
		||||
            List<IItem> items = game.getLocalItems();
 | 
			
		||||
            for (IItem item : items) {
 | 
			
		||||
                IItem pickedUp = game.pickUp(item);
 | 
			
		||||
                if (pickedUp != null) {
 | 
			
		||||
                    equipped.add(pickedUp);
 | 
			
		||||
                    game.displayMessage("Picked up " + pickedUp.getName());
 | 
			
		||||
                    return;
 | 
			
		||||
            turnConsumed = dropInit(game);
 | 
			
		||||
        } else if (key.isDigitKey()) {
 | 
			
		||||
            //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) {
 | 
			
		||||
                if (dropping) {
 | 
			
		||||
                    drop(game, keyValue - 1);
 | 
			
		||||
                    dropping = false;
 | 
			
		||||
                    turnConsumed = true;
 | 
			
		||||
                } else if (picking) {
 | 
			
		||||
                    pickUp(game, keyValue - 1);
 | 
			
		||||
                    picking = false;
 | 
			
		||||
                    turnConsumed = true;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            game.displayMessage("There is nothing to pick up.");
 | 
			
		||||
        } else {
 | 
			
		||||
            game.displayMessage("Your inventory is full.");
 | 
			
		||||
        } else if (key == KeyCode.N) {
 | 
			
		||||
            game.displayMessage("Please enter your name: ");
 | 
			
		||||
            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();
 | 
			
		||||
        if (items.size() < 1) {
 | 
			
		||||
            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:");
 | 
			
		||||
        for (int i = 0; i < items.size(); i++) {
 | 
			
		||||
            msg.append(" [").append(i).append("] ").append(items.get(i).getName());
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 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) {
 | 
			
		||||
            //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 {
 | 
			
		||||
            game.displayMessage("Your inventory is full.");
 | 
			
		||||
        }
 | 
			
		||||
    }*/
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void drop(IGame game) {
 | 
			
		||||
        //TODO: Find a way to implement V2.
 | 
			
		||||
        if (!equipped.isEmpty()) {
 | 
			
		||||
            if (game.drop(equipped.get(0))) {
 | 
			
		||||
                equipped.remove(0);
 | 
			
		||||
    /**
 | 
			
		||||
     * Drops an item at index i.
 | 
			
		||||
     *
 | 
			
		||||
     * @param game  An IGame object
 | 
			
		||||
     * @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.");
 | 
			
		||||
            } else {
 | 
			
		||||
                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) {
 | 
			
		||||
        List<String> items = new ArrayList<>();
 | 
			
		||||
        for (IItem item : equipped.getContent()) {
 | 
			
		||||
            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));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 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) {
 | 
			
		||||
        ILocation loc = game.getLocation();
 | 
			
		||||
        if (game.canGo(dir)) {
 | 
			
		||||
@@ -129,7 +227,7 @@ public class Player implements IPlayer {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String getName() {
 | 
			
		||||
        return "Person";
 | 
			
		||||
        return name;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,6 @@ package inf101.v18.rogue101.shared;
 | 
			
		||||
import inf101.v18.grid.GridDirection;
 | 
			
		||||
import inf101.v18.grid.ILocation;
 | 
			
		||||
import inf101.v18.rogue101.enemies.Girl;
 | 
			
		||||
import inf101.v18.rogue101.examples.Carrot;
 | 
			
		||||
import inf101.v18.rogue101.game.IGame;
 | 
			
		||||
import inf101.v18.rogue101.objects.IActor;
 | 
			
		||||
import inf101.v18.rogue101.objects.IItem;
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,9 @@ package inf101.v18.rogue101.states;
 | 
			
		||||
 | 
			
		||||
import java.util.Random;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * An enum to distinguish different enemies of the same type.
 | 
			
		||||
 */
 | 
			
		||||
public enum Age {
 | 
			
		||||
    TODDLER, CHILD, TEEN, ADULT, ELDER;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,9 @@ package inf101.v18.rogue101.states;
 | 
			
		||||
 | 
			
		||||
import java.util.Random;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * An enum to distinguish different enemies of the same type.
 | 
			
		||||
 */
 | 
			
		||||
public enum Occupation {
 | 
			
		||||
    KNIGHT, MAGE, BOWSMAN;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,9 @@ package inf101.v18.rogue101.states;
 | 
			
		||||
 | 
			
		||||
import java.util.Random;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * An enum to distinguish different enemies of the same type.
 | 
			
		||||
 */
 | 
			
		||||
public enum Personality {
 | 
			
		||||
    AGRESSIVE, CALM, AFRAID;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user