protected virtual bool Try_ChangeHealth(HealthEventData healthEventData) { if (Entity.Health.Get() == 0f) { return(false); } if (healthEventData.Delta > 0f && Entity.Health.Get() == 100f) { return(false); } float healthDelta = healthEventData.Delta; if (healthDelta < 0f) { healthDelta *= (1f - m_Resistance); } float newHealth = Mathf.Clamp(Entity.Health.Get() + healthDelta, 0f, 100f); Entity.Health.Set(newHealth); if (healthDelta < 0f) { m_HealthRegeneration.Pause(); } return(true); }
private void OnSuccess_PlayerHealthChanged(HealthEventData healthEventData) { if (healthEventData.Delta < 0f && healthEventData.Delta < -8f) { m_DamageShake.Shake(Mathf.Abs(healthEventData.Delta / 100f)); } }
protected virtual void Update() { if (m_HealthRegeneration.CanRegenerate && Entity.Health.Get() < 100f && Entity.Health.Get() > 0f) { var data = new HealthEventData(m_HealthRegeneration.RegenDelta); Entity.ChangeHealth.Try(data); } }
protected void DoHitscan(Camera camera) { float spread = Player.Aim.Active ? m_AimSpread : m_NormalSpread; RaycastHit hitInfo; Ray ray = camera.ViewportPointToRay(Vector2.one * 0.5f); Vector3 spreadVector = camera.transform.TransformVector(new Vector3(Random.Range(-spread, spread), Random.Range(-spread, spread), 0f)); ray.direction = Quaternion.Euler(spreadVector) * ray.direction; if (Physics.Raycast(ray, out hitInfo, m_MaxDistance, m_Mask, QueryTriggerInteraction.Ignore)) { float impulse = m_RayImpact.GetImpulseAtDistance(hitInfo.distance, m_MaxDistance); // Do damage. float damage = m_RayImpact.GetDamageAtDistance(hitInfo.distance, m_MaxDistance); var damageable = hitInfo.collider.GetComponent <IDamageable>(); if (damageable != null) { var damageData = new HealthEventData(-damage, Player, hitInfo.point, ray.direction, impulse); damageable.ReceiveDamage(damageData); } // Apply an impact impulse. else if (hitInfo.rigidbody) { hitInfo.rigidbody.AddForceAtPosition(ray.direction * impulse, hitInfo.point, ForceMode.Impulse); } // Bullet impact effects. if (GameController.SurfaceDatabase) { var data = GameController.SurfaceDatabase.GetSurfaceData(hitInfo); data.CreateBulletDecal(hitInfo.point + hitInfo.normal * 0.01f, Quaternion.LookRotation(hitInfo.normal), hitInfo.collider.transform); data.CreateBulletImpactFX(hitInfo.point + hitInfo.normal * 0.01f, Quaternion.LookRotation(hitInfo.normal)); data.PlaySound(ItemSelectionMethod.Randomly, SoundType.BulletImpact, 1f, hitInfo.point); } } // Create the tracer if a prefab is assigned. if (m_Tracer) { Instantiate(m_Tracer, transform.position + transform.TransformVector(m_TracerOffset), Quaternion.LookRotation(ray.direction)); } // Create the shell if a prefab is assigned. if (m_ShellPrefab && m_ShellSpawnMethod == ShellSpawnMethod.Auto) { SpawnShell(); } }
private void OnTriggerStay(Collider other) { if (!Active) { return; } var entity = other.GetComponent <EntityEventHandler>(); if (entity) { var healthEventData = new HealthEventData(-Random.Range(m_DamagePerSecond.x, m_DamagePerSecond.y) * Time.deltaTime); entity.ChangeHealth.Try(healthEventData); } }
public void ReceiveDamage(HealthEventData damageData) { if (enabled) { if (m_ParentEntity.Health.Get() > 0f) { damageData.Delta *= m_DamageMultiplier; m_ParentEntity.ChangeHealth.Try(damageData); } if (m_ParentEntity.Health.Get() == 0f) { m_Rigidbody.AddForceAtPosition(damageData.HitDirection * damageData.HitImpulse, damageData.HitPoint, ForceMode.Impulse); } } }
protected virtual void On_Hit() { var raycastData = Player.RaycastData.Get(); if (!raycastData) { return; } if (GameController.SurfaceDatabase) { var data = GameController.SurfaceDatabase.GetSurfaceData(raycastData.HitInfo); data.PlaySound(ItemSelectionMethod.Randomly, m_SoundType, 1f, raycastData.HitInfo.point); if (m_SoundType == SoundType.Hit) { data.CreateHitFX(raycastData.HitInfo.point, Quaternion.LookRotation(raycastData.HitInfo.normal)); } else if (m_SoundType == SoundType.Chop) { data.CreateChopFX(raycastData.HitInfo.point, Quaternion.LookRotation(raycastData.HitInfo.normal)); } } var damageable = raycastData.GameObject.GetComponent <IDamageable>(); if (damageable != null) { var damageData = new HealthEventData(-m_DamagePerHit, Player, transform.position, GameController.WorldCamera.transform.forward, m_ImpactForce); damageable.ReceiveDamage(damageData); } // Lower the durability... if (m_Durability != null) { var value = m_Durability.Float; value.Current--; m_Durability.SetValue(ItemProperty.Type.Float, value); if (value.Current == 0) { Player.DestroyEquippedItem.Try(); } } Hit.Send(); }
private void OnChanged_PlayerHealth(HealthEventData data) { if (data.Delta < 0f) { for (int i = 0; i < m_EquipmentHolders.Count; i++) { if (m_EquipmentHolders[i].HasItem && m_EquipmentHolders[i].CurrentItem.HasProperty("Durability")) { var durabilityProp = m_EquipmentHolders[i].CurrentItem.GetPropertyValue("Durability"); var floatVal = durabilityProp.Float; floatVal.Current--; durabilityProp.SetValue(ItemProperty.Type.Float, floatVal); if (floatVal.Current <= 0f) { m_EquipmentHolders[i].SetItem(null); } } } } }
protected override bool Try_ChangeHealth(HealthEventData healthEventData) { healthEventData.Delta *= (1f - Player.Defense.Get() / 100f); return(base.Try_ChangeHealth(healthEventData)); }
private void FixedUpdate() { if (m_Done) { return; } RaycastHit hitInfo; Ray ray = new Ray(transform.position, transform.forward); if (Physics.Raycast(ray, out hitInfo, m_MaxDistance, m_Mask, QueryTriggerInteraction.Ignore)) { var data = SurfaceDatabase.Instance.GetSurfaceData(hitInfo); float currentSpeed = m_Rigidbody.velocity.magnitude; if (currentSpeed >= m_PenetrationThreeshold && data != null) { data.PlaySound(ItemSelectionMethod.RandomlyButExcludeLast, m_PenetrationType, 1f, m_AudioSource); transform.SetParent(hitInfo.collider.transform, true); // Stick the projectile in the object. transform.position = hitInfo.point + transform.forward * m_PenetrationOffset; m_Pivot.localPosition = Vector3.back * m_PenetrationOffset; m_Model.SetParent(m_Pivot, true); float impulse = m_Rigidbody.mass * currentSpeed; var damageable = hitInfo.collider.GetComponent <IDamageable>(); // If the object is damageable... if (damageable != null) { float damage = m_MaxDamage * m_DamageCurve.Evaluate(1f - currentSpeed / m_LaunchSpeed); var damageData = new HealthEventData(-damage, m_EntityThatLaunched, hitInfo.point, ray.direction, impulse); damageable.ReceiveDamage(damageData); } // If the object is not damageable, but it's a rigidbody, apply the impact impulse. else if (hitInfo.rigidbody) { hitInfo.rigidbody.AddForceAtPosition(transform.forward * impulse, transform.position, ForceMode.Impulse); } StartCoroutine(C_DoTwang()); var hitbox = hitInfo.collider.GetComponent <HitBox>(); if (hitbox) { var cols = Physics.OverlapSphere(transform.position, 1.5f, m_Mask, QueryTriggerInteraction.Ignore); for (int i = 0; i < cols.Length; i++) { var colHitbox = cols[i].GetComponent <HitBox>(); if (colHitbox) { Physics.IgnoreCollision(colHitbox.Collider, m_Collider); } } } Physics.IgnoreCollision(m_Collider, hitInfo.collider); m_Rigidbody.isKinematic = true; } else { m_HitAudio.Play(ItemSelectionMethod.Randomly, m_AudioSource); m_Rigidbody.isKinematic = false; } m_Collider.enabled = true; m_Collider.isTrigger = true; m_Done = true; } }