private void SyncMagNetID(uint magazineID) { magNetID = magazineID; //if the magazine ID is invalid remove the magazine if (magazineID == NetId.Invalid) { CurrentMagazine = null; } else { //find the magazine by NetworkID GameObject magazine; if (isServer) { magazine = NetworkServer.FindLocalObject(magazineID); } else { magazine = ClientScene.FindLocalObject(magazineID); } if (magazine != null) { MagazineBehaviour magazineBehavior = magazine.GetComponent <MagazineBehaviour>(); CurrentMagazine = magazineBehavior; var cnt = magazine.GetComponent <CustomNetTransform>(); if (isServer) { cnt.DisappearFromWorldServer(); } else { cnt.DisappearFromWorld(); //force a sync when mag is loaded CurrentMagazine.SyncPredictionWithServer(); } Logger.LogTraceFormat("MagazineBehaviour found ok: {0}", Category.Firearms, magazineID); } } }
public bool WillInteract(InventoryApply interaction, NetworkSide side) { if (!DefaultWillInteract.Default(interaction, side)) { return(false); } MagazineBehaviour mag = interaction.TargetObject.GetComponent <MagazineBehaviour>(); if (mag == null) { return(false); } if (mag.ammoType != ammoType || !isClip) { return(false); } return(true); }
/// <summary> /// Loads as much ammo as possible from the given clip. Returns reloading message. /// </summary> public String LoadFromClip(MagazineBehaviour clip) { if (clip == null) { return(""); } int toTransfer = Math.Min(magazineSize - serverAmmoRemains, clip.serverAmmoRemains); clip.ExpendAmmo(toTransfer); if (magType == MagType.Standard) { for (int i = toTransfer; i != 0; i--) { containedBullets.Add(clip.initalProjectile); containedProjectilesFired.Add(clip.ProjectilesFired); } } ServerSetAmmoRemains(serverAmmoRemains + toTransfer); return("Loaded " + toTransfer + (toTransfer == 1 ? " piece" : " pieces") + " of ammunition."); }
/// <summary> /// attempt to reload the weapon with the item given /// </summary> private void TryReload(MagazineBehaviour magazine) { if (CurrentMagazine == null) { //RELOAD // If the item used on the gun is a magazine, check type and reload string ammoType = magazine.ammoType; if (AmmoType == ammoType) { var hand = UIManager.Hands.CurrentSlot.NamedSlot; RequestReload(magazine.gameObject, hand, true); } if (AmmoType != ammoType) { Chat.AddExamineMsgToClient("You try to load the wrong ammo into your weapon"); } } else if (AmmoType == magazine.ammoType) { Chat.AddExamineMsgToClient("You weapon is already loaded, you can't fit more Magazines in it, silly!"); } }
public void CmdShootBullet(GameObject weapon, GameObject magazine, Vector2 direction, string bulletName) { if (!playerMove.allowInput || playerMove.isGhost) { return; } //get componants Weapon wepBehavior = weapon.GetComponent <Weapon>(); MagazineBehaviour magBehaviour = magazine.GetComponent <MagazineBehaviour>(); //reduce ammo for shooting magBehaviour.ammoRemains--; //TODO: remove more bullets if burst //get the bullet prefab being shot GameObject bullet = GameObject.Instantiate(Resources.Load(bulletName) as GameObject, transform.position, Quaternion.identity); var angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; //if we have recoil variance add it, and get the new attack angle if (wepBehavior != null && wepBehavior.CurrentRecoilVariance > 0) { direction = GetRecoilOffset(wepBehavior, angle); } BulletBehaviour b = bullet.GetComponent <BulletBehaviour>(); b.Shoot(direction, angle, gameObject.name); //add additional recoil after shooting for the next round AppendRecoil(wepBehavior, angle); //This is used to determine where bullet shot should head towards on client Ray2D ray = new Ray2D(transform.position, direction); RpcShootBullet(weapon, ray.GetPoint(30f), bulletName); //TODO add a check to see if bullet or energy weapon SpawnBulletCaseing(); }
/// <summary> /// attempt to reload the weapon with the item given /// </summary> public bool TryReload(GameObject item) { string hand; if (item != null) { MagazineBehaviour magazine = item.GetComponent <MagazineBehaviour>(); if (magazine) { if (CurrentMagazine == null) { //RELOAD // If the item used on the gun is a magazine, check type and reload string ammoType = magazine.ammoType; if (AmmoType == ammoType) { hand = UIManager.Hands.CurrentSlot.eventName; RequestReload(item, hand, true); } if (AmmoType != ammoType) { ChatRelay.Instance.AddToChatLogClient("You try to load the wrong ammo into your weapon", ChatChannel.Examine); } } else if (AmmoType == magazine.ammoType) { ChatRelay.Instance.AddToChatLogClient( "You weapon is already loaded, you can't fit more Magazines in it, silly!", ChatChannel.Examine); } } return(false); } else { return(true); } }
public override void ServerPerformInteraction(InventoryApply interaction) { if (interaction.TargetObject == gameObject && interaction.IsFromHandSlot) { if (Validations.HasItemTrait(interaction.UsedObject, CommonTraits.Instance.Screwdriver) && CurrentMagazine != null && allowScrewdriver) { PowerCellRemoval(interaction); } MagazineBehaviour mag = interaction.UsedObject.GetComponent <MagazineBehaviour>(); if (mag) { base.RequestReload(mag.gameObject); } else if (Validations.HasItemTrait(interaction.UsedObject, CommonTraits.Instance.Wirecutter) && allowPinSwap) { PinRemoval(interaction); } else if (Validations.HasItemTrait(interaction.UsedObject, CommonTraits.Instance.FiringPin) && allowPinSwap) { PinAddition(interaction); } } }
/// <summary> /// Invoked on all clients and server when the server updates MagNetID, updating the local version of this weapon to /// load / unload the mag. /// </summary> /// <param name="magazineID">id of the new magazine to load, NetworkInstanceId.Invalid if we should unload /// the current mag</param> private void OnMagNetIDChanged(NetworkInstanceId magazineID) { //if the magazine ID is invalid remove the magazine if (magazineID == NetworkInstanceId.Invalid) { CurrentMagazine = null; } else { if (CurrentMagazine != null) { Logger.LogError("Attempted to load a new mag while a " + "mag is still in the weapon in our local instance of the game. " + "This is probably a bug. Continuing anyway..."); } //find the magazine by NetworkID GameObject magazine = ClientScene.FindLocalObject(magazineID); if (magazine != null) { MagazineBehaviour magazineBehavior = magazine.GetComponent <MagazineBehaviour>(); CurrentMagazine = magazineBehavior; var cnt = magazine.GetComponent <CustomNetTransform>(); if (isServer) { cnt.DisappearFromWorldServer(); } else { cnt.DisappearFromWorld(); } Logger.LogTraceFormat("MagazineBehaviour found ok: {0}", Category.Firearms, magazineID); //sync ammo count now that we will potentially be shooting this locally. SyncMagAmmoAndRNG(); } } }
/// <summary> /// attempt to reload the weapon with the item given /// </summary> private void TryReload(GameObject ammo) { MagazineBehaviour magazine = ammo.GetComponent <MagazineBehaviour>(); if (CurrentMagazine == null || (MagInternal && magazine.isClip)) { //RELOAD // If the item used on the gun is a magazine, check type and reload AmmoType ammoType = magazine.ammoType; if (this.ammoType == ammoType) { var hand = UIManager.Hands.CurrentSlot.NamedSlot; RequestReload(magazine.gameObject, hand, true); } if (this.ammoType != ammoType) { Chat.AddExamineMsgToClient("You try to load the wrong ammo into your weapon"); } } else if (ammoType == magazine.ammoType) { Chat.AddExamineMsgToClient("You weapon is already loaded, you can't fit more Magazines in it, silly!"); } }
/// <summary> /// Loads as much ammo as possible from the given clip. Returns reloading message. /// </summary> public String LoadFromClip(MagazineBehaviour clip) { if (clip == null) { return(""); } int toTransfer = Math.Min(magazineSize - serverAmmoRemains, clip.serverAmmoRemains); clip.ExpendAmmo(toTransfer); if (!isClip && !isCell) { for (int i = toTransfer; i != 0; i--) { containedBullets.Add(clip.Projectile); containedProjectilesFired.Add(clip.ProjectilesFired); } UpdateProjectile(); //sets the projectile that will be fired next //this is here in the case that we had no ammo loaded, so the 0th entry was changed } ServerSetAmmoRemains(serverAmmoRemains + toTransfer); return("Loaded " + toTransfer + (toTransfer == 1 ? " piece" : " pieces") + " of ammunition."); }
private void UpdateMe() { //don't process if we are server and the gun is not held by anyone if (isServer && serverHolder == null) { return; } //if we are client, make sure we've initialized if (!isServer && !PlayerManager.LocalPlayer) { return; } //if we are client, only process this if we are holding it if (!isServer) { if (UIManager.Hands == null || UIManager.Hands.CurrentSlot == null) { return; } var heldItem = UIManager.Hands.CurrentSlot.ItemObject; if (gameObject != heldItem) { return; } } //update the time until the next shot can happen if (FireCountDown > 0) { FireCountDown -= Time.deltaTime; //prevents the next projectile taking miliseconds longer than it should if (FireCountDown < 0) { FireCountDown = 0; } } //remaining logic is server side only //this will only be executed on the server since only the server //maintains the queued actions if (queuedShots.Count > 0 && FireCountDown <= 0) { //fire the next shot in the queue DequeueAndProcessServerShot(); } if (queuedUnload && queuedShots.Count == 0) { // done processing shot queue, // perform the queued unload action, causing all clients and server to update their version of this Weapon // due to the syncvar hook // there should not be an unload action for internal magazines Inventory.ServerDrop(magSlot); queuedUnload = false; } if (queuedLoadMagNetID != NetId.Invalid && queuedShots.Count == 0) { //done processing shot queue, perform the reload, causing all clients and server to update their version of this Weapon //due to the syncvar hook if (MagInternal) { var clip = NetworkIdentity.spawned[queuedLoadMagNetID]; MagazineBehaviour clipComp = clip.GetComponent <MagazineBehaviour>(); string message = CurrentMagazine.LoadFromClip(clipComp); Chat.AddExamineMsg(serverHolder, message); queuedLoadMagNetID = NetId.Invalid; } else { var magazine = NetworkIdentity.spawned[queuedLoadMagNetID]; var fromSlot = magazine.GetComponent <Pickupable>().ItemSlot; Inventory.ServerTransfer(fromSlot, magSlot); queuedLoadMagNetID = NetId.Invalid; } } }