Commit 258baf77 authored by Danny Huang's avatar Danny Huang
Browse files

implem life steal modifier, update enemy and hero prefabs, moved enemy...

implem life steal modifier, update enemy and hero prefabs, moved enemy oncollosionenter2d to health script
parent 80d20cb9
......@@ -3,5 +3,16 @@ using System.Collections;
public class EnemyHealthCtrl : HealthCtrl
{
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag ("Projectile")) {
ProjectileController projCtrl = other.gameObject.GetComponent<ProjectileController> ();
if (projCtrl.isHeroProjectile ()) {
int damage = projCtrl.getDamage ();
this.ModifyCurrentHealth(-damage);
projCtrl.RegisterHit ();
Destroy (other.gameObject);
}
}
}
}
......@@ -74,18 +74,7 @@ public class enemyScript : MonoBehaviour {
transform.position = new Vector3(transform.position.x, transform.position.y, 0);
}
void OnCollisionEnter2D(Collision2D other){
if (other.gameObject.CompareTag ("Projectile")) {
//get damage
ProjectileController projCtrl = other.gameObject.GetComponent<ProjectileController> ();
int damage = projCtrl.getDamage ();
this.healthCtrl.ModifyCurrentHealth(-damage);
Destroy (other.gameObject);
}
}
// void OnCollisionEnter2D(Collision2D other){
// if (other.CompareTag ("Projectile")) {
......
......@@ -46,18 +46,20 @@ public class HeroHealthCtrl : HealthCtrl
if (other.gameObject.CompareTag("Projectile"))
{
ProjectileController projCtrl = other.gameObject.GetComponent<ProjectileController>();
int damage = projCtrl.getDamage();
if (!projCtrl.isHeroProjectile ()) {
int damage = projCtrl.getDamage();
/* If hat equipped, let it absorb the projectile damage */
GameObject hat = this.heroInventory.GetEquippedHat();
if (hat != null)
{
damage = hat.GetComponent<HatController>().ReceiveProjectileImpact(other.gameObject);
}
/* If hat equipped, let it absorb the projectile damage */
GameObject hat = this.heroInventory.GetEquippedHat();
if (hat != null)
{
damage = hat.GetComponent<HatController>().ReceiveProjectileImpact(other.gameObject);
}
this.ModifyCurrentHealth(-damage);
this.ModifyCurrentHealth(-damage);
Destroy(other.gameObject);
Destroy(other.gameObject);
}
}
}
......
......@@ -539,4 +539,10 @@ public class HeroInventory : MonoBehaviour
return false;
}
}
/* Wrapper function that's called from a gun controller */
public void ModifyCurrentHealth(int amount)
{
this.healthCtrl.ModifyCurrentHealth (amount);
}
}
......@@ -269,9 +269,33 @@ public class GunController : ItemCtrl {
Vector3 newPos = triggerFingerPos - triggerLocalPos;
this.transform.position = new Vector2(newPos.x, newPos.y);
}
protected int GetLifeStolenPerHit()
{
int lifeSteal = 0;
foreach (ItemModifier modifier in base.item.modifiers) {
if (modifier.itemModifierInfo.desc == "Steal {}% life per hit") {
lifeSteal = modifier.getValue ();
break;
}
}
return lifeSteal;
}
/* Handle event where projectile successfully hit an enemy. Enemy should call this function */
public void RegisterHit()
{
int lifeStolen = GetLifeStolenPerHit ();
Transform gunUser = this.transform.parent.parent;
if (lifeStolen > 0 && gunUser != null) {
gunUser.BroadcastMessage("ModifyCurrentHealth", lifeStolen,
SendMessageOptions.DontRequireReceiver);
}
}
/* Functions for UI */
......
......@@ -7,7 +7,7 @@ public class ProjectileController : MonoBehaviour {
private Rigidbody2D rb;
/*Reference to the gun that fired this projectile*/
private GunController gunCtrl;
protected GunController gunCtrl;
/* Number of seconds until projectile destroys itself after collision */
public float durationAfterCollision_sec;
......@@ -70,4 +70,9 @@ public class ProjectileController : MonoBehaviour {
{
return (this.gunCtrl.gameObject.transform.parent.name == "InventoryBag");
}
public void RegisterHit()
{
this.gunCtrl.RegisterHit ();
}
}
\ No newline at end of file
......@@ -109,9 +109,8 @@ public class HatController : ItemCtrl {
*/
public int ReceiveProjectileImpact(GameObject incomingProjectile)
{
ProjectileController projCtrl = (ProjectileController) this.transform.parent.GetComponent (
typeof(ProjectileController));
ProjectileController projCtrl = (ProjectileController) incomingProjectile.GetComponent<ProjectileController>();
Debug.Log ("received");
/*Get amount of damage that this projectile inflicts and subtract it from hat's durability*/
int damage = projCtrl.getDamage ();
this.durability -= damage;
......
m_EditorVersion: 5.4.0f3
m_EditorVersion: 5.4.1f1
m_StandardAssetsVersion: 0
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment