feat(client/anim): Added animation flow for calls (#104)

Co-authored-by: Taso <[email protected]>
This commit is contained in:
RockySouthpawandTaso authored and GitHub committed 2021-02-18 12:39:30 -05:00
1 parent d6d2bed335
commit 3a572cbc7b
7 files changed
+243 -189

No files matched your search

+11 -8
View File
@@ -1,9 +1,10 @@
import events from '../utils/events';
import { ICall } from '../../phone/src/common/typings/call';
import {phoneCallStartAnim, phoneCallEndAnim} from './functions';
const exp = (global as any).exports;
RegisterNuiCallbackType(events.PHONE_INITIALIZE_CALL);
RegisterNuiCallbackType(events.PHONE_INITIALIZE_CALL); // Fires when the call is started.
on(`__cfx_nui:${events.PHONE_INITIALIZE_CALL}`, (data: any, cb: Function) => {
const start = Date.now();
emitNet(events.PHONE_INITIALIZE_CALL, data.number, start);
@@ -30,7 +31,7 @@ onNet(
}
);
RegisterNuiCallbackType(events.PHONE_ACCEPT_CALL);
RegisterNuiCallbackType(events.PHONE_ACCEPT_CALL); // Fires when the TARGET accepts.
on(`__cfx_nui:${events.PHONE_ACCEPT_CALL}`, (data: any, cb: Function) => {
emitNet(events.PHONE_ACCEPT_CALL, data.transmitterNumber);
cb();
@@ -40,7 +41,7 @@ onNet(
events.PHONE_CALL_WAS_ACCEPTED,
(channelId: number, currentCall: ICall, isTransmitter: boolean) => {
exp['mumble-voip'].SetCallChannel(channelId);
phoneCallStartAnim() // Trigger call animation only if the call was accepted.
SendNuiMessage(
JSON.stringify({
app: 'CALL',
@@ -56,16 +57,15 @@ onNet(
}
);
RegisterNuiCallbackType(events.PHONE_CALL_REJECTED);
RegisterNuiCallbackType(events.PHONE_CALL_REJECTED); // Fires when cancelling and rejecting a call.
on(`__cfx_nui:${events.PHONE_CALL_REJECTED}`, (data: any, cb: Function) => {
const end = Date.now();
emitNet(events.PHONE_CALL_REJECTED, data.transmitterNumber, end);
emitNet(events.PHONE_CALL_REJECTED, data.phoneNumber, end);
cb();
});
onNet(events.PHONE_CALL_WAS_REJECTED, () => {
openCallModal(false);
SendNuiMessage(
JSON.stringify({
app: 'CALL',
@@ -80,7 +80,11 @@ onNet(events.PHONE_CALL_WAS_REJECTED, () => {
);
});
RegisterNuiCallbackType(events.PHONE_END_CALL);
onNet(events.PHONE_CALL_SEND_HANGUP_ANIM, () => {
phoneCallEndAnim()
});
RegisterNuiCallbackType(events.PHONE_END_CALL); // Fires when ending an ACTIVE call
on(`__cfx_nui:${events.PHONE_END_CALL}`, (data: any, cb: Function) => {
const end = Date.now();
emitNet(events.PHONE_END_CALL, data.transmitterNumber, end);
@@ -89,7 +93,6 @@ on(`__cfx_nui:${events.PHONE_END_CALL}`, (data: any, cb: Function) => {
onNet(events.PHONE_CALL_WAS_ENDED, () => {
exp['mumble-voip'].SetCallChannel(0);
openCallModal(false);
SendNuiMessage(
+1 -171
View File
@@ -1,6 +1,6 @@
import config from '../utils/config';
import { Delay } from '../utils/fivem';
import events from '../utils/events';
import {phoneCloseAnim, phoneOpenAnim, removePhoneProp} from './functions';
let prop = 0;
let isPhoneOpen = false;
@@ -48,176 +48,6 @@ RegisterCommand(
RegisterKeyMapping('phone', 'Open Phone', 'keyboard', 'f1');
/* * * * * * * * * * * * *
*
* Prop Deletion/Creation handling
*
* * * * * * * * * * * * */
const newPhoneProp = async () => {
removePhoneProp(); //deletes the already existing prop before creating another.
if (!propCreated) {
RequestModel(phoneModel);
while (!HasModelLoaded(phoneModel)) {
await Delay(1);
}
const playerPed = PlayerPedId();
const [x, y, z] = GetEntityCoords(playerPed, true);
prop = CreateObject(
GetHashKey(phoneModel),
x,
y,
z + 0.2,
true,
true,
true
);
//prop = CreateObject(GetHashKey(phoneModel), 1.0, 1.0, 1.0, 1, 1, 0)
const boneIndex = GetPedBoneIndex(playerPed, 28422);
AttachEntityToEntity(
prop,
playerPed,
boneIndex,
0.0,
0.0,
0.0,
0.0,
0.0,
-0.0,
true,
true,
false,
true,
1.0,
true
); //-- Attaches the phone to the player.
propCreated = true;
console.log('prop created');
} else if (propCreated) {
console.log('prop already created');
}
};
function removePhoneProp() {
//-- Triggered in newphoneProp function. Only way to destory the prop correctly.
if (prop != 0) {
//Citizen.invokeNative(0xAE3CBE5BF394C9C9 , Citizen.pointerValueIntInitialized(prop))
DeleteEntity(prop);
prop = 0;
propCreated = false;
}
}
async function loadAnimDict(dict: any) {
//-- Loads the animation dict. Used in the anim functions.
while (!HasAnimDictLoaded(dict)) {
RequestAnimDict(dict);
await Delay(100);
}
}
/* * * * * * * * * * * * *
*
* Animations
*
* * * * * * * * * * * * */
const handleOpenVehicleAnim = async (playerPed: number): Promise<void> => {
const dict = 'anim@cellphone@in_car@ps';
ClearPedTasks(playerPed);
await loadAnimDict(dict);
TaskPlayAnim(
playerPed,
dict,
'cellphone_text_in',
8.0,
-1,
-1,
50,
0,
false,
false,
false
);
await Delay(300); //Gives time for animation starts before creating the phone
await newPhoneProp(); //Creates the phone and attaches it.
};
const handleOpenNormalAnim = async (playerPed: number): Promise<void> => {
//While not in a vehicle it will use this dict.
const dict = 'cellphone@';
ClearPedTasks(playerPed);
await loadAnimDict(dict);
TaskPlayAnim(
playerPed,
dict,
'cellphone_text_in',
8.0,
-1,
-1,
50,
0,
false,
false,
false
);
await Delay(300); //Gives time for animation starts before creating the phone
await newPhoneProp(); //Creates the phone and attaches it.
};
const handleCloseVehicleAnim = async (playerPed: number): Promise<void> => {
//true refers to at get in.
const DICT = 'anim@cellphone@in_car@ps';
const ANIM = 'cellphone_text_out';
StopAnimTask(playerPed, DICT, 'cellphone_text_in', 1.0); //Stop the pull out animation
removePhoneProp(); //Deletes the prop early incase they get out of the vehicle.
await Delay(250); //lets it get to a certain point
await loadAnimDict(DICT); //loads the new animation
TaskPlayAnim(playerPed, DICT, ANIM, 8.0, -1, -1, 50, 0, false, false, false); //puts phone into pocket
await Delay(200); //waits until the phone is in the pocket
StopAnimTask(playerPed, DICT, ANIM, 1.0); //clears the animation
};
const handleCloseNormalAnim = async (playerPed: number): Promise<void> => {
const DICT = 'cellphone@';
const ANIM = 'cellphone_text_out';
StopAnimTask(playerPed, DICT, 'cellphone_text_in', 1.0); //Stop the pull out animation
await Delay(100); //lets it get to a certain point
await loadAnimDict(DICT); //loads the new animation
TaskPlayAnim(playerPed, DICT, ANIM, 8.0, -1, -1, 50, 0, false, false, false); //puts phone into pocket
await Delay(200); //waits until the phone is in the pocket
StopAnimTask(playerPed, DICT, ANIM, 1.0); //clears the animation
removePhoneProp(); //Deletes the prop.
};
async function phoneOpenAnim(): Promise<void> {
const playerPed = PlayerPedId();
removePhoneProp(); //Deleting before creating a new phone where itll be deleted again.
if (IsPedInAnyVehicle(playerPed, true)) {
return await handleOpenVehicleAnim(playerPed);
}
return await handleOpenNormalAnim(playerPed);
}
async function phoneCloseAnim() {
const playerPed = PlayerPedId();
if (IsPedInAnyVehicle(playerPed, true)) {
return await handleCloseVehicleAnim(playerPed);
}
await handleCloseNormalAnim(playerPed);
}
// setTick(() => {
// if (IsControlJustPressed(1, config.KeyTogglePhone)) {
// Phone();
// }
// });
/* * * * * * * * * * * * *
*
* Phone Visibility Handling
+1
View File
@@ -10,6 +10,7 @@ import './cl_notes';
import './cl_photo';
import './cl_messages';
import './cl_call';
import './functions';
export let ESX: Client = null;
+213
View File
@@ -0,0 +1,213 @@
import { Delay } from '../utils/fivem';
let prop = 0;
let isPhoneOpen = false;
let propCreated = false;
const phoneModel = 'prop_amb_phone'
/* * * * * * * * * * * * *
*
* Prop Deletion/Creation handling
*
* * * * * * * * * * * * */
const newPhoneProp = async () => {
removePhoneProp(); //deletes the already existing prop before creating another.
if (!propCreated) {
RequestModel(phoneModel);
while (!HasModelLoaded(phoneModel)) {
await Delay(1);
}
const playerPed = PlayerPedId();
const [x, y, z] = GetEntityCoords(playerPed, true);
prop = CreateObject(
GetHashKey(phoneModel),
x,
y,
z + 0.2,
true,
true,
true
);
//prop = CreateObject(GetHashKey(phoneModel), 1.0, 1.0, 1.0, 1, 1, 0)
const boneIndex = GetPedBoneIndex(playerPed, 28422);
AttachEntityToEntity(
prop,
playerPed,
boneIndex,
0.0,
0.0,
0.0,
0.0,
0.0,
-0.0,
true,
true,
false,
true,
1.0,
true
); //-- Attaches the phone to the player.
propCreated = true;
} else if (propCreated) {
console.log('prop already created');
}
};
export function removePhoneProp() {
//-- Triggered in newphoneProp function. Only way to destory the prop correctly.
if (prop != 0) {
//Citizen.invokeNative(0xAE3CBE5BF394C9C9 , Citizen.pointerValueIntInitialized(prop))
DeleteEntity(prop);
prop = 0;
propCreated = false;
}
}
/* * * * * * * * * * * * *
*
* Animations
*
* * * * * * * * * * * * */
export async function loadAnimDict(dict: any) {
//-- Loads the animation dict. Used in the anim functions.
while (!HasAnimDictLoaded(dict)) {
RequestAnimDict(dict);
await Delay(100);
}
}
const handleOpenVehicleAnim = async (playerPed: number): Promise<void> => {
const dict = 'anim@cellphone@in_car@ps';
SetCurrentPedWeapon(playerPed, 0xA2719263, true)
ClearPedTasks(playerPed);
await loadAnimDict(dict);
TaskPlayAnim(
playerPed,
dict,
'cellphone_text_in',
7.0,
-1,
-1,
50,
0,
false,
false,
false
);
await Delay(300);
await newPhoneProp();
};
const handleOpenNormalAnim = async (playerPed: number): Promise<void> => {
//While not in a vehicle it will use this dict.
const dict = 'cellphone@';
SetCurrentPedWeapon(playerPed, 0xA2719263, true)
ClearPedTasks(playerPed);
await loadAnimDict(dict);
TaskPlayAnim(
playerPed,
dict,
'cellphone_text_in',
8.0,
-1,
-1,
50,
0,
false,
false,
false
);
await Delay(300);
await newPhoneProp();
};
const handleCloseVehicleAnim = async (playerPed: number): Promise<void> => {
const DICT = 'anim@cellphone@in_car@ps';
const ANIM = 'cellphone_text_out';
StopAnimTask(playerPed, DICT, 'cellphone_text_in', 1.0); // Do both incase they were on the phone.
StopAnimTask(playerPed, DICT, 'cellphone_call_to_text', 1.0);
removePhoneProp();
};
const handleCloseNormalAnim = async (playerPed: number): Promise<void> => {
const DICT = 'cellphone@';
const ANIM = 'cellphone_text_out';
StopAnimTask(playerPed, DICT, 'cellphone_text_in', 1.0);
await Delay(100);
await loadAnimDict(DICT);
TaskPlayAnim(playerPed, DICT, ANIM, 7.0, -1, -1, 50, 0, false, false, false);
await Delay(200);
StopAnimTask(playerPed, DICT, ANIM, 1.0);
removePhoneProp();
};
const handleCallStartVehicleAnim = async (playerPed: number): Promise<void> => {
const DICT = 'anim@cellphone@in_car@ps';
const ANIM = 'cellphone_call_listen_base';
StopAnimTask(playerPed, DICT, 'cellphone_text_in', 1.0);
await loadAnimDict(DICT);
TaskPlayAnim(playerPed, DICT, ANIM, 3.0, 3.0, -1, 49, 0, false, false, false);
};
const handleCallEndVehicleAnim = async (playerPed: number): Promise<void> => {
const DICT = 'anim@cellphone@in_car@ps';
const ANIM = 'cellphone_call_to_text';
StopAnimTask(playerPed, DICT, 'cellphone_call_listen_base', 1.0);
await loadAnimDict(DICT);
TaskPlayAnim(playerPed, DICT, ANIM, 1.3, 5.0, -1, 50, 0, false, false, false);
};
const handleCallStartNormalAnim = async (playerPed: number): Promise<void> => {
const DICT = 'cellphone@';
const ANIM = 'cellphone_call_listen_base';
await loadAnimDict(DICT);
TaskPlayAnim(playerPed, DICT, ANIM, 3.0, 3.0, -1, 49, 0, false, false, false);
};
const handleCallEndNormalAnim = async (playerPed: number): Promise<void> => {
const DICT = 'cellphone@';
const ANIM = 'cellphone_call_to_text';
await loadAnimDict(DICT);
TaskPlayAnim(playerPed, DICT, ANIM, 2.5, 8.0, -1, 50, 0, false, false, false);
};
export async function phoneOpenAnim(): Promise<void> {
const playerPed = PlayerPedId();
removePhoneProp();
if (IsPedInAnyVehicle(playerPed, true)) {
return await handleOpenVehicleAnim(playerPed);
}
return await handleOpenNormalAnim(playerPed);
}
export async function phoneCloseAnim() {
const playerPed = PlayerPedId();
if (IsPedInAnyVehicle(playerPed, true)) {
return await handleCloseVehicleAnim(playerPed);
}
await handleCloseNormalAnim(playerPed);
}
export async function phoneCallStartAnim(): Promise<void> {
const playerPed = PlayerPedId();
if (IsPedInAnyVehicle(playerPed, true)) {
return await handleCallStartVehicleAnim(playerPed);
}
return await handleCallStartNormalAnim(playerPed);
}
export async function phoneCallEndAnim(): Promise<void> {
const playerPed = PlayerPedId();
if (IsPedInAnyVehicle(playerPed, true)) {
return await handleCallEndVehicleAnim(playerPed);
}
return await handleCallEndNormalAnim(playerPed);
}
+14 -9
View File
@@ -70,7 +70,7 @@ onNet(
const _source = getSource();
const callIdentifier = uuidv4();
// the client that is calling
const xTransmitter = ESX.GetPlayerFromId(_source);
const transmitterNumber = await usePhoneNumber(
@@ -89,6 +89,7 @@ onNet(
receiver: receiverNumber,
receiverSource: xReceiver.source,
start: timestamp / 1000,
accepted: false,
});
const currentCall = calls.get(transmitterNumber);
@@ -140,6 +141,8 @@ onNet(events.PHONE_ACCEPT_CALL, async (transmitterNumber: string) => {
currentCall,
true
);
currentCall.accepted = true;
} catch (e) {
callLogger.error('Accepting Call Error', e.message, {
source: pSource,
@@ -153,12 +156,11 @@ onNet(
const pSource = getSource();
try {
const currentCall = calls.get(transmitterNumber);
await updateCall(currentCall, false, timestamp);
// player who is being called
emitNet(events.PHONE_CALL_WAS_REJECTED, pSource);
// player who is calling
// player who is called and initiasted the rejection.
emitNet(events.PHONE_CALL_WAS_REJECTED, currentCall.receiverSource);
// player who is calling and recieved the rejection.
emitNet(events.PHONE_CALL_WAS_REJECTED, currentCall.transmitterSource);
} catch (e) {
callLogger.error(`Phone Call Rejected Event Error ${e.message}`, {
@@ -174,16 +176,19 @@ onNet(
const pSource = getSource();
try {
const currentCall = calls.get(transmitterNumber);
const endTime = timestamp / 1000;
await updateCall(currentCall, false, endTime);
const accepted = calls.get(pSource);
// player who is being called
emitNet(events.PHONE_CALL_WAS_ENDED, currentCall.receiverSource);
// player who is calling
emitNet(events.PHONE_CALL_WAS_ENDED, currentCall.transmitterSource);
// ends animations if call is active
if (currentCall.accepted) {
emitNet(events.PHONE_CALL_SEND_HANGUP_ANIM, currentCall.receiverSource);
emitNet(events.PHONE_CALL_SEND_HANGUP_ANIM, currentCall.transmitterSource);
}
calls.delete(transmitterNumber);
} catch (e) {
callLogger.error(`Error ending Phone Call, ${e.message}`, {
+2 -1
View File
@@ -110,4 +110,5 @@ export default {
PHONE_CALL_WAS_REJECTED: 'phone:callRejected',
PHONE_CALL_FETCH_CALLS: 'phone:fetchCalls',
PHONE_CALL_SEND_HISTORY: 'phone:sendCallHistory',
};
PHONE_CALL_SEND_HANGUP_ANIM: 'phone:sendHangupAnim',
};