|
|
@@ -21,19 +21,19 @@
|
|
|
//
|
|
|
|
|
|
#include <Urho3D/Core/CoreEvents.h>
|
|
|
-#include <Urho3D/Engine/Engine.h>
|
|
|
#include <Urho3D/Graphics/Camera.h>
|
|
|
+#include <Urho3D/Engine/Engine.h>
|
|
|
+#include <Urho3D/UI/Font.h>
|
|
|
#include <Urho3D/Graphics/Graphics.h>
|
|
|
+#include <Urho3D/Input/Input.h>
|
|
|
#include <Urho3D/Graphics/Octree.h>
|
|
|
#include <Urho3D/Graphics/Renderer.h>
|
|
|
-#include <Urho3D/Graphics/Zone.h>
|
|
|
-#include <Urho3D/Input/Input.h>
|
|
|
#include <Urho3D/Resource/ResourceCache.h>
|
|
|
#include <Urho3D/Scene/Scene.h>
|
|
|
-#include <Urho3D/UI/Font.h>
|
|
|
+#include <Urho3D/Urho2D/StaticSprite2D.h>
|
|
|
#include <Urho3D/UI/Text.h>
|
|
|
-#include <Urho3D/Urho2D/Drawable2D.h>
|
|
|
#include <Urho3D/Urho2D/TileMap2D.h>
|
|
|
+#include <Urho3D/Urho2D/TileMapLayer2D.h>
|
|
|
#include <Urho3D/Urho2D/TmxFile2D.h>
|
|
|
|
|
|
#include "Urho2DTileMap.h"
|
|
|
@@ -52,6 +52,9 @@ void Urho2DTileMap::Start()
|
|
|
// Execute base class startup
|
|
|
Sample::Start();
|
|
|
|
|
|
+ // Enable OS cursor
|
|
|
+ GetSubsystem<Input>()->SetMouseVisible(true);
|
|
|
+
|
|
|
// Create the scene content
|
|
|
CreateScene();
|
|
|
|
|
|
@@ -112,7 +115,7 @@ void Urho2DTileMap::CreateInstructions()
|
|
|
|
|
|
// Construct new Text object, set string to display and font to use
|
|
|
Text* instructionText = ui->GetRoot()->CreateChild<Text>();
|
|
|
- instructionText->SetText("Use WASD keys to move, use PageUp PageDown keys to zoom.");
|
|
|
+ instructionText->SetText("Use WASD keys to move, use PageUp PageDown keys to zoom.\n LMB to remove a tile, RMB to swap grass and water.");
|
|
|
instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
|
|
|
|
|
|
// Position the text relative to the screen center
|
|
|
@@ -169,6 +172,9 @@ void Urho2DTileMap::SubscribeToEvents()
|
|
|
// Subscribe HandleUpdate() function for processing update events
|
|
|
SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(Urho2DTileMap, HandleUpdate));
|
|
|
|
|
|
+ // Listen to mouse clicks
|
|
|
+ SubscribeToEvent(E_MOUSEBUTTONDOWN, URHO3D_HANDLER(Urho2DTileMap, HandleMouseButtonDown));
|
|
|
+
|
|
|
// Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
|
|
|
UnsubscribeFromEvent(E_SCENEUPDATE);
|
|
|
}
|
|
|
@@ -183,3 +189,42 @@ void Urho2DTileMap::HandleUpdate(StringHash eventType, VariantMap& eventData)
|
|
|
// Move the camera, scale movement with time step
|
|
|
MoveCamera(timeStep);
|
|
|
}
|
|
|
+
|
|
|
+void Urho2DTileMap::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
|
|
|
+{
|
|
|
+ Input* input = GetSubsystem<Input>();
|
|
|
+
|
|
|
+ Node* tileMapNode = scene_->GetChild("TileMap", true);
|
|
|
+ TileMap2D* map = tileMapNode->GetComponent<TileMap2D>();
|
|
|
+ TileMapLayer2D* layer = map->GetLayer(0);
|
|
|
+
|
|
|
+ Vector2 pos = GetMousePositionXY();
|
|
|
+ int x, y;
|
|
|
+ if (map->PositionToTileIndex(x, y, pos))
|
|
|
+ {
|
|
|
+ // Get tile's sprite. Note that layer.GetTile(x, y).sprite is read-only, so we get the sprite through tile's node
|
|
|
+ Node* n = layer->GetTileNode(x, y);
|
|
|
+ if (!n)
|
|
|
+ return;
|
|
|
+ StaticSprite2D* sprite = n->GetComponent<StaticSprite2D>();
|
|
|
+
|
|
|
+ if (input->GetMouseButtonDown(MOUSEB_RIGHT))
|
|
|
+ {
|
|
|
+ // Swap grass and water
|
|
|
+ if (layer->GetTile(x, y)->GetGid() < 9) // First 8 sprites in the "isometric_grass_and_water.png" tileset are mostly grass and from 9 to 24 they are mostly water
|
|
|
+ sprite->SetSprite(layer->GetTile(0, 0)->GetSprite()); // Replace grass by water sprite used in top tile
|
|
|
+ else sprite->SetSprite(layer->GetTile(24, 24)->GetSprite()); // Replace water by grass sprite used in bottom tile
|
|
|
+ }
|
|
|
+ else sprite->SetSprite(NULL); // 'Remove' sprite
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+Vector2 Urho2DTileMap::GetMousePositionXY()
|
|
|
+{
|
|
|
+ Input* input = GetSubsystem<Input>();
|
|
|
+ Graphics* graphics = GetSubsystem<Graphics>();
|
|
|
+ Camera* camera = cameraNode_->GetComponent<Camera>();
|
|
|
+ Vector3 screenPoint = Vector3((float)input->GetMousePosition().x_ / graphics->GetWidth(), (float)input->GetMousePosition().y_ / graphics->GetHeight(), 10.0f);
|
|
|
+ Vector3 worldPoint = camera->ScreenToWorldPoint(screenPoint);
|
|
|
+ return Vector2(worldPoint.x_, worldPoint.y_);
|
|
|
+}
|