Browse Source

Add tile map Lua binding and sample.

aster2013 11 years ago
parent
commit
a3c819f32a

+ 154 - 0
Bin/Data/LuaScripts/36_Urho2DTileMap.lua

@@ -0,0 +1,154 @@
+-- Urho2D tile map example.
+-- This sample demonstrates:
+--     - Creating a 2D scene with tile map
+--     - Displaying the scene using the Renderer subsystem
+--     - Handling keyboard to move and zoom 2D camera
+
+
+require "LuaScripts/Utilities/Sample"
+
+function Start()
+    -- Execute the common startup for samples
+    SampleStart()
+
+    -- Create the scene content
+    CreateScene()
+
+    -- Create the UI content
+    CreateInstructions()
+
+    -- Setup the viewport for displaying the scene
+    SetupViewport()
+
+    -- Hook up to the frame update events
+    SubscribeToEvents()
+end
+
+function CreateScene()
+    scene_ = Scene()
+
+    -- Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
+    -- show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates it
+    -- is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
+    -- optimizing manner
+    scene_:CreateComponent("Octree")
+
+    -- Create a scene node for the camera, which we will move around
+    -- The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
+    cameraNode = scene_:CreateChild("Camera")
+    -- Set an initial position for the camera scene node above the plane
+    cameraNode.position = Vector3(0.0, 0.0, -10.0)
+    local camera = cameraNode:CreateComponent("Camera")
+    camera.orthographic = true
+    camera.orthoSize = graphics.height * PIXEL_SIZE
+
+    -- Get tmx file
+    local tmxFile = cache:GetResource("TmxFile2D", "Urho2D/Desert.tmx")
+    if tmxFile == nil then
+        return
+    end
+
+    local tileMapNode = scene_:CreateChild("TileMap")
+    tileMapNode.position = Vector3(0.0, 0.0, -1.0)
+
+    local tileMap = tileMapNode:CreateComponent("TileMap2D")
+    tileMap.tmxFile = tmxFile
+
+    -- Set camera's position
+    local x = tileMap.width * tileMap.tileWidth * 0.5
+    local y = tileMap.height * tileMap.tileHeight * 0.5
+    cameraNode.position = Vector3(x, y, -10.0)
+end
+
+function CreateInstructions()
+    -- Construct new Text object, set string to display and font to use
+    local instructionText = ui.root:CreateChild("Text")
+    instructionText:SetText("Use WASD keys and mouse to move, Use PageUp PageDown to zoom.")
+    instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
+
+    -- Position the text relative to the screen center
+    instructionText.horizontalAlignment = HA_CENTER
+    instructionText.verticalAlignment = VA_CENTER
+    instructionText:SetPosition(0, ui.root.height / 4)
+end
+
+function SetupViewport()
+    -- Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
+    -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
+    -- use, but now we just use full screen and default render path configured in the engine command line options
+    local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
+    renderer:SetViewport(0, viewport)
+end
+
+function MoveCamera(timeStep)
+    -- Do not move if the UI has a focused element (the console)
+    if ui.focusElement ~= nil then
+        return
+    end
+
+    -- Movement speed as world units per second
+    local MOVE_SPEED = 4.0
+
+    -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
+    if input:GetKeyDown(KEY_W) then
+        cameraNode:Translate(Vector3(0.0, 1.0, 0.0) * MOVE_SPEED * timeStep)
+    end
+    if input:GetKeyDown(KEY_S) then
+        cameraNode:Translate(Vector3(0.0, -1.0, 0.0) * MOVE_SPEED * timeStep)
+    end
+    if input:GetKeyDown(KEY_A) then
+        cameraNode:Translate(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
+    end
+    if input:GetKeyDown(KEY_D) then
+        cameraNode:Translate(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
+    end
+
+    if input:GetKeyDown(KEY_PAGEUP) then
+        local camera = cameraNode:GetComponent("Camera")
+        camera.zoom = camera.zoom * 1.01
+    end
+
+    if input:GetKeyDown(KEY_PAGEDOWN) then
+        local camera = cameraNode:GetComponent("Camera")
+        camera.zoom = camera.zoom * 0.99
+    end
+end
+
+function SubscribeToEvents()
+    -- Subscribe HandleUpdate() function for processing update events
+    SubscribeToEvent("Update", "HandleUpdate")
+
+    -- Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
+    UnsubscribeFromEvent("SceneUpdate")
+end
+
+function HandleUpdate(eventType, eventData)
+    -- Take the frame time step, which is stored as a float
+    local timeStep = eventData:GetFloat("TimeStep")
+
+    -- Move the camera, scale movement with time step
+    MoveCamera(timeStep)
+end
+
+-- Create XML patch instructions for screen joystick layout specific to this sample app
+function GetScreenJoystickPatchString()
+    return
+        "<patch>" ..
+        "    <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" ..
+        "    <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom In</replace>" ..
+        "    <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" ..
+        "        <element type=\"Text\">" ..
+        "            <attribute name=\"Name\" value=\"KeyBinding\" />" ..
+        "            <attribute name=\"Text\" value=\"PAGEUP\" />" ..
+        "        </element>" ..
+        "    </add>" ..
+        "    <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" ..
+        "    <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom Out</replace>" ..
+        "    <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" ..
+        "        <element type=\"Text\">" ..
+        "            <attribute name=\"Name\" value=\"KeyBinding\" />" ..
+        "            <attribute name=\"Text\" value=\"PAGEDOWN\" />" ..
+        "        </element>" ..
+        "    </add>" ..
+        "</patch>"
+end

+ 20 - 0
Source/Engine/LuaScript/pkgs/Urho2D/TileMap2D.pkg

@@ -0,0 +1,20 @@
+$#include "TileMap2D.h"
+
+class TileMap2D : Component
+{
+    void SetTmxFile(TmxFile2D* tmxFile);
+    TmxFile2D* GetTmxFile() const;
+    int GetWidth() const;
+    int GetHeight() const;
+    float GetTileWidth() const;
+    float GetTileHeight() const;
+    unsigned GetNumLayers() const;
+    TileMapLayer2D* GetLayer(unsigned index) const;
+
+    tolua_property__get_set TmxFile2D* tmxFile;
+    tolua_readonly tolua_property__get_set int width;
+    tolua_readonly tolua_property__get_set int height;
+    tolua_readonly tolua_property__get_set float tileWidth;
+    tolua_readonly tolua_property__get_set float tileHeight;
+    tolua_readonly tolua_property__get_set unsigned numLayers;
+};

+ 56 - 0
Source/Engine/LuaScript/pkgs/Urho2D/TileMapDefs2D.pkg

@@ -0,0 +1,56 @@
+$#include "TileMapDefs2D.h"
+
+enum TileMapLayerType2D
+{
+    LT_TILE_LAYER,
+    LT_OBJECT_GROUP,
+    LT_IMAGE_LAYER,
+    LT_INVALID
+};
+
+enum TileObjectType2D
+{
+    OT_RECTANGLE,
+    OT_ELLIPSE,
+    OT_POLYGON,
+    OT_POLYLINE,
+    OT_TILE,
+    OT_INVALID
+};
+
+class PropertySet2D
+{
+    bool HasProperty(const String& name) const;
+    const String& GetProperty(const String& name) const;
+};
+
+class Tile2D
+{
+    int GetGid() const;
+    Sprite2D* GetSprite() const;
+    bool HasProperty(const String& name) const;
+    const String& GetProperty(const String& name) const;
+
+    tolua_readonly tolua_property__get_set int gid;
+    tolua_readonly tolua_property__get_set Sprite2D* sprite;
+};
+
+class TileObject2D
+{
+    TileObjectType2D GetType() const;
+    const Vector2& GetPosition() const;
+    const Vector2& GetSize() const;
+    unsigned GetNumPoints() const;
+    const Vector2& GetPoint(unsigned index) const;
+    int GetTileGid() const;
+    Sprite2D* GetTileSprite() const;
+    bool HasProperty(const String& name) const;
+    const String& GetProperty(const String& name) const;
+
+    tolua_readonly tolua_property__get_set TileObjectType2D type;
+    tolua_readonly tolua_property__get_set Vector2 position;
+    tolua_readonly tolua_property__get_set Vector2 size;
+    tolua_readonly tolua_property__get_set unsigned numPoints;
+    tolua_readonly tolua_property__get_set int tileGid;
+    tolua_readonly tolua_property__get_set Sprite2D* tileSprite;
+};

+ 32 - 0
Source/Engine/LuaScript/pkgs/Urho2D/TileMapLayer2D.pkg

@@ -0,0 +1,32 @@
+$#include "TileMapLayer2D.h"
+
+class TileMapLayer2D : Component
+{
+    void SetDrawOrder(int drawOrder);
+    void SetVisible(bool visible);
+
+    int GetDrawOrder() const;
+    bool IsVisible() const;
+    int GetWidth() const;
+    int GetHeight() const;
+    bool HasProperty(const String& name) const;
+    const String& GetProperty(const String& name) const;
+    TileMapLayerType2D GetLayerType() const;
+    Node* GetTileNode(int x, int y) const;
+    Tile2D* GetTile(int x, int y) const;
+    unsigned GetNumObjectNodes() const;
+    Node* GetObjectNode(unsigned index) const;
+    unsigned GetNumObjects() const;
+    TileObject2D* GetObject(unsigned index) const;
+    Node* GetImageNode() const;
+
+
+    tolua_readonly tolua_property__get_set int drawOrder;
+    tolua_readonly tolua_property__is_set bool visible;
+    tolua_readonly tolua_property__get_set int width;
+    tolua_readonly tolua_property__get_set int height;
+    tolua_readonly tolua_property__get_set TileMapLayerType2D layerType;
+    tolua_readonly tolua_property__get_set unsigned numObjectNodes;
+    tolua_readonly tolua_property__get_set unsigned numObjects;
+    tolua_readonly tolua_property__get_set Node* imageNode;
+};

+ 5 - 0
Source/Engine/LuaScript/pkgs/Urho2D/TmxFile2D.pkg

@@ -0,0 +1,5 @@
+$#include "TmxFile2D.h"
+
+class TmxFile2D : Resource
+{
+};

+ 5 - 0
Source/Engine/LuaScript/pkgs/Urho2DLuaAPI.pkg

@@ -10,6 +10,11 @@ $pfile "Urho2D/AnimatedSprite2D.pkg"
 $pfile "Urho2D/ParticleEffect2D.pkg"
 $pfile "Urho2D/ParticleEffect2D.pkg"
 $pfile "Urho2D/ParticleEmitter2D.pkg"
 $pfile "Urho2D/ParticleEmitter2D.pkg"
 
 
+$pfile "Urho2D/TileMapDefs2D.pkg"
+$pfile "Urho2D/TmxFile2D.pkg"
+$pfile "Urho2D/TileMap2D.pkg"
+$pfile "Urho2D/TileMapLayer2D.pkg"
+
 $pfile "Urho2D/RigidBody2D.pkg"
 $pfile "Urho2D/RigidBody2D.pkg"
 $pfile "Urho2D/PhysicsWorld2D.pkg"
 $pfile "Urho2D/PhysicsWorld2D.pkg"
 
 

+ 0 - 8
Source/Samples/36_Urho2DTileMap/Urho2DTileMap.cpp

@@ -85,14 +85,6 @@ void Urho2DTileMap::CreateScene()
     camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE);
     camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE);
 
 
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     ResourceCache* cache = GetSubsystem<ResourceCache>();
-    // Get sprite
-    Sprite2D* sprite = cache->GetResource<Sprite2D>("Urho2D/Aster.png");
-    if (!sprite)
-        return;
-
-    float halfWidth = graphics->GetWidth() * 0.5f * PIXEL_SIZE;
-    float halfHeight = graphics->GetHeight() * 0.5f * PIXEL_SIZE;
-
     // Get tmx file
     // Get tmx file
     TmxFile2D* tmxFile = cache->GetResource<TmxFile2D>("Urho2D/Desert.tmx");
     TmxFile2D* tmxFile = cache->GetResource<TmxFile2D>("Urho2D/Desert.tmx");
     if (!tmxFile)
     if (!tmxFile)