瀏覽代碼

Updated follow and added move_to example

Björn Ritzl 3 年之前
父節點
當前提交
2c24ecb728

+ 18 - 0
examples/_main/loader.go

@@ -838,3 +838,21 @@ embedded_components {
     w: 1.0
   }
 }
+embedded_components {
+  id: "basics/move_to"
+  type: "collectionproxy"
+  data: "collection: \"/examples/basics/move_to/move_to.collection\"\n"
+  "exclude: false\n"
+  ""
+  position {
+    x: 0.0
+    y: 0.0
+    z: 0.0
+  }
+  rotation {
+    x: 0.0
+    y: 0.0
+    z: 0.0
+    w: 1.0
+  }
+}

+ 1 - 1
examples/_main/menu.gui_script

@@ -101,7 +101,7 @@ end
 function init(self)
 	self.index = { "basics", "physics", "animation", "gui", "input", "particles", "sound", "render", "debug", "collection", "sprite", "file", "tilemap" }
 	self.index = {}
-	self.index["basics"] = { "simple_move", "message_passing", "follow", "parent_child", "spawn", "z_order", "movement_speed" }
+	self.index["basics"] = { "simple_move", "message_passing", "follow", "move_to", "parent_child", "spawn", "z_order", "movement_speed" }
 	self.index["physics"] = { "dynamic", "kinematic", "raycast", "trigger", "hinge_joint", "pendulum"}
 	self.index["animation"] = { "spinner", "flipbook", "chained_tween", "basic_tween", "spine", "cursor" }
 	self.index["gui"] = { "button", "stencil", "load_texture", "pointer_over", "color", "slice9" }

+ 26 - 6
examples/basics/follow/follow.collection

@@ -37,7 +37,27 @@ embedded_instances {
   "    w: 1.0\n"
   "  }\n"
   "}\n"
-  "embedded_components {\n"
+  ""
+  position {
+    x: 335.192
+    y: 316.802
+    z: 0.0
+  }
+  rotation {
+    x: 0.0
+    y: 0.0
+    z: 0.0
+    w: 1.0
+  }
+  scale3 {
+    x: 1.0
+    y: 1.0
+    z: 1.0
+  }
+}
+embedded_instances {
+  id: "instructions"
+  data: "embedded_components {\n"
   "  id: \"label\"\n"
   "  type: \"label\"\n"
   "  data: \"size {\\n"
@@ -75,13 +95,13 @@ embedded_instances {
   "pivot: PIVOT_CENTER\\n"
   "blend_mode: BLEND_MODE_ALPHA\\n"
   "line_break: false\\n"
-  "text: \\\"Click and I will follow...\\\"\\n"
+  "text: \\\"I will follow the mouse/touch position\\\"\\n"
   "font: \\\"/assets/text48.font\\\"\\n"
   "material: \\\"/builtins/fonts/label.material\\\"\\n"
   "\"\n"
   "  position {\n"
-  "    x: 156.84\n"
-  "    y: 118.002\n"
+  "    x: 0.0\n"
+  "    y: 0.0\n"
   "    z: 0.0\n"
   "  }\n"
   "  rotation {\n"
@@ -93,8 +113,8 @@ embedded_instances {
   "}\n"
   ""
   position {
-    x: 335.192
-    y: 316.802
+    x: 350.0
+    y: 38.0
     z: 0.0
   }
   rotation {

+ 2 - 2
examples/basics/follow/follow.md

@@ -1,7 +1,7 @@
 ---
 title: Follow input
-brief: This example shows how to make a game object move to the position the user clicks.
+brief: This example shows how to make a game object continuously follow the mouse.
 scripts: follow.script
 ---
 
-![follow](follow.png)
+![follow](follow.png)

二進制
examples/basics/follow/follow.png


+ 17 - 24
examples/basics/follow/follow.script

@@ -1,33 +1,26 @@
-function init(self)
-    msg.post(".", "acquire_input_focus") -- <1>
-    self.moving = false -- <2>
-end
+go.property("speed", 350) -- <1>
 
-local function landed(self) -- <9>
-    self.moving = false
+function init(self)
+    msg.post(".", "acquire_input_focus") -- <2>
 end
 
 function on_input(self, action_id, action)
-    if action_id == hash("touch") and action.pressed then -- <3>
-		if not self.moving then -- <4>
-			msg.post("#label", "disable") -- <5>
-			self.moving = true -- <6>
-			pos = vmath.vector3(action.x, action.y, 0) -- <7>
-			go.animate(".", "position", go.PLAYBACK_ONCE_FORWARD, pos, go.EASING_LINEAR, 0.5, 0, landed) -- <8>
-		end
+	if action_id == hash("touch") or not action_id then -- <3>
+		local current_pos = go.get_position() -- <4>
+		local target_pos = vmath.vector3(action.x, action.y, 0) -- <5>
+		local distance = vmath.length(target_pos - current_pos) -- <6>
+		local duration = distance / self.speed -- <7>
+		go.animate(".", "position", go.PLAYBACK_ONCE_FORWARD, target_pos, go.EASING_LINEAR, duration, 0) -- <8>
 	end
 end
 
 --[[
-1. Tell the engine that this game object ("." is shorthand for the current game object) should listen to input. Any input will be received in the `on_input()` function.
-2. Store a flag in `self` (the current script component) to indicate if the game object is moving or not.
-3. If we receive an input action named "touch" and it is pressed then run the following.
-4. If the `moving` flag is not set.
-5. Disable (don't show) the help text label.
-6. Set the `moving` flag.
-7. Create a new position called `pos` (of type `vector3`) where the user clicked.
-8. Animate the game object's ("." is shorthand for the current game object) position to `pos`.
-   When the animation is done, call the function `landed()`.
-9. The function `landed()` is called when the animation is done. It just resets the `moving` flag
-   so subsequent clicks will result in a new movement.
+1. The speed of the game object in pixels/second
+2. Tell the engine that this game object ("." is shorthand for the current game object) should listen to input. Any input will be received in the `on_input()` function.
+3. Check if we received mouse movement (no action id) or an input action named "touch" (touch or mouse click)
+4. Get the current position of the game object.
+5. Set the target position to the position of the mouse or touch.
+6. Calculate the distance (length) between the current and target position.
+7. Calculate the time it takes to travel the distance given the speed of the game object.
+8. Animate the game object's ("." is shorthand for the current game object) position to `target_pos`.
 --]]

+ 111 - 0
examples/basics/move_to/move_to.collection

@@ -0,0 +1,111 @@
+name: "move_to"
+scale_along_z: 0
+embedded_instances {
+  id: "go"
+  data: "components {\n"
+  "  id: \"move_to\"\n"
+  "  component: \"/examples/basics/move_to/move_to.script\"\n"
+  "  position {\n"
+  "    x: 0.0\n"
+  "    y: 0.0\n"
+  "    z: 0.0\n"
+  "  }\n"
+  "  rotation {\n"
+  "    x: 0.0\n"
+  "    y: 0.0\n"
+  "    z: 0.0\n"
+  "    w: 1.0\n"
+  "  }\n"
+  "}\n"
+  "embedded_components {\n"
+  "  id: \"sprite\"\n"
+  "  type: \"sprite\"\n"
+  "  data: \"tile_set: \\\"/assets/sprites.atlas\\\"\\n"
+  "default_animation: \\\"bunny1_ready\\\"\\n"
+  "material: \\\"/builtins/materials/sprite.material\\\"\\n"
+  "blend_mode: BLEND_MODE_ALPHA\\n"
+  "\"\n"
+  "  position {\n"
+  "    x: 0.0\n"
+  "    y: 0.0\n"
+  "    z: 0.0\n"
+  "  }\n"
+  "  rotation {\n"
+  "    x: 0.0\n"
+  "    y: 0.0\n"
+  "    z: 0.0\n"
+  "    w: 1.0\n"
+  "  }\n"
+  "}\n"
+  "embedded_components {\n"
+  "  id: \"label\"\n"
+  "  type: \"label\"\n"
+  "  data: \"size {\\n"
+  "  x: 128.0\\n"
+  "  y: 32.0\\n"
+  "  z: 0.0\\n"
+  "  w: 0.0\\n"
+  "}\\n"
+  "scale {\\n"
+  "  x: 0.5\\n"
+  "  y: 0.5\\n"
+  "  z: 1.0\\n"
+  "  w: 0.0\\n"
+  "}\\n"
+  "color {\\n"
+  "  x: 0.0\\n"
+  "  y: 0.5647059\\n"
+  "  z: 0.99215686\\n"
+  "  w: 1.0\\n"
+  "}\\n"
+  "outline {\\n"
+  "  x: 1.0\\n"
+  "  y: 1.0\\n"
+  "  z: 1.0\\n"
+  "  w: 1.0\\n"
+  "}\\n"
+  "shadow {\\n"
+  "  x: 1.0\\n"
+  "  y: 1.0\\n"
+  "  z: 1.0\\n"
+  "  w: 1.0\\n"
+  "}\\n"
+  "leading: 1.0\\n"
+  "tracking: 0.0\\n"
+  "pivot: PIVOT_CENTER\\n"
+  "blend_mode: BLEND_MODE_ALPHA\\n"
+  "line_break: false\\n"
+  "text: \\\"Click and I will move...\\\"\\n"
+  "font: \\\"/assets/text48.font\\\"\\n"
+  "material: \\\"/builtins/fonts/label.material\\\"\\n"
+  "\"\n"
+  "  position {\n"
+  "    x: 156.84\n"
+  "    y: 118.002\n"
+  "    z: 0.0\n"
+  "  }\n"
+  "  rotation {\n"
+  "    x: 0.0\n"
+  "    y: 0.0\n"
+  "    z: 0.0\n"
+  "    w: 1.0\n"
+  "  }\n"
+  "}\n"
+  ""
+  position {
+    x: 335.0
+    y: 335.0
+    z: 0.0
+  }
+  rotation {
+    x: 0.0
+    y: 0.0
+    z: 0.0
+    w: 1.0
+  }
+  scale3 {
+    x: 1.0
+    y: 1.0
+    z: 1.0
+  }
+}

+ 7 - 0
examples/basics/move_to/move_to.md

@@ -0,0 +1,7 @@
+---
+title: Move to target
+brief: This example shows how to make a game object move to the position the user clicks.
+scripts: move_to.script
+---
+
+![move_to](move_to.png)

二進制
examples/basics/move_to/move_to.png


+ 33 - 0
examples/basics/move_to/move_to.script

@@ -0,0 +1,33 @@
+function init(self)
+	msg.post(".", "acquire_input_focus") -- <1>
+	self.moving = false -- <2>
+end
+
+local function moved_to_position(self) -- <9>
+	self.moving = false
+end
+
+function on_input(self, action_id, action)
+	if action_id == hash("touch") and action.pressed then -- <3>
+		if not self.moving then -- <4>
+			msg.post("#label", "disable") -- <5>
+			self.moving = true -- <6>
+			local pos = vmath.vector3(action.x, action.y, 0) -- <7>
+			go.animate(".", "position", go.PLAYBACK_ONCE_FORWARD, pos, go.EASING_LINEAR, 0.5, 0, moved_to_position) -- <8>
+		end
+	end
+end
+
+--[[
+1. Tell the engine that this game object ("." is shorthand for the current game object) should listen to input. Any input will be received in the `on_input()` function.
+2. Store a flag in `self` (the current script component) to indicate if the game object is moving or not.
+3. If we receive an input action named "touch" and it is pressed then run the following.
+4. If the `moving` flag is not set.
+5. Disable (don't show) the help text label.
+6. Set the `moving` flag.
+7. Create a new position called `pos` (of type `vector3`) where the user clicked.
+8. Animate the game object's ("." is shorthand for the current game object) position to `pos`.
+When the animation is done, call the function `moved_to_position()`.
+9. The function `moved_to_position()` is called when the animation is done. It just resets the `moving` flag
+so subsequent clicks will result in a new movement.
+--]]

+ 3 - 2
game.project

@@ -1,7 +1,7 @@
 [project]
 title = Defold-examples
 version = 0.1
-dependencies#0 = https://github.com/defold/extension-spine/archive/refs/tags/1.0.5.zip
+dependencies#0 = https://github.com/defold/extension-spine/archive/refs/tags/1.1.1.zip
 
 [bootstrap]
 main_collection = /examples/main.collectionc
@@ -35,7 +35,7 @@ subpixels = 1
 subpixels = 1
 
 [windows]
-iap_provider =
+iap_provider = 
 
 [android]
 package = com.defold.examples
@@ -50,3 +50,4 @@ bundle_identifier = com.defold.examples
 show_fullscreen_button = 0
 show_made_with_defold = 0
 scale_mode = no_scale
+