Skip to content

Commit 8fc2b4c

Browse files
committed
Added simple lighting graphics - Night
1 parent 0238323 commit 8fc2b4c

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

src/com/redomar/game/Game.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.redomar.game.event.MouseHandler;
1010
import com.redomar.game.gfx.Screen;
1111
import com.redomar.game.gfx.SpriteSheet;
12+
import com.redomar.game.gfx.lighting.Night;
1213
import com.redomar.game.level.LevelHandler;
1314
import com.redomar.game.lib.Either;
1415
import com.redomar.game.lib.Font;
@@ -496,8 +497,6 @@ public void render() {
496497
* whether it is running in developer mode, or if the application is closing.
497498
*/
498499
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;
501500
if (TerminalMode) {
502501
// make the background transparent
503502
g.setColor(new Color(0, 0, 0, 100));
@@ -516,23 +515,24 @@ private void status(Graphics2D g, boolean TerminalMode, boolean TerminalQuit) {
516515
g.setColor(Color.WHITE);
517516
g.fillRect(getMouse().getX() - 12, getMouse().getY() - 12, 16, 16);
518517
g.drawString("Player: " + (int) player.getX() + "x |" + (int) player.getY() + "y", 0, 115);
519-
double angle = Math.atan2(getMouse().getY() - playerAbsY, getMouse().getX() - playerAbsX) * (180.0 / Math.PI);
518+
double angle = Math.atan2(getMouse().getY() - player.getPlayerAbsY(), getMouse().getX() - player.getPlayerAbsX()) * (180.0 / Math.PI);
520519
g.drawString("Angle: " + angle, 0, 130);
521520

522521
g.setColor(Color.cyan);
523-
g.drawString("Player: \t\t\t\t\t\t\t\t\t\t\t\t" + playerAbsX + "x |" + playerAbsY + "y", 0, 145);
522+
g.drawString("Player: \t\t\t\t\t\t\t\t\t\t\t\t" + player.getPlayerAbsX() + "x |" + player.getPlayerAbsY() + "y", 0, 145);
524523
g.drawString("Player Offset: \t" + screen.getxOffset() + "x |" + screen.getyOffset() + "y", 0, 160);
525524

526-
// Set a different color for the player-origin line
525+
// Set a different color for the player-origin line
527526
g.setStroke(new BasicStroke(1));
528527
g.setColor(Color.GREEN); // Green for the new line from the player's origin
529-
g.drawLine(playerAbsX, playerAbsY, getMouse().getX(), getMouse().getY()); // Draw the line from the player's origin to the cursor
528+
g.drawLine(player.getPlayerAbsX(), player.getPlayerAbsY(), getMouse().getX(), getMouse().getY()); // Draw the line from the player's origin to the cursor
530529
g.setColor(Color.DARK_GRAY);
531530
g.drawLine(getWidth()/2+8, getHeight()/2-8, getMouse().getX(), getMouse().getY()); // Draw the line from the player's origin to the cursor
532531
g.drawLine(getWidth()/2+8, 0, getWidth()/2+8, getHeight()-30);
533532
g.drawLine(0, getHeight()/2-8, getWidth(), getHeight()/2-8);
534533
g.setColor(Color.yellow);
535-
g.fillRect(playerAbsX, playerAbsY, 1, 1);
534+
g.fillRect(player.getPlayerAbsX(), player.getPlayerAbsY(), 1, 1);
535+
new Night(g, screen).render(player.getPlayerAbsX(), player.getPlayerAbsY());
536536

537537
}
538538
// If the game is shutting off

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public class Player extends Mob {
1919
private static double speed = 1;
2020
private final InputHandler inputHandler;
2121
private int fireRate;
22+
private int playerAbsX;
23+
private int playerAbsY;
2224

2325
public Player(LevelHandler level, int x, int y, InputHandler inputHandler, String name, int shirtColour, int faceColour) {
2426
super(level, "Player", x, y, PLAYER_TILE, speed, COLLISION_BORDERS, shirtColour, faceColour);
@@ -32,6 +34,11 @@ public void tick() {
3234
double xa = 0;
3335
double ya = 0;
3436

37+
// Calculate and set player's absolute X and Y positions
38+
setPlayerAbsX((int) ((getX() - Game.getScreen().getxOffset()) * 2) + 8);
39+
setPlayerAbsY((int) ((getY() - Game.getScreen().getyOffset()) * 2) + 7);
40+
41+
3542
if (inputHandler != null) {
3643

3744
speed = inputHandler.getSHIFTED().isPressed() ? 2 : 1;
@@ -60,8 +67,6 @@ public void tick() {
6067
fireRate = Medium.FIRE_RATE;
6168
}
6269
if (!swim.isActive(swimType)) {
63-
int playerAbsX = (int) ((getX() - Game.getScreen().getxOffset()) * 2) + 8;
64-
int playerAbsY = (int) ((getY() - Game.getScreen().getyOffset()) * 2) + 7;
6570

6671
// Cursor position
6772
int cursorX = Game.getMouse().getX();
@@ -119,4 +124,19 @@ public String getSanitisedUsername() {
119124
return this.name;
120125
}
121126

127+
public int getPlayerAbsX() {
128+
return playerAbsX;
129+
}
130+
131+
public void setPlayerAbsX(int playerAbsX) {
132+
this.playerAbsX = playerAbsX;
133+
}
134+
135+
public int getPlayerAbsY() {
136+
return playerAbsY;
137+
}
138+
139+
public void setPlayerAbsY(int playerAbsY) {
140+
this.playerAbsY = playerAbsY;
141+
}
122142
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.redomar.game.gfx.lighting;
2+
3+
import com.redomar.game.gfx.Screen;
4+
5+
import java.awt.*;
6+
import java.awt.geom.Area;
7+
import java.awt.geom.Ellipse2D;
8+
9+
public class Night {
10+
11+
private final Graphics2D g;
12+
private final Screen screen;
13+
14+
public Night(Graphics2D g, Screen screen) {
15+
this.g = g;
16+
this.screen = screen;
17+
}
18+
19+
public void render(int playerX, int playerY) {
20+
double size = 160;
21+
22+
g.setColor(new Color(0f, 0f, 0f, 0.8f));
23+
Shape shape = new Rectangle(0, 0, screen.getViewPortWidth(), screen.getViewPortHeight() - 30);
24+
Shape circle = new Ellipse2D.Double(playerX - size / 2, playerY - size / 2, size, size);
25+
Area area = new Area(shape);
26+
area.subtract(new Area(circle));
27+
g.fill(area);
28+
}
29+
}

0 commit comments

Comments
 (0)