2323import java .awt .image .BufferStrategy ;
2424import java .awt .image .BufferedImage ;
2525import java .awt .image .DataBufferInt ;
26+ import java .io .Serial ;
2627
2728/*
2829 * This module forms the core architecture of the JavaGame. It coordinates the various
3334public class Game extends Canvas implements Runnable {
3435
3536 // Setting the size and name of the frame/canvas
37+ @ Serial
3638 private static final long serialVersionUID = 1L ;
3739 private static final String game_Version = "v1.8.6 Alpha" ;
38- private static final int WIDTH = 160 ;
39- private static final int HEIGHT = (WIDTH / 3 * 2 );
40- private static final int SCALE = 3 ;
40+ private static final int SCALE = 100 ;
41+ private static final int WIDTH = 3 * SCALE ;
42+ private static final int HEIGHT = 2 * SCALE ;
43+ private static final int SCREEN_WIDTH = 160 * 4 ;
44+ private static final int SCREEN_HEIGHT = 106 * 4 ;
4145 private static final String NAME = "Game" ; // The name of the JFrame panel
4246 private static final Time time = new Time (); // Represents the calendar's time value, in hh:mm:ss
4347 private static final boolean [] alternateCols = new boolean [2 ]; // Boolean array describing shirt and face colour
@@ -87,9 +91,9 @@ public Game() {
8791 context = InputContext .getInstance ();
8892
8993 // The game can only be played in one distinct window size
90- setMinimumSize (new Dimension (WIDTH * SCALE , HEIGHT * SCALE ));
91- setMaximumSize (new Dimension (WIDTH * SCALE , HEIGHT * SCALE ));
92- setPreferredSize (new Dimension (WIDTH * SCALE , HEIGHT * SCALE ));
94+ setMinimumSize (new Dimension (SCREEN_WIDTH , SCREEN_HEIGHT ));
95+ setMaximumSize (new Dimension (SCREEN_WIDTH , SCREEN_HEIGHT ));
96+ setPreferredSize (new Dimension (SCREEN_WIDTH , SCREEN_HEIGHT ));
9397
9498 setFrame (new JFrame (NAME )); // Creates the frame with a defined name
9599 getFrame ().setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE ); // Exits the program when user closes the frame
@@ -307,6 +311,8 @@ public void init() {
307311 }
308312
309313 screen = new Screen (WIDTH , HEIGHT , new SpriteSheet ("/sprite_sheet.png" ));
314+ screen .setViewPortHeight (SCREEN_HEIGHT );
315+ screen .setViewPortWidth (SCREEN_WIDTH );
310316 input = new InputHandler (this ); // Input begins to record key presses
311317 setMouse (new MouseHandler (this )); // Mouse tracking and clicking is now recorded
312318// setWindow(new WindowHandler(this));
@@ -417,6 +423,7 @@ public void render() {
417423 level .renderEntities (screen );
418424 level .renderProjectileEntities (screen );
419425
426+
420427 for (int y = 0 ; y < screen .getHeight (); y ++) {
421428 for (int x = 0 ; x < screen .getWidth (); x ++) {
422429 int colourCode = screen .getPixels ()[x + y * screen .getWidth ()];
@@ -489,7 +496,12 @@ public void render() {
489496 * whether it is running in developer mode, or if the application is closing.
490497 */
491498 private void status (Graphics g , boolean TerminalMode , boolean TerminalQuit ) {
499+ int xOffset = (int ) player .getX () - (screen .getWidth () / 2 );
500+ int yOffset = (int ) player .getY () - (screen .getHeight () / 2 );
492501 if (TerminalMode ) {
502+ // make the background transparent
503+ g .setColor (new Color (0 , 0 , 0 , 100 ));
504+ g .fillRect (0 , 0 , 240 , 230 );
493505 g .setColor (Color .CYAN );
494506 g .drawString ("JavaGame Stats" , 0 , 10 );
495507 g .drawString ("FPS/TPS: " + fps + "/" + tps , 0 , 25 );
@@ -499,9 +511,87 @@ private void status(Graphics g, boolean TerminalMode, boolean TerminalQuit) {
499511 g .drawString ("Foot Steps: " + steps , 0 , 40 );
500512 g .drawString ("NPC: " + WordUtils .capitalize (String .valueOf (isNpc ())), 0 , 55 );
501513 g .drawString ("Mouse: " + getMouse ().getX () + "x |" + getMouse ().getY () + "y" , 0 , 70 );
502- if (getMouse ().getButton () != -1 ) g .drawString ("Button: " + getMouse ().getButton (), 0 , 85 );
514+ g .drawString ("Mouse: " + (getMouse ().getX () - 639 / 2d ) + "x |" + (getMouse ().getY () - 423 / 2d ) + "y" , 0 , 85 );
515+ if (getMouse ().getButton () != -1 ) g .drawString ("Button: " + getMouse ().getButton (), 0 , 100 );
503516 g .setColor (Color .CYAN );
504517 g .fillRect (getMouse ().getX () - 12 , getMouse ().getY () - 12 , 24 , 24 );
518+ g .setColor (Color .BLACK );
519+ g .fillRect ((639 / 2 ), (423 / 2 ) - 10 , 2 , 10 );
520+ g .fillRect ((639 / 2 ), (423 / 2 ) - 10 , 8 , 2 );
521+ g .setColor (Color .WHITE );
522+ g .fillRect ((639 / 2 ) + 8 , (423 / 2 ) - 10 , 2 , 2 );
523+ g .drawString ("Player: " + (int ) player .getX () + "x |" + (int ) player .getY () + "y" , 0 , 115 );
524+ g .drawString ("Current Angle: " + Math .atan2 ((getMouse ().getY () - (double ) 423 / 2 ) + 8 , (getMouse ().getX () - (double ) 639 / 2 ) - 9 ) * (180.0 / Math .PI ) , 0 , 130 );
525+
526+ double angle = Math .atan2 (getMouse ().getY () - (player .getY () - yOffset ), getMouse ().getX () - (player .getX () - xOffset )) * (180.0 / Math .PI );
527+ g .drawString ("Angle: " + angle , 0 , 145 );
528+
529+ // Existing code for drawing the line from the viewport center to the cursor
530+ int originX = 639 / 2 + 8 ; // Center of the viewport with a slight offset
531+ int originY = 423 / 2 - 9 ; // Center of the viewport with a slight offset
532+ int cursorX = getMouse ().getX ();
533+ int cursorY = getMouse ().getY ();
534+
535+ g .setColor (Color .RED ); // Set the color for the original line
536+ ((Graphics2D ) g ).setStroke (new BasicStroke (2 )); // Set the stroke
537+ g .drawLine (originX , originY , cursorX , cursorY ); // Draw the original line
538+
539+ int playerWorldX = (int ) player .getX ();
540+ int playerWorldY = (int ) player .getY ();
541+
542+ int playerScreenX = playerWorldX - xOffset ;
543+ int playerScreenY = playerWorldY - yOffset ;
544+
545+ int screenWidth = screen .getWidth ();
546+ int screenHeight = screen .getHeight ();
547+
548+ int actualPlayerScreenX = originX + 2 ; // Default to originX
549+ int actualPlayerScreenY = originY ; // Default to originY
550+
551+ // Adjusting for the X-axis
552+ if (xOffset < 0 ) {
553+ // Player is to the left of the center
554+ float ratioX = (float ) playerWorldX / playerScreenX ;
555+ actualPlayerScreenX = (int ) (originX * ratioX ) + 10 ;
556+ } else if (xOffset > ((level .getWidth () << 3 ) - screenWidth )) {
557+ // Player is to the right, near the edge
558+ float ratioX = 2.12f ; // Your specific ratio
559+ // Adjust using your ratio and the specific offset adjustment of 3
560+ actualPlayerScreenX = (int ) ((originX ) - -1 * (screenWidth - (level .getWidth () << 3 ) + xOffset ) * ratioX ) + 4 ;
561+ }
562+
563+ // Adjusting for the Y-axis, applying a similar logic
564+ if (yOffset < 0 ) {
565+ // Player is above the vertical center
566+ float ratioY = (float ) playerWorldY / playerScreenY ;
567+ actualPlayerScreenY = (int ) (originY * ratioY ) + 2 ;
568+ } else if (yOffset > ((level .getHeight () << 3 ) - screenHeight )) {
569+ // Player is below, near the bottom edge
570+ float ratioY = 2f ; // Using the same ratio for consistency
571+ // Adjust using your ratio and the specific offset adjustment of 3
572+ actualPlayerScreenY = (int ) ((originY ) - -1 * (screenHeight - (level .getHeight () << 3 ) + yOffset ) * ratioY ) - 4 ;
573+ }
574+
575+
576+
577+
578+
579+ g .setColor (Color .cyan );
580+ g .drawString ("Player: " + playerScreenX + "x |" + playerScreenY + "y" , 0 , 160 );
581+ g .drawString ("Player Offset: " + xOffset + "x |" + yOffset + "y" , 0 , 175 );
582+ g .drawString ("xoffest get x" + screen .getxOffset () + "y" + screen .getyOffset (), 0 , 190 );
583+ g .setColor (Color .PINK );
584+ g .drawString ("Player Screen: " + actualPlayerScreenX + "x |" + actualPlayerScreenY + "y" , 0 , 205 );
585+
586+
587+ // Set a different color for the player-origin line
588+ g .setColor (Color .GREEN ); // Green for the new line from the player's origin
589+ g .drawLine (actualPlayerScreenX , actualPlayerScreenY , cursorX , cursorY ); // Draw the line from the player's origin to the cursor
590+
591+ // Reset the stroke if necessary
592+ ((Graphics2D ) g ).setStroke (new BasicStroke (1 ));
593+
594+
505595 }
506596 // If the game is shutting off
507597 if (!TerminalQuit ) {
@@ -530,4 +620,7 @@ public void setVendor(Vendor vendor) {
530620 this .vendor = vendor ;
531621 }
532622
623+ public Screen getScreen () {
624+ return screen ;
625+ }
533626}
0 commit comments