From e14d7170aa08520353cf5c54252f52610b77ece2 Mon Sep 17 00:00:00 2001 From: Vortrex <3858226+VortrexFTW@users.noreply.github.com> Date: Sat, 25 Feb 2023 04:39:00 -0600 Subject: [PATCH] Add payphone client script --- scripts/client/payphone.js | 246 +++++++++++++++++++++++++++++++++++++ 1 file changed, 246 insertions(+) create mode 100644 scripts/client/payphone.js diff --git a/scripts/client/payphone.js b/scripts/client/payphone.js new file mode 100644 index 00000000..f5f6b09f --- /dev/null +++ b/scripts/client/payphone.js @@ -0,0 +1,246 @@ +// =========================================================================== +// Vortrex's Roleplay Resource +// https://github.com/VortrexFTW/v-roleplay +// =========================================================================== +// FILE: payphone.js +// DESC: Provides payphone functions and processing +// TYPE: Client (JavaScript) +// =========================================================================== + +let payPhoneMaxAudibleDistance = 75; +let payPhoneRingMaxVolume = 50; + +let payPhoneRingingIndicatorImage = null; +let payPhoneRingingIndicatorImagePath = "files/images/payphone-ringing.png"; + +// Will be unnecessary once MafiaC has game sound playback in scripting +let payPhoneRingingSound = null; +let payPhoneDialingSound = null; +let payPhonePickupSound = null; +let payPhoneHangupSound = null; +let payPhoneRingingSoundFilePath = "files/sounds/payphone/old-payphone-ring.mp3"; +let payPhoneDialingSoundFilePath = "files/sounds/payphone/old-payphone-dial.mp3"; +let payPhonePickupSoundFilePath = "files/sounds/payphone/old-payphone-pickup.mp3"; +let payPhoneHangupSoundFilePath = "files/sounds/payphone/old-payphone-hangup.mp3"; + +let ringingPayPhone = -1; + +// =========================================================================== + +class PayPhoneData { + constructor(payPhoneId, state, position) { + this.index = -1; + this.payPhoneId = payPhoneId; + this.position = position; + this.state = state; + } +} + +// =========================================================================== + +function initPayPhoneScript() { + logToConsole(LOG_DEBUG, "[V.RP.PayPhone]: Initializing payphone script ..."); + //payPhoneRingingIndicatorImage = loadPayPhoneRingingIndicatorImage(); + payPhoneRingingSound = loadPayPhoneRingingSound(); + payPhoneDialingSound = loadPayPhoneDialingSound(); + payPhonePickupSound = loadPayPhonePickupSound(); + payPhoneHangupSound = loadPayPhoneHangupSound(); + logToConsole(LOG_DEBUG, "[V.RP.PayPhone]: Payphone script initialized!"); +} + +// =========================================================================== + +function loadPayPhoneRingingIndicatorImage() { + let imageStream = openFile(payPhoneRingingIndicatorImagePath); + let tempImage = null; + if (imageStream != null) { + tempImage = graphics.loadPNG(imageStream); + imageStream.close(); + } + + return tempImage; +} + +// =========================================================================== + +function loadPayPhoneRingingSound() { + let soundStream = openFile(payPhoneRingingSoundFilePath); + let tempSound = null; + if (soundStream != null) { + tempSound = audio.createSound(soundStream, true); + soundStream.close(); + } + + return tempSound; +} + +// =========================================================================== + +function loadPayPhoneDialingSound() { + let soundStream = openFile(payPhoneDialingSoundFilePath); + let tempSound = null; + if (soundStream != null) { + tempSound = audio.createSound(soundStream, false); + soundStream.close(); + } + + if (tempSound != null) { + tempSound.volume = 1.0; + } + + return tempSound; +} + +// =========================================================================== + +function loadPayPhonePickupSound() { + let soundStream = openFile(payPhonePickupSoundFilePath); + let tempSound = null; + if (soundStream != null) { + tempSound = audio.createSound(soundStream, false); + soundStream.close(); + } + + if (tempSound != null) { + tempSound.volume = 1.0; + } + + return tempSound; +} + +// =========================================================================== + +function loadPayPhoneHangupSound() { + let soundStream = openFile(payPhoneHangupSoundFilePath); + let tempSound = null; + if (soundStream != null) { + tempSound = audio.createSound(soundStream, false); + soundStream.close(); + } + + if (tempSound != null) { + tempSound.volume = 1.0; + } + + return tempSound; +} + +// =========================================================================== + +function processPayPhonesDistance() { + let tempRingingPhone = -1; + for (let i in getServerData().payPhones) { + if (getServerData().payPhones[i].state == V_PAYPHONE_STATE_RINGING) { + if (getDistance(getLocalPlayerPosition(), getServerData().payPhones[i].position) <= payPhoneMaxAudibleDistance) { + if (tempRingingPhone != -1) { + if (getDistance(getLocalPlayerPosition(), getServerData().payPhones[i].position) <= getDistance(getLocalPlayerPosition(), getServerData().payPhones[tempRingingPhone].position)) { + tempRingingPhone = i; + } + } else { + tempRingingPhone = i; + } + } + } + } + + if (tempRingingPhone == -1) { + logToConsole(LOG_DEBUG, "[V.RP.PayPhone]: No phones are ringing, stopping all ring sounds"); + payPhoneRingingSound.stop(); + } else { + let distance = getDistance(getLocalPlayerPosition(), getServerData().payPhones[tempRingingPhone].position); + let distancePercent = (payPhoneRingMaxVolume - (distance * 100 / payPhoneMaxAudibleDistance)); + payPhoneRingingSound.volume = distancePercent / 100; + + if (ringingPayPhone == -1) { + logToConsole(LOG_DEBUG, "[V.RP.PayPhone]: No previous phone ringing, starting ring sound"); + payPhoneRingingSound.play(); + } + } + + ringingPayPhone = tempRingingPhone; +} + +// =========================================================================== + +function receivePayPhoneFromServer(payPhoneId, isDeleted, state, position) { + logToConsole(LOG_DEBUG, `[V.RP.PayPhone] Received payphone ${payPhoneId} from server`); + + if (!areServerElementsSupported() || getGame() == V_GAME_MAFIA_ONE || getGame() == V_GAME_GTA_IV) { + if (isDeleted == true) { + getServerData().payPhones.splice(payPhoneId, 1); + return false; + } + + if (getPayPhoneData(payPhoneId) != false) { + logToConsole(LOG_DEBUG, `[V.RP.PayPhone] Payphone ${payPhoneId} already exists. Updating ...`); + let payPhoneData = getPayPhoneData(payPhoneId); + payPhoneData.state = state; + payPhoneData.position = position; + } else { + logToConsole(LOG_DEBUG, `[V.RP.PayPhone] Payphone ${payPhoneId} doesn't exist. Adding ...`); + let tempPayPhoneData = new PayPhoneData(payPhoneId, state, position); + getServerData().payPhones.push(tempPayPhoneData); + } + } +} + +// =========================================================================== + +function receivePayPhoneStateFromServer(payPhoneId, state) { + logToConsole(LOG_DEBUG, `[V.RP.PayPhone] Received payphone ${payPhoneId} state (${state}) from server`); + + if (getPayPhoneData(payPhoneId) == false) { + return false; + } + + getPayPhoneData(payPhoneId).state = state; +} + +// =========================================================================== + +/** + * @param {number} payPhoneId - The ID of the payphone (initially provided by server) + * @return {PayPhoneData} The payphone's data (class instance) + */ +function getPayPhoneData(payPhoneId) { + if (payPhoneId == -1) { + return false; + } + + for (let i in getServerData().payPhones) { + if (getServerData().payPhones[i].payPhoneId == payPhoneId) { + return getServerData().payPhones[i]; + } + } + + return false; +} + +// =========================================================================== + +function removePayPhonesFromClient() { + clearArray(getServerData().payPhones); +} + +// =========================================================================== + +function payPhoneDial() { + logToConsole(LOG_DEBUG, "[V.RP.PayPhone]: Playing payphone dial sound"); + payPhoneDialingSound.play(); +} + +// =========================================================================== + +function payPhoneHangup() { + logToConsole(LOG_DEBUG, "[V.RP.PayPhone]: Playing payphone hangup sound"); + payPhoneHangupSound.play(); +} + +// =========================================================================== + +function payPhonePickup() { + logToConsole(LOG_DEBUG, "[V.RP.PayPhone]: Playing payphone pickup sound"); + payPhonePickupSound.play(); +} + +// =========================================================================== \ No newline at end of file