void TileMapLayer2D::SetTileLayer(const TmxTileLayer2D* tileLayer) { tileLayer_ = tileLayer; int width = tileLayer->GetWidth(); int height = tileLayer->GetHeight(); nodes_.Resize((unsigned)(width * height)); const TileMapInfo2D& info = tileMap_->GetInfo(); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { const Tile2D* tile = tileLayer->GetTile(x, y); if (!tile) continue; SharedPtr<Node> tileNode(GetNode()->CreateChild("Tile")); tileNode->SetTemporary(true); tileNode->SetPosition(info.TileIndexToPosition(x, y)); StaticSprite2D* staticSprite = tileNode->CreateComponent<StaticSprite2D>(); staticSprite->SetSprite(tile->GetSprite()); staticSprite->SetLayer(drawOrder_); staticSprite->SetOrderInLayer(y * width + x); // ATOMIC BEGIN // collision RigidBody2D *body = NULL; TmxObjectGroup2D* group = tile->GetObjectGroup(); if (group) { for (unsigned i = 0; i < group->GetNumObjects(); i++) { TileMapObject2D* o = group->GetObject(i); if (o->ValidCollisionShape()) { if (!body) { body = tileNode->CreateComponent<RigidBody2D>(); body->SetBodyType(BT_STATIC); } o->CreateCollisionShape(tileNode); } } } // ATOMIC END nodes_[y * width + x] = tileNode; } } }
void TileMapLayer2D::DrawDebugGeometry(DebugRenderer* debug, bool depthTest) { if (!debug) return; if (objectGroup_) { for (unsigned i = 0; i < objectGroup_->GetNumObjects(); ++i) { TileMapObject2D* object = objectGroup_->GetObject(i); const Color& color = Color::YELLOW; switch (object->GetObjectType()) { case OT_RECTANGLE: { const Vector2& lb = object->GetPosition(); const Vector2& rt = lb + object->GetSize(); debug->AddLine(Vector2(lb.x_, lb.y_), Vector2(rt.x_, lb.y_), color, depthTest); debug->AddLine(Vector2(rt.x_, lb.y_), Vector2(rt.x_, rt.y_), color, depthTest); debug->AddLine(Vector2(rt.x_, rt.y_), Vector2(lb.x_, rt.y_), color, depthTest); debug->AddLine(Vector2(lb.x_, rt.y_), Vector2(lb.x_, lb.y_), color, depthTest); } break; case OT_ELLIPSE: { const Vector2 halfSize = object->GetSize() * 0.5f; const Vector2 center = object->GetPosition() + halfSize; for (unsigned i = 0; i < 360; i += 30) { unsigned j = i + 30; float x1 = halfSize.x_ * Cos((float)i); float y1 = halfSize.y_ * Sin((float)i); float x2 = halfSize.x_ * Cos((float)j); float y2 = halfSize.y_ * Sin((float)j); debug->AddLine(center + Vector2(x1, y1), center + Vector2(x2, y2), color, depthTest); } } break; case OT_POLYGON: case OT_POLYLINE: { for (unsigned j = 0; j < object->GetNumPoints() - 1; ++j) debug->AddLine(object->GetPoint(j), object->GetPoint(j + 1), color, depthTest); if (object->GetObjectType() == OT_POLYGON) debug->AddLine(object->GetPoint(0), object->GetPoint(object->GetNumPoints() - 1), color, depthTest); } break; default: break; } } } }
void TileMapLayer2D::DrawDebugGeometry(DebugRenderer* debug, bool depthTest) { if (!debug) return; if (objectGroup_) { const Matrix3x4 transform = GetTileMap()->GetNode()->GetTransform(); for (unsigned i = 0; i < objectGroup_->GetNumObjects(); ++i) { TileMapObject2D* object = objectGroup_->GetObject(i); const Color& color = Color::YELLOW; const Vector2& size = object->GetSize(); const TileMapInfo2D& info = tileMap_->GetInfo(); switch (object->GetObjectType()) { case OT_RECTANGLE: { Vector<Vector2> points; switch (info.orientation_) { case O_ORTHOGONAL: case O_HEXAGONAL: case O_STAGGERED: { points.Push(Vector2::ZERO); points.Push(Vector2(size.x_, 0.0f)); points.Push(Vector2(size.x_, -size.y_)); points.Push(Vector2(0.0f, -size.y_)); break; } case O_ISOMETRIC: { float ratio = (info.tileWidth_ / info.tileHeight_) * 0.5f; points.Push(Vector2::ZERO); points.Push(Vector2(size.y_ * ratio, size.y_ * 0.5f)); points.Push(Vector2((size.x_ + size.y_) * ratio, (-size.x_ + size.y_) * 0.5f)); points.Push(Vector2(size.x_ * ratio, -size.x_ * 0.5f)); break; } } for (unsigned j = 0; j < points.Size(); ++j) debug->AddLine(Vector3(TransformNode2D(transform, points[j] + object->GetPosition())), Vector3(TransformNode2D(transform, points[(j + 1) % points.Size()] + object->GetPosition())), color, depthTest); } break; case OT_ELLIPSE: { const Vector2 halfSize = object->GetSize() * 0.5f; float ratio = (info.tileWidth_ / info.tileHeight_) * 0.5f; // For isometric only Vector2 pivot = object->GetPosition(); if (info.orientation_ == O_ISOMETRIC) { pivot += Vector2((halfSize.x_ + halfSize.y_) * ratio, (-halfSize.x_ + halfSize.y_) * 0.5f); } else { pivot += halfSize; } for (unsigned i = 0; i < 360; i += 30) { unsigned j = i + 30; float x1 = halfSize.x_ * Cos((float)i); float y1 = halfSize.y_ * Sin((float)i); float x2 = halfSize.x_ * Cos((float)j); float y2 = halfSize.y_ * Sin((float)j); Vector2 point1 = Vector2(x1, - y1); Vector2 point2 = Vector2(x2, - y2); if (info.orientation_ == O_ISOMETRIC) { point1 = Vector2((point1.x_ + point1.y_) * ratio, (point1.y_ - point1.x_) * 0.5f); point2 = Vector2((point2.x_ + point2.y_) * ratio, (point2.y_ - point2.x_) * 0.5f); } debug->AddLine(Vector3(TransformNode2D(transform, pivot + point1)), Vector3(TransformNode2D(transform, pivot + point2)), color, depthTest); } } break; case OT_POLYGON: case OT_POLYLINE: { for (unsigned j = 0; j < object->GetNumPoints() - 1; ++j) debug->AddLine(Vector3(TransformNode2D(transform, object->GetPoint(j))), Vector3(TransformNode2D(transform, object->GetPoint(j + 1))), color, depthTest); if (object->GetObjectType() == OT_POLYGON) debug->AddLine(Vector3(TransformNode2D(transform, object->GetPoint(0))), Vector3(TransformNode2D(transform, object->GetPoint(object->GetNumPoints() - 1))), color, depthTest); // Also draw a circle at origin to indicate direction else debug->AddCircle(Vector3(TransformNode2D(transform, object->GetPoint(0))), Vector3::FORWARD, 0.05f, color, 64, depthTest); } break; default: break; } } } }