Skip to content

Commit 0238323

Browse files
committed
Refactor code for player positioning and shooting
1 parent ba960b3 commit 0238323

File tree

2 files changed

+27
-91
lines changed

2 files changed

+27
-91
lines changed

src/com/redomar/game/Game.java

Lines changed: 23 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public class Game extends Canvas implements Runnable {
3939
private static final String game_Version = "v1.8.6 Alpha";
4040
private static final int SCALE = 100;
4141
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;
42+
private static final int HEIGHT = (2 * SCALE);
43+
private static final int SCREEN_WIDTH = WIDTH*2;
44+
private static final int SCREEN_HEIGHT = (HEIGHT*2)+30;
4545
private static final String NAME = "Game"; // The name of the JFrame panel
4646
private static final Time time = new Time(); // Represents the calendar's time value, in hh:mm:ss
4747
private static final boolean[] alternateCols = new boolean[2]; // Boolean array describing shirt and face colour
@@ -77,7 +77,7 @@ public class Game extends Canvas implements Runnable {
7777
private final Printer printer = new Printer();
7878
boolean musicPlaying = false;
7979
private int tickCount = 0;
80-
private Screen screen;
80+
private static final Screen screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png"));
8181
private LevelHandler level; // Loads and renders levels along with tiles, entities, projectiles and more.
8282
//The entities of the game
8383
private Player player;
@@ -310,7 +310,6 @@ public void init() {
310310
}
311311
}
312312

313-
screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png"));
314313
screen.setViewPortHeight(SCREEN_HEIGHT);
315314
screen.setViewPortWidth(SCREEN_WIDTH);
316315
input = new InputHandler(this); // Input begins to record key presses
@@ -460,7 +459,7 @@ public void render() {
460459
changeLevel = false;
461460
}
462461

463-
Graphics g = bs.getDrawGraphics();
462+
Graphics2D g = (Graphics2D) bs.getDrawGraphics();
464463
g.drawRect(0, 0, getWidth(), getHeight());
465464
g.drawImage(image, 0, 0, getWidth(), getHeight() - 30, null);
466465
status(g, isDevMode(), isClosing());
@@ -496,13 +495,13 @@ public void render() {
496495
* This method displays information regarding various aspects/stats of the game, dependent upon
497496
* whether it is running in developer mode, or if the application is closing.
498497
*/
499-
private void status(Graphics g, boolean TerminalMode, boolean TerminalQuit) {
500-
int xOffset = (int) player.getX() - (screen.getWidth() / 2);
501-
int yOffset = (int) player.getY() - (screen.getHeight() / 2);
498+
private void status(Graphics2D g, boolean TerminalMode, boolean TerminalQuit) {
499+
int playerAbsX = (int) ((player.getX() - screen.getxOffset()) * 2) + 8;
500+
int playerAbsY = (int) ((player.getY() - screen.getyOffset()) * 2) + 7;
502501
if (TerminalMode) {
503502
// make the background transparent
504503
g.setColor(new Color(0, 0, 0, 100));
505-
g.fillRect(0, 0, 240, 230);
504+
g.fillRect(0, 0, 195, 165);
506505
g.setColor(Color.CYAN);
507506
g.drawString("JavaGame Stats", 0, 10);
508507
g.drawString("FPS/TPS: " + fps + "/" + tps, 0, 25);
@@ -514,84 +513,26 @@ private void status(Graphics g, boolean TerminalMode, boolean TerminalQuit) {
514513
g.drawString("Mouse: " + getMouse().getX() + "x |" + getMouse().getY() + "y", 0, 70);
515514
g.drawString("Mouse: " + (getMouse().getX() - 639 / 2d) + "x |" + (getMouse().getY() - 423 / 2d) + "y", 0, 85);
516515
if (getMouse().getButton() != -1) g.drawString("Button: " + getMouse().getButton(), 0, 100);
517-
g.setColor(Color.CYAN);
518-
g.fillRect(getMouse().getX() - 12, getMouse().getY() - 12, 24, 24);
519-
g.setColor(Color.BLACK);
520-
g.fillRect((639 / 2), (423 / 2) - 10, 2, 10);
521-
g.fillRect((639 / 2), (423 / 2) - 10, 8, 2);
522516
g.setColor(Color.WHITE);
523-
g.fillRect((639 / 2) + 8, (423 / 2) - 10, 2, 2);
517+
g.fillRect(getMouse().getX() - 12, getMouse().getY() - 12, 16, 16);
524518
g.drawString("Player: " + (int) player.getX() + "x |" + (int) player.getY() + "y", 0, 115);
525-
g.drawString("Current Angle: " + Math.atan2((getMouse().getY() - (double) 423 / 2) + 8, (getMouse().getX() - (double) 639 / 2) - 9) * (180.0 / Math.PI) , 0, 130);
526-
527-
double angle = Math.atan2(getMouse().getY() - (player.getY() - yOffset), getMouse().getX() - (player.getX() - xOffset)) * (180.0 / Math.PI);
528-
g.drawString("Angle: " + angle, 0, 145);
529-
530-
// Existing code for drawing the line from the viewport center to the cursor
531-
int originX = 639 / 2 + 8; // Center of the viewport with a slight offset
532-
int originY = 423 / 2 - 9; // Center of the viewport with a slight offset
533-
int cursorX = getMouse().getX();
534-
int cursorY = getMouse().getY();
535-
536-
g.setColor(Color.RED); // Set the color for the original line
537-
((Graphics2D) g).setStroke(new BasicStroke(2)); // Set the stroke
538-
g.drawLine(originX, originY, cursorX, cursorY); // Draw the original line
539-
540-
int playerWorldX = (int) player.getX();
541-
int playerWorldY = (int) player.getY();
542-
543-
int playerScreenX = playerWorldX - xOffset;
544-
int playerScreenY = playerWorldY - yOffset;
545-
546-
int screenWidth = screen.getWidth();
547-
int screenHeight = screen.getHeight();
548-
549-
int actualPlayerScreenX = originX + 2; // Default to originX
550-
int actualPlayerScreenY = originY; // Default to originY
551-
552-
// Adjusting for the X-axis
553-
if (xOffset < 0) {
554-
// Player is to the left of the center
555-
float ratioX = (float) playerWorldX / playerScreenX;
556-
actualPlayerScreenX = (int) (originX * ratioX) + 10;
557-
} else if (xOffset > ((level.getWidth() << 3) - screenWidth)) {
558-
// Player is to the right, near the edge
559-
float ratioX = 2.12f; // Your specific ratio
560-
// Adjust using your ratio and the specific offset adjustment of 3
561-
actualPlayerScreenX = (int) ((originX) - -1 * (screenWidth - (level.getWidth() << 3) + xOffset) * ratioX) + 4;
562-
}
563-
564-
// Adjusting for the Y-axis, applying a similar logic
565-
if (yOffset < 0) {
566-
// Player is above the vertical center
567-
float ratioY = (float) playerWorldY / playerScreenY;
568-
actualPlayerScreenY = (int) (originY * ratioY) + 2;
569-
} else if (yOffset > ((level.getHeight() << 3) - screenHeight)) {
570-
// Player is below, near the bottom edge
571-
float ratioY = 2f; // Using the same ratio for consistency
572-
// Adjust using your ratio and the specific offset adjustment of 3
573-
actualPlayerScreenY = (int) ((originY) - -1 * (screenHeight - (level.getHeight() << 3) + yOffset) * ratioY) - 4 ;
574-
}
575-
576-
577-
578-
519+
double angle = Math.atan2(getMouse().getY() - playerAbsY, getMouse().getX() - playerAbsX) * (180.0 / Math.PI);
520+
g.drawString("Angle: " + angle, 0, 130);
579521

580522
g.setColor(Color.cyan);
581-
g.drawString("Player: " + playerScreenX + "x |" + playerScreenY + "y", 0, 160);
582-
g.drawString("Player Offset: " + xOffset + "x |" + yOffset + "y", 0, 175);
583-
g.drawString("xoffest get x" + screen.getxOffset() + "y" + screen.getyOffset(), 0, 190);
584-
g.setColor(Color.PINK);
585-
g.drawString("Player Screen: " + actualPlayerScreenX + "x |" + actualPlayerScreenY + "y", 0, 205);
586-
523+
g.drawString("Player: \t\t\t\t\t\t\t\t\t\t\t\t" + playerAbsX + "x |" + playerAbsY + "y", 0, 145);
524+
g.drawString("Player Offset: \t" + screen.getxOffset() + "x |" + screen.getyOffset() + "y", 0, 160);
587525

588526
// Set a different color for the player-origin line
527+
g.setStroke(new BasicStroke(1));
589528
g.setColor(Color.GREEN); // Green for the new line from the player's origin
590-
g.drawLine(actualPlayerScreenX, actualPlayerScreenY, cursorX, cursorY); // Draw the line from the player's origin to the cursor
591-
592-
// Reset the stroke if necessary
593-
((Graphics2D) g).setStroke(new BasicStroke(1));
594-
529+
g.drawLine(playerAbsX, playerAbsY, getMouse().getX(), getMouse().getY()); // Draw the line from the player's origin to the cursor
530+
g.setColor(Color.DARK_GRAY);
531+
g.drawLine(getWidth()/2+8, getHeight()/2-8, getMouse().getX(), getMouse().getY()); // Draw the line from the player's origin to the cursor
532+
g.drawLine(getWidth()/2+8, 0, getWidth()/2+8, getHeight()-30);
533+
g.drawLine(0, getHeight()/2-8, getWidth(), getHeight()/2-8);
534+
g.setColor(Color.yellow);
535+
g.fillRect(playerAbsX, playerAbsY, 1, 1);
595536

596537
}
597538
// If the game is shutting off
@@ -621,7 +562,7 @@ public void setVendor(Vendor vendor) {
621562
this.vendor = vendor;
622563
}
623564

624-
public Screen getScreen() {
565+
public static Screen getScreen() {
625566
return screen;
626567
}
627568
}

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,16 @@ public void tick() {
6060
fireRate = Medium.FIRE_RATE;
6161
}
6262
if (!swim.isActive(swimType)) {
63-
// Origin adjustments
64-
int originAdjustX = 8; // Adjust X-coordinate of the origin
65-
int originAdjustY = -9; // Adjust Y-coordinate of the origin
66-
67-
// Origin position
68-
int originX = (640 / 2) + originAdjustX;
69-
int originY = (425 / 2) + originAdjustY;
63+
int playerAbsX = (int) ((getX() - Game.getScreen().getxOffset()) * 2) + 8;
64+
int playerAbsY = (int) ((getY() - Game.getScreen().getyOffset()) * 2) + 7;
7065

7166
// Cursor position
7267
int cursorX = Game.getMouse().getX();
7368
int cursorY = Game.getMouse().getY();
7469

7570
// Calculate differences (dx, dy) between cursor and origin
76-
double dx = cursorX - originX;
77-
double dy = cursorY - originY;
71+
double dx = cursorX - playerAbsX;
72+
double dy = cursorY - playerAbsY;
7873

7974
// Calculate direction using atan2
8075
double dir = Math.atan2(dy, dx);

0 commit comments

Comments
 (0)