so here is my main class. My main problem that I can’t wrap my head around is if I do the gui in a seperate class how do I make it do things. I know I would use actionlistener but if all that is another class even if I pass the data in on creation of the button lets say once the button is pressed it would just give an error because the object is out of scoop. Looking at the above does that mean I would create the action listeners and buttons in the class that would use them and just have a method in my gui class that would add them and remove them from the panel. Sorry if large code dump I’m not sure whats consided too large and needed for pastebin. Thank-you for your help everyone
public class Escape {
//object
Player player = new Player("Sam", 50, 8, 4, 6, 4);
Scanner input = new Scanner(System.in);
locationObjects location = new locationObjects();
//gui stuff later
//global varibles
private boolean done = false;
String command= null;
int currentXCord = 0;
int currentYCord = 0;
Room currentRoom;
public static void main(String[] args) {
Escape game = new Escape();
game.go();
}
//constructor for future gui creation.
private Escape(){
}
public void go(){
System.out.println("You awaken in a dark empty cell you can't remember how you got here nor can you remember who you are.\nAll you know is that you need you need to get out of here.");
System.out.println("To go forward type go, and to search the room type search");
location.addRooms(1);
currentRoom = location.getRoom(0, 0);
while(done == false){
if(player.getHealth() <= 0){
gameOver();
done = true;
break;
}
command = input.nextLine();
testWhatToDo(command);
}
}
private void testWhatToDo(String action) {
if(action.equalsIgnoreCase("search")){
search();
}else if(action.equalsIgnoreCase("go")){
if(currentRoom.hasExit() == true){
this.Win();
}else{
boolean gettingMove = true;
while(gettingMove == true){
System.out.println("Which Direction do you want to go? (try things like left, forward, down, or south)");
String move = input.nextLine();
if(move.equalsIgnoreCase("forward") || move.equalsIgnoreCase("up") || move.equalsIgnoreCase("north")){
if(currentRoom.getUp() == true && currentRoom.DoorNotLocked("up") == true){
currentYCord++;
gettingMove = false;
newRoom();
}else{
gettingMove = false;
}
}else if(move.equalsIgnoreCase("back") || move.equalsIgnoreCase("south")){
gettingMove = false;
if(currentRoom.getDown() == true && currentRoom.DoorNotLocked("down") == true){
currentYCord--;
newRoom();
}else{
gettingMove = false;
System.out.println("Theres no way to travel though the wall");
}
}else if(move.equalsIgnoreCase("left") || move.equalsIgnoreCase("west") || move.equalsIgnoreCase("left side") ){
if(currentRoom.isLeft() == true && currentRoom.DoorNotLocked("left") == true){
gettingMove = false;
currentXCord--;
newRoom();
}else{
gettingMove = false;
System.out.println("there is no way.");
}
}else if(move.equalsIgnoreCase("right") || move.equalsIgnoreCase("east") || move.equalsIgnoreCase("right side") ){
if(currentRoom.isRight() == true && currentRoom.DoorNotLocked("right") == true){
gettingMove = false;
currentXCord++;
newRoom();
}else{
gettingMove = false;
System.out.println("this once contained a meme referenace but now its gone");
}
}else{
gettingMove = false;
}
}
}
//end of go block
}else if(action.equalsIgnoreCase("bag") || action.equalsIgnoreCase("inventory") || action.equalsIgnoreCase("items")){
if(player.getItemList() == true){
System.out.println("You have no items");
}else{
System.out.println("What item do you want to use? enter a number associated with an item to use it.");
player.getContentsOfBag();
boolean itemUsed = false;
itemuse:
while(itemUsed == false){
int i;
try{
i = Integer.parseInt(input.nextLine());
}catch(Exception ex){
System.out.println("umm was that a numeric number? lets try that again");
break itemuse;
}
itemUsed = true;
Item item = player.getItemFromBag(i);
item.doWhatYouDo(player, currentRoom, i);
}
}
}else{
System.out.println("Sorry thats not a valid action");
}
}
private void Win() {
System.out.println("You have beaten the game Congraulations");
done = true;
}
public void newRoom(){
Room nRoom;
nRoom = location.getRoom(currentXCord, currentYCord);
currentRoom = nRoom;
System.out.println("\nYou walk into " + currentRoom.getName() +". Upon entrance you look around you and see "
+ currentRoom.getDescription());
Random rand = new Random();
if(currentRoom.onEntranceEventTest() == true){
currentRoom.getOnEntranceEvent();
}else{
int i = rand.nextInt(100);
if(currentRoom.isEvent() == true){
//location.getRoom(currentRoom).getEvent(s);
}
else if(currentRoom.hasBaddies() == true){
if(i < 35){
int e = rand.nextInt(5);
Baddie badguy = currentRoom.getBaddie(e);
Battle battle = new Battle(player, badguy, input);
}
}
System.out.println("\nWhat will you do now?");
}
}
public void search() {
Random rand = new Random();
int i;
i = rand.nextInt(50);
if(i <= 25){
if(currentRoom.isBaddiesEmpty() == false){
int e = rand.nextInt(5);
Baddie badguy = currentRoom.getBaddie(e);
Battle battle = new Battle(player, badguy, input);
}else{
System.out.println("You didn't find anything.");
}
}else if(i > 25 && i < 50 ){
if(currentRoom.NoItems() == true){
System.out.println("There are no items of use in this room.");
}else{
currentRoom.getAnItem(i, player);
}
}else if(i == 50){
}
}
private void gameOver() {
System.out.println("You have fallen and your journey ends here! You never escape and now you never will.");
}
}//end class
heres my gui class as stands
public class Gui {
JFrame frame;
JPanel mainArea;
JPanel choiceArea;
JScrollPane scrollBar;
JPanel charArea;//for later use
JPanel mapArea;
JTextArea textArea;
public Gui(){
frame = new JFrame("Escape");
frame.setSize(700, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainArea = new JPanel();
choiceArea = new JPanel();
choiceArea.setSize(700, 150);
textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
scrollBar = new JScrollPane();
scrollBar.setVerticalScrollBarPolicy(scrollBar.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollBar.setHorizontalScrollBarPolicy(scrollBar.HORIZONTAL_SCROLLBAR_NEVER);
scrollBar.add(textArea);
mainArea.add(scrollBar);
frame.add(BorderLayout.CENTER, mainArea);
frame.add(BorderLayout.SOUTH, choiceArea);
frame.validate();
frame.setVisible(true);
}
public void addTextToTextArea(String s){
textArea.append("\n" + s);
}
}