Skip to content

Commit 72e3fa0

Browse files
committed
Adding monitoring lines for player debug
1 parent 41d217e commit 72e3fa0

File tree

3 files changed

+149
-9
lines changed

3 files changed

+149
-9
lines changed

src/com/redomar/game/Game.java

Lines changed: 100 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.awt.image.BufferStrategy;
2424
import java.awt.image.BufferedImage;
2525
import 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
@@ -33,11 +34,14 @@
3334
public 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
}

src/com/redomar/game/entities/Player.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.redomar.game.event.InputHandler;
99
import com.redomar.game.level.LevelHandler;
1010
import com.redomar.game.lib.HashGen;
11+
import com.redomar.game.log.PrintTypes;
1112

1213
import java.util.Objects;
1314

@@ -60,10 +61,29 @@ public void tick() {
6061
fireRate = Medium.FIRE_RATE;
6162
}
6263
if (!swim.isActive(swimType)) {
63-
double dx = Game.getMouse().getX() - 480 / 2d;
64-
double dy = Game.getMouse().getY() - 320 / 2d;
64+
// Origin adjustments
65+
int originAdjustX = 8; // Adjust X-coordinate of the origin
66+
int originAdjustY = -9; // Adjust Y-coordinate of the origin
67+
68+
// Origin position
69+
int originX = (640 / 2) + originAdjustX;
70+
int originY = (425 / 2) + originAdjustY;
71+
72+
// Cursor position
73+
int cursorX = Game.getMouse().getX();
74+
int cursorY = Game.getMouse().getY();
75+
76+
// Calculate differences (dx, dy) between cursor and origin
77+
double dx = cursorX - originX;
78+
double dy = cursorY - originY;
79+
80+
// Calculate direction using atan2
6581
double dir = Math.atan2(dy, dx);
82+
83+
// Continue with shooting logic
6684
shoot(x, y, dir, Game.getMouse().getButton());
85+
86+
entityPrinter.cast().print("Direction: " + dir + \t" + dx + "x\t" + dy + "y", PrintTypes.ERROR);
6787
}
6888
}
6989
}

src/com/redomar/game/gfx/Screen.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public class Screen {
1414
private int width;
1515
private int height;
1616

17+
private int viewPortWidth;
18+
private int viewPortHeight;
19+
1720
/**
1821
* Constructs the draw area
1922
*
@@ -108,6 +111,14 @@ public void setOffset(int xOffset, int yOffset) {
108111
this.yOffset = yOffset;
109112
}
110113

114+
public int getxOffset() {
115+
return xOffset;
116+
}
117+
118+
public int getyOffset() {
119+
return yOffset;
120+
}
121+
111122
public int getWidth() {
112123
return width;
113124
}
@@ -131,4 +142,20 @@ public int getHeight() {
131142
public void setHeight(int height) {
132143
this.height = height;
133144
}
145+
146+
public int getViewPortWidth() {
147+
return viewPortWidth;
148+
}
149+
150+
public void setViewPortWidth(int viewPortWidth) {
151+
this.viewPortWidth = viewPortWidth;
152+
}
153+
154+
public int getViewPortHeight() {
155+
return viewPortHeight;
156+
}
157+
158+
public void setViewPortHeight(int viewPortHeight) {
159+
this.viewPortHeight = viewPortHeight;
160+
}
134161
}

0 commit comments

Comments
 (0)