Browse Source

Use ResourceCache:GetResource("XXX", ...) and Node:CreateComponent("XXX") method.

Aster Jian 12 years ago
parent
commit
b1a95c1ba8

+ 1 - 1
Bin/Data/LuaScripts/01_HelloWorld.lua

@@ -30,7 +30,7 @@ function CreateText()
 
 
     -- Set font and text color
     -- Set font and text color
     local cache = GetCache()
     local cache = GetCache()
-    helloText:SetFont(cache:GetFont("Fonts/Anonymous Pro.ttf"), 30)
+    helloText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 30)
     helloText.color = Color(0.0, 1.0, 0.0)
     helloText.color = Color(0.0, 1.0, 0.0)
     
     
     -- Align Text center-screen
     -- Align Text center-screen

+ 1 - 1
Bin/Data/LuaScripts/02_HelloGUI.lua

@@ -18,7 +18,7 @@ function Start()
 
 
     -- Load XML file containing default UI style sheet
     -- Load XML file containing default UI style sheet
     local cache = GetCache()
     local cache = GetCache()
-    local style = cache:GetXMLFile("UI/DefaultStyle.xml")
+    local style = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
 
 
     -- Set the loaded style as default style
     -- Set the loaded style as default style
     local ui = GetUI()
     local ui = GetUI()

+ 1 - 1
Bin/Data/LuaScripts/03_Sprites.lua

@@ -32,7 +32,7 @@ function Stop()
 end
 end
 
 
 function CreateSprites()
 function CreateSprites()
-    local decalTex = cache:GetTexture2D("Textures/UrhoDecal.dds")
+    local decalTex = cache:GetResource("Texture2D", "Textures/UrhoDecal.dds")
     
     
     local width = graphics.width
     local width = graphics.width
     local height = graphics.height
     local height = graphics.height

+ 58 - 58
Bin/Data/LuaScripts/TestScene.lua

@@ -48,7 +48,7 @@ function Start()
         SubscribeToEvent("ClientConnected", "HandleClientConnected")
         SubscribeToEvent("ClientConnected", "HandleClientConnected")
 
 
         -- Disable physics interpolation to ensure clients get sent physically correct transforms
         -- Disable physics interpolation to ensure clients get sent physically correct transforms
-        testScene:GetPhysicsWorld():SetInterpolation(false)
+        testScene:GetComponent("PhysicsWorld"):SetInterpolation(false)
     end
     end
     
     
     if runClient then
     if runClient then
@@ -63,7 +63,7 @@ function Stop()
 end
 end
 
 
 function InitConsole()
 function InitConsole()
-    local uiStyle = cache:GetXMLFile("UI/DefaultStyle.xml")
+    local uiStyle = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
 
 
     local debugHud = engine:CreateDebugHud()
     local debugHud = engine:CreateDebugHud()
     debugHud.defaultStyle = uiStyle
     debugHud.defaultStyle = uiStyle
@@ -74,7 +74,7 @@ function InitConsole()
 end
 end
 
 
 function InitUI()
 function InitUI()
-    local uiStyle = cache:GetXMLFile("UI/DefaultStyle.xml")
+    local uiStyle = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
     
     
     local newCursor = Cursor:new(context)
     local newCursor = Cursor:new(context)
     newCursor.styleAuto = uiStyle
     newCursor.styleAuto = uiStyle
@@ -91,7 +91,7 @@ function InitScene()
     
     
     -- Create the camera outside the scene so it is unaffected by scene load/save
     -- Create the camera outside the scene so it is unaffected by scene load/save
     cameraNode = Node(context)
     cameraNode = Node(context)
-    camera = cameraNode:CreateCamera()
+    camera = cameraNode:CreateComponent("Camera")
     cameraNode.position = Vector3(0, 2, 0)
     cameraNode.position = Vector3(0, 2, 0)
 
 
     if not engine:IsHeadless() then
     if not engine:IsHeadless() then
@@ -101,24 +101,24 @@ function InitScene()
         -- local newRenderPathPtr = renderer:GetViewport(0):GetRenderPath():Clone()
         -- local newRenderPathPtr = renderer:GetViewport(0):GetRenderPath():Clone()
         -- local newRenderPath = newRenderPathPtr:Get()
         -- local newRenderPath = newRenderPathPtr:Get()
         local newRenderPath = renderer:GetViewport(0):GetRenderPath():Clone()
         local newRenderPath = renderer:GetViewport(0):GetRenderPath():Clone()
-        newRenderPath:Append(cache:GetXMLFile("PostProcess/Bloom.xml"))
-        newRenderPath.Append(cache:GetXMLFile("PostProcess/EdgeFilter.xml"))
+        newRenderPath:Append(cache:GetResource("XMLFile", "PostProcess/Bloom.xml"))
+        newRenderPath.Append(cache:GetResource("XMLFile", "PostProcess/EdgeFilter.xml"))
         newRenderPath:SetEnabled("Bloom", false)
         newRenderPath:SetEnabled("Bloom", false)
         newRenderPath:SetEnabled("EdgeFilter", false)
         newRenderPath:SetEnabled("EdgeFilter", false)
         renderer:GetViewport(0):SetRenderPath(newRenderPath)
         renderer:GetViewport(0):SetRenderPath(newRenderPath)
-        audio:SetListener(cameraNode:CreateSoundListener())
+        audio:SetListener(cameraNode:CreateComponent("SoundListener"))
     end
     end
     
     
     if runClient then
     if runClient then
         return
         return
     end
     end
 
 
-    local world = testScene:CreatePhysicsWorld()
-    testScene:CreateOctree()
-    testScene:CreateDebugRenderer()
+    local world = testScene:CreateComponent("PhysicsWorld")
+    testScene:CreateComponent("Octree")
+    testScene:CreateComponent("DebugRenderer")
 
 
     local zoneNode = testScene:CreateChild("Zone")
     local zoneNode = testScene:CreateChild("Zone")
-    local zone = zoneNode:CreateZone()
+    local zone = zoneNode:CreateComponent("Zone")
     zone.ambientColor = Color(0.15, 0.15, 0.15)
     zone.ambientColor = Color(0.15, 0.15, 0.15)
     zone.fogColor = Color(0.5, 0.5, 0.7)
     zone.fogColor = Color(0.5, 0.5, 0.7)
     zone.fogStart = 100.0
     zone.fogStart = 100.0
@@ -129,7 +129,7 @@ function InitScene()
         local lightNode = testScene:CreateChild("GlobalLight")
         local lightNode = testScene:CreateChild("GlobalLight")
         lightNode.direction = Vector3(0.3, -0.5, 0.425)
         lightNode.direction = Vector3(0.3, -0.5, 0.425)
         
         
-        local light = lightNode:CreateLight()
+        local light = lightNode:CreateComponent("Light")
         light.lightType = LIGHT_DIRECTIONAL
         light.lightType = LIGHT_DIRECTIONAL
         light.castShadows = true
         light.castShadows = true
         light.shadowBias = BiasParameters(0.0001, 0.5)
         light.shadowBias = BiasParameters(0.0001, 0.5)
@@ -142,13 +142,13 @@ function InitScene()
         objectNode.position = Vector3(0, -0.5, 0)
         objectNode.position = Vector3(0, -0.5, 0)
         objectNode.scale = Vector3(200, 1, 200)
         objectNode.scale = Vector3(200, 1, 200)
         
         
-        local object = objectNode:CreateStaticModel()
-        object.model = cache:GetModel("Models/Box.mdl")
-        object.material = cache:GetMaterial("Materials/StoneTiled.xml")
+        local object = objectNode:CreateComponent("StaticModel")
+        object.model = cache:GetResource("Model", "Models/Box.mdl")
+        object.material = cache:GetResource("Material", "Materials/StoneTiled.xml")
         object.occluder = true
         object.occluder = true
 
 
-        local body = objectNode:CreateRigidBody()
-        local shape = objectNode:CreateCollisionShape()
+        local body = objectNode:CreateComponent("RigidBody")
+        local shape = objectNode:CreateComponent("CollisionShape")
         shape:SetBox(Vector3(1, 1, 1))
         shape:SetBox(Vector3(1, 1, 1))
     end
     end
 
 
@@ -157,13 +157,13 @@ function InitScene()
         objectNode.position = Vector3(Random() * 180 - 90, 1, Random() * 180 - 90)
         objectNode.position = Vector3(Random() * 180 - 90, 1, Random() * 180 - 90)
         objectNode:SetScale(2)
         objectNode:SetScale(2)
 
 
-        local object = objectNode:CreateStaticModel()
-        object.model = cache:GetModel("Models/Box.mdl")
-        object.material = cache:GetMaterial("Materials/Stone.xml")
+        local object = objectNode:CreateComponent("StaticModel")
+        object.model = cache:GetResource("Model", "Models/Box.mdl")
+        object.material = cache:GetResource("Material", "Materials/Stone.xml")
         object.castShadows = true
         object.castShadows = true
 
 
-        local body = objectNode:CreateRigidBody()
-        local shape = objectNode:CreateCollisionShape()
+        local body = objectNode:CreateComponent("RigidBody")
+        local shape = objectNode:CreateComponent("CollisionShape")
         shape:SetBox(Vector3(1, 1, 1))
         shape:SetBox(Vector3(1, 1, 1))
     end
     end
 
 
@@ -172,14 +172,14 @@ function InitScene()
         objectNode.position = Vector3(Random() * 180 - 90, 10, Random() * 180 - 90)
         objectNode.position = Vector3(Random() * 180 - 90, 10, Random() * 180 - 90)
         objectNode:SetScale(20)
         objectNode:SetScale(20)
 
 
-        local object = objectNode:CreateStaticModel()
-        object.model = cache:GetModel("Models/Box.mdl")
-        object.material = cache:GetMaterial("Materials/Stone.xml")
+        local object = objectNode:CreateComponent("StaticModel")
+        object.model = cache:GetResource("Model", "Models/Box.mdl")
+        object.material = cache:GetResource("Material", "Materials/Stone.xml")
         object.castShadows = true
         object.castShadows = true
         object.occluder = true
         object.occluder = true
 
 
-        local body = objectNode:CreateRigidBody()
-        local shape = objectNode:CreateCollisionShape()
+        local body = objectNode:CreateComponent("RigidBody")
+        local shape = objectNode:CreateComponent("CollisionShape")
         shape:SetBox(Vector3(1, 1, 1))
         shape:SetBox(Vector3(1, 1, 1))
     end
     end
     
     
@@ -191,13 +191,13 @@ function InitScene()
         objectNode.rotation = Quaternion(0, Random(360.0), 0)
         objectNode.rotation = Quaternion(0, Random(360.0), 0)
         objectNode:SetScale(5)
         objectNode:SetScale(5)
 
 
-        local object = objectNode:CreateStaticModel()
-        object.model = cache:GetModel("Models/Mushroom.mdl")
-        object.material = cache:GetMaterial("Materials/Mushroom.xml")
+        local object = objectNode:CreateComponent("StaticModel")
+        object.model = cache:GetResource("Model", "Models/Mushroom.mdl")
+        object.material = cache:GetResource("Material", "Materials/Mushroom.xml")
         object.castShadows = true
         object.castShadows = true
 
 
-        local body = objectNode:CreateRigidBody()
-        local shape = objectNode:CreateCollisionShape()
+        local body = objectNode:CreateComponent("RigidBody")
+        local shape = objectNode:CreateComponent("CollisionShape")
         shape:SetTriangleMesh(object:GetModel())
         shape:SetTriangleMesh(object:GetModel())
     end
     end
 
 
@@ -207,18 +207,18 @@ function InitScene()
         objectNode:SetPosition(Vector3(Random() * 180 - 90, 0, Random() * 180 - 90))
         objectNode:SetPosition(Vector3(Random() * 180 - 90, 0, Random() * 180 - 90))
         objectNode:SetRotation(Quaternion(0, Random() * 360, 0))
         objectNode:SetRotation(Quaternion(0, Random() * 360, 0))
 
 
-        local object = objectNode:CreateAnimatedModel()
-        object.model = cache:GetModel("Models/Jack.mdl")
-        object.material = cache:GetMaterial("Materials/Jack.xml")
+        local object = objectNode:CreateComponent("AnimatedModel")
+        object.model = cache:GetResource("Model", "Models/Jack.mdl")
+        object.material = cache:GetResource("Material", "Materials/Jack.xml")
         object.castShadows = true
         object.castShadows = true
         
         
         -- Create a capsule shape for detecting collisions
         -- Create a capsule shape for detecting collisions
-        local body = objectNode:CreateRigidBody()
+        local body = objectNode:CreateComponent("RigidBody")
         body.phantom = true
         body.phantom = true
         
         
-        local shape = objectNode:CreateCollisionShape()
+        local shape = objectNode:CreateComponent("CollisionShape")
         shape:SetCapsule(0.7, 1.8, Vector3(0.0, 0.9, 0.0))
         shape:SetCapsule(0.7, 1.8, Vector3(0.0, 0.9, 0.0))
-        local ctrl = objectNode:CreateAnimationController()
+        local ctrl = objectNode:CreateComponent("AnimationController")
         ctrl:Play("Models/Jack_Walk.ani", 0, true, 0.0)
         ctrl:Play("Models/Jack_Walk.ani", 0, true, 0.0)
     end
     end
 end
 end
@@ -405,15 +405,15 @@ function HandleMouseButtonDown(eventType, eventData)
             end
             end
         else
         else
             local pos = ui:GetCursorPosition()
             local pos = ui:GetCursorPosition()
-            if ui:GetElementAt(pos, true) == nil and testScene:GetOctree() ~= nil then
+            if ui:GetElementAt(pos, true) == nil and testScene:GetComponent("Octree") ~= nil then
                 local cameraRay = camera:GetScreenRay(pos.x / graphics:GetWidth(), pos.y / graphics:GetHeight())
                 local cameraRay = camera:GetScreenRay(pos.x / graphics:GetWidth(), pos.y / graphics:GetHeight())
-                local result = testScene:GetOctree():RaycastSingle(cameraRay, RAY_TRIANGLE, 250.0, DRAWABLE_GEOMETRY)
+                local result = testScene:GetComponent("Octree"):RaycastSingle(cameraRay, RAY_TRIANGLE, 250.0, DRAWABLE_GEOMETRY)
                 if result.drawable ~= nil then
                 if result.drawable ~= nil then
                     local rayHitPos = cameraRay.origin + cameraRay.direction * result.distance
                     local rayHitPos = cameraRay.origin + cameraRay.direction * result.distance
-                    local decal = result.drawable:GetNode():GetDecalSet()
+                    local decal = result.drawable:GetNode():GetComponent("DecalSet")
                     if decal == nil then
                     if decal == nil then
-                        decal = result.drawable:GetNode():CreateDecalSet()
-                        decal.material = cache:GetMaterial("Materials/UrhoDecal.xml")
+                        decal = result.drawable:GetNode():CreateComponent("DecalSet")
+                        decal.material = cache:GetResource("Material", "Materials/UrhoDecal.xml")
                         -- Increase max. vertices/indices if the target is skinned
                         -- Increase max. vertices/indices if the target is skinned
                         if result.drawable:GetTypeName() == "AnimatedModel" then
                         if result.drawable:GetTypeName() == "AnimatedModel" then
                             decal.maxVertices = 2048
                             decal.maxVertices = 2048
@@ -436,17 +436,17 @@ function HandleSpawnBox(eventType, eventData)
     newNode.rotation =rotation
     newNode.rotation =rotation
     newNode:SetScale(0.2)
     newNode:SetScale(0.2)
     
     
-    local body = newNode:CreateRigidBody()
+    local body = newNode:CreateComponent("RigidBody")
     body.mass = 1.0
     body.mass = 1.0
     body.friction = 1.0
     body.friction = 1.0
     body.linearVelocity = rotation * Vector3(0.0, 1.0, 10.0)
     body.linearVelocity = rotation * Vector3(0.0, 1.0, 10.0)
 
 
-    local shape = newNode:CreateCollisionShape()
+    local shape = newNode:CreateComponent("CollisionShape")
     shape:SetBox(Vector3(1, 1, 1))
     shape:SetBox(Vector3(1, 1, 1))
 
 
-    local object = newNode:CreateStaticModel()
-    object.model = cache:GetModel("Models/Box.mdl")
-    object.material = cache:GetMaterial("Materials/StoneSmall.xml")
+    local object = newNode:CreateComponent("StaticModel")
+    object.model = cache:GetResource("Model", "Models/Box.mdl")
+    object.material = cache:GetResource("Material", "Materials/StoneSmall.xml")
     object.castShadows = true
     object.castShadows = true
     object.shadowDistance = 150.0
     object.shadowDistance = 150.0
     object.drawDistance = 200.0
     object.drawDistance = 200.0
@@ -465,19 +465,19 @@ function HandlePostRenderUpdate()
     
     
     -- Draw rendering debug geometry without depth test to see the effect of occlusion
     -- Draw rendering debug geometry without depth test to see the effect of occlusion
     if drawDebug == 1 then
     if drawDebug == 1 then
-        renderer:DrawDebugGeometry(false)
+        renderer:DrawDebugGeometry(true)
     end
     end
     if drawDebug == 2 then
     if drawDebug == 2 then
-        testScene:GetPhysicsWorld():DrawDebugGeometry(true)
+        testScene:GetComponent("PhysicsWorld"):DrawDebugGeometry(true)
     end
     end
     
     
     local pos = ui.cursorPosition
     local pos = ui.cursorPosition
-    if ui:GetElementAt(pos, true) == nil and testScene:GetOctree() ~= nil then
+    if ui:GetElementAt(pos, true) == nil and testScene:GetComponent("Octree") ~= nil then
         local cameraRay = camera:GetScreenRay(pos.x / graphics:GetWidth(), pos.y / graphics:GetHeight())
         local cameraRay = camera:GetScreenRay(pos.x / graphics:GetWidth(), pos.y / graphics:GetHeight())
-        local result = testScene:GetOctree():RaycastSingle(cameraRay, RAY_TRIANGLE, 250.0, DRAWABLE_GEOMETRY)
+        local result = testScene:GetComponent("Octree"):RaycastSingle(cameraRay, RAY_TRIANGLE, 250.0, DRAWABLE_GEOMETRY)
         if result.drawable ~= nil then
         if result.drawable ~= nil then
             local rayHitPos = cameraRay.origin + cameraRay.direction * result.distance
             local rayHitPos = cameraRay.origin + cameraRay.direction * result.distance
-            testScene:GetDebugRenderer():AddBoundingBox(BoundingBox(rayHitPos + Vector3(-0.01, -0.01, -0.01), rayHitPos +
+            testScene:GetComponent("DebugRenderer"):AddBoundingBox(BoundingBox(rayHitPos + Vector3(-0.01, -0.01, -0.01), rayHitPos +
                 Vector3(0.01, 0.01, 0.01)), Color(1.0, 1.0, 1.0), true)
                 Vector3(0.01, 0.01, 0.01)), Color(1.0, 1.0, 1.0), true)
         end
         end
     end
     end
@@ -505,7 +505,7 @@ function HandleHit(node)
     -- Remove the trigger physics shape, and create the ragdoll
     -- Remove the trigger physics shape, and create the ragdoll
     node:RemoveComponent("RigidBody")
     node:RemoveComponent("RigidBody")
     node:RemoveComponent("CollisionShape")
     node:RemoveComponent("CollisionShape")
-    CreateRagdoll(node:GetAnimatedModel())
+    CreateRagdoll(node:GetComponent("AnimatedModel"))
 end
 end
 
 
 function CreateRagdoll(model)
 function CreateRagdoll(model)
@@ -549,14 +549,14 @@ function CreateRagdollBone(root, boneName, type, size, position, rotation)
     -- In networked operation both client and server detect collisions separately, and create ragdolls on their own
     -- In networked operation both client and server detect collisions separately, and create ragdolls on their own
     -- (bones are not synced over network.) To prevent replicated component ID range clashes when the client creates
     -- (bones are not synced over network.) To prevent replicated component ID range clashes when the client creates
     -- any components, it is important that the LOCAL creation mode is specified.
     -- any components, it is important that the LOCAL creation mode is specified.
-    local body = boneNode:CreateRigidBody(LOCAL)
+    local body = boneNode:CreateComponent("RigidBody", LOCAL)
     body.mass = 1.0
     body.mass = 1.0
     body.linearDamping = 0.05
     body.linearDamping = 0.05
     body.angularDamping = 0.85
     body.angularDamping = 0.85
     body.linearRestThreshold = 1.5
     body.linearRestThreshold = 1.5
     body.angularRestThreshold = 2.5
     body.angularRestThreshold = 2.5
 
 
-    local shape = boneNode:CreateCollisionShape(LOCAL)
+    local shape = boneNode:CreateComponent("CollisionShape", LOCAL)
     shape.shapeType = type
     shape.shapeType = type
     shape.size = size
     shape.size = size
     shape.position = position
     shape.position = position
@@ -570,11 +570,11 @@ function CreateRagdollConstraint(root, boneName, parentName, type, axis, parentA
         return
         return
     end
     end
 
 
-    local constraint = boneNode:CreateConstraint(LOCAL)
+    local constraint = boneNode:CreateComponent("Constraint", LOCAL)
     constraint.constraintType = type
     constraint.constraintType = type
     constraint.disableCollision = disableCollision
     constraint.disableCollision = disableCollision
     -- The connected body must be specified before setting the world position
     -- The connected body must be specified before setting the world position
-    constraint.otherBody = parentNode:GetRigidBody()
+    constraint.otherBody = parentNode:GetComponent("RigidBody")
     constraint.worldPosition = boneNode.worldPosition
     constraint.worldPosition = boneNode.worldPosition
     constraint:SetAxis(axis)
     constraint:SetAxis(axis)
     constraint:SetOtherAxis(parentAxis)
     constraint:SetOtherAxis(parentAxis)

+ 3 - 3
Bin/Data/LuaScripts/Utilities/Sample.lua

@@ -26,14 +26,14 @@ end
 function CreateLogo()
 function CreateLogo()
     -- Get logo texture
     -- Get logo texture
     local cache = GetCache()
     local cache = GetCache()
-    local logoTexture = cache:GetTexture2D("Textures/LogoLarge.png")
+    local logoTexture = cache:GetResource("Texture2D", "Textures/LogoLarge.png")
     if logoTexture == nil then
     if logoTexture == nil then
         return
         return
     end
     end
     
     
     -- Create logo sprite and add to the UI layout
     -- Create logo sprite and add to the UI layout
     local ui = GetUI()
     local ui = GetUI()
-    logoSprite = ui.root:CreateSprite()
+    logoSprite = ui.root:CreateChild("Sprite")
     
     
     -- Set logo sprite texture
     -- Set logo sprite texture
     logoSprite:SetTexture(logoTexture)
     logoSprite:SetTexture(logoTexture)
@@ -63,7 +63,7 @@ end
 function CreateConsoleAndDebugHud()
 function CreateConsoleAndDebugHud()
     -- Get default style
     -- Get default style
     local cache = GetCache()
     local cache = GetCache()
-    local uiStyle = cache:GetXMLFile("UI/DefaultStyle.xml")
+    local uiStyle = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
     if uiStyle == nil then
     if uiStyle == nil then
         return
         return
     end
     end