woaidaima2016 发表于 2017-6-13 11:03:54

千炮金蝉捕鱼【源码】

http://img.woaidaima.com//upload/image/20170526/1495760373117019029.jpghttp://img.woaidaima.com//upload/image/20170526/1495760374939087679.jpg#include "stdafx.h"
#include "Exception.h"
#include "ActionInterval.h"
#include "ActionCombine.h"

/////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////
Action_Sequence::Sequence::Sequence(Action *act1, Action *act2)
:Action_Interval(0)
{
    actions_ = (Action_Interval *)act1;
    actions_ = (Action_Interval *)act2;

    duration_ = actions_->duration() + actions_->duration();
}

Action_Sequence::Sequence::~Sequence()
{
    delete actions_;
    delete actions_;
}

void Action_Sequence::Sequence::update(float time)
{
    int found = 0;
    float new_t = 0.0f;   

    if( time >= split_ )
    {      
      found = 1;
      if ( split_ == 1 )
            new_t = 1;
      else
            new_t = (time-split_) / (1 - split_ );
    } else
    {
      found = 0;
      if( split_ != 0 )
            new_t = time / split_;
      else
            new_t = 1;
    }

    if (last_ == -1 && found==1)
    {
      actions_->start_with_target(target_);
      actions_->update(1.0f);
      actions_->stop();
    }

    if (last_ != found )
    {
      if( last_ != -1 )
      {
            actions_->update(1.0f);
            actions_->stop();
      }

      actions_->start_with_target(target_);
    }

    actions_->update(new_t);

    last_ = found;
}

void Action_Sequence::Sequence::stop()
{
    actions_->stop();
    actions_->stop();

    __super::stop();
}

void Action_Sequence::Sequence::start_with_target(Entity *target)
{
    __super:: start_with_target(target);   

    split_ = actions_->duration() / duration_;
    last_ = -1;
}

Action_Sequence::Action_Sequence(Action *act1, ...)
:Action_Interval(0)
{
    va_list params;
    va_start(params,act1);   

    Action_Interval *now;
    Action_Interval *prev = (Action_Interval *)act1;

    while( act1 )
    {
      now = va_arg(params,Action_Interval*);
      if ( now )
      {
            prev = new Sequence(prev, now);
      }
      else
            break;
    }

    va_end(params);

    duration_ = prev->duration();

    sequence_ = (Sequence *)prev;
}

Action_Sequence::~Action_Sequence()
{
    delete sequence_;
}

void Action_Sequence::stop()
{
    sequence_->stop();

    __super::stop();
}

void Action_Sequence::update(float time)
{
    sequence_->update(time);
}

void Action_Sequence::start_with_target(Entity *target)
{
    __super::start_with_target(target);

    sequence_->start_with_target(target);
}

///////////////////////////////////////////////////////////////////////////////////////////
Action_Spawn::Spawn::Spawn(Action *act1, Action *act2)
:Action_Interval(0)
{
    actions_ = (Action_Interval *)act1;
    actions_ = (Action_Interval *)act2;

    float d1, d2;

    d1 = actions_->duration();
    d2 = actions_->duration();

    if (d1 > d2)
    {
      actions_ = new Action_Sequence(actions_, Action_Delay(d1 - d2), 0);
    }
    else if (d1 < d2)
    {
      actions_ = new Action_Sequence(actions_, Action_Delay(d2 - d1), 0);
    }

    duration_ = max((actions_->duration()), (actions_->duration()));
}

Action_Spawn::Spawn::~Spawn()
{
    delete actions_;
    delete actions_;
}

void Action_Spawn::Spawn::update(float time)
{
    actions_->update(time);
    actions_->update(time);
}

void Action_Spawn::Spawn::stop()
{
    actions_->stop();
    actions_->stop();

    __super::stop();
}

void Action_Spawn::Spawn::start_with_target(Entity *target)
{
    __super:: start_with_target(target);   

    actions_->start_with_target(target);
    actions_->start_with_target(target);
}

Action_Spawn::Action_Spawn(Action *act1, ...)
:Action_Interval(0)
{
    va_list params;
    va_start(params,act1);   

    Action_Interval *now;
    Action_Interval *prev = (Action_Interval *)act1;

    while( act1 )
    {
      now = va_arg(params,Action_Interval*);
      if ( now )
      {
            prev = new Spawn(prev, now);
      }
      else
            break;
    }

    va_end(params);

    duration_ = prev->duration();

    spawn_ = (Spawn *)prev;
}

Action_Spawn::~Action_Spawn()
{
    delete spawn_;
}

void Action_Spawn::stop()
{
    spawn_->stop();

    __super::stop();
}

void Action_Spawn::update(float time)
{
    spawn_->update(time);
}

void Action_Spawn::start_with_target(Entity *target)
{
    __super::start_with_target(target);

    spawn_->start_with_target(target);
}

/////////////////////////////////////////////////////////////////////////////////////////
Action_Repeat_Forever::Action_Repeat_Forever(Action *a)
:stop_(false),
other_(a)
{
}

Action_Repeat_Forever::~Action_Repeat_Forever()
{
    if (other_)
    {
      delete other_;
    }
}

void Action_Repeat_Forever::step(float dt)
{
    other_->step(dt);
    if( other_->is_done())
    {
      other_->stop();
      other_->start_with_target(target_);
    }
}

void Action_Repeat_Forever::start_with_target(Entity *target)
{
    other_->start_with_target(target);
    __super::start_with_target(target);
}

/////////////////////////////////////////////////////////////////////////////////////////

页: [1]
查看完整版本: 千炮金蝉捕鱼【源码】