(C#) A Useful Health Class

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
flipnode
Posts: 15
Joined: Thu Jul 27, 2006 1:24 am

(C#) A Useful Health Class

Post by flipnode »

Comments on this .NET class that I've created? Good design or Bad? Am I missing anything?

Helpful comments requested, please.

Code: Select all

using System;

namespace BloodRage
{
    namespace Core
    {
        class BRHealth
        {
            protected int _Health = 0;
            protected int _Death = 0;
            protected int _FullHealth = 0;
            protected bool _Alive = false;

            public bool IsAlive
            {
                get { return (this._Alive); }
            }

            public int Health
            {
                get { return (this._Health); }
                set { UpdateHealth(value); }
            }

            // constructor
            public BRHealth(int health, int death, int fullHealth)
            {
                this.Init(health, death, fullHealth);
            }

            // Deconstructor
            ~BRHealth()
            {
            }

            private bool Init(int health, int death, int fullHealth)
            {
                //if(health < death || fullHealth < death)
                //{
                //    throw
                //}
                this._Health = health;
                this._Death = death;
                this._FullHealth = fullHealth;

                // IsAlive?
                return(this.CheckHealth());
            }
                        
            private void UpdateHealth(int health)
            {
                this._Health = health;
                this.CheckHealth();
            }

            protected bool CheckHealth()
            {
                // false : oh crap he is dead!
                // true  : yup heart is still beating!
                return(this._Alive = (this._Health == this._Death ? false : true));                
            }
        }
    }
}
Last edited by flipnode on Wed Jun 13, 2007 12:49 pm, edited 1 time in total.
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

Well, technically your Health could go under Death and since you check

protected bool CheckHealth()
{
// false : oh crap he is dead!
// true : yup heart is still beating!
return(this._Alive = (this._Health == this._Death ? false : true));
}

with == you might miss it... better check like you do above, with <=
lester
Posts: 86
Joined: Mon Jan 29, 2007 3:33 pm

Post by lester »

you've certainly missed the public void TurnUndead() function
flipnode
Posts: 15
Joined: Thu Jul 27, 2006 1:24 am

Post by flipnode »

You are correct. I missed that <= logic. I think that thought popped into my head, but I forgot to code it.

Hmm you might be right about the TurnUndead() function.
I'll add that to the class.

Anything else?
lester
Posts: 86
Joined: Mon Jan 29, 2007 3:33 pm

Post by lester »

lol I was just kidding about TurnUndead. Maybe I should be more cautious in sentences :-/
Post Reply