// // Copyright (c) 2008-2022 the Urho3D project. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "Utilities2D/Mover.h" #include "Sample2D.h" Sample2D::Sample2D(Context* context) : Object(context) { } void Sample2D::CreateCollisionShapesFromTMXObjects(Node* tileMapNode, TileMapLayer2D* tileMapLayer, const TileMapInfo2D& info) { // Create rigid body to the root node auto* body = tileMapNode->CreateComponent(); body->SetBodyType(BT_STATIC); // Generate physics collision shapes and rigid bodies from the tmx file's objects located in "Physics" layer for (unsigned i = 0; i < tileMapLayer->GetNumObjects(); ++i) { TileMapObject2D* tileMapObject = tileMapLayer->GetObject(i); // Get physics objects // Create collision shape from tmx object switch (tileMapObject->GetObjectType()) { case OT_RECTANGLE: { CreateRectangleShape(tileMapNode, tileMapObject, tileMapObject->GetSize(), info); } break; case OT_ELLIPSE: { CreateCircleShape(tileMapNode, tileMapObject, tileMapObject->GetSize().x_ / 2, info); // Ellipse is built as a Circle shape as it doesn't exist in Box2D } break; case OT_POLYGON: { CreatePolygonShape(tileMapNode, tileMapObject); } break; case OT_POLYLINE: { CreatePolyLineShape(tileMapNode, tileMapObject); } break; } } } CollisionBox2D* Sample2D::CreateRectangleShape(Node* node, TileMapObject2D* object, const Vector2& size, const TileMapInfo2D& info) { auto* shape = node->CreateComponent(); shape->SetSize(size); if (info.orientation_ == O_ORTHOGONAL) shape->SetCenter(object->GetPosition() + size / 2); else { shape->SetCenter(object->GetPosition() + Vector2(info.tileWidth_ / 2, 0.0f)); shape->SetAngle(45.0f); // If our tile map is isometric then shape is losange } shape->SetFriction(0.8f); if (object->HasProperty("Friction")) shape->SetFriction(ToFloat(object->GetProperty("Friction"))); return shape; } CollisionCircle2D* Sample2D::CreateCircleShape(Node* node, TileMapObject2D* object, float radius, const TileMapInfo2D& info) { auto* shape = node->CreateComponent(); Vector2 size = object->GetSize(); if (info.orientation_ == O_ORTHOGONAL) shape->SetCenter(object->GetPosition() + size / 2); else { shape->SetCenter(object->GetPosition() + Vector2(info.tileWidth_ / 2, 0.0f)); } shape->SetRadius(radius); shape->SetFriction(0.8f); if (object->HasProperty("Friction")) shape->SetFriction(ToFloat(object->GetProperty("Friction"))); return shape; } CollisionPolygon2D* Sample2D::CreatePolygonShape(Node* node, TileMapObject2D* object) { auto* shape = node->CreateComponent(); int numVertices = object->GetNumPoints(); shape->SetVertexCount(numVertices); for (int i = 0; i < numVertices; ++i) shape->SetVertex(i, object->GetPoint(i)); shape->SetFriction(0.8f); if (object->HasProperty("Friction")) shape->SetFriction(ToFloat(object->GetProperty("Friction"))); return shape; } void Sample2D::CreatePolyLineShape(Node* node, TileMapObject2D* object) { /* auto* shape = node->CreateComponent(); int numVertices = object->GetNumPoints(); shape->SetVertexCount(numVertices); for (int i = 0; i < numVertices; ++i) shape->SetVertex(i, object->GetPoint(i)); shape->SetFriction(0.8f); if (object->HasProperty("Friction")) shape->SetFriction(ToFloat(object->GetProperty("Friction"))); return shape; */ // Latest Box2D supports only one sided chains with ghost vertices, use two sided edges instead. // But this can cause stuck at the edges ends https://box2d.org/posts/2020/06/ghost-collisions/ u32 numVertices = object->GetNumPoints(); for (u32 i = 1; i < numVertices; ++i) { CollisionEdge2D* shape = node->CreateComponent(); shape->SetVertices(object->GetPoint(i - 1), object->GetPoint(i)); shape->SetFriction(0.8f); if (object->HasProperty("Friction")) shape->SetFriction(ToFloat(object->GetProperty("Friction"))); } } Node* Sample2D::CreateCharacter(const TileMapInfo2D& info, float friction, const Vector3& position, float scale) { auto* cache = GetSubsystem(); Node* spriteNode = scene_->CreateChild("Imp"); spriteNode->SetPosition(position); spriteNode->SetScale(scale); auto* animatedSprite = spriteNode->CreateComponent(); // Get scml file and Play "idle" anim auto* animationSet = cache->GetResource("Urho2D/imp/imp.scml"); animatedSprite->SetAnimationSet(animationSet); animatedSprite->SetAnimation("idle"); animatedSprite->SetLayer(3); // Put character over tile map (which is on layer 0) and over Orcs (which are on layer 2) auto* impBody = spriteNode->CreateComponent(); impBody->SetBodyType(BT_DYNAMIC); impBody->SetAllowSleep(false); impBody->SetFixedRotation(true); auto* shape = spriteNode->CreateComponent(); shape->SetRadius(1.1f); // Set shape size shape->SetFriction(friction); // Set friction shape->SetRestitution(0.1f); // Bounce shape->SetDensity(6.6f); return spriteNode; } Node* Sample2D::CreateTrigger() { Node* node = scene_->CreateChild(); // Clones will be renamed according to object type auto* body = node->CreateComponent(); body->SetBodyType(BT_STATIC); auto* shape = node->CreateComponent(); // Create box shape shape->SetTrigger(true); return node; } Node* Sample2D::CreateEnemy() { auto* cache = GetSubsystem(); Node* node = scene_->CreateChild("Enemy"); auto* staticSprite = node->CreateComponent(); staticSprite->SetSprite(cache->GetResource("Urho2D/Aster.png")); auto* body = node->CreateComponent(); body->SetBodyType(BT_STATIC); auto* shape = node->CreateComponent(); // Create circle shape shape->SetRadius(0.25f); // Set radius return node; } Node* Sample2D::CreateOrc() { auto* cache = GetSubsystem(); Node* node = scene_->CreateChild("Orc"); node->SetScale(scene_->GetChild("Imp", true)->GetScale()); auto* animatedSprite = node->CreateComponent(); auto* animationSet = cache->GetResource("Urho2D/Orc/Orc.scml"); animatedSprite->SetAnimationSet(animationSet); animatedSprite->SetAnimation("run"); // Get scml file and Play "run" anim animatedSprite->SetLayer(2); // Make orc always visible auto* body = node->CreateComponent(); auto* shape = node->CreateComponent(); shape->SetRadius(1.3f); // Set shape size shape->SetTrigger(true); return node; } Node* Sample2D::CreateCoin() { auto* cache = GetSubsystem(); Node* node = scene_->CreateChild("Coin"); node->SetScale(0.5); auto* animatedSprite = node->CreateComponent(); auto* animationSet = cache->GetResource("Urho2D/GoldIcon.scml"); animatedSprite->SetAnimationSet(animationSet); // Get scml file and Play "idle" anim animatedSprite->SetAnimation("idle"); animatedSprite->SetLayer(4); auto* body = node->CreateComponent(); body->SetBodyType(BT_STATIC); auto* shape = node->CreateComponent(); // Create circle shape shape->SetRadius(0.32f); // Set radius shape->SetTrigger(true); return node; } Node* Sample2D::CreateMovingPlatform() { auto* cache = GetSubsystem(); Node* node = scene_->CreateChild("MovingPlatform"); node->SetScale(Vector3(3.0f, 1.0f, 0.0f)); auto* staticSprite = node->CreateComponent(); staticSprite->SetSprite(cache->GetResource("Urho2D/Box.png")); auto* body = node->CreateComponent(); body->SetBodyType(BT_STATIC); auto* shape = node->CreateComponent(); // Create box shape shape->SetSize(Vector2(0.32f, 0.32f)); // Set box size shape->SetFriction(0.8f); // Set friction return node; } void Sample2D::PopulateMovingEntities(TileMapLayer2D* movingEntitiesLayer) { // Create enemy (will be cloned at each placeholder) Node* enemyNode = CreateEnemy(); Node* orcNode = CreateOrc(); Node* platformNode = CreateMovingPlatform(); // Instantiate enemies and moving platforms at each placeholder (placeholders are Poly Line objects defining a path from points) for (unsigned i=0; i < movingEntitiesLayer->GetNumObjects(); ++i) { // Get placeholder object TileMapObject2D* movingObject = movingEntitiesLayer->GetObject(i); // Get placeholder object if (movingObject->GetObjectType() == OT_POLYLINE) { // Clone the enemy and position it at placeholder point Node* movingClone; Vector2 offset = Vector2(0.0f, 0.0f); if (movingObject->GetType() == "Enemy") { movingClone = enemyNode->Clone(); offset = Vector2(0.0f, -0.32f); } else if (movingObject->GetType() == "Orc") movingClone = orcNode->Clone(); else if (movingObject->GetType() == "MovingPlatform") movingClone = platformNode->Clone(); else continue; movingClone->SetPosition2D(movingObject->GetPoint(0) + offset); // Create script object that handles entity translation along its path auto* mover = movingClone->CreateComponent(); // Set path from points PODVector path = CreatePathFromPoints(movingObject, offset); mover->path_ = path; // Override default speed if (movingObject->HasProperty("Speed")) mover->speed_ = ToFloat(movingObject->GetProperty("Speed")); } } // Remove nodes used for cloning purpose enemyNode->Remove(); orcNode->Remove(); platformNode->Remove(); } void Sample2D::PopulateCoins(TileMapLayer2D* coinsLayer) { // Create coin (will be cloned at each placeholder) Node* coinNode = CreateCoin(); // Instantiate coins to pick at each placeholder for (unsigned i=0; i < coinsLayer->GetNumObjects(); ++i) { TileMapObject2D* coinObject = coinsLayer->GetObject(i); // Get placeholder object Node* coinClone = coinNode->Clone(); coinClone->SetPosition2D(coinObject->GetPosition() + coinObject->GetSize() / 2 + Vector2(0.0f, 0.16f)); } // Remove node used for cloning purpose coinNode->Remove(); } void Sample2D::PopulateTriggers(TileMapLayer2D* triggersLayer) { // Create trigger node (will be cloned at each placeholder) Node* triggerNode = CreateTrigger(); // Instantiate triggers at each placeholder (Rectangle objects) for (unsigned i=0; i < triggersLayer->GetNumObjects(); ++i) { TileMapObject2D* triggerObject = triggersLayer->GetObject(i); // Get placeholder object if (triggerObject->GetObjectType() == OT_RECTANGLE) { Node* triggerClone = triggerNode->Clone(); triggerClone->SetName(triggerObject->GetType()); auto* shape = triggerClone->GetComponent(); shape->SetSize(triggerObject->GetSize()); triggerClone->SetPosition2D(triggerObject->GetPosition() + triggerObject->GetSize() / 2); } } } float Sample2D::Zoom(Camera* camera) { auto* input = GetSubsystem(); float zoom_ = camera->GetZoom(); if (input->GetMouseMoveWheel() != 0) { zoom_ = Clamp(zoom_ + input->GetMouseMoveWheel() * 0.1f, CAMERA_MIN_DIST, CAMERA_MAX_DIST); camera->SetZoom(zoom_); } if (input->GetKeyDown(KEY_PAGEUP)) { zoom_ = Clamp(zoom_ * 1.01f, CAMERA_MIN_DIST, CAMERA_MAX_DIST); camera->SetZoom(zoom_); } if (input->GetKeyDown(KEY_PAGEDOWN)) { zoom_ = Clamp(zoom_ * 0.99f, CAMERA_MIN_DIST, CAMERA_MAX_DIST); camera->SetZoom(zoom_); } return zoom_; } PODVector Sample2D::CreatePathFromPoints(TileMapObject2D* object, const Vector2& offset) { PODVector path; for (unsigned i=0; i < object->GetNumPoints(); ++i) path.Push(object->GetPoint(i) + offset); return path; } void Sample2D::CreateUIContent(const String& demoTitle, int remainingLifes, int remainingCoins) { auto* cache = GetSubsystem(); auto* ui = GetSubsystem(); // Set the default UI style and font ui->GetRoot()->SetDefaultStyle(cache->GetResource("UI/DefaultStyle.xml")); auto* font = cache->GetResource("Fonts/Anonymous Pro.ttf"); // We create in-game UIs (coins and lifes) first so that they are hidden by the fullscreen UI (we could also temporary hide them using SetVisible) // Create the UI for displaying the remaining coins auto* coinsUI = ui->GetRoot()->CreateChild("Coins"); coinsUI->SetTexture(cache->GetResource("Urho2D/GoldIcon.png")); coinsUI->SetSize(50, 50); coinsUI->SetImageRect(IntRect(0, 64, 60, 128)); coinsUI->SetAlignment(HA_LEFT, VA_TOP); coinsUI->SetPosition(5, 5); auto* coinsText = coinsUI->CreateChild("CoinsText"); coinsText->SetAlignment(HA_CENTER, VA_CENTER); coinsText->SetFont(font, 24); coinsText->SetTextEffect(TE_SHADOW); coinsText->SetText(String(remainingCoins)); // Create the UI for displaying the remaining lifes auto* lifeUI = ui->GetRoot()->CreateChild("Life"); lifeUI->SetTexture(cache->GetResource("Urho2D/imp/imp_all.png")); lifeUI->SetSize(70, 80); lifeUI->SetAlignment(HA_RIGHT, VA_TOP); lifeUI->SetPosition(-5, 5); auto* lifeText = lifeUI->CreateChild("LifeText"); lifeText->SetAlignment(HA_CENTER, VA_CENTER); lifeText->SetFont(font, 24); lifeText->SetTextEffect(TE_SHADOW); lifeText->SetText(String(remainingLifes)); // Create the fullscreen UI for start/end auto* fullUI = ui->GetRoot()->CreateChild("FullUI"); fullUI->SetStyleAuto(); fullUI->SetSize(ui->GetRoot()->GetWidth(), ui->GetRoot()->GetHeight()); fullUI->SetEnabled(false); // Do not react to input, only the 'Exit' and 'Play' buttons will // Create the title auto* title = fullUI->CreateChild("Title"); title->SetMinSize(fullUI->GetWidth(), 50); title->SetTexture(cache->GetResource("Textures/HeightMap.png")); title->SetFullImageRect(); title->SetAlignment(HA_CENTER, VA_TOP); auto* titleText = title->CreateChild("TitleText"); titleText->SetAlignment(HA_CENTER, VA_CENTER); titleText->SetFont(font, 24); titleText->SetText(demoTitle); // Create the image auto* spriteUI = fullUI->CreateChild("Sprite"); spriteUI->SetTexture(cache->GetResource("Urho2D/imp/imp_all.png")); spriteUI->SetSize(238, 271); spriteUI->SetAlignment(HA_CENTER, VA_CENTER); spriteUI->SetPosition(0, - ui->GetRoot()->GetHeight() / 4); // Create the 'EXIT' button auto* exitButton = ui->GetRoot()->CreateChild