| 468 | } |
| 469 | |
| 470 | bool TileNode::update(double deltaTime) { |
| 471 | if (isUpdating()) { |
| 472 | bool tileUpdated = false; |
| 473 | for (auto& animation : _animations) { |
| 474 | const auto* ts = r_cast<const tmx::Tileset*>(animation.tileSet); |
| 475 | animation.duration += deltaTime; |
| 476 | if (animation.duration > animation.totalTime) { |
| 477 | animation.duration -= animation.totalTime; |
| 478 | } |
| 479 | auto tileInfo = r_cast<const tmx::Tileset::Tile*>(animation.tileInfo); |
| 480 | float time = 0.0f; |
| 481 | const tmx::Tileset::Tile* currentTile = nullptr; |
| 482 | for (const auto& frame : tileInfo->animation.frames) { |
| 483 | time += frame.duration / 1000.0f; |
| 484 | if (time >= animation.duration) { |
| 485 | currentTile = ts->getTile(frame.tileID); |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | if (currentTile && currentTile != animation.currentTile) { |
| 490 | animation.currentTile = currentTile; |
| 491 | Texture2D* texture = animation.texture; |
| 492 | animation.u1 = s_cast<float>(currentTile->imagePosition.x) / texture->getWidth(); |
| 493 | animation.v1 = s_cast<float>(currentTile->imagePosition.y) / texture->getHeight(); |
| 494 | animation.u2 = s_cast<float>(currentTile->imagePosition.x + currentTile->imageSize.x) / texture->getWidth(); |
| 495 | animation.v2 = s_cast<float>(currentTile->imagePosition.y + currentTile->imageSize.y) / texture->getHeight(); |
| 496 | animation.paddingU = 0.5f / texture->getWidth(); |
| 497 | animation.paddingV = 0.5f / texture->getHeight(); |
| 498 | tileUpdated = true; |
| 499 | } |
| 500 | } |
| 501 | if (tileUpdated) { |
| 502 | for (auto& animTile : _animatedTiles) { |
| 503 | const auto& animation = _animations[animTile.animationIndex]; |
| 504 | auto& quad = animTile.tileQuad->quads[animTile.tileIndex]; |
| 505 | quad.rb.u = animation.u2 - animation.paddingU; |
| 506 | quad.rb.v = animation.v2 - animation.paddingV; |
| 507 | quad.lb.u = animation.u1 + animation.paddingU; |
| 508 | quad.lb.v = animation.v2 - animation.paddingV; |
| 509 | quad.lt.u = animation.u1 + animation.paddingU; |
| 510 | quad.lt.v = animation.v1 + animation.paddingV; |
| 511 | quad.rt.u = animation.u2 - animation.paddingU; |
| 512 | quad.rt.v = animation.v1 + animation.paddingV; |
| 513 | if (animTile.flipFlags) { |
| 514 | bool dflip = (animTile.flipFlags & tmx::TileLayer::Diagonal) != 0; |
| 515 | if (dflip) { |
| 516 | std::swap(quad.lb.u, quad.rt.u); |
| 517 | std::swap(quad.lb.v, quad.rt.v); |
| 518 | } |
| 519 | bool hflip = (animTile.flipFlags & tmx::TileLayer::Horizontal) != 0; |
| 520 | if (hflip) { |
| 521 | std::swap(quad.lb.u, quad.rb.u); |
| 522 | std::swap(quad.lb.v, quad.rb.v); |
| 523 | std::swap(quad.lt.u, quad.rt.u); |
| 524 | std::swap(quad.lt.v, quad.rt.v); |
| 525 | } |
| 526 | bool vflip = (animTile.flipFlags & tmx::TileLayer::Vertical) != 0; |
| 527 | if (vflip) { |