Hi guys, I’m new here, not so new at codding (but I’m in no way a pro) and new at creating a game.
I’m creating a simple text game that runs through a GUI, no interaction or action will be made using command lines, only (mostly) using buttons (I know that this isn’t exactly a text game).
This is how it will to run:
A main method in a Main class will call the OpenGame class:
new OpenGame().setVisible(true);
// This class extends JFrame, here the user will start a new game or load an existing one
Let’s say that I want to start a new game, I’ll click in the “New” button:
private void btnNewGameActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
new Game(GameMode.NEW); // I'll remove "GameMode" later, don't know if I'll keep using it
}
The Game class will have all of the info in the game, like the character created, list of npcs, list of locations and etc (ignore the fact that all variables are public or static, I’ll change that later):
public class Game
{
public static final int MAXNPCS = 20;
public static int dayCount;
public static GameMode gameMode = GameMode.OPEN; // enum
public EnumWeek currentDay; // enum
public ActivityType currentActivity;
public Player player;
public ArrayList<NPC> npcs;
public ArrayList<Location> locations;
public ArrayList<ActivityType> availableActivities; // enum
public ArrayList<ActionType> availableActions; // enum
public ArrayList<OptionType> availableOptions; // enum
public MainWindow gameWindow; // This class extends JFrame, the contents of this JFrame will be changed with one of the following JPanels
public GamePanel gamePanel; // This class extends JPanel, all of the interactions and conversations will happen here, it is a text area, some labels and buttons to execute the action the user wants
public PlanningPanel planningPanel; // This class extends JPanel
public CreateNewCharPanel newCharPanel; // This class extends JPanel
private NPC npcTemp;
private Location locationTemp;
public Game(GameMode gm)
{
if(gm.equals(GameMode.NEW))
{
gameMode = GameMode.NEW;
startNewGame();
}
else
System.err.println("Invalid option");
}
public void startNewGame()
{
player = new Player();
locationTemp = new Location();
npcTemp = new NPC();
locations = new ArrayList<>();
npcs = new ArrayList<NPC>();
dayCount = 0;
currentDay = EnumWeek.SUNDAY;
currentActivity = ActivityType.INTRO;
availableActivities = new ArrayList<>();
availableActions = new ArrayList<>();
availableOptions = new ArrayList<>();
gameWindow = new MainWindow();
gamePanel = new GamePanel();
planningPanel = new PlanningPanel();
newCharPanel = CreateNewCharPanel();
// this will generate everything the game needs (at this point, its used only for tests):
player = player.generatePlayer(player);
npcs = npcTemp.generateNPCs(MAXNPCS);
locations = locationTemp.generateLocations();
availableActivities = currentDay.availableActivities(availableActivities);
showGameWindow();
}
public void showGameWindow()
{
// some look and feel code here
gameWindow.setVisible(true);
}
}
I didn’t include the code here, but right now, the game will be setting the gameWindow with the contents of gamePanel to show the user the intro (a short story).
Now my problems begin, I know how to show, hide or remove JPanels from a JFrame, but once gamePanel starts playing the intro and finishes it (after the user clicks on “Next” button), I don’t know how to go back and “tell” the Game class that the currentActivity is finished, set the currentActivity with “NEWCHAR”, set the gameWindow with the contents of newCharPanel and so on (this is the only moment that I know for sure which activity will be the next activity, after the game is “really” started or loaded, its up to the user).
I don’t know if this is relevant, but I’m using NetBeans and already created the game’s GUI using NetBeans.
I have read about state machines, game loops, analised code from projects like https://github.com/Progether/JAdventure and https://github.com/DanielKoren/Game-State-Manager, but I haven’t had any success in implementing something that suits my needs.
Is it possible to do what I want or have I been doing it the wrong way since the begining? Can someone help me?