Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I've posted a simple implementation of this some time ago on StackOverflow. It's written in Python, but maybe it can still help you:
http://stackoverflow.com/a/7294148/627005https://stackoverflow.com/a/7294148/627005

I've posted a simple implementation of this some time ago on StackOverflow. It's written in Python, but maybe it can still help you:
http://stackoverflow.com/a/7294148/627005

I've posted a simple implementation of this some time ago on StackOverflow. It's written in Python, but maybe it can still help you:
https://stackoverflow.com/a/7294148/627005

replaced http://gamedev.stackexchange.com/ with https://gamedev.stackexchange.com/
Source Link

I've come to use attributes and behaviors myselfI've come to use attributes and behaviors myself, instead of plain old components. However, from your description of how you'd use the system in a Breakout game, I think you're overdoing it.

@heishe's commentheishe's comment

I've come to use attributes and behaviors myself, instead of plain old components. However, from your description of how you'd use the system in a Breakout game, I think you're overdoing it.

@heishe's comment

I've come to use attributes and behaviors myself, instead of plain old components. However, from your description of how you'd use the system in a Breakout game, I think you're overdoing it.

@heishe's comment

deleted 17 characters in body
Source Link
Paul Manta
  • 3.2k
  • 3
  • 31
  • 42
class HealthAttribute : public EntityAttribute
{
  public:
    HealthAttribute(Entity* entity, double max, double critical)
        : max_(max), critical_(critical), current_(max)
    { }

    double value() const {
        return current_;
    }    

    void value(double val)
    {
        // Ensure that 0 <= current <= max 
        current_ =if std::max(0, <= val && val <= max_);
        current_ = std::min(  current_, max_);= val;

        // Notify other components belonging to this entity that
        // health is too low.
        if (current_ <= critical_) {
            auto ev = std::shared_ptr<Event>(new HealthCriticalEvent())
            entity_->events().post(ev)
        }
    }

  private:
    double current_, max_, critical_;
};
class HealthAttribute : public EntityAttribute
{
  public:
    HealthAttribute(Entity* entity, double max, double critical)
        : max_(max), critical_(critical), current_(max)
    { }

    double value() const {
        return current_;
    }    

    void value(double val)
    {
        // Ensure that 0 <= current <= max 
        current_ = std::max(0, val);
        current_ = std::min(current_, max_);

        // Notify other components belonging to this entity that
        // health is too low.
        if (current_ <= critical_) {
            auto ev = std::shared_ptr<Event>(new HealthCriticalEvent())
            entity_->events().post(ev)
        }
    }

  private:
    double current_, max_, critical_;
};
class HealthAttribute : public EntityAttribute
{
  public:
    HealthAttribute(Entity* entity, double max, double critical)
        : max_(max), critical_(critical), current_(max)
    { }

    double value() const {
        return current_;
    }    

    void value(double val)
    {
        // Ensure that 0 <= current <= max 
        if (0 <= val && val <= max_)
            current_ = val;

        // Notify other components belonging to this entity that
        // health is too low.
        if (current_ <= critical_) {
            auto ev = std::shared_ptr<Event>(new HealthCriticalEvent())
            entity_->events().post(ev)
        }
    }

  private:
    double current_, max_, critical_;
};
added 789 characters in body
Source Link
Paul Manta
  • 3.2k
  • 3
  • 31
  • 42
Loading
added 34 characters in body
Source Link
Paul Manta
  • 3.2k
  • 3
  • 31
  • 42
Loading
added 753 characters in body
Source Link
Paul Manta
  • 3.2k
  • 3
  • 31
  • 42
Loading
added 2041 characters in body
Source Link
Paul Manta
  • 3.2k
  • 3
  • 31
  • 42
Loading
added 1 characters in body
Source Link
Paul Manta
  • 3.2k
  • 3
  • 31
  • 42
Loading
edited body
Source Link
Paul Manta
  • 3.2k
  • 3
  • 31
  • 42
Loading
added 23 characters in body
Source Link
Paul Manta
  • 3.2k
  • 3
  • 31
  • 42
Loading
added 5 characters in body
Source Link
Paul Manta
  • 3.2k
  • 3
  • 31
  • 42
Loading
Source Link
Paul Manta
  • 3.2k
  • 3
  • 31
  • 42
Loading