2323
2424/*
2525 * This module forms the core architecture of the JavaGame. It coordinates the various
26- * audio and input handler components, generates the frame, renders the screen graphics, spawns
26+ * audio and input handler components, generates the frame, renders the screen graphics, spawns
2727 * NPCs and customizes the player. Game is also responsible for changing the maps and levels, as well
2828 * as displaying various messages on the screen (e.g. fps)
2929 */
@@ -37,7 +37,7 @@ public class Game extends Canvas implements Runnable {
3737 private static final int SCALE = 3 ;
3838 private static final String NAME = "Game" ; // The name of the JFrame panel
3939 private static Game game ;
40- private static Time time = new Time (); // Represents the calender 's time value, in hh:mm:ss
40+ private static Time time = new Time (); // Represents the calendar 's time value, in hh:mm:ss
4141
4242 // The properties of the player, npc, and fps/tps
4343 private static int Jdata_Host ; // The host of a multiplayer game (only available in earlier versions)
@@ -62,30 +62,25 @@ public class Game extends Canvas implements Runnable {
6262 private static InputHandler input ; // Accepts keyboard input and follows the appropriate actions
6363 private static MouseHandler mouse ; // Tracks mouse movement and clicks, and follows the appropriate actions
6464 private static InputContext context ; // Provides methods to control text input facilities
65- private int tickCount = 0 ;
6665
6766 // Graphics
68- private BufferedImage image = new BufferedImage (WIDTH , HEIGHT ,
69- BufferedImage . TYPE_INT_RGB );
70- private int [] pixels = (( DataBufferInt ) image . getRaster (). getDataBuffer ()) // Array of red, green and blue values for each pixel
71- . getData ( );
72- private int [] colours = new int [ 6 * 6 * 6 ] ; // Array of 216 unique colours (6 shades of red, 6 of green, and 6 of blue)
73- private BufferedImage image2 = new BufferedImage ( WIDTH , HEIGHT - 30 ,
74- BufferedImage . TYPE_INT_RGB ) ;
67+ private final BufferedImage image = new BufferedImage (WIDTH , HEIGHT , BufferedImage . TYPE_INT_RGB );
68+ private final int [] pixels = (( DataBufferInt ) image . getRaster (). getDataBuffer ()). getData (); // Array of red, green and blue values for each pixel
69+ private final int [] colours = new int [ 6 * 6 * 6 ]; // Array of 216 unique colours (6 shades of red, 6 of green, and 6 of blue)
70+ private final BufferedImage image2 = new BufferedImage ( WIDTH , HEIGHT - 30 , BufferedImage . TYPE_INT_RGB );
71+ private final Font font = new Font () ; // Font object capable of displaying 2 fonts: Arial and Segoe UI
72+ private final Printing print = new Printing ();
73+ private int tickCount = 0 ;
7574 private Screen screen ;
7675 private WindowHandler window ;
77- private LevelHandler level ; // Loads and renders levels along with tiles, entities, projectiles and more.
76+ private LevelHandler level ; // Loads and renders levels along with tiles, entities, projectiles and more.
7877
7978 //The entities of the game
8079 private Player player ;
81- private Dummy dummy ; // Dummy NPC follows the player around
82- private Vendor vendor ; // Vendor NPC exhibits random movement and is only found on cutom_level
83- private Spruce spruce ; // Tree -- Spruce
84- private Font font = new Font (); // Font object capable of displaying 2 fonts: Arial and Segoe UI
80+ private Dummy dummy ; // Dummy NPC follows the player around
81+ private Vendor vendor ; // Vendor NPC exhibits random movement and is only found on custom_level
82+ private Spruce spruce ; // Tree -- Spruce
8583 private String nowPlaying ;
86- private boolean notActive = true ;
87- private int trigger = 0 ;
88- private Printing print = new Printing ();
8984
9085 /**
9186 * @author Redomar
@@ -99,13 +94,13 @@ public Game() {
9994 setMaximumSize (new Dimension (WIDTH * SCALE , HEIGHT * SCALE ));
10095 setPreferredSize (new Dimension (WIDTH * SCALE , HEIGHT * SCALE ));
10196
102- setFrame (new JFrame (NAME )); // Creates the frame with a defined name
103- getFrame ().setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE ); // Exits the program when user closes the frame
97+ setFrame (new JFrame (NAME )); // Creates the frame with a defined name
98+ getFrame ().setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE ); // Exits the program when user closes the frame
10499 getFrame ().setLayout (new BorderLayout ());
105- getFrame ().add (this , BorderLayout .CENTER ); // Centers the canvas inside the JFrame
106- getFrame ().pack (); // Sizes the frame so that all its contents are at or above their preferred sizes
100+ getFrame ().add (this , BorderLayout .CENTER ); // Centers the canvas inside the JFrame
101+ getFrame ().pack (); // Sizes the frame so that all its contents are at or above their preferred sizes
107102 getFrame ().setResizable (false );
108- getFrame ().setLocationRelativeTo (null ); // Centres the window on the screen
103+ getFrame ().setLocationRelativeTo (null ); // Centres the window on the screen
109104 getFrame ().setVisible (true );
110105
111106 requestFocus ();
@@ -114,22 +109,22 @@ public Game() {
114109 }
115110
116111 /**
117- * This method will spawn a dummy NPC into the level only if they are allowed to be spawned in.
112+ * This method will spawn a dummy NPC into the level only if they are allowed to be spawned in.
118113 * They will be spawned at position (100, 150) with a red shirt and caucasian face.
119114 */
120115 public static void npcSpawn () {
121- if (isNpc () == true ) { // If NPCs are allowed in the level
116+ if (isNpc ()) { // If NPCs are allowed in the level
122117 game .setDummy (new Dummy (game .level , "Dummy" , 100 , 150 , 500 , // Create a new dummy NPC on the current game level, with name 'Dummy'
123- 543 )); // at position (100, 150), with a red shirt and caucasian ethnicity
118+ 543 )); // at position (100, 150), with a red shirt and caucasian ethnicity
124119 game .level .addEntity (Game .getDummy ());
125120 }
126121 }
127122
128123 /**
129- * This method will remove a dummy NPC from the level only if they are not allowed to be in it.
124+ * This method will remove a dummy NPC from the level only if they are not allowed to be in it.
130125 */
131126 public static void npcKill () {
132- if (isNpc () == false ) { // If NPCs are not allowed in the level
127+ if (! isNpc ()) { // If NPCs are not allowed in the level
133128 game .level .removeEntity (Game .getDummy ());
134129 }
135130 }
@@ -196,9 +191,8 @@ public static int getMap() {
196191
197192 /**
198193 * Sets the level to the map [.png] provided. Starts at x100 y100.
199- * @param Map_str
200194 *
201- * Also sets predefined character colours.
195+ * @param Map_str Also sets predefined character colours.
202196 */
203197 public void setMap (String Map_str ) {
204198 setLevel (new LevelHandler (Map_str ));
@@ -214,10 +208,9 @@ public void setMap(String Map_str) {
214208 if (!alternateCols [1 ]) { // If the last element (face colour) is set to False
215209 Game .setFaceCol (543 ); // The player will be caucasian
216210 }
217- setPlayer (new Player (level , 100 , 100 , input ,
218- getJdata_UserName (), shirtCol , faceCol ));
211+ setPlayer (new Player (level , 100 , 100 , input , getJdata_UserName (), shirtCol , faceCol ));
219212 level .addEntity (player );
220- spruce = new Spruce (level , 70 ,170 , 2 );
213+ spruce = new Spruce (level , 70 , 170 , 2 );
221214 level .addEntity (spruce );
222215 }
223216
@@ -344,9 +337,9 @@ public static void setClosing(boolean closing) {
344337 }
345338
346339 /*
347- * This method initializes the game once it starts. It populates the colour array with actual colours (6 shades each of RGB).
348- * This method also builds the initial game level (custom_level), spawns a new vendor NPC, and begins accepting keyboard and mouse input/tracking.
349- */
340+ * This method initializes the game once it starts. It populates the colour array with actual colours (6 shades each of RGB).
341+ * This method also builds the initial game level (custom_level), spawns a new vendor NPC, and begins accepting keyboard and mouse input/tracking.
342+ */
350343 public void init () {
351344 setGame (this );
352345 int index = 0 ;
@@ -365,9 +358,9 @@ public void init() {
365358 input = new InputHandler (this ); // Input begins to record key presses
366359 setMouse (new MouseHandler (this )); // Mouse tracking and clicking is now recorded
367360 setWindow (new WindowHandler (this ));
368- try {
361+ try {
369362 setMap ("/levels/custom_level.png" );
370- } catch (Exception e ){
363+ } catch (Exception e ) {
371364 System .err .println (e );
372365 }
373366 setMap (1 ); // 1 corresponds to custom_level
@@ -377,24 +370,24 @@ public void init() {
377370 }
378371
379372 /**
380- * This method will start the game and allow the user to start playing
381- */
373+ * This method will start the game and allow the user to start playing
374+ */
382375 public synchronized void start () {
383376 Game .setRunning (true ); // Game will run
384377 new Thread (this , "GAME" ).start (); // Thread is an instance of Runnable. Whenever it is started, it will run the run() method
385378 }
386379
387380 /**
388- * This method will stop the game
389- */
381+ * This method will stop the game
382+ */
390383 public synchronized void stop () {
391384 Game .setRunning (false ); // Game will not run
392385 }
393386
394387 /**
395- * This method forms the game loop, determining how the game runs. It runs throughout the entire game,
396- * continuously updating the game state and rendering the game.
397- */
388+ * This method forms the game loop, determining how the game runs. It runs throughout the entire game,
389+ * continuously updating the game state and rendering the game.
390+ */
398391 public void run () {
399392 long lastTime = System .nanoTime ();
400393 double nsPerTick = 1000000000D / 60D ; // The number of nanoseconds in one tick (number of ticks limited to 60 per update)
@@ -422,7 +415,8 @@ public void run() {
422415
423416 try {
424417 Thread .sleep (2 ); // Delays the thread by 2 milliseconds - prevents the loop from using too much CPU
425- } catch (InterruptedException e ) { // If the current thread is interrupted, the interrupted status is cleared
418+ } catch (
419+ InterruptedException e ) { // If the current thread is interrupted, the interrupted status is cleared
426420 e .printStackTrace ();
427421 }
428422
@@ -433,10 +427,7 @@ public void run() {
433427
434428 if (System .currentTimeMillis () - lastTimer >= 1000 ) { // If elapsed time is greater than or equal to 1 second, update
435429 lastTimer += 1000 ; // Updates in another second
436- getFrame ().setTitle (
437- "JavaGame - Version "
438- + WordUtils .capitalize (game_Version ).substring (
439- 1 , game_Version .length ()));
430+ getFrame ().setTitle ("JavaGame - Version " + WordUtils .capitalize (game_Version ).substring (1 , game_Version .length ()));
440431 fps = frames ;
441432 tps = ticks ;
442433 frames = 0 ; // Reset the frames once per second
@@ -447,16 +438,16 @@ public void run() {
447438 }
448439
449440 /**
450- * This method updates the logic of the game.
451- */
441+ * This method updates the logic of the game.
442+ */
452443 public void tick () {
453444 setTickCount (getTickCount () + 1 );
454445 level .tick ();
455446 }
456447
457448 /**
458- * This method displays the current state of the game.
459- */
449+ * This method displays the current state of the game.
450+ */
460451 public void render () {
461452 BufferStrategy bs = getBufferStrategy ();
462453 if (bs == null ) {
@@ -490,12 +481,12 @@ public void render() {
490481 }
491482 }
492483
493- if (isChangeLevel () == true && getTickCount () % 60 == 0 ) {
484+ if (isChangeLevel () && getTickCount () % 60 == 0 ) {
494485 Game .setChangeLevel (true );
495486 setChangeLevel (false );
496487 }
497488
498- if (changeLevel == true ) { // If the player is teleporting to a different level
489+ if (changeLevel ) { // If the player is teleporting to a different level
499490 print .print ("Teleported into new world" , PrintTypes .GAME );
500491 if (getMap () == 1 ) { // If the player is currently on custom_level
501492 setMap ("/levels/water_level.png" );
@@ -524,19 +515,15 @@ public void render() {
524515 g .drawImage (image2 , 0 , getHeight () - 30 , getWidth (), getHeight (), null );
525516 g .setColor (Color .WHITE );
526517 g .setFont (font .getSegoe ());
527- g .drawString (
528- "Welcome "
529- + WordUtils .capitalizeFully (player
530- .getSanitisedUsername ()), 3 , getHeight () - 17 );
518+ g .drawString ("Welcome " + WordUtils .capitalizeFully (player .getSanitisedUsername ()), 3 , getHeight () - 17 );
531519 g .setColor (Color .ORANGE );
532520
533521 if (context .getLocale ().getCountry ().equals ("BE" ) // If the player resides in Belgium or France (i.e. uses AZERTY keyboard)
534- || context .getLocale ().getCountry ().equals ("FR" )) { // Displays "Press A to quit" in orange at the bottom-middle portion of the screen
535- g .drawString ("Press A to quit" , (getWidth () / 2 )
536- - ("Press A to quit" .length () * 3 ), getHeight () - 17 );
522+ || context .getLocale ().getCountry ().equals ("FR" )) { // Displays "Press A to quit" in orange at the bottom-middle portion of the screen
523+ g .drawString ("Press A to quit" , (getWidth () / 2 ) - ("Press A to quit" .length () * 3 ), getHeight () - 17 );
537524 } else { // If the player resides anywhere else (i.e. uses QWERTY keyboard)
538525 g .drawString ("Press Q to quit" , (getWidth () / 2 ) // Displays "Press Q to quit" in orange at the bottom-middle portion of the screen
539- - ("Press Q to quit" .length () * 3 ), getHeight () - 17 );
526+ - ("Press Q to quit" .length () * 3 ), getHeight () - 17 );
540527 }
541528 g .setColor (Color .YELLOW );
542529 g .drawString (time .getTime (), (getWidth () - 58 ), (getHeight () - 3 )); // Displays the current time in yellow in the bottom right corner of the screen (hh:mm:ss)
@@ -549,9 +536,9 @@ public void render() {
549536 }
550537
551538 /*
552- * This method displays information regarding various aspects/stats of the game, dependent upon
553- * whether it is running in developer mode, or if the application is closing.
554- */
539+ * This method displays information regarding various aspects/stats of the game, dependent upon
540+ * whether it is running in developer mode, or if the application is closing.
541+ */
555542 private void status (Graphics g , boolean TerminalMode , boolean TerminalQuit ) {
556543 if (TerminalMode ) { // If running in developer mode
557544 g .setColor (Color .CYAN );
@@ -561,11 +548,10 @@ private void status(Graphics g, boolean TerminalMode, boolean TerminalQuit) {
561548 steps += 1 ;
562549 }
563550 g .drawString ("Foot Steps: " + steps , 0 , 40 ); // Display the number of "Foot Steps" (in cyan, above the previous)
564- g .drawString (
565- "NPC: " + WordUtils .capitalize (String .valueOf (isNpc ())), 0 , // Displays whether the NPC is on the level (in cyan, above the previous)
566- 55 );
551+ g .drawString ("NPC: " + WordUtils .capitalize (String .valueOf (isNpc ())), 0 , // Displays whether the NPC is on the level (in cyan, above the previous)
552+ 55 );
567553 g .drawString ("Mouse: " + getMouse ().getX () + "x |" // Displays the position of the cursor (in cyan, above the previous)
568- + getMouse ().getY () + "y" , 0 , 70 );
554+ + getMouse ().getY () + "y" , 0 , 70 );
569555 if (getMouse ().getButton () != -1 ) // If a mouse button is pressed
570556 g .drawString ("Button: " + getMouse ().getButton (), 0 , 85 ); // Displays the mouse button that is pressed (in cyan, above the previous)
571557 g .setColor (Color .CYAN );
@@ -579,7 +565,7 @@ private void status(Graphics g, boolean TerminalMode, boolean TerminalQuit) {
579565 g .fillRect (0 , 0 , getWidth (), getHeight ()); // Make the screen fully black
580566 g .setColor (Color .RED );
581567 g .drawString ("Shutting down the Game" , (getWidth () / 2 ) - 70 , // Display "Shutting down the Game" in red in the middle of the screen
582- (getHeight () / 2 ) - 8 );
568+ (getHeight () / 2 ) - 8 );
583569 g .dispose (); // Free up memory for graphics
584570 }
585571
0 commit comments