Room

import java.util.*; //We want to have a list of actions that the player can take in this room, so we do this import statement to use the List and ArrayList classes public class Room { /*Like with the Exit.java file in this collection, we'll look later at changing these from default access to something more appropriate. I just wanted to make progress with implementation, so I'll clean it up later. */ String description = ""; Exit exit; //We will eventually make each room able to have more than one exit, but let's just start with one for now. List<Object> actionList = new ArrayList<Object>(); public void setDescription(String description) { this.description = description; } public void showDescription() { System.out.println(description); } public void showExits() { System.out.println("You see the following exits:"); exit.showDescription(); } private void createActionList() /*What actions are available to the player in this room? Right now, we're just dealing with exits, but eventually there will be other action options, such as examining items, fighting enemies, and so on. Keep it simple to start, and build on!*/ { //TODO: Eventually this function will go through the list of items and exits actionList.clear(); //Get exit options String exitOption = "Exit through -> " + exit.description; actionList.add(exit); } private void showActions() { if(actionList.size() == 0) //If we haven't created an action list before, we need to do so now { createActionList(); } System.out.println("What would you like to do? Type the option number and press Enter:"); for(int i = 0; i < actionList.size(); i++) { Object o = actionList.get(i); if(o instanceof Exit) /*instanceof is a helpful Java operator. It tells us if an object is of a certain type. Exits will have different option wording than items, enemies, and so on. */ { Exit e = (Exit)o; System.out.println((i+1) + ": Exit through ->" + e.description); } } } public void explainRoom() //When the player enters a room, the game needs to explain the room, and show a list of options to the player { showDescription(); showExits(); showActions(); } }
This class is a basic representation of a room in my text-based RPG, The Java Secret. Again, this will change as the game is developed.

A lot of this code might seem confusing if you're a new Java developer, but I'll be explaining it all in due time. If you'd like to start learning, I'm writing lessons and tutorials on Java game development here: https://itch.io/jam/summer-learn-java-jam-month-1/community

If you want to learn more about the game, visit http://bit.ly/2K3mYoZ

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.