Tracks character health and lives. Triggers character damage/death
Inheritance: UnityEngine.MonoBehaviour
    void FixtureCreateHealth() {
      Configuration.ClearInstance();
      UpdateManager.ClearInstance();
      System.GC.Collect();

      var obj = new GameObject();
      config = obj.AddComponent<Configuration>();
      Assert.NotNull(config);
      umgr = obj.AddComponent<UpdateManager>();
      Assert.NotNull(umgr);


      var objx = new GameObject();
      character = objx.AddComponent<Character>();
      Assert.NotNull(character);
      health = objx.GetComponent<CharacterHealth>();
      Assert.NotNull(health);
      col2d = objx.GetComponent<PlatformerCollider2D>();
      Assert.NotNull(col2d);

      health.onHeal += () => { onHealCalled = true; };
      health.onDamage += () => { onDamageCalled = true; };
      health.onImmunity += () => { onImmunityCalled = true; };
      health.onMaxHealth += () => { onMaxHealthCalled = true; };
      health.onInjured += (Damage dt, CharacterHealth to) => { onInjuredCalled = true; };
      health.onHurt += (Damage dt, CharacterHealth to) => { onHurtCalled = true; };
      health.onDeath += () => { onDeathCalled = true; };
      health.onGameOver += () => { onGameOverCalled = true; };
      health.onInvulnerabilityStart += () => { onInvulnerabilityStartCalled = true; };
      health.onInvulnerabilityEnd += () => { onInvulnerabilityEndCalled = true; };
      health.onRespawn += () => { onRespawnCalled = true; };

      ResetCallbacks();
    }
    public TestCharacter(string name) {
      charObj = new GameObject();
      charObj.name = name;
      character = charObj.AddComponent<Character>();
      Assert.NotNull(character);
      health = charObj.GetComponent<CharacterHealth>();
      Assert.NotNull(health);
      health.Start();

      enterHitBox = charObj.CreateChild("entreAreas").AddComponent<HitBox>();
      enterHitBox.type = HitBoxType.EnterAreas;
      enterHitBox.gameObject.layer = 1;

      enterBox2D = enterHitBox.gameObject.AddComponent<BoxCollider2D>();
      enterBox2D.size = new Vector2(1, 1);
      enterHitBox.owner = health;
      enterHitBox.Start();

      dealHitBox = charObj.CreateChild("dealHitBox").AddComponent<HitBox>();
      Assert.NotNull(dealHitBox);

      dealHitBox.type = HitBoxType.DealDamage;
      dealHitBox.collideWith = (1 << 2); // | (1 << ?)
      dealHitBox.gameObject.layer = 2;
      damage = dealHitBox.gameObject.AddComponent<Damage>();
      damage.causer = health;
      damage.Start();
      dealHitBox.owner = health;

      dealBox2D = dealHitBox.gameObject.AddComponent<BoxCollider2D>();
      dealBox2D.size = new Vector2(1, 1);
      dealHitBox.Start();

      recieveHitBox = charObj.CreateChild("recieveHitBox").AddComponent<HitBox>();
      Assert.NotNull(recieveHitBox);

      recieveHitBox.type = HitBoxType.RecieveDamage;
      recieveHitBox.collideWith = (1 << 2);
      recieveHitBox.gameObject.layer = 2;
      recieveHitBox.owner = health;

      recieveBox2D = recieveHitBox.gameObject.AddComponent<BoxCollider2D>();
      recieveBox2D.size = new Vector2(1, 1);
      recieveHitBox.Start();

      // init health callbacks
      health.onHeal += () => { onHealCalled = true; };
      health.onDamage += () => { onDamageCalled = true; };
      health.onImmunity += () => { onImmunityCalled = true; };
      health.onMaxHealth += () => { onMaxHealthCalled = true; };
      health.onInjured += (Damage dt, CharacterHealth to) => { onInjuredCalled = true; };
      health.onHurt += (Damage dt, CharacterHealth to) => { onHurtCalled = true; };
      health.onDeath += () => { onDeathCalled = true; };
      health.onGameOver += () => { onGameOverCalled = true; };
      health.onInvulnerabilityStart += () => { onInvulnerabilityStartCalled = true; };
      health.onInvulnerabilityEnd += () => { onInvulnerabilityEndCalled = true; };
      health.onRespawn += () => { onRespawnCalled = true; };

    }
    /// <summary>
    /// Try to Damage the Character
    ///
    /// triggers onDamage
    /// triggers onDeath
    /// NOTE this won't trigger onHurtCharacter
    /// </summary>
    public bool Damage(int amount = 1, CharacterHealth causer = null) {
      if (amount <= 0) {
        Debug.LogWarning("amount <= 0 ??");
      }

      if (IsInvulnerable()) {
        Debug.Log(this.name + " is invulnerable, ignore damage");

        if (onDamage != null) {
          onDamage();
        }

        if (onImmunity != null) {
          onImmunity();
        }

        return false;
      }

      health -= amount;

      // do not set invulnerable a dead Character
      if (health > 0) {
        SetInvulnerable(invulnerabilityTimeAfterDamage);
      }

      if (onDamage != null) {
        onDamage();
      }

      if (onInjured != null) {
        onInjured(null, causer);
      }

      if (health <= 0) {
        Die();
      }

      return true;

    }
    /// <summary>
    /// Try to Damage the Character
    /// </summary>
    public bool Damage(int amount, DamageType dt, CharacterHealth causer = null) {
      Debug.LogFormat("immunity {0} DamageType {1}", immunity, dt);
      if ((immunity & dt) == dt) {
        Debug.LogFormat("Inmune to {0} attacks", dt);

        if (onDamage != null) {
          onDamage();
        }

        if (onImmunity != null) {
          onImmunity();
        }

        return false;
      }

      return Damage(amount, causer);
    }
 /// <summary>
 /// Not used atm
 /// </summary>
 public virtual void OnInjuredCharacter(Damage dt, CharacterHealth h, Character to) {
   //Debug.LogFormat("hurt {1} with {2} damage", to.gameObject, dt.amount);
 }
Example #6
0
    void Start() {
      // create the rope from bottom to top
      // and chain them
      gameObject.transform.DestroyImmediateChildren();

      health = GetComponent<CharacterHealth>();
      if (health != null) {
        health.onDeath += BreakRope;
      }

      sections = new GameObject[segments];

      GameObject anchor = CreateAnchor();

      // Create segments
      Rigidbody2D nextConnectedBody = anchor.GetComponent<Rigidbody2D>();

      for (int i = 0; i < segments; i++) {
        GameObject section = CreateSection(i, nextConnectedBody);

        // Update for next loop
        nextConnectedBody = section.GetComponent<Rigidbody2D>();
      }

      time = initialTime;
      if (time == 1) {
        timeSign = -1;
      }
      UpdateRotation();
    }
 void OnHurtCharacter(Damage dt, CharacterHealth to)
 {
     actionJump.ForceJump(new JumpConstant(character,
                                           jumpProperties.Clone((int)character.faceDir)
                                           ));
 }
 void OnEnable()
 {
     cHealth         = GetComponent <CharacterHealth>();
     character       = GetComponent <Character>();
     cHealth.onHurt += OnHurtCharacter;
 }
 void OnHurtCharacter(Damage dt, CharacterHealth to) {
   actionJump.ForceJump(new JumpConstant(character,
     jumpProperties.Clone((int) character.faceDir)
   ));
 }
 void OnEnable() {
   cHealth = GetComponent<CharacterHealth>();
   character = GetComponent<Character>();
   cHealth.onHurt += OnHurtCharacter;
 }