Jelajahi Sumber

Merge pull request #17 from paweljarosz/master

Added cursor animation example. Fixed a comment in hinge joint example.
Björn Ritzl 3 tahun lalu
induk
melakukan
9d87adc9fd

+ 18 - 0
examples/_main/loader.go

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

@@ -103,7 +103,7 @@ function init(self)
 	self.index = {}
 	self.index["basics"] = { "simple_move", "message_passing", "follow", "parent_child", "spawn", "z_order", "movement_speed" }
 	self.index["physics"] = { "dynamic", "kinematic", "raycast", "trigger", "hinge_joint"}
-	self.index["animation"] = { "spinner", "flipbook", "chained_tween", "basic_tween", "spine" }
+	self.index["animation"] = { "spinner", "flipbook", "chained_tween", "basic_tween", "spine", "cursor" }
 	self.index["gui"] = { "button", "stencil", "load_texture", "pointer_over", "color"}
 	self.index["input"] = { "move", "text", "down_duration", "mouse_and_touch" }
 	self.index["particles"] = { "particlefx", "modifiers" }

+ 111 - 0
examples/animation/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/animation/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/animation/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)

TEMPAT SAMPAH
examples/animation/cursor/cursor.png


+ 30 - 0
examples/animation/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 > 3 then  -- <5>
+			self.duration = 0
+		end
+		label.set_text("#info", "Cursor animation 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 3, 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.
+--]]

+ 1 - 1
examples/physics/hinge_joint/hinge_joint.script

@@ -33,7 +33,7 @@ end
 2. 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.
 3. Set a flag self.forward used to define the direction of the rotation of the joined wheels.
 4. Create a joint of type "hinge" (a revolute joint, a pin or an axle) between a center of frontwheel collision object and an anchor ((-60,-60) from the center) on body collision object with provided properties. Do the same for the second wheel.
-5. If we receive input (touch or mouse movement) we switch the direction of rotation of the wheels.
+5. If we receive input (touch or mouse click) we switch the direction of rotation of the wheels.
 6. Negate the current flag defining the direction.
 7. If the direction flag is true, we are going forward.
 8. Set the motor_speed property to 5 revolutions per second in clockwise direction.