/// <summary> /// Called on every frame update /// </summary> public override void Update() { bool didShoot; shootEvent.TryReceive(out didShoot); bool didReload; reloadEvent.TryReceive(out didReload); cooldownRemaining = (cooldownRemaining > 0) ? (cooldownRemaining - this.GetSimulation().FixedTimeStep) : 0f; if (cooldownRemaining > 0) { return; // Can't shoot yet } if ((remainingBullets == 0 && didShoot) || (remainingBullets < 9 && didReload)) { ReloadWeapon(); return; } if (!didShoot) { return; } remainingBullets--; UpdateBulletsLED(); cooldownRemaining = Cooldown; var raycastStart = Entity.Transform.WorldMatrix.TranslationVector; var forward = Entity.Transform.WorldMatrix.Forward; var raycastEnd = raycastStart + forward * MaxShootDistance; var result = this.GetSimulation().Raycast(raycastStart, raycastEnd); var weaponFired = new WeaponFiredResult { HitResult = result, DidFire = true, DidHit = false }; if (result.Succeeded && result.Collider != null) { weaponFired.DidHit = true; var rigidBody = result.Collider as RigidbodyComponent; if (rigidBody != null) { rigidBody.Activate(); rigidBody.ApplyImpulse(forward * ShootImpulse); rigidBody.ApplyTorqueImpulse(forward * ShootImpulse + new Vector3(0, 1, 0)); } } // Broadcast the fire event WeaponFired.Broadcast(weaponFired); }
/// <summary> /// Called on every frame update /// </summary> public override void Update() { bool didShoot; shootEvent.TryReceive(out didShoot); bool didReload; reloadEvent.TryReceive(out didReload); cooldownRemaining = (cooldownRemaining > 0) ? (cooldownRemaining - this.GetSimulation().FixedTimeStep) : 0f; if (cooldownRemaining > 0) return; // Can't shoot yet if ((remainingBullets == 0 && didShoot) || (remainingBullets < 9 && didReload)) { ReloadWeapon(); return; } if (!didShoot) return; remainingBullets--; UpdateBulletsLED(); cooldownRemaining = Cooldown; var raycastStart = Entity.Transform.WorldMatrix.TranslationVector; var forward = Entity.Transform.WorldMatrix.Forward; var raycastEnd = raycastStart + forward * MaxShootDistance; var result = this.GetSimulation().Raycast(raycastStart, raycastEnd); var weaponFired = new WeaponFiredResult {HitResult = result, DidFire = true, DidHit = false }; if (result.Succeeded && result.Collider != null) { weaponFired.DidHit = true; var rigidBody = result.Collider as RigidbodyComponent; if (rigidBody != null) { rigidBody.Activate(); rigidBody.ApplyImpulse(forward * ShootImpulse); rigidBody.ApplyTorqueImpulse(forward * ShootImpulse + new Vector3(0, 1, 0)); } } // Broadcast the fire event WeaponFired.Broadcast( weaponFired ); }