2020-05-31 17:07:22 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using SanAndreasUnity.Utilities;
|
2020-05-18 23:00:24 +00:00
|
|
|
|
using SanAndreasUnity.Net;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Behaviours
|
|
|
|
|
{
|
2021-09-06 22:28:37 +00:00
|
|
|
|
|
|
|
|
|
public static class PedDamageInfoExtensions
|
|
|
|
|
{
|
|
|
|
|
public static Ped GetAttackerPed(this DamageInfo damageInfo)
|
|
|
|
|
{
|
|
|
|
|
return damageInfo.attacker as Ped;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-23 23:25:49 +00:00
|
|
|
|
public partial class Ped {
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
public Damageable Damageable { get; private set; }
|
|
|
|
|
|
|
|
|
|
public float Health { get { return this.Damageable.Health; } set { this.Damageable.Health = value; } }
|
|
|
|
|
[SerializeField] private float m_maxHealth = 100f;
|
|
|
|
|
public float MaxHealth { get { return m_maxHealth; } set { m_maxHealth = value; } }
|
|
|
|
|
|
|
|
|
|
public Bar HealthBar { get; private set; }
|
|
|
|
|
|
2021-01-04 00:28:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Damage info that killed the ped.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DamageInfo KillingDamageInfo { get; set; }
|
|
|
|
|
|
2021-10-19 21:45:07 +00:00
|
|
|
|
public float LastTimeWhenDamaged { get; private set; }
|
|
|
|
|
public float TimeSinceDamaged => Time.time - this.LastTimeWhenDamaged;
|
|
|
|
|
|
2021-01-05 04:18:47 +00:00
|
|
|
|
private bool m_alreadyKilled = false;
|
|
|
|
|
|
2021-02-19 01:55:09 +00:00
|
|
|
|
public class DamageResult
|
|
|
|
|
{
|
|
|
|
|
public float DamageAmount { get; }
|
|
|
|
|
|
|
|
|
|
public DamageResult()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DamageResult(float damageAmount)
|
|
|
|
|
{
|
|
|
|
|
DamageAmount = damageAmount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static event System.Action<Ped, DamageInfo, DamageResult> onDamaged = delegate {};
|
|
|
|
|
|
2022-01-01 21:28:32 +00:00
|
|
|
|
public struct UnderAimInfo
|
|
|
|
|
{
|
|
|
|
|
public DamageInfo damageInfo;
|
|
|
|
|
public float time;
|
|
|
|
|
public Ped ped;
|
|
|
|
|
|
|
|
|
|
public UnderAimInfo(DamageInfo damageInfo, float time, Ped ped)
|
|
|
|
|
{
|
|
|
|
|
this.damageInfo = damageInfo;
|
|
|
|
|
this.time = time;
|
|
|
|
|
this.ped = ped;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<UnderAimInfo> _underAimInfos = new List<UnderAimInfo>();
|
|
|
|
|
public IReadOnlyList<UnderAimInfo> UnderAimInfos => _underAimInfos;
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void AwakeForDamage ()
|
|
|
|
|
{
|
|
|
|
|
this.Damageable = this.GetComponentOrThrow<Damageable> ();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StartForDamage ()
|
|
|
|
|
{
|
|
|
|
|
this.CreateHealthBar ();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateHealthBar ()
|
|
|
|
|
{
|
|
|
|
|
this.HealthBar = Object.Instantiate (GameManager.Instance.barPrefab, this.transform).GetComponentOrThrow<Bar> ();
|
|
|
|
|
// this.HealthBar.SetBorderWidth (0.1f);
|
2020-04-22 21:26:55 +00:00
|
|
|
|
this.HealthBar.BackgroundColor = PedManager.Instance.healthBackgroundColor;
|
|
|
|
|
this.HealthBar.FillColor = PedManager.Instance.healthColor;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
this.HealthBar.BorderColor = Color.black;
|
|
|
|
|
|
|
|
|
|
this.UpdateHealthBar ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateDamageStuff ()
|
|
|
|
|
{
|
|
|
|
|
this.UpdateHealthBar ();
|
2022-01-01 21:28:32 +00:00
|
|
|
|
|
|
|
|
|
// remove UnderAim info that is no longer valid
|
|
|
|
|
_underAimInfos.RemoveAll(this.ShouldUnderAimInfoBeRemoved);
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateHealthBar ()
|
|
|
|
|
{
|
2021-10-19 21:45:07 +00:00
|
|
|
|
bool shouldBeVisible = this.TimeSinceDamaged < PedManager.Instance.healthBarVisibleTimeAfterDamage && !this.IsControlledByLocalPlayer;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
this.HealthBar.gameObject.SetActive (shouldBeVisible);
|
|
|
|
|
|
|
|
|
|
if (shouldBeVisible)
|
|
|
|
|
{
|
|
|
|
|
this.HealthBar.BarSize = new Vector3 (PedManager.Instance.healthBarWorldWidth, PedManager.Instance.healthBarWorldHeight, 1.0f);
|
|
|
|
|
this.HealthBar.SetFillPerc (this.Health / this.MaxHealth);
|
|
|
|
|
this.HealthBar.transform.position = this.GetPosForHealthBar ();
|
|
|
|
|
this.HealthBar.MaxHeightOnScreen = PedManager.Instance.healthBarMaxScreenHeight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DrawHealthBar ()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Vector3 pos = this.GetPosForHealthBar ();
|
|
|
|
|
|
|
|
|
|
Rect rect = GUIUtils.GetRectForBarAsBillboard (pos, PedManager.Instance.healthBarWorldWidth,
|
|
|
|
|
PedManager.Instance.healthBarWorldHeight, Camera.main);
|
|
|
|
|
|
|
|
|
|
// limit height
|
|
|
|
|
rect.height = Mathf.Min (rect.height, PedManager.Instance.healthBarMaxScreenHeight);
|
|
|
|
|
|
|
|
|
|
float borderWidth = Mathf.Min( 2f, rect.height / 4f );
|
2020-04-22 21:26:55 +00:00
|
|
|
|
GUIUtils.DrawBar( rect, this.Health / this.MaxHealth, PedManager.Instance.healthColor, PedManager.Instance.healthBackgroundColor, borderWidth );
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Vector3 GetPosForHealthBar ()
|
|
|
|
|
{
|
|
|
|
|
if (null == this.PlayerModel.Head)
|
|
|
|
|
return this.transform.position;
|
|
|
|
|
|
|
|
|
|
Vector3 pos = this.PlayerModel.Head.position;
|
|
|
|
|
pos += this.transform.up * PedManager.Instance.healthBarVerticalOffset;
|
|
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-16 23:50:30 +00:00
|
|
|
|
public void OnDamaged()
|
|
|
|
|
{
|
2020-05-18 23:00:24 +00:00
|
|
|
|
if (!NetStatus.IsServer)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-06-27 20:12:22 +00:00
|
|
|
|
if (this.Health <= 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-10-19 21:45:07 +00:00
|
|
|
|
this.LastTimeWhenDamaged = Time.time;
|
|
|
|
|
|
2021-02-19 01:55:09 +00:00
|
|
|
|
var damageInfo = this.Damageable.LastDamageInfo;
|
|
|
|
|
|
|
|
|
|
var damageResult = this.CurrentState.OnDamaged(damageInfo);
|
2020-05-18 23:00:24 +00:00
|
|
|
|
|
2021-02-19 01:55:09 +00:00
|
|
|
|
F.InvokeEventExceptionSafe(onDamaged, this, damageInfo, damageResult);
|
2020-05-16 23:50:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 23:00:24 +00:00
|
|
|
|
public void SendDamagedEventToClients(DamageInfo damageInfo, float damageAmount)
|
|
|
|
|
{
|
2021-09-06 22:28:37 +00:00
|
|
|
|
Ped attackingPed = damageInfo.GetAttackerPed();
|
2020-05-18 23:00:24 +00:00
|
|
|
|
|
|
|
|
|
PedSync.SendDamagedEvent(this.gameObject, attackingPed != null ? attackingPed.gameObject : null, damageAmount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnReceivedDamageEventFromServer(float damageAmount, Ped attackingPed)
|
|
|
|
|
{
|
2021-11-20 18:08:02 +00:00
|
|
|
|
this.LastTimeWhenDamaged = Time.time;
|
|
|
|
|
|
2021-02-22 22:56:58 +00:00
|
|
|
|
if (attackingPed != null && attackingPed.IsControlledByLocalPlayer && attackingPed != this)
|
2020-05-18 23:00:24 +00:00
|
|
|
|
{
|
|
|
|
|
this.DisplayInflictedDamageMessage(damageAmount);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DisplayInflictedDamageMessage(float damageAmount)
|
|
|
|
|
{
|
2020-06-06 16:09:26 +00:00
|
|
|
|
var msg = OnScreenMessageManager.Instance.CreateMessage();
|
|
|
|
|
|
|
|
|
|
msg.velocity = Random.insideUnitCircle.normalized * PedManager.Instance.inflictedDamageMessageVelocityInScreenPerc;
|
|
|
|
|
msg.TextColor = PedManager.Instance.inflictedDamageMessageColor;
|
|
|
|
|
msg.timeLeft = PedManager.Instance.inflictedDamageMessageLifetime;
|
2021-02-22 22:56:58 +00:00
|
|
|
|
msg.Text = Mathf.RoundToInt(damageAmount).ToString();
|
2020-05-18 23:00:24 +00:00
|
|
|
|
}
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-01-05 04:18:47 +00:00
|
|
|
|
public void Kill()
|
|
|
|
|
{
|
|
|
|
|
F.RunExceptionSafe(this.KillInternal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KillInternal()
|
|
|
|
|
{
|
|
|
|
|
if (m_alreadyKilled)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_alreadyKilled = true;
|
|
|
|
|
|
2021-02-06 20:11:14 +00:00
|
|
|
|
this.CurrentState.KillPed();
|
2021-01-05 04:18:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-01 21:28:32 +00:00
|
|
|
|
public void OnUnderAimOfOtherPed(DamageInfo damageInfo)
|
|
|
|
|
{
|
|
|
|
|
Ped attackerPed = damageInfo.GetAttackerPed();
|
|
|
|
|
if (null == attackerPed)
|
|
|
|
|
throw new System.Exception("No attacker ped given");
|
|
|
|
|
|
|
|
|
|
int index = _underAimInfos.FindIndex(_ => _.ped == attackerPed);
|
|
|
|
|
if (index >= 0)
|
|
|
|
|
{
|
|
|
|
|
_underAimInfos[index] = new UnderAimInfo(damageInfo, Time.time, attackerPed);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_underAimInfos.Add(new UnderAimInfo(damageInfo, Time.time, attackerPed));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool ShouldUnderAimInfoBeRemoved(UnderAimInfo underAimInfo)
|
|
|
|
|
{
|
|
|
|
|
if (null == underAimInfo.ped)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return Time.time - underAimInfo.time > PedManager.Instance.timeIntervalToUpdateUnderAimStatus;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|