adapt all scripts to command system - stop using PlayerRequests

This commit is contained in:
in0finite 2021-02-22 19:43:17 +01:00
parent a6204749b2
commit 16380a4fe7
10 changed files with 50 additions and 365 deletions

View file

@ -13,7 +13,6 @@ GameObject:
- component: {fileID: 2203233818454028071}
- component: {fileID: 2657593453642018504}
- component: {fileID: 3097963212107052643}
- component: {fileID: 5546908767498174603}
- component: {fileID: 842985084553803207}
m_Layer: 0
m_Name: NetPlayer
@ -90,19 +89,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: de46f572586cdb697b6c95283affa511, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &5546908767498174603
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3871880573184668455}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 05a2fa04c5d0f35dd8a703b9b86cca04, type: 3}
m_Name:
m_EditorClassIdentifier:
syncInterval: 0.1
--- !u!114 &842985084553803207
MonoBehaviour:
m_ObjectHideFlags: 0

View file

@ -1,5 +1,4 @@
using SanAndreasUnity.Behaviours;
using UnityEngine;
using UnityEngine;
namespace SanAndreasUnity.Behaviours
{
@ -8,46 +7,15 @@ namespace SanAndreasUnity.Behaviours
{
public KeyCode actionKey = KeyCode.P;
// Use this for initialization
private void Start()
{
}
// Update is called once per frame
private void Update()
{
if (Input.GetKeyDown(actionKey) && GameManager.CanPlayerReadInput())
{
if (Utilities.NetUtils.IsServer)
ChangePedestrianModel();
else if (Net.PlayerRequests.Local != null)
Net.PlayerRequests.Local.RequestPedModelChange();
}
}
public static void ChangePedestrianModel()
{
if (Ped.Instance != null)
{
ChangePedestrianModel(Ped.Instance.PlayerModel, -1);
}
}
public static void ChangePedestrianModel(PedModel ped, int newModelId)
{
if (-1 == newModelId)
newModelId = Ped.RandomPedId;
// Retry with another random model if this one doesn't work
try
{
ped.Load(newModelId);
}
catch (System.NullReferenceException ex)
{
Debug.LogException (ex);
ChangePedestrianModel(ped, -1);
if (Ped.Instance != null)
{
Chat.ChatManager.SendChatMessageToAllPlayersAsLocalPlayer("/skin");
}
}
}
}

View file

@ -1,5 +1,4 @@
using SanAndreasUnity.Behaviours;
using UnityEngine;
using UnityEngine;
namespace SanAndreasUnity.Behaviours
{
@ -13,24 +12,13 @@ namespace SanAndreasUnity.Behaviours
{
if (Input.GetKeyDown(spawnKey) && GameManager.CanPlayerReadInput())
{
if (Utilities.NetUtils.IsServer)
SpawnVehicle();
else if (Net.PlayerRequests.Local != null)
Net.PlayerRequests.Local.RequestVehicleSpawn(-1);
if (Ped.Instance != null)
{
Chat.ChatManager.SendChatMessageToAllPlayersAsLocalPlayer("/veh");
}
}
}
private void SpawnVehicle()
{
var ped = Ped.Instance;
if (null == ped)
return;
Vehicles.Vehicle.CreateRandomInFrontOf(ped.transform);
}
}
}

View file

@ -1,252 +0,0 @@
using UnityEngine;
using Mirror;
using SanAndreasUnity.Utilities;
using SanAndreasUnity.Behaviours;
using SanAndreasUnity.Behaviours.Vehicles;
using System.Linq;
using System.Collections.Generic;
namespace SanAndreasUnity.Net
{
public class PlayerRequests : NetworkBehaviour
{
Player m_player;
Ped m_ped => m_player.OwnedPed;
public static PlayerRequests Local { get; private set; }
float m_timeWhenSpawnedVehicle = 0f;
float m_timeWhenMadePedRequest = 0f;
float m_timeWhenMadeWeaponRequest = 0f;
List<Vehicle> m_myVehicles = new List<Vehicle>();
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();
}
}
public bool CanPlayerSpawnVehicle()
{
if (null == m_player.OwnedPed)
return false;
if (Time.time - m_timeWhenSpawnedVehicle < 3f)
return false;
m_myVehicles.RemoveDeadObjects();
if (m_myVehicles.Count >= 3)
return false;
return true;
}
bool CanMakePedRequest()
{
bool bCan = (NetStatus.IsServer && this.isLocalPlayer) || (Time.time - m_timeWhenMadePedRequest > 2f);
bCan &= (m_ped != null);
m_timeWhenMadePedRequest = Time.time;
return bCan;
}
public void RequestVehicleSpawn(int vehicleId) => this.CmdRequestVehicleSpawn(vehicleId);
[Command]
void CmdRequestVehicleSpawn(int vehicleId)
{
if (!this.CanPlayerSpawnVehicle())
return;
m_timeWhenSpawnedVehicle = Time.time;
F.RunExceptionSafe( () => m_myVehicles.Add( Vehicle.CreateInFrontOf(vehicleId, m_ped.transform) ) );
}
public void RequestPedModelChange()
{
this.CmdRequestPedModelChange();
}
[Command]
void CmdRequestPedModelChange()
{
if (!this.CanMakePedRequest())
return;
F.RunExceptionSafe( () => m_player.OwnedPed.PlayerModel.Load(Ped.RandomPedId) );
}
public void SpawnPedStalker() => this.CmdSpawnPedStalker();
[Command]
void CmdSpawnPedStalker()
{
if (!this.CanMakePedRequest())
return;
F.RunExceptionSafe( () => Ped.SpawnPedStalker(Ped.RandomPedId, m_ped.transform, m_ped) );
}
public void RequestSuicide()
{
this.CmdRequestSuicide();
}
[Command]
void CmdRequestSuicide()
{
F.RunExceptionSafe(() =>
{
if (m_player.OwnedPed != null)
m_player.OwnedPed.Kill();
});
}
public void RequestToDestroyMyVehicles() => this.CmdRequestToDestroyMyVehicles();
[Command]
void CmdRequestToDestroyMyVehicles()
{
F.RunExceptionSafe( () => {
this.ExplodePlayersVehicles();
});
}
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();
}
}
public void RequestTeleport(Vector3 pos, Quaternion rot)
{
this.CmdRequestTeleport(pos, rot);
}
[Command]
void CmdRequestTeleport(Vector3 pos, Quaternion rot)
{
if (m_player.OwnedPed != null)
{
F.RunExceptionSafe( () => m_player.OwnedPed.Teleport(pos, rot) );
}
}
#region weapons
bool CanMakeWeaponRequest
{
get
{
bool bCan = (NetStatus.IsServer && this.isLocalPlayer) || Time.time - m_timeWhenMadeWeaponRequest > 2f;
m_timeWhenMadeWeaponRequest = Time.time;
return bCan;
}
}
public void AddRandomWeapons() => this.CmdAddRandomWeapons();
[Command]
void CmdAddRandomWeapons()
{
if (!this.CanMakeWeaponRequest)
return;
if (m_ped != null)
F.RunExceptionSafe( () => m_ped.WeaponHolder.AddRandomWeapons() );
}
public void RemoveAllWeapons() => this.CmdRemoveAllWeapons();
[Command]
void CmdRemoveAllWeapons()
{
if (!this.CanMakeWeaponRequest)
return;
if (m_ped != null)
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) );
}
public void GiveAmmo() => this.CmdGiveAmmo();
[Command]
void CmdGiveAmmo()
{
if (!this.CanMakeWeaponRequest)
return;
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)
{
if (!this.CanMakeWeaponRequest)
return;
if (m_ped != null)
{
F.RunExceptionSafe( () => {
var w = m_ped.WeaponHolder.SetWeaponAtSlot(modelId, 0);
m_ped.WeaponHolder.SwitchWeapon(w.SlotIndex);
WeaponHolder.AddRandomAmmoAmountToWeapon(w);
} );
}
}
#endregion
}
}

View file

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 05a2fa04c5d0f35dd8a703b9b86cca04
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 7650
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -253,10 +253,7 @@ namespace SanAndreasUnity.UI {
Vector3 worldPos = MiniMap.MapPosToWorldPos (m_waypointMapPos);
if (Utilities.NetUtils.IsServer)
Ped.Instance.Teleport (worldPos);
else if (Net.PlayerRequests.Local != null)
Net.PlayerRequests.Local.RequestTeleport(worldPos, Ped.Instance.transform.rotation);
Chat.ChatManager.SendChatMessageToAllPlayersAsLocalPlayer($"/teleport {worldPos.x} {worldPos.y} {worldPos.z}");
}

View file

@ -110,15 +110,19 @@ namespace SanAndreasUnity.UI {
if (GUILayout.Button (_spawnNames[i]))
{
if (Utilities.NetUtils.IsServer)
Ped.Instance.Teleport (spawnLocation.position, spawnLocation.rotation);
else if (Net.PlayerRequests.Local != null)
Net.PlayerRequests.Local.RequestTeleport(spawnLocation.position, spawnLocation.rotation);
Vector3 pos = spawnLocation.position;
Vector3 eulers = spawnLocation.rotation.eulerAngles;
SendCommand($"/teleport {pos.x} {pos.y} {pos.z} {eulers.x} {eulers.y} {eulers.z}");
}
}
}
void SendCommand(string command)
{
Chat.ChatManager.SendChatMessageToAllPlayersAsLocalPlayer(command);
}
}
}

View file

@ -45,7 +45,7 @@ namespace SanAndreasUnity.UI {
if (Utilities.NetUtils.IsServer)
DisplayServerGui();
else if (Net.PlayerRequests.Local != null)
else if (Net.Player.Local != null)
DisplayClientOnlyGui();
}
@ -61,7 +61,8 @@ namespace SanAndreasUnity.UI {
if (GUILayout.Button("Change player model"))
{
CharacterModelChanger.ChangePedestrianModel();
if (Ped.Instance != null)
Ped.Instance.PlayerModel.Load(Ped.RandomPedId);
}
if (GUILayout.Button("Spawn 5 peds"))
@ -91,34 +92,37 @@ namespace SanAndreasUnity.UI {
}
void SendCommand(string command)
{
Chat.ChatManager.SendChatMessageToAllPlayersAsLocalPlayer(command);
}
void DisplayClientOnlyGui()
{
var pr = Net.PlayerRequests.Local;
if (GUILayout.Button("Request vehicle"))
{
pr.RequestVehicleSpawn(-1);
SendCommand("/veh -1");
}
if (GUILayout.Button("Request ped model change"))
{
pr.RequestPedModelChange();
SendCommand("/skin");
}
if (GUILayout.Button("Request suicide"))
{
pr.RequestSuicide();
SendCommand("/suicide");
}
if (GUILayout.Button("Request ped stalker"))
{
pr.SpawnPedStalker();
SendCommand("/stalker");
}
if (GUILayout.Button("Request to destroy my vehicles"))
{
pr.RequestToDestroyMyVehicles();
SendCommand("/dveh");
}
}

View file

@ -41,6 +41,11 @@ namespace SanAndreasUnity.UI {
}
void SendCommand(string command)
{
Chat.ChatManager.SendChatMessageToAllPlayersAsLocalPlayer(command);
}
void GetVehicleDefs() {
// get all vehicle definitions
@ -113,14 +118,9 @@ namespace SanAndreasUnity.UI {
if (GUILayout.Button (v.GameName, GUILayout.Width(this.columnWidths[0]))) {
if (Utilities.NetUtils.IsServer)
if (Net.Player.Local != null)
{
if (Ped.Instance != null)
Vehicle.CreateInFrontOf (v.Id, Ped.Instance.transform);
}
else if (Net.PlayerRequests.Local != null)
{
Net.PlayerRequests.Local.RequestVehicleSpawn(v.Id);
SendCommand($"/veh {v.Id}");
}
}

View file

@ -1,10 +1,6 @@
using System.Collections.Generic;
using UnityEngine;
using SanAndreasUnity.Importing.Items;
using UnityEngine;
using SanAndreasUnity.Behaviours;
using System.Linq;
using SanAndreasUnity.Utilities;
using SanAndreasUnity.Net;
namespace SanAndreasUnity.UI {
@ -30,6 +26,11 @@ namespace SanAndreasUnity.UI {
}
void SendCommand(string command)
{
Chat.ChatManager.SendChatMessageToAllPlayersAsLocalPlayer(command);
}
protected override void OnWindowGUIBeforeContent ()
{
base.OnWindowGUIBeforeContent ();
@ -41,22 +42,22 @@ namespace SanAndreasUnity.UI {
GUILayout.BeginHorizontal ();
if (GUILayout.Button ("Add random weapons", GUILayout.ExpandWidth(false)))
PlayerRequests.Local.AddRandomWeapons ();
SendCommand("/rand_w");
GUILayout.Space (5);
if (GUILayout.Button ("Remove all weapons", GUILayout.ExpandWidth(false)))
PlayerRequests.Local.RemoveAllWeapons ();
SendCommand("/rem_w");
GUILayout.Space (5);
if (GUILayout.Button ("Remove current weapon", GUILayout.ExpandWidth(false)))
PlayerRequests.Local.RemoveCurrentWeapon ();
SendCommand("/rem_current_w");
GUILayout.Space (5);
if (GUILayout.Button ("Give ammo", GUILayout.ExpandWidth (false)))
PlayerRequests.Local.GiveAmmo();
SendCommand("/ammo");
GUILayout.EndHorizontal ();
GUILayout.Space (15);
@ -87,7 +88,7 @@ namespace SanAndreasUnity.UI {
if (playerExists) {
if (GUILayout.Button ("Give", GUILayout.Width(70))) {
// give weapon to player
PlayerRequests.Local.GiveWeapon(data.modelId1);
SendCommand($"/w {data.modelId1}");
}
}