weapon creation done ?

This commit is contained in:
in0finite 2019-07-06 17:36:17 +02:00
parent 986e05d2ae
commit 8075e21449
3 changed files with 100 additions and 2 deletions

View file

@ -9,6 +9,7 @@ using System.Linq;
using System.Collections.Generic;
using SanAndreasUnity.Importing.Animation;
using System.Reflection;
using SanAndreasUnity.Net;
namespace SanAndreasUnity.Behaviours
{
@ -145,6 +146,8 @@ namespace SanAndreasUnity.Behaviours
#endregion
*/
NetworkedWeapon m_netWeapon;
static Weapon ()
@ -161,11 +164,36 @@ namespace SanAndreasUnity.Behaviours
public static Weapon Load (int modelId)
{
WeaponDef def = Item.GetDefinition<WeaponDef> (modelId);
NetStatus.ThrowIfNotOnServer();
WeaponDef def;
WeaponData weaponData;
GameObject go = CreatePart1(modelId, out def, out weaponData);
if (null == go)
return null;
// assign model id before spawning it
go.GetComponentOrThrow<NetworkedWeapon>().ModelId = modelId;
// assign owner ped
// spawn game object here
NetManager.Spawn(go);
return CreatePart2(go, def, weaponData);
}
static GameObject CreatePart1(int modelId, out WeaponDef def, out WeaponData weaponData)
{
def = null;
weaponData = null;
def = Item.GetDefinition<WeaponDef> (modelId);
if (null == def)
return null;
WeaponData weaponData = WeaponData.LoadedWeaponsData.FirstOrDefault (wd => wd.modelId1 == def.Id);
var defCopyRef = def;
weaponData = WeaponData.LoadedWeaponsData.FirstOrDefault (wd => wd.modelId1 == defCopyRef.Id);
if (null == weaponData)
return null;
@ -183,6 +211,14 @@ namespace SanAndreasUnity.Behaviours
geoms.AttachFrames (go.transform, MaterialFlags.Default);
return go;
}
static Weapon CreatePart2(GameObject go, WeaponDef def, WeaponData weaponData)
{
int modelId = def.Id;
Weapon weapon = AddWeaponComponent (go, weaponData);
weapon.definition = def;
weapon.data = weaponData;
@ -216,6 +252,19 @@ namespace SanAndreasUnity.Behaviours
return weapon;
}
internal static void OnCreatedByServer(int modelId)
{
WeaponDef def;
WeaponData weaponData;
GameObject go = CreatePart1(modelId, out def, out weaponData);
if (null == go)
return;
CreatePart2(go, def, weaponData);
}
private static Weapon AddWeaponComponent (GameObject go, WeaponData data)
{
// find type which inherits Weapon class, and whose name matches the one in data
@ -254,6 +303,7 @@ namespace SanAndreasUnity.Behaviours
protected virtual void Awake ()
{
m_netWeapon = this.GetComponentOrThrow<NetworkedWeapon>();
this.GunFlash = this.transform.FindChildRecursive("gunflash");
}

View file

@ -0,0 +1,37 @@
using UnityEngine;
using Mirror;
using SanAndreasUnity.Utilities;
namespace SanAndreasUnity.Behaviours.Weapons
{
public class NetworkedWeapon : NetworkBehaviour
{
[SyncVar] int m_net_modelId;
[SyncVar] int m_net_ammoInClip;
[SyncVar] int m_net_amooOutsideOfClip;
public int ModelId { get { return m_net_modelId; } set { m_net_modelId = value; } }
public override void OnStartClient()
{
base.OnStartClient();
if (NetUtils.IsServer)
return;
// create weapon
F.RunExceptionSafe( () => Weapon.OnCreatedByServer(m_net_modelId) );
}
void Start()
{
}
}
}

View file

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