Преглед на файлове

Added cursor animation example

Paweł Jarosz преди 3 години
родител
ревизия
e0c47b4caf

+ 18 - 0
examples/_main/loader.go

@@ -766,3 +766,21 @@ embedded_components {
     w: 1.0
   }
 }
+embedded_components {
+  id: "sprite/cursor"
+  type: "collectionproxy"
+  data: "collection: \"/examples/sprite/cursor/cursor.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

@@ -111,7 +111,7 @@ function init(self)
 	self.index["render"] = { "camera" }
 	self.index["debug"] = { "physics", "profile" }
 	self.index["collection"] = { "proxy", "splash" }
-	self.index["sprite"] = { "size", "tint" }
+	self.index["sprite"] = { "size", "tint", "cursor" }
 	self.index["file"] = { "sys_save_load" }
 	self.index["tilemap"] = { "collisions", "get_set_tile" }
 	local categories = {}

+ 111 - 0
examples/sprite/cursor/cursor.collection

@@ -0,0 +1,111 @@
+name: "default"
+scale_along_z: 0
+embedded_instances {
+  id: "bee"
+  data: "components {\n"
+  "  id: \"cursor\"\n"
+  "  component: \"/examples/sprite/cursor/cursor.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: \\\"bee\\\"\\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: \"info\"\n"
+  "  type: \"label\"\n"
+  "  data: \"size {\\n"
+  "  x: 400.0\\n"
+  "  y: 200.0\\n"
+  "  z: 0.0\\n"
+  "  w: 0.0\\n"
+  "}\\n"
+  "scale {\\n"
+  "  x: 1.0\\n"
+  "  y: 1.0\\n"
+  "  z: 1.0\\n"
+  "  w: 0.0\\n"
+  "}\\n"
+  "color {\\n"
+  "  x: 0.0\\n"
+  "  y: 0.0\\n"
+  "  z: 0.0\\n"
+  "  w: 1.0\\n"
+  "}\\n"
+  "outline {\\n"
+  "  x: 0.0\\n"
+  "  y: 0.0\\n"
+  "  z: 0.0\\n"
+  "  w: 1.0\\n"
+  "}\\n"
+  "shadow {\\n"
+  "  x: 0.0\\n"
+  "  y: 0.0\\n"
+  "  z: 0.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: true\\n"
+  "text: \\\"Click to change cursor animation duration\\\"\\n"
+  "font: \\\"/assets/text48.font\\\"\\n"
+  "material: \\\"/builtins/fonts/label.material\\\"\\n"
+  "\"\n"
+  "  position {\n"
+  "    x: 0.0\n"
+  "    y: 200.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"
+  ""
+  position {
+    x: 360.0
+    y: 360.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
+  }
+}

+ 9 - 0
examples/sprite/cursor/cursor.md

@@ -0,0 +1,9 @@
+---
+title: Cursor animation
+brief: This example shows how to set and animate the normalized cursor value.
+scripts: cursor.script
+---
+
+The example uses one game object with a sprite component and a label (to show the duration). The game object contains the script that sets and animates the normalized cursor value making the sprite's animation progress and shows the duration value on the label:
+
+![cursor](cursor.png)

BIN
examples/sprite/cursor/cursor.png


+ 30 - 0
examples/sprite/cursor/cursor.script

@@ -0,0 +1,30 @@
+function init(self)
+	msg.post(".", "acquire_input_focus") -- <1>
+	-- Get the current value on component "sprite"
+	self.duration = 0 -- <2>
+end
+
+function on_input(self, action_id, action)
+	if action_id == hash("touch") and action.pressed then -- <3>
+		self.duration = self.duration + 1 -- <4>
+		if self.duration > 2 then  -- <5>
+			self.duration = 0
+		end
+		label.set_text("#info", "Duration: "..self.duration)  -- <6>
+		go.cancel_animations("#sprite", "cursor")  -- <7>
+		go.set("#sprite", "cursor", 0.0)  -- <8>
+		go.animate("#sprite", "cursor", go.PLAYBACK_LOOP_FORWARD, 1, go.EASING_LINEAR, self.duration) -- <9>
+	end
+end
+
+--[[
+1. Tell the engine that this 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 duration time used in this example (for defining how long the cursor animation should take) in self reference.
+3. If we receive input (touch or mouse click) we change the duration of the cursor animation.
+4. Increase the duration.
+5. If the duration is larger than 2, set it back to 0 to make a circular change of the duration.
+6. Set the text of the label with id `info` to show the current duration of the animation to user.
+7. Cancel previous animation on cursor value.
+8. Reset cursor value to 0.
+9. Start new animation of cursor value with playback set to be looped and in foward direction (increasing cursor value), linear easing and new duration.
+--]]