Skip to content

감옥 인터랙션 마무리 #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions client/src/main/java/org/kunp/map/MapComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ public void focusGained(FocusEvent e) {
moveTimer.start();

serverCommunicator.addMessageListener(message -> {
System.out.println(message);
String[] tokens = message.split("\\|");
String type = tokens[0];
if(type.equals("210") || type.equals("211") || type.equals("212")){
if(type.equals("210")){
String mover_sessionId = tokens[1];
int x = Integer.parseInt(tokens[2]), y = Integer.parseInt(tokens[3]);
int mapIdx = Integer.parseInt(tokens[4]);
Expand All @@ -70,6 +69,20 @@ public void focusGained(FocusEvent e) {
else locations.get(mover_sessionId).setLocation(role, mapIdx, x, y);
}
repaint();
}
else if(type.equals("211") || type.equals("212")){
String target_sessionId = tokens[1];
int x = Integer.parseInt(tokens[2]), y = Integer.parseInt(tokens[3]);
int mapIdx = Integer.parseInt(tokens[4]);
int role = Integer.parseInt(tokens[6]);
synchronized (locations){
if(!locations.containsKey(target_sessionId)) locations.put(target_sessionId, new Location(role, mapIdx, x, y));
else locations.get(target_sessionId).setLocation(role, mapIdx, x, y);
}
if(target_sessionId.equals(this.sessionId)){
setView(mapIdx, x, y);
}
repaint();
}else if(type.equals("213")){
String overed_gameId = tokens[1];
String result = Integer.parseInt(tokens[2]) == 1 ? "도망자 승리" : "술래 승리";
Expand Down Expand Up @@ -176,4 +189,15 @@ private void moveMap(int x, int y, int portal) {
revalidate();
requestFocusInWindow();
}

private void setView(int mapIdx, int x, int y){
remove(maps[currentMapX][currentMapY]);
currentMapX = 0;
currentMapY = 0;
player.setMapIdx(mapIdx);
player.setLoc(x, y);
add(maps[currentMapX][currentMapY], BorderLayout.CENTER);
revalidate();
requestFocusInWindow();
}
}
6 changes: 6 additions & 0 deletions client/src/main/java/org/kunp/map/PlayerComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public void setMapIdx(int mapIdx) {
this.mapIdx = mapIdx;
}

public void setLoc(int x, int y) {
this.x = x;
this.y = y;
sendLocation();
}

public void move(int dx, int dy) {
x += dx;
y += dy;
Expand Down
17 changes: 11 additions & 6 deletions server/src/main/java/org/kunp/Servlet/game/GameContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class GameContext {
private static final int RUNNER = 1;
private static final int JAIL_X = 0; // 감옥 x 좌표
private static final int JAIL_Y = 0; // 감옥 y 좌표
private static final int TOUCHDOWN_X = 70;
private static final int TOUCHDOWN_Y = 60;
private static final int ESCAPE_X = 0;
private static final int ESCAPE_Y = 70;
private static final int JAIL_ROOM_NUMBER = 1; // 감옥 roomNumber

// Connection 관련 객체
Expand Down Expand Up @@ -258,7 +262,7 @@ private void updateClientPosition(String sessionId, int x, int y, int roomId, in

private void handleRunnerInteraction(String sessionId, int roomNumber) throws IOException {
int[] runnerPos = positions.get(sessionId);
if(isNearJail(runnerPos)) {
if(isNearTouchDown(runnerPos)) {
releaseRunnersInJail(roomNumber);
}
}
Expand All @@ -269,7 +273,8 @@ private void releaseRunnersInJail(int roomNumber) {
.forEach(e -> {
playerStates.put(e.getKey(), false);
int[] targetPos = positions.get(e.getKey());
String response = createFreedResponse(e.getKey(), targetPos[0], targetPos[1], roomNumber, gameId, targetPos[3]);
positions.put(e.getKey(), new int[]{0, 50, roomNumber, targetPos[3]}); // 감옥 위치
String response = createFreedResponse(e.getKey(), ESCAPE_X, ESCAPE_Y, roomNumber, gameId, targetPos[3]);
sendMessageToAll(response);
});
}
Expand All @@ -285,16 +290,16 @@ private void handleChaserInteraction(String id, int roomNumber) throws IOExcepti
if (!isRunner(targetId) || isPlayerDisabled(targetId)) continue; // 술래거나 잡힌 사용자 제외
if (isAvailable(chaserPos, targetPos)) {
// 도망자를 감옥으로 이동
positions.put(targetId, new int[]{JAIL_X, JAIL_Y, roomNumber, targetPos[3]}); // 감옥 위치
positions.put(targetId, new int[]{JAIL_X, JAIL_Y, JAIL_ROOM_NUMBER, targetPos[3]}); // 감옥 위치
playerStates.put(targetId, true); // 상태 업데이트 (갇힌 상태)
sendMessageToAll(createCaughtResponse(targetId,roomNumber, gameId, targetPos[3]));
sendMessageToAll(createCaughtResponse(targetId,JAIL_ROOM_NUMBER, gameId, targetPos[3]));
}
}
}

private boolean isNearJail(int[] runnerPos) {
private boolean isNearTouchDown(int[] runnerPos) {
// 감옥 근처에 있는지 확인 (JAIL_X, JAIL_Y 기준)
return Math.abs(runnerPos[0] - JAIL_X) < 10 && Math.abs(runnerPos[1] - JAIL_Y) < 10;
return Math.abs(runnerPos[0] - TOUCHDOWN_X) < 10 && Math.abs(runnerPos[1] - TOUCHDOWN_Y) < 10;
}

public void determineWinner() {
Expand Down
Loading