示例#1
0
void Shape::Update(float dt) {
  if (_jumpTimeout > 0.0f) {
    _jumpTimeout -= dt;
  }


  b2Vec2 currentVelocity = GetBody()->GetLinearVelocity();
  float maxVel = theTuning.GetFloat("BlockyMaxSpeed");
  float xVector = 0.0f;
  if (theController.IsConnected()) {
    xVector = theController.GetLeftThumbVec2().X;
  }
  if (theInput.IsKeyDown(ANGEL_KEY_RIGHTARROW)) {
    xVector = 1.0f;
  }
  if (theInput.IsKeyDown(ANGEL_KEY_LEFTARROW)) {
    xVector = -1.0f;
  }
  if (theInput.IsKeyDown(ANGEL_KEY_RIGHTARROW) && theInput.IsKeyDown(ANGEL_KEY_LEFTARROW)) {
    xVector = 0.0f;
  }

  float desiredVelocity = xVector * maxVel;
  float velocityChange = desiredVelocity - currentVelocity.x;
  float impulse = GetBody()->GetMass() * velocityChange;
  ApplyLinearImpulse(Vector2(impulse, 0), Vector2());

  PhysicsActor::Update(dt);
}
BehaviorTree::BEHAVIOR_STATUS MoveTowardNode::execute(void* p_agent)
{
  if (m_target == nullptr || m_target->getId() == 0)
  {
    return BehaviorTree::BT_FAILURE;
  }

  auto entity = static_cast<ECS::Entity*>(p_agent);
  auto parentCRender = entity->get<RenderComponent>();
  auto parentSprite = dynamic_cast<sf::Sprite*>(parentCRender->drawable);

  auto targetCRender = m_target->get<RenderComponent>();
  auto targetSprite = dynamic_cast<sf::Sprite*>(targetCRender->drawable);

  auto dx = targetSprite->getPosition().x - parentSprite->getPosition().x;
  auto dy = targetSprite->getPosition().y - parentSprite->getPosition().y;

  auto parentCPhysics = entity->get<PhysicsComponent>();
  auto parentBody = parentCPhysics->body;

  auto moved = false;

  if (abs(dx) > m_leashDistance)
  {
    auto sign = dx < 0 ? -1 : 1;
    parentBody->ApplyLinearImpulse(b2Vec2(m_speed * sign, 0), parentBody->GetWorldCenter(), true);

    moved = true;
  }

  if (abs(dy) > m_leashDistance)
  {
    auto sign = dy < 0 ? -1 : 1;
    parentBody->ApplyLinearImpulse(b2Vec2(0, m_speed * sign), parentBody->GetWorldCenter(), true);

    moved = true;
  }

  return moved ? BehaviorTree::BT_RUNNING : BehaviorTree::BT_SUCCESS;
}
示例#3
0
文件: Shape.cpp 项目: nofxboy1234/sa
void Shape::Update(float dt) {
  if (_jumpTimeout > 0.0f) {
    _jumpTimeout -= dt;
  }

  b2Vec2 currentVelocity = GetBody()->GetLinearVelocity();

// #################
  if (! _onGround) {
    // SetSpriteFrame(12);
    SetSpriteFrame(39);
    _spriteFrameDelay = 0.0f;
  }
  else if (MathUtil::FuzzyEquals(currentVelocity.x, 0.0f)) {
    SetSpriteFrame(10);
    _spriteFrameDelay = 0.0f;
  }
  else {
    if (!IsSpriteAnimPlaying()) {
      // PlaySpriteAnimation(0.1f, SAT_Loop, 1, 11);
      PlaySpriteAnimation(0.02f, SAT_Loop, 10, 30);
    }
  }

  if ((currentVelocity.x < 0.0f) && _facingRight) {
    FlipLeft();
  }
  else if ((currentVelocity.x > 0.0f) && !_facingRight) {
    FlipRight();
  }
// #################

  if (_poweringUp) {
    return;
  }

  float maxVel = theTuning.GetFloat("BlockyMaxSpeed");

  float xVector = 0.0f;
  if (theController.IsConnected()) {
    bool dpad_left_down;
    bool dpad_right_down;
    dpad_left_down = theController.IsButtonDown(XINPUT_GAMEPAD_DPAD_LEFT);
    dpad_right_down = theController.IsButtonDown(XINPUT_GAMEPAD_DPAD_RIGHT);

    if (dpad_left_down) {
      xVector = -1.0f;
    }
    else if (dpad_right_down) {
      xVector = 1.0f;
    }
    else if (dpad_left_down && dpad_right_down) {
      xVector = 0.0f;
    }
    else {
      xVector = theController.GetLeftThumbVec2().X;
    }
  }

  if (theInput.IsKeyDown(ANGEL_KEY_RIGHTARROW)) {
    xVector = 1.0f;
  }
  if (theInput.IsKeyDown(ANGEL_KEY_LEFTARROW)) {
    xVector = -1.0f;
  }
  if (theInput.IsKeyDown(ANGEL_KEY_RIGHTARROW) && theInput.IsKeyDown(ANGEL_KEY_LEFTARROW)) {
    xVector = 0.0f;
  }

  float desiredVelocity = xVector * maxVel;
  float velocityChange = desiredVelocity - currentVelocity.x;
  float impulse = GetBody()->GetMass() * velocityChange;
  ApplyLinearImpulse(Vector2(impulse, 0), Vector2());

  PhysicsActor::Update(dt);
}