feat: Player reach and some player configs
This commit is contained in:
11
src/main/java/cz/jzitnik/config/PlayerConfig.java
Normal file
11
src/main/java/cz/jzitnik/config/PlayerConfig.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package cz.jzitnik.config;
|
||||
|
||||
import cz.jzitnik.annotations.Config;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@Config
|
||||
public class PlayerConfig {
|
||||
private final double playerReach = 20;
|
||||
private final int playerMoveDistance = 3;
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import cz.jzitnik.annotations.injectors.InjectConfig;
|
||||
import cz.jzitnik.annotations.injectors.InjectDependency;
|
||||
import cz.jzitnik.annotations.injectors.InjectState;
|
||||
import cz.jzitnik.config.Debugging;
|
||||
import cz.jzitnik.config.PlayerConfig;
|
||||
import cz.jzitnik.events.MouseAction;
|
||||
import cz.jzitnik.events.MouseMoveEvent;
|
||||
import cz.jzitnik.events.RerenderScreen;
|
||||
@@ -55,6 +56,24 @@ public class MouseMoveEventHandler extends AbstractEventHandler<MouseMoveEvent>
|
||||
@InjectConfig
|
||||
private Debugging debugging;
|
||||
|
||||
@InjectConfig
|
||||
private PlayerConfig playerConfig;
|
||||
|
||||
private double distancePointToRect(
|
||||
double px, double py,
|
||||
double left, double top,
|
||||
double right, double bottom) {
|
||||
|
||||
// Clamp point to rectangle
|
||||
double closestX = Math.max(left, Math.min(px, right));
|
||||
double closestY = Math.max(top, Math.min(py, bottom));
|
||||
|
||||
double dx = px - closestX;
|
||||
double dy = py - closestY;
|
||||
|
||||
return Math.sqrt(dx * dx + dy * dy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(MouseMoveEvent event) {
|
||||
MouseAction mouseAction = event.getMouseAction();
|
||||
@@ -63,6 +82,10 @@ public class MouseMoveEventHandler extends AbstractEventHandler<MouseMoveEvent>
|
||||
|
||||
GameRoom currentRoom = gameState.getCurrentRoom();
|
||||
BufferedImage room = resourceManager.getResource(currentRoom.getTexture());
|
||||
Player player = gameState.getPlayer();
|
||||
RoomCords playerCords = player.getPlayerCords();
|
||||
BufferedImage playerTexture = RerenderUtils.getPlayer(resourceManager, player);
|
||||
|
||||
var start = RerenderUtils.getStart(room, terminalState.getTerminalScreen().getTerminalSize());
|
||||
int startX = start.getX();
|
||||
int startY = start.getY();
|
||||
@@ -71,11 +94,32 @@ public class MouseMoveEventHandler extends AbstractEventHandler<MouseMoveEvent>
|
||||
if (!gameObject.isSelectable()) return false;
|
||||
BufferedImage texture = gameObject.getTexture();
|
||||
RoomCords cords = gameObject.getCords();
|
||||
|
||||
int relativeMouseX = mouseX - startX;
|
||||
int relativeMouseY = (mouseY * 2 - startY);
|
||||
int objXStart = cords.getX();
|
||||
int objYStart = cords.getY();
|
||||
int objXEnd = cords.getX() + texture.getWidth();
|
||||
int objYEnd = cords.getY() + texture.getHeight();
|
||||
|
||||
int playerMiddleX = playerCords.getX() + (playerTexture.getWidth() / 2);
|
||||
int playerMiddleY = playerCords.getY() + (playerTexture.getHeight() / 3); // This is not middle but whatever
|
||||
|
||||
double distance = distancePointToRect(
|
||||
playerMiddleX, playerMiddleY,
|
||||
objXStart, objYStart,
|
||||
objXEnd, objYEnd
|
||||
);
|
||||
|
||||
if (distance > playerConfig.getPlayerReach()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return
|
||||
mouseX - startX >= cords.getX() &&
|
||||
mouseX - startX < cords.getX() + texture.getWidth() &&
|
||||
(mouseY * 2 - startY) >= cords.getY() &&
|
||||
(mouseY * 2 - startY) < cords.getY() + texture.getHeight();
|
||||
relativeMouseX >= cords.getX() &&
|
||||
relativeMouseX < cords.getX() + texture.getWidth() &&
|
||||
relativeMouseY >= cords.getY() &&
|
||||
relativeMouseY < cords.getY() + texture.getHeight();
|
||||
}).collect(Collectors.toSet());
|
||||
|
||||
Set<GameObject> changedObjects = new HashSet<>();
|
||||
@@ -95,8 +139,6 @@ public class MouseMoveEventHandler extends AbstractEventHandler<MouseMoveEvent>
|
||||
for (GameObject object : changedObjects) {
|
||||
RoomCords cords = object.getCords();
|
||||
BufferedImage objectTexture = object.getTexture();
|
||||
Player player = gameState.getPlayer();
|
||||
BufferedImage playerTexture = RerenderUtils.getPlayer(resourceManager, player);
|
||||
|
||||
int forStartX = cords.getX();
|
||||
int forStartY = cords.getY();
|
||||
|
||||
@@ -7,6 +7,7 @@ import cz.jzitnik.annotations.injectors.InjectConfig;
|
||||
import cz.jzitnik.annotations.injectors.InjectDependency;
|
||||
import cz.jzitnik.annotations.injectors.InjectState;
|
||||
import cz.jzitnik.config.Debugging;
|
||||
import cz.jzitnik.config.PlayerConfig;
|
||||
import cz.jzitnik.events.PlayerMoveEvent;
|
||||
import cz.jzitnik.events.RerenderScreen;
|
||||
import cz.jzitnik.events.RoomChangeEvent;
|
||||
@@ -50,6 +51,9 @@ public class PlayerMoveEventHandler extends AbstractEventHandler<PlayerMoveEvent
|
||||
@InjectConfig
|
||||
private Debugging debugging;
|
||||
|
||||
@InjectConfig
|
||||
private PlayerConfig playerConfig;
|
||||
|
||||
@Override
|
||||
public void handle(PlayerMoveEvent event) {
|
||||
if (gameState.isTerminalTooSmall()) {
|
||||
@@ -61,7 +65,7 @@ public class PlayerMoveEventHandler extends AbstractEventHandler<PlayerMoveEvent
|
||||
RoomCords playerCords = player.getPlayerCords();
|
||||
GameRoom currentRoom = gameState.getCurrentRoom();
|
||||
|
||||
int moveStep = 3;
|
||||
int moveStep = playerConfig.getPlayerMoveDistance();
|
||||
|
||||
int originalPlayerX = playerCords.getX();
|
||||
int originalPlayerY = playerCords.getY();
|
||||
|
||||
Reference in New Issue
Block a user