improve vehicle spawning API, and add ability to request specific vehicle

This commit is contained in:
in0finite 2019-07-08 23:43:13 +02:00
parent 97bd52728d
commit a645e5e8dc
7 changed files with 27 additions and 54 deletions

View file

@ -6,15 +6,9 @@ namespace SanAndreasUnity.Behaviours
public class UIVehicleSpawner : MonoBehaviour
{
public Vector3 spawnOffset = new Vector3(0, 2, 5);
public KeyCode spawnKey = KeyCode.V;
private void Start()
{
}
private void Update()
{
if (Input.GetKeyDown(spawnKey))
@ -22,40 +16,21 @@ namespace SanAndreasUnity.Behaviours
if (Utilities.NetUtils.IsServer)
SpawnVehicle();
else if (Net.PlayerRequests.Local != null)
Net.PlayerRequests.Local.RequestVehicleSpawn();
Net.PlayerRequests.Local.RequestVehicleSpawn(-1);
}
}
public void SpawnVehicle()
private void SpawnVehicle()
{
var ped = Ped.Instance;
if (null == ped)
return;
SpawnVehicle(ped);
Vehicles.Vehicle.CreateRandomInFrontOf(ped.transform);
}
public void SpawnVehicle(Ped ped)
{
Vector3 pos = ped.transform.position + ped.transform.forward * spawnOffset.z + ped.transform.up * spawnOffset.y
+ ped.transform.right * spawnOffset.x;
Quaternion rotation = Quaternion.LookRotation(-ped.transform.right, Vector3.up);
SpawnVehicle(pos, rotation);
}
public void SpawnVehicle(Vector3 pos, Quaternion rotation)
{
// SanAndreasUnity.Behaviours.Vehicles.VehicleSpawner.Create ();
var v = SanAndreasUnity.Behaviours.Vehicles.Vehicle.Create(-1, null, pos, rotation);
Debug.Log("Spawned vehicle with id " + v.Definition.Id);
}
}
}

View file

@ -187,6 +187,8 @@ namespace SanAndreasUnity.Behaviours.Vehicles
void Start()
{
this.ApplySyncRate(VehicleManager.Instance.vehicleSyncRate);
Debug.LogFormat("Created vehicle - id {0}, name {1}", this.Definition.Id, this.Definition.GameName);
}
public void SetColors(params int[] clrIndices)

View file

@ -98,24 +98,16 @@ namespace SanAndreasUnity.Behaviours.Vehicles
}
/// <summary>
/// Gets the position for spawning based on current position of player.
/// </summary>
public static void GetPositionForSpawning(out Vector3 pos, out Quaternion rot) {
public static void GetPositionForSpawning(Transform inFrontOfTransform, out Vector3 pos, out Quaternion rot) {
pos = Vector3.zero;
rot = Quaternion.identity;
//if (null == PlayerController.Instance)
// return;
var cont = PlayerController.Instance;
Vector3 spawnOffset = new Vector3 (0, 2, 5);
pos = cont.transform.position + cont.transform.forward * spawnOffset.z + cont.transform.up * spawnOffset.y
+ cont.transform.right * spawnOffset.x;
rot = Quaternion.LookRotation(-cont.transform.right, Vector3.up);
pos = inFrontOfTransform.position + inFrontOfTransform.forward * spawnOffset.z + inFrontOfTransform.up * spawnOffset.y
+ inFrontOfTransform.right * spawnOffset.x;
rot = Quaternion.LookRotation(-inFrontOfTransform.right, Vector3.up);
}
@ -130,16 +122,21 @@ namespace SanAndreasUnity.Behaviours.Vehicles
return Create (carId, null, position, rotation);
}
public static Vehicle CreateInFrontOfPlayer(int carId) {
public static Vehicle CreateInFrontOf(int carId, Transform inFrontOfTransform) {
Vector3 pos;
Quaternion rot;
GetPositionForSpawning (out pos, out rot);
GetPositionForSpawning (inFrontOfTransform, out pos, out rot);
return Create (carId, pos, rot);
}
public static Vehicle CreateRandomInFrontOf(Transform inFrontOfTransform)
{
return CreateInFrontOf(-1, inFrontOfTransform);
}
public static Vehicle Create(int carId, int[] colors, Vector3 position, Quaternion rotation)
{
// this probably should not be done like this

View file

@ -56,19 +56,17 @@ namespace SanAndreasUnity.Net
return bCan;
}
public void RequestVehicleSpawn()
{
this.CmdRequestVehicleSpawn();
}
public void RequestVehicleSpawn(int vehicleId) => this.CmdRequestVehicleSpawn(vehicleId);
[Command]
void CmdRequestVehicleSpawn()
void CmdRequestVehicleSpawn(int vehicleId)
{
if (!this.CanPlayerSpawnVehicle())
return;
m_timeWhenSpawnedVehicle = Time.time;
F.RunExceptionSafe( () => FindObjectOfType<UIVehicleSpawner> ().SpawnVehicle(m_player.OwnedPed) );
F.RunExceptionSafe( () => Vehicle.CreateInFrontOf(vehicleId, m_ped.transform) );
}
public void RequestPedModelChange()

View file

@ -47,9 +47,8 @@ namespace SanAndreasUnity.UI {
Transform nearbyTransform = Ped.Instance != null ? Ped.Instance.transform : null;
if (GUILayout.Button ("Spawn random vehicle")) {
var spawner = FindObjectOfType<UIVehicleSpawner> ();
if (spawner)
spawner.SpawnVehicle ();
if (Ped.Instance != null)
Behaviours.Vehicles.Vehicle.CreateRandomInFrontOf(Ped.Instance.transform);
}
if (GUILayout.Button("Change player model"))
@ -91,7 +90,7 @@ namespace SanAndreasUnity.UI {
if (GUILayout.Button("Request vehicle"))
{
pr.RequestVehicleSpawn();
pr.RequestVehicleSpawn(-1);
}
if (GUILayout.Button("Request ped model change"))

View file

@ -86,7 +86,7 @@ namespace SanAndreasUnity.UI {
//GUILayout.BeginHorizontal ();
if (GUILayout.Button (v.GameName, GUILayout.Width(this.columnWidths[0]))) {
Behaviours.Vehicles.Vehicle.CreateInFrontOfPlayer (v.Id);
Behaviours.Vehicles.Vehicle.CreateInFrontOf (v.Id, Behaviours.Ped.Instance.transform);
}
//GUILayout.Label (v.ClassName, GUILayout.Width (this.columnWidths [1]));
//GUILayout.Label (v.Id.ToString(), GUILayout.Width (this.columnWidths [2]));

View file

@ -12,12 +12,14 @@
- send button input events to server:
- add ability for client to request: destroy my vehicles ; spawn stalker ped ;
- add ability for client to request: destroy my vehicles ;
- port the whole UI to multiplayer
- fix connection timeout bug
- add current date to logs
- create weapon prefab ; add it to spawnable prefabs list ;