|
20 | 20 | import java.awt.image.BufferedImage; |
21 | 21 | import java.awt.image.DataBufferInt; |
22 | 22 |
|
| 23 | +/* |
| 24 | + * This module forms the core architecture of the JavaGame. It coordinates the various |
| 25 | + * audio and input handler components, generates the frame, renders the screen graphics, spawns |
| 26 | + * NPCs and customizes the player. Game is also responsible for changing the maps and levels, as well |
| 27 | + * as displaying various messages on the screen (e.g. fps) |
| 28 | + */ |
23 | 29 | public class Game extends Canvas implements Runnable { |
24 | 30 |
|
25 | 31 | // Setting the size and name of the frame/canvas |
26 | 32 | private static final long serialVersionUID = 1L; |
27 | | - private static final String game_Version = "v1.8.3 Alpha"; |
28 | | - private static final int WIDTH = 160; |
29 | | - private static final int HEIGHT = (WIDTH / 3 * 2); |
30 | | - private static final int SCALE = 3; |
31 | | - private static final String NAME = "Game"; |
| 33 | + private static final String game_Version = "v1.8.3 Alpha"; // Current version of the game |
| 34 | + private static final int WIDTH = 160; // The width of the screen |
| 35 | + private static final int HEIGHT = (WIDTH / 3 * 2); // The height of the screen (two thirds of the width) |
| 36 | + private static final int SCALE = 3; // Scales the size of the screen (in either the x-direction, y-direction, or both) |
| 37 | + private static final String NAME = "Game"; // The name of the JFrame panel |
32 | 38 | private static Game game; |
33 | | - private static Time time = new Time(); |
34 | | - private static int Jdata_Host; |
35 | | - private static String Jdata_UserName = ""; |
36 | | - private static String Jdata_IP = "127.0.0.1"; |
37 | | - private static boolean changeLevel = false; |
38 | | - private static boolean npc = false; |
39 | | - private static int map = 0; |
40 | | - private static int shirtCol; |
41 | | - private static int faceCol; |
42 | | - private static boolean[] alternateCols = new boolean[2]; |
43 | | - private static int fps; |
44 | | - private static int tps; |
45 | | - private static int steps; |
46 | | - private static boolean devMode; |
47 | | - private static boolean closingMode; |
48 | | - |
49 | | - private static JFrame frame; |
50 | | - private static AudioHandler backgroundMusic; |
51 | | - private static boolean running = false; |
52 | | - private static InputHandler input; |
53 | | - private static MouseHandler mouse; |
54 | | - private static InputContext context; |
| 39 | + private static Time time = new Time(); // Date object that represents the calender's time value, in hh:mm:ss |
| 40 | + |
| 41 | + // The properties of the player, npc, and fps/tps |
| 42 | + private static int Jdata_Host; // The host of a multiplayer game (only available in earlier versions) |
| 43 | + private static String Jdata_UserName = ""; // The player's username (initialized as an empty string) |
| 44 | + private static String Jdata_IP = "127.0.0.1"; // Displays an IP address |
| 45 | + private static boolean changeLevel = false; // Determines whether the level should change (initialized to not change) |
| 46 | + private static boolean npc = false; // Non-player character (NPC) initialized to non-existing |
| 47 | + private static int map = 0; // Map of the level, initialized to no map (0) |
| 48 | + private static int shirtCol; // The colour of the character's shirt |
| 49 | + private static int faceCol; // The colour (ethnicity) of the character (their face) |
| 50 | + private static boolean[] alternateCols = new boolean[2]; // Boolean array with 2 elements (for determining shirt and face colour), all initialized to false |
| 51 | + private static int fps; // The frame rate (frames per second), frequency at which images are displayed on the canvas |
| 52 | + private static int tps; // The ticks (ticks per second), unit measure of time for one iteration of the game logic loop. |
| 53 | + private static int steps; |
| 54 | + private static boolean devMode; // Determines whether the game is in developer mode |
| 55 | + private static boolean closingMode; // Determines whether the game will exit |
| 56 | + |
| 57 | + // Audio, input, and mouse handler objects |
| 58 | + private static JFrame frame; // Frame with support for JFC/swing component architecture |
| 59 | + private static AudioHandler backgroundMusic; // AudioHandler object that can play music in the background (but can't turn it off) |
| 60 | + private static boolean running = false; // Determines whether the game is currently in process (i.e. whether the game is running) |
| 61 | + private static InputHandler input; // InputHandler object that accepts keyboard input and follows the appropriate actions |
| 62 | + private static MouseHandler mouse; // MouseHandler object that tracks mouse movement and clicks, and follows the appropriate actions |
| 63 | + private static InputContext context; // InputContext object that provides methods to control text input facilities |
55 | 64 | private int tickCount = 0; |
56 | | - private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, |
57 | | - BufferedImage.TYPE_INT_RGB); |
58 | | - private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()) |
| 65 | + |
| 66 | + // Graphics |
| 67 | + private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, // Describes a rasterized image with dimensions WIDTH and HEIGHT, and RGB colour |
| 68 | + BufferedImage.TYPE_INT_RGB); // Set to TYPE_INT_ARGB to support transparency |
| 69 | + private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()) // Array of red, green and blue values for each pixel, as well as an alpha value (if there is an alpha channel) |
59 | 70 | .getData(); |
60 | | - private int[] colours = new int[6 * 6 * 6]; |
| 71 | + private int[] colours = new int[6 * 6 * 6]; // Array of 216 unique colours |
61 | 72 | private BufferedImage image2 = new BufferedImage(WIDTH, HEIGHT - 30, |
62 | 73 | BufferedImage.TYPE_INT_RGB); |
63 | | - private Screen screen; |
| 74 | + private Screen screen; // Screen object that accepts a width, height, and sprite sheet - to generate and render a screen |
64 | 75 | private WindowHandler window; |
65 | | - private LevelHandler level; |
66 | | - private Player player; |
67 | | - private Dummy dummy; |
68 | | - private Vendor vendor; |
69 | | - private Font font = new Font(); |
70 | | - private String nowPlaying; |
71 | | - private boolean notActive = true; |
72 | | - private int trigger = 0; |
73 | | - private Printing print = new Printing(); |
| 76 | + private LevelHandler level; // LevelHandler object that loads and renders levels along with tiles, entities, projectiles and more. |
| 77 | + |
| 78 | + //The entities of the game |
| 79 | + private Player player; // This is the actual player |
| 80 | + private Dummy dummy; // This is a dummy npc (follows the player around) |
| 81 | + private Vendor vendor; // This is a vendor npc (random movement) |
| 82 | + private Font font = new Font(); // Font object capable of displaying 2 fonts: Arial and Segoe UI |
| 83 | + private String nowPlaying; |
| 84 | + private boolean notActive = true; |
| 85 | + private int trigger = 0; |
| 86 | + private Printing print = new Printing(); // Print object that can display various messages and error logs |
74 | 87 |
|
75 | 88 | /** |
76 | 89 | * @author Redomar |
77 | 90 | * @version Alpha 1.8.3 |
78 | 91 | */ |
79 | 92 | public Game() { |
80 | | - context = InputContext.getInstance(); |
| 93 | + context = InputContext.getInstance(); // Stores the input context for the window |
| 94 | + |
| 95 | + // The game can only be played in one distinct window size |
81 | 96 | setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); |
82 | 97 | setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); |
83 | 98 | setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); |
84 | 99 |
|
85 | | - setFrame(new JFrame(NAME)); |
86 | | - getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
87 | | - getFrame().setLayout(new BorderLayout()); |
88 | | - getFrame().add(this, BorderLayout.CENTER); |
89 | | - getFrame().pack(); |
90 | | - getFrame().setResizable(false); |
91 | | - getFrame().setLocationRelativeTo(null); |
92 | | - getFrame().setVisible(true); |
| 100 | + setFrame(new JFrame(NAME)); // Creates the frame |
| 101 | + getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Exits the program when user closes the frame |
| 102 | + getFrame().setLayout(new BorderLayout()); // Border lays out a container |
| 103 | + getFrame().add(this, BorderLayout.CENTER); // The centre layout constraint (middle of container) |
| 104 | + getFrame().pack(); // Sizes the frame so that all its contents are at or above their preferred sizes |
| 105 | + getFrame().setResizable(false); // Prevents the user from resizing the frame |
| 106 | + getFrame().setLocationRelativeTo(null); // Centres the window on the screen |
| 107 | + getFrame().setVisible(true); // Displays the frame |
93 | 108 |
|
94 | 109 | requestFocus(); |
95 | 110 | setDevMode(false); |
|
0 commit comments