Browse Source

Added look-at example

Björn Ritzl 3 năm trước cách đây
mục cha
commit
6aec867ef5

BIN
assets/images/hitman1_machine.png


+ 4 - 0
assets/sprites.atlas

@@ -58,6 +58,10 @@ images {
   image: "/assets/images/plank_slice.png"
   sprite_trim_mode: SPRITE_TRIM_MODE_OFF
 }
+images {
+  image: "/assets/images/hitman1_machine.png"
+  sprite_trim_mode: SPRITE_TRIM_MODE_OFF
+}
 animations {
   id: "bee"
   images {

+ 18 - 0
examples/_main/loader.go

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

+ 2 - 0
examples/_main/loader.script

@@ -5,6 +5,7 @@ function init(self)
 
 	-- Start from specific example config or menu
 	local example = sys.get_config("examples.start", nil)
+	print("examples.start", example)
 	if example then
 		msg.post("#", "load_example", { example = hash(example), nomenu = true })
 	else
@@ -14,6 +15,7 @@ end
 
 function on_message(self, message_id, message, sender)
 	if message_id == hash("load_example") then
+		print("load_example", message.example)
 		self.current_proxy = msg.url(nil, "loader", message.example)
 		msg.post(self.current_proxy, "load")
 		self.nomenu = message.nomenu

+ 1 - 1
examples/_main/menu.gui_script

@@ -101,7 +101,7 @@ end
 function init(self)
 	self.index = {}
 	self.index["basics"] = { "message_passing", "parent_child", "spawn", "z_order" }
-	self.index["movement"] = { "simple_move", "follow", "move_to", "move_forward", "movement_speed" }
+	self.index["movement"] = { "simple_move", "follow", "move_to", "move_forward", "movement_speed", "look_at" }
 	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" }

+ 131 - 0
examples/movement/look_at/look_at.collection

@@ -0,0 +1,131 @@
+name: "look_at"
+scale_along_z: 0
+embedded_instances {
+  id: "go"
+  data: "components {\n"
+  "  id: \"look_at\"\n"
+  "  component: \"/examples/movement/look_at/look_at.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: \\\"hitman1_machine\\\"\\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"
+  ""
+  position {
+    x: 350.0
+    y: 350.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
+  }
+}
+embedded_instances {
+  id: "instructions"
+  data: "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: \\\"Move with arrow keys (left+right = rotate, up = forward)\\\"\\n"
+  "font: \\\"/assets/text48.font\\\"\\n"
+  "material: \\\"/builtins/fonts/label.material\\\"\\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"
+  ""
+  position {
+    x: 350.0
+    y: 38.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
+  }
+}

+ 5 - 0
examples/movement/look_at/look_at.md

@@ -0,0 +1,5 @@
+---
+title: Look at
+brief: This example shows how to rotate a game object to look at the mouse cursor
+scripts: look_at.script
+---

+ 16 - 0
examples/movement/look_at/look_at.script

@@ -0,0 +1,16 @@
+function init(self)
+	-- make sure the script will receive user input
+	msg.post(".", "acquire_input_focus")
+end
+
+function on_input(self, action_id, action)
+	-- the position to look at (mouse/finger)
+	local look_at = vmath.vector3(action.x, action.y, 0)
+	-- own positon
+	local my_position = go.get_position()
+
+	-- calculate the angle that this object has to rotate to look at the given point
+	local angle = math.atan2(my_position.x - look_at.x, look_at.y - my_position.y)
+	-- set rotation as a quaternion
+	go.set_rotation(vmath.quat_rotation_z(angle))
+end

+ 2 - 2
game.project

@@ -1,10 +1,10 @@
 [project]
 title = Defold-examples
 version = 0.1
-dependencies#0 = https://github.com/defold/extension-spine/archive/refs/tags/1.1.1.zip
+dependencies#0 = https://github.com/defold/extension-spine/archive/refs/tags/1.2.0.zip
 
 [bootstrap]
-main_collection = /examples/main.collectionc
+main_collection = /examples/movement/look_at/look_at.collectionc
 
 [input]
 game_binding = /input/game.input_bindingc