using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class DamageManager : MonoBehaviour
{
public int HP = 100;
public int HPmax = 100;
public bool CollideDeath = false;
public int Armor = 0;
public int ArmorMax = 100;
public int Level = 0;
public int MaxLevel = 100;
public int CurrentExp = 0;
public int ExpMax = 1000;
public int ExpAmount = 1;
public GameObject DeadReplacement;
public float DeadReplaceLifeTime = 60;
public bool isAlive = true;
public AudioClip[] SoundPain;
public AudioSource Audiosource;
public string LastHitByID = "";
[HideInInspector]
public int spawnLevel;
[HideInInspector]
public bool dieByLifeTime = false;
[HideInInspector]
public bool spectreThis = false;
public string Team = "";
void Start ()
{
Audiosource = this.GetComponent<AudioSource> ();
}
private bool isQuitting = false;
void OnApplicationQuit ()
{
isQuitting = true;
}
void Update ()
{
UpdateDamage ();
}
public void UpdateDamage ()
{
if (isAlive) {
if (HP > HPmax)
HP = HPmax;
if (Armor > ArmorMax)
Armor = ArmorMax;
if (Armor < 0)
Armor = 0;
if (CurrentExp >= ExpMax && Level < MaxLevel) {
Level = Level + 1;
CurrentExp = 0;
}
//if (Network.isServer || (!Network.isServer && !Network.isClient)) {
if (HP <= 0) {
isAlive = false;
if ((!Network.isServer && !Network.isClient)) {
AfterDead ();
GameObject.Destroy (this.gameObject);
} else {
if (networkView.isMine) {
OnDead ();
} //else {
//networkView.RPC ("OnDead", RPCMode.Others, null);
//}
}
}
//}
}
}
void OnTriggerEnter( Collider deathVolume ) // C#, type first, name in second
{
if (deathVolume.gameObject.tag == "InstantDeath")
{
HP -= 10000;
if (networkView.isMine && (Network.isClient || Network.isServer)) {
networkView.RPC ("HPUpdate", RPCMode.Others, HP, Armor);
}
}
}
void OnCollisionEnter (Collision collision)
{
if (CollideDeath) {
isAlive = false;
if ((!Network.isServer && !Network.isClient)) {
AfterDead ();
GameObject.Destroy (this.gameObject);
} else {
if (networkView.isMine) {
OnDead ();
} //else {
//networkView.RPC ("OnDead", RPCMode.Others, null);
//}
}
}
}
Vector3 directionHit;
public void ApplyDamage (int damage, Vector3 direction, string attackerID, string team)
{
LastHitByID = attackerID;
directionHit = direction;
if (HP < 0)
return;
if (Team != team || team == "") {
if (Armor > 0) {
if (damage > Armor) {
Armor = 0;
damage = damage - Armor;
HP -= damage;
}else{
Armor -= damage;
}
}else{
HP -= damage;
}
if (networkView.isMine && (Network.isClient || Network.isServer)) {
networkView.RPC ("HPUpdate", RPCMode.Others, HP, Armor);
}
}
if (Audiosource && SoundPain.Length > 0) {
Audiosource.PlayOneShot (SoundPain [Random.Range (0, SoundPain.Length)]);
}
}
public void HealDamage (int heal, Vector3 direction, string attackerID, string team)
{
if (HP < 0)
return;
if (Team != team || team == "") {
HP += heal;
if (networkView.isMine && (Network.isClient || Network.isServer)) {
networkView.RPC ("HPUpdate", RPCMode.Others, HP, Armor);
}
}
}
public void FixArmor (int restoreArmor, Vector3 direction, string attackerID, string team)
{
if (Team != team || team == "") {
Armor += restoreArmor;
if (networkView.isMine && (Network.isClient || Network.isServer)) {
networkView.RPC ("HPUpdate", RPCMode.Others, HP, Armor);
}
}
}
public void DirectDamage (DamagePackage pack)
{
ApplyDamage ((int)((float)pack.Damage),pack.Direction,pack.ID,pack.Team);
}
[RPC]
void HPUpdate (int hp, int armor)
{
HP = hp;
Armor = armor;
}
[RPC]
void OnDead ()
{
Network.Destroy (this.gameObject);
Network.RemoveRPCs (networkView.viewID);
AfterDead ();
}
void AfterDead ()
{
CharacterItemDroper itemdrop = this.GetComponent<CharacterItemDroper> ();
CharacterSystem character = this.GetComponent<CharacterSystem> ();
if (character) {
if (character.gameObject && character.gameManager.ScoreManage) {
if (character.ID == character.gameManager.ID) {
character.gameManager.ScoreManage.AddDead (1, character.ID);
}
character.gameManager.ScoreManage.AddScore (1, LastHitByID);
GiveExpToKiller();
}
}
if (itemdrop)
itemdrop.DropItem ();
}
public void GiveExpToKiller ()
{
expCalculation (ExpAmount);
Debug.Log ("Exp Gained");
}
void OnDestroy ()
{
if (!isQuitting && DeadReplacement && !Application.isLoadingLevel) {
Network.RemoveRPCs (networkView.viewID);
GameObject deadbody = (GameObject)GameObject.Instantiate (DeadReplacement, this.transform.position, Quaternion.identity);
if (spectreThis && deadbody) {
LookAfterDead (deadbody.gameObject);
}
CopyTransformsRecurse (this.transform, deadbody);
if (dieByLifeTime)
DeadReplaceLifeTime = 3;
GameObject.Destroy (deadbody, DeadReplaceLifeTime);
}
}
public void CopyTransformsRecurse (Transform src, GameObject dst)
{
dst.transform.position = src.position;
dst.transform.rotation = src.rotation;
if (dst.rigidbody)
dst.rigidbody.AddForce (directionHit, ForceMode.Impulse);
foreach (Transform child in dst.transform) {
var curSrc = src.Find (child.name);
if (curSrc) {
CopyTransformsRecurse (curSrc, child.gameObject);
}
}
}
void LookAfterDead (GameObject obj)
{
SpectreCamera spectre = (SpectreCamera)GameObject.FindObjectOfType (typeof(SpectreCamera));
if (spectre) {
spectre.LookingAtObject (obj);
}
}
public void expCalculation (int amount)
{
if (networkView.isMine || (!Network.isClient && !Network.isServer)) {
Experience(amount);
}
}
[RPC]
void expUpdate (int num)
{
CurrentExp = num;
Debug.Log ("Current EXP: "+num);
}
public void Experience (int num)
{
CurrentExp += num;
if (!networkView.isMine && (Network.isClient || Network.isServer)) {
networkView.RPC ("expUpdate", RPCMode.Others, CurrentExp);
}
}
}
It reaches GiveExpToKiller after the kill but does not grant the exp to the player character.