当前位置: 首页>>代码示例>>C++>>正文


C++ BodyNode::Update方法代码示例

本文整理汇总了C++中BodyNode::Update方法的典型用法代码示例。如果您正苦于以下问题:C++ BodyNode::Update方法的具体用法?C++ BodyNode::Update怎么用?C++ BodyNode::Update使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BodyNode的用法示例。


在下文中一共展示了BodyNode::Update方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Update

void Player::Update(float dt)
{
  bool canTurn = false;

  _difference += dt;
  if (_difference > 0.5f)
  {
    _difference -= 0.5f;
    canTurn = true;
  }

  // If we've moved enough to warrant a turn, let's see what direction we'll be going in.
  if (canTurn && _nextDirection.size() > 0)
  {
    // Get the new direction.
    BodyNode::Direction newDirection = _nextDirection[0];
    BodyNode::Direction oldHeadDirection = GetHeadDirection();
    _nextDirection.erase(_nextDirection.begin());

    // Set the head's direction.
    _body[0]->SetDirection(newDirection);

    // If we've got nodes, and we're moving in a new direction, we have to update the body!
    if (_body.size() > 1 && oldHeadDirection != newDirection)
    {
      BodyNode::DirectionPair pair;
      pair.direction = newDirection;
      pair.pivot = _body[0]->GetTransform().position;

      // Update the body.
      for (int bodyIndex = 1; bodyIndex < _body.size(); bodyIndex++)
      {
        _body[bodyIndex]->directionChange.push_back(pair);
      }
    }
  }

  // Move player based on current direction.
  for (int bodyIndex = _body.size() - 1; bodyIndex >= 0; bodyIndex--)
  {
    BodyNode *currentNode = _body[bodyIndex];
    bool canMove = true;

    if (_body.size() > 1 && bodyIndex > 0)
    {
      BodyNode *nextNode = _body[bodyIndex - 1];
      Vector3 currentPosition = currentNode->GetTransform().position;

      /* If we're at the last piece of the body and it's just been added, we don't want it to move until the previous piece is
       * far enough away so we don't risk colliding with it. */
      if (bodyIndex == _body.size() - 1 && currentNode->active == false)
      {
        Vector3 nextBodyPiecePosition = nextNode->GetTransform().position;

        float distance = Vector3::Magnitude(Vector3::Difference(currentPosition, nextBodyPiecePosition));

        // Make sure it's far enough away before we say it's okay.
        if (distance >= 1.0f)
        {
          currentNode->active = true;
          currentNode->SetDirection(nextNode->GetDirection());

          switch (nextNode->GetDirection())
          {
          case BodyNode::UP:
            currentNode->GetTransform().position.x = nextNode->GetTransform().position.x;
            currentNode->GetTransform().position.y = nextNode->GetTransform().position.y - 1.0f;
            break;
          case BodyNode::DOWN:
            currentNode->GetTransform().position.x = nextNode->GetTransform().position.x;
            currentNode->GetTransform().position.y = nextNode->GetTransform().position.y + 1.0f;
            break;
          case BodyNode::LEFT:
            currentNode->GetTransform().position.x = nextNode->GetTransform().position.x + 1.0f;
            currentNode->GetTransform().position.y = nextNode->GetTransform().position.y;
            break;
          case BodyNode::RIGHT:
            currentNode->GetTransform().position.x = nextNode->GetTransform().position.x - 1.0f;
            currentNode->GetTransform().position.y = nextNode->GetTransform().position.y;
            break;
          }
        }
      }
      else
      {
        float nextNodeDifference = Vector3::Magnitude(Vector3::Difference(nextNode->GetTransform().position, currentNode->GetTransform().position));

        // Move other pieces.
        if (currentNode->directionChange.size() > 0)
        {
          BodyNode::DirectionPair pair = currentNode->directionChange[0];
          Vector3 directionChange = pair.pivot;

          float difference = Vector3::Magnitude(Vector3::Difference(directionChange, currentNode->GetTransform().position));

          // Ensure that we're close enough to the pivot.
          if (difference <= 0.05f)
          {
            currentNode->SetDirection(BodyNode::NONE);
            currentNode->GetTransform().position = directionChange;
//.........这里部分代码省略.........
开发者ID:RobbAP,项目名称:FinalExam,代码行数:101,代码来源:Player.cpp


注:本文中的BodyNode::Update方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。