Skip to content
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

Sound Detector #210

Merged
merged 8 commits into from
May 15, 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
1 change: 1 addition & 0 deletions TestServer/plugins/TF_PrisonEscape/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ RareItemsProbability: 0.25
ChestSize: 5
HelicopterSpawnDelay: 10
HelicopterDepartureDelay: 5
SoundDetectorRange: 2.5
AvailableLanguages:
- "english"
DefaultLanguage: "english"
Expand Down
5 changes: 4 additions & 1 deletion TestServer/plugins/TF_PrisonEscape/languages/english.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Scoreboard:
LastLine: "&eTest Server"
GuardSideBar:
BalanceLine: "&fBalance: &a{BALANCE}"
SoundDectorLine: "&fSound Detectors: "
Items:
SelectPrisionerTeam:
Name: "&6Select prisioner team"
Expand Down Expand Up @@ -81,7 +82,7 @@ Items:
Name: "Search"
Radar:
Name: "Radar"
Sensor:
SoundDetector:
Name: "Sensor"
Camera:
Name: "Camera"
Expand Down Expand Up @@ -138,6 +139,8 @@ Messages:
CameraPlaced: "&aCamera placed successfully. Use your camera viewer to watch what is going on."
NoCamerasPlaced: "&cThere aren't cameras placed. Buy some from the shop."
SneakToLeaveCamera: "&eSneak to leave the camera"
SoundDetectorPlaced: "&aSound detector placed sucessfully. Check if there are players walking by in the sidebar."
InvalidSoundDetectorLoc: "&cSound detector can not be placed here. &eTry placing it on directly on the ground."
Announcements:
GameStarting:
- "&6==================================="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.tiagofar78.prisonescape.items.EnergyDrinkItem;
import net.tiagofar78.prisonescape.items.Item;
import net.tiagofar78.prisonescape.items.RadarItem;
import net.tiagofar78.prisonescape.items.SensorItem;
import net.tiagofar78.prisonescape.items.SoundDetectorItem;
import net.tiagofar78.prisonescape.items.TrapItem;
import net.tiagofar78.prisonescape.managers.MessageLanguageManager;

Expand Down Expand Up @@ -189,7 +189,7 @@ public static void openShop(String playerName) {

shopMenu.setItem(0, new EnergyDrinkItem().toItemStack(messages));
shopMenu.setItem(1, new TrapItem().toItemStack(messages));
shopMenu.setItem(2, new SensorItem().toItemStack(messages));
shopMenu.setItem(2, new SoundDetectorItem().toItemStack(messages));
shopMenu.setItem(3, new CameraItem().toItemStack(messages));
shopMenu.setItem(4, new RadarItem().toItemStack(messages));

Expand Down
34 changes: 31 additions & 3 deletions src/main/java/net/tiagofar78/prisonescape/game/Guard.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import net.tiagofar78.prisonescape.game.prisonbuilding.PrisonEscapeLocation;
import net.tiagofar78.prisonescape.items.CameraItem;
import net.tiagofar78.prisonescape.items.Item;
import net.tiagofar78.prisonescape.items.SensorItem;
import net.tiagofar78.prisonescape.items.SoundDetectorItem;
import net.tiagofar78.prisonescape.items.TrapItem;
import net.tiagofar78.prisonescape.managers.ConfigManager;
import net.tiagofar78.prisonescape.managers.GameManager;
import net.tiagofar78.prisonescape.managers.MessageLanguageManager;

import org.bukkit.GameMode;
Expand All @@ -17,6 +18,7 @@
public class Guard extends PrisonEscapePlayer {

private static final int BALANCE_LINE_INDEX = 1;
private static final int SOUND_DETECTORS_FIRST_LINE_INDEX = 3;

private int _balance;

Expand Down Expand Up @@ -82,7 +84,7 @@ private boolean canBuyItem(Item item) {
return false;
} else if (item instanceof CameraItem && _numOfCamerasBought >= ((CameraItem) item).getLimit()) {
return false;
} else if (item instanceof SensorItem && _numOfSensorsBought >= ((SensorItem) item).getLimit()) {
} else if (item instanceof SoundDetectorItem && _numOfSensorsBought >= ((SoundDetectorItem) item).getLimit()) {
return false;
}
return true;
Expand All @@ -93,7 +95,7 @@ private void updateItemCount(Item item) {
_numOfTrapsBought++;
} else if (item instanceof CameraItem) {
_numOfCamerasBought++;
} else if (item instanceof SensorItem) {
} else if (item instanceof SoundDetectorItem) {
_numOfSensorsBought++;
}
}
Expand Down Expand Up @@ -154,4 +156,30 @@ private void updateBalanceLine() {
_scoreboardData.updateLine(BALANCE_LINE_INDEX, balanceLine);
}

public void addSoundDetectorLine(int value) {
int soundDetectorsAmount = GameManager.getGame().getPrison().countSoundDetectors();
if (soundDetectorsAmount == 0) {
String emptyLine = "§a";
_scoreboardData.addLine(SOUND_DETECTORS_FIRST_LINE_INDEX, emptyLine);

MessageLanguageManager messages = MessageLanguageManager.getInstanceByPlayer(getName());
String line = messages.getGuardSideBarSoundDetectorLine();
_scoreboardData.addLine(SOUND_DETECTORS_FIRST_LINE_INDEX, line);
}

String soundDetectorValueLine = createSoundDetectorValueLine(soundDetectorsAmount + 1, value);
_scoreboardData.addLine(SOUND_DETECTORS_FIRST_LINE_INDEX + 1 + soundDetectorsAmount, soundDetectorValueLine);
}

public void updateSoundDetectorValue(int index, int value) {
String soundDetectorValueLine = createSoundDetectorValueLine(index + 1, value);
_scoreboardData.updateLine(SOUND_DETECTORS_FIRST_LINE_INDEX + 1 + index, soundDetectorValueLine);
}

private String createSoundDetectorValueLine(int index, int value) {
// TODO improve how this looks
String valueBars = value == 10 ? "§c|||§e||||§a|||" : "§8||||||||||";
return "§f" + index + ": " + valueBars;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.tiagofar78.prisonescape.game.prisonbuilding.PrisonBuilding;
import net.tiagofar78.prisonescape.game.prisonbuilding.PrisonEscapeLocation;
import net.tiagofar78.prisonescape.game.prisonbuilding.Regenerable;
import net.tiagofar78.prisonescape.game.prisonbuilding.SoundDetector;
import net.tiagofar78.prisonescape.game.prisonbuilding.Vault;
import net.tiagofar78.prisonescape.game.prisonbuilding.WallCrack;
import net.tiagofar78.prisonescape.game.prisonbuilding.doors.ClickDoorReturnAction;
Expand Down Expand Up @@ -241,6 +242,10 @@ public boolean isPrisioner(PrisonEscapePlayer player) {
return player.isPrisioner();
}

public PrisonEscapeTeam<Guard> getGuardsTeam() {
return _policeTeam;
}

// ########################################
// # Admin zone #
// ########################################
Expand Down Expand Up @@ -407,6 +412,7 @@ private void disableGame() {

_prison.deleteVaults();
_prison.deleteCameras();
_prison.deleteSoundDetectors();

GameManager.removeGame();
}
Expand Down Expand Up @@ -484,6 +490,10 @@ public void playerMove(String playerName, PrisonEscapeLocation loc) {
return;
}

for (SoundDetector soundDetector : _prison.getSoundDetectors()) {
soundDetector.playerMoved(player, loc);
}

if (!isPrisioner(player)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,18 @@ public void createSideBar(String displayName, List<String> baseSideBar) {
int size = _sideBarLines.size();
for (int i = 0; i < size; i++) {
obj.getScore(_sideBarLines.get(i)).setScore(size - i);
setLine(obj, i, size);
setLine(obj, i);
}
}

public void addLine(int index, String line) {
_sideBarLines.add(index, line);

int size = _sideBarLines.size();

Objective obj = _sb.getObjective(SIDE_BAR_NAME);
setLine(obj, index, size);

for (int i = index + 1; i < size; i++) {
for (int i = 0; i < _sideBarLines.size(); i++) {
_sb.resetScores(_sideBarLines.get(i));
setLine(obj, i, size);
setLine(obj, i);
}

}
Expand All @@ -61,11 +58,11 @@ public void updateLine(int index, String newLine) {
_sb.resetScores(previousLine);

_sideBarLines.set(index, newLine);
setLine(_sb.getObjective(SIDE_BAR_NAME), index, _sideBarLines.size());
setLine(_sb.getObjective(SIDE_BAR_NAME), index);
}

private void setLine(Objective obj, int i, int size) {
obj.getScore(_sideBarLines.get(i)).setScore(size - i);
private void setLine(Objective obj, int i) {
obj.getScore(_sideBarLines.get(i)).setScore(_sideBarLines.size() - i);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class PrisonBuilding {
private Helicopter _helicopter;

private List<Camera> _cameras;
private List<SoundDetector> _soundDetectors;

// #########################################
// # Constructor #
Expand Down Expand Up @@ -138,6 +139,7 @@ public PrisonBuilding(PrisonEscapeLocation reference) {
_helicopter.departed();

_cameras = new ArrayList<>();
_soundDetectors = new ArrayList<>();
}

private List<PrisonEscapeLocation> createLocationsList(
Expand Down Expand Up @@ -363,6 +365,28 @@ public void deleteCameras() {
}
}

// ########################################
// # Sound Detector #
// ########################################

public int countSoundDetectors() {
return _soundDetectors.size();
}

public void addSoundDetector(PrisonEscapeLocation location) {
_soundDetectors.add(new SoundDetector(_soundDetectors.size(), location));
}

public List<SoundDetector> getSoundDetectors() {
return _soundDetectors;
}

public void deleteSoundDetectors() {
for (SoundDetector soundDetector : _soundDetectors) {
soundDetector.delete();
}
}

// #########################################
// # Locations #
// #########################################
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package net.tiagofar78.prisonescape.game.prisonbuilding;

import net.tiagofar78.prisonescape.bukkit.BukkitWorldEditor;
import net.tiagofar78.prisonescape.game.Guard;
import net.tiagofar78.prisonescape.game.PrisonEscapeGame;
import net.tiagofar78.prisonescape.game.PrisonEscapePlayer;
import net.tiagofar78.prisonescape.managers.ConfigManager;
import net.tiagofar78.prisonescape.managers.GameManager;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;

import java.util.ArrayList;
import java.util.List;

public class SoundDetector {

private static final int MAX_VALUE = 10;
private static final int MIN_VALUE = 1;

private int _index;
private PrisonEscapeLocation _location;
private List<PrisonEscapePlayer> _playersInRange;

public SoundDetector(int index, PrisonEscapeLocation location) {
_index = index;
_location = location;
_playersInRange = new ArrayList<>();

createOnWorld();

PrisonEscapeGame game = GameManager.getGame();
List<Guard> guards = game.getGuardsTeam().getMembers();
for (Guard guard : guards) {
guard.addSoundDetectorLine(MIN_VALUE);
}
}

public boolean isDetectingSound() {
return _playersInRange.size() != 0;
}

public void playerMoved(PrisonEscapePlayer player, PrisonEscapeLocation playerLocation) {
if (isInRange(playerLocation) && !_playersInRange.contains(player)) {
_playersInRange.add(player);
updateValue();
} else if (!isInRange(playerLocation) && _playersInRange.contains(player)) {
_playersInRange.remove(player);
updateValue();
}
}

private boolean isInRange(PrisonEscapeLocation location) {
double range = ConfigManager.getInstance().getSoundDetectorRange();
return distance(_location, location) <= range;
}

private double distance(PrisonEscapeLocation loc1, PrisonEscapeLocation loc2) {
double xComponent = Math.pow(loc1.getX() - loc2.getX(), 2);
double yComponent = Math.pow(loc1.getY() - loc2.getY(), 2);
double zComponent = Math.pow(loc1.getZ() - loc2.getZ(), 2);

return Math.sqrt(xComponent + yComponent + zComponent);
}

public void delete() {
deleteFromWorld();
}

private void createOnWorld() {
World world = BukkitWorldEditor.getWorld();
Location location = new Location(world, _location.getX(), _location.getY(), _location.getZ());

location.getBlock().setType(Material.LIGHTNING_ROD);
}

private void deleteFromWorld() {
World world = BukkitWorldEditor.getWorld();
Location location = new Location(world, _location.getX(), _location.getY(), _location.getZ());
location.getBlock().setType(Material.AIR);
}

private void updateValue() {
int value = isDetectingSound() ? MAX_VALUE : MIN_VALUE;

List<Guard> guards = GameManager.getGame().getGuardsTeam().getMembers();
for (Guard guard : guards) {
guard.updateSoundDetectorValue(_index, value);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class ItemFactory {
new ShopItem(),
new TrapItem(),
new CameraItem(),
new SensorItem(),
new SoundDetectorItem(),
new RadarItem(),
new OpenCamerasItem(),
new MissionsItem()};
Expand Down
39 changes: 0 additions & 39 deletions src/main/java/net/tiagofar78/prisonescape/items/SensorItem.java

This file was deleted.

Loading
Loading