SanAndreasUnity/Assets/Scripts/Networking/PlayerRequests.cs

253 lines
6.7 KiB
C#
Raw Normal View History

2019-07-06 01:16:37 +02:00
using UnityEngine;
2019-07-05 23:46:59 +02:00
using Mirror;
using SanAndreasUnity.Utilities;
using SanAndreasUnity.Behaviours;
2019-07-06 00:10:09 +02:00
using SanAndreasUnity.Behaviours.Vehicles;
using System.Linq;
using System.Collections.Generic;
2019-07-05 21:18:29 +02:00
2019-07-05 23:46:59 +02:00
namespace SanAndreasUnity.Net
2019-07-05 21:18:29 +02:00
{
2019-07-05 23:46:59 +02:00
public class PlayerRequests : NetworkBehaviour
2019-07-05 21:18:29 +02:00
{
2019-07-05 23:46:59 +02:00
Player m_player;
2019-07-08 17:03:04 +02:00
Ped m_ped => m_player.OwnedPed;
2019-07-05 23:46:59 +02:00
public static PlayerRequests Local { get; private set; }
float m_timeWhenSpawnedVehicle = 0f;
float m_timeWhenMadePedRequest = 0f;
2019-07-08 17:03:04 +02:00
float m_timeWhenMadeWeaponRequest = 0f;
2019-07-05 23:46:59 +02:00
List<Vehicle> m_myVehicles = new List<Vehicle>();
2019-07-05 23:46:59 +02:00
void Awake()
{
m_player = this.GetComponentOrThrow<Player>();
}
public override void OnStartLocalPlayer()
{
base.OnStartLocalPlayer();
Local = this;
}
void OnDisable()
{
// destroy player's vehicles
if (NetStatus.IsServer)
{
this.DestroyPlayersVehicles();
}
}
2019-07-05 23:46:59 +02:00
public bool CanPlayerSpawnVehicle()
{
if (null == m_player.OwnedPed)
return false;
if (Time.time - m_timeWhenSpawnedVehicle < 3f)
2019-07-05 23:46:59 +02:00
return false;
m_myVehicles.RemoveDeadObjects();
if (m_myVehicles.Count >= 3)
2019-07-05 23:46:59 +02:00
return false;
return true;
}
bool CanMakePedRequest()
2019-07-05 23:46:59 +02:00
{
bool bCan = (NetStatus.IsServer && this.isLocalPlayer) || (Time.time - m_timeWhenMadePedRequest > 2f);
bCan &= (m_ped != null);
m_timeWhenMadePedRequest = Time.time;
return bCan;
2019-07-05 23:46:59 +02:00
}
public void RequestVehicleSpawn(int vehicleId) => this.CmdRequestVehicleSpawn(vehicleId);
2019-07-05 23:46:59 +02:00
[Command]
void CmdRequestVehicleSpawn(int vehicleId)
2019-07-05 23:46:59 +02:00
{
if (!this.CanPlayerSpawnVehicle())
return;
m_timeWhenSpawnedVehicle = Time.time;
F.RunExceptionSafe( () => m_myVehicles.Add( Vehicle.CreateInFrontOf(vehicleId, m_ped.transform) ) );
2019-07-05 23:46:59 +02:00
}
public void RequestPedModelChange()
{
this.CmdRequestPedModelChange();
}
[Command]
void CmdRequestPedModelChange()
{
if (!this.CanMakePedRequest())
2019-07-05 23:46:59 +02:00
return;
F.RunExceptionSafe( () => m_player.OwnedPed.PlayerModel.Load(Ped.RandomPedId) );
}
2019-07-08 21:45:33 +02:00
public void SpawnPedStalker() => this.CmdSpawnPedStalker();
[Command]
void CmdSpawnPedStalker()
{
if (!this.CanMakePedRequest())
return;
2019-07-08 22:11:26 +02:00
F.RunExceptionSafe( () => Ped.SpawnPedStalker(Ped.RandomPedId, m_ped.transform, m_ped) );
2019-07-08 21:45:33 +02:00
}
2019-07-06 00:10:09 +02:00
public void RequestSuicide()
{
this.CmdRequestSuicide();
}
[Command]
void CmdRequestSuicide()
{
2021-01-08 04:44:56 +01:00
F.RunExceptionSafe(() =>
{
if (m_player.OwnedPed != null)
m_player.OwnedPed.Kill();
});
2019-07-06 00:10:09 +02:00
}
public void RequestToDestroyMyVehicles() => this.CmdRequestToDestroyMyVehicles();
2019-07-06 00:10:09 +02:00
[Command]
void CmdRequestToDestroyMyVehicles()
2019-07-06 00:10:09 +02:00
{
F.RunExceptionSafe( () => {
this.ExplodePlayersVehicles();
});
2019-07-06 00:10:09 +02:00
}
void DestroyPlayersVehicles()
{
m_myVehicles.RemoveDeadObjects();
foreach (var v in m_myVehicles)
{
Destroy(v.gameObject);
}
}
void ExplodePlayersVehicles()
{
m_myVehicles.RemoveDeadObjects();
foreach (var v in m_myVehicles)
{
v.Explode();
}
}
2019-07-06 00:10:09 +02:00
public void RequestTeleport(Vector3 pos, Quaternion rot)
{
this.CmdRequestTeleport(pos, rot);
}
[Command]
void CmdRequestTeleport(Vector3 pos, Quaternion rot)
{
if (m_player.OwnedPed != null)
{
2019-07-06 01:16:37 +02:00
F.RunExceptionSafe( () => m_player.OwnedPed.Teleport(pos, rot) );
2019-07-06 00:10:09 +02:00
}
}
2019-07-08 17:03:04 +02:00
#region weapons
2019-07-08 17:31:53 +02:00
bool CanMakeWeaponRequest
{
get
{
bool bCan = (NetStatus.IsServer && this.isLocalPlayer) || Time.time - m_timeWhenMadeWeaponRequest > 2f;
m_timeWhenMadeWeaponRequest = Time.time;
return bCan;
}
}
2019-07-08 17:03:04 +02:00
public void AddRandomWeapons() => this.CmdAddRandomWeapons();
[Command]
void CmdAddRandomWeapons()
{
2019-07-08 17:31:53 +02:00
if (!this.CanMakeWeaponRequest)
return;
2019-07-08 17:03:04 +02:00
if (m_ped != null)
2019-07-08 21:02:07 +02:00
F.RunExceptionSafe( () => m_ped.WeaponHolder.AddRandomWeapons() );
2019-07-08 17:03:04 +02:00
}
public void RemoveAllWeapons() => this.CmdRemoveAllWeapons();
[Command]
void CmdRemoveAllWeapons()
{
2019-07-08 17:31:53 +02:00
if (!this.CanMakeWeaponRequest)
return;
2019-07-08 17:03:04 +02:00
if (m_ped != null)
2019-07-08 21:02:07 +02:00
F.RunExceptionSafe( () => m_ped.WeaponHolder.RemoveAllWeapons() );
}
public void RemoveCurrentWeapon() => this.CmdRemoveCurrentWeapon();
[Command]
void CmdRemoveCurrentWeapon()
{
if (!this.CanMakeWeaponRequest)
return;
if (m_ped != null && m_ped.CurrentWeapon != null)
F.RunExceptionSafe( () => Object.Destroy(m_ped.CurrentWeapon.gameObject) );
2019-07-08 17:03:04 +02:00
}
public void GiveAmmo() => this.CmdGiveAmmo();
[Command]
void CmdGiveAmmo()
{
2019-07-08 17:31:53 +02:00
if (!this.CanMakeWeaponRequest)
return;
2019-07-08 17:03:04 +02:00
if (m_ped != null)
{
F.RunExceptionSafe( () => {
foreach (var w in m_ped.WeaponHolder.AllWeapons)
WeaponHolder.AddRandomAmmoAmountToWeapon(w);
} );
}
}
public void GiveWeapon(int modelId) => this.CmdGiveWeapon(modelId);
[Command]
void CmdGiveWeapon(int modelId)
{
2019-07-08 17:31:53 +02:00
if (!this.CanMakeWeaponRequest)
return;
2019-07-08 17:03:04 +02:00
if (m_ped != null)
{
F.RunExceptionSafe( () => {
var w = m_ped.WeaponHolder.SetWeaponAtSlot(modelId, 0);
m_ped.WeaponHolder.SwitchWeapon(w.SlotIndex);
WeaponHolder.AddRandomAmmoAmountToWeapon(w);
} );
}
}
#endregion
2019-07-05 21:18:29 +02:00
}
2019-07-05 23:46:59 +02:00
2019-07-05 21:18:29 +02:00
}