Explorar el Código

Added dynamic factory example

Björn Ritzl hace 1 año
padre
commit
7efb40245d

BIN
assets/images/lighting_blue.png


BIN
assets/images/particle_brown.png


+ 11 - 0
assets/sprites.atlas

@@ -74,6 +74,14 @@ images {
   image: "/assets/images/enemyBlack1.png"
   sprite_trim_mode: SPRITE_TRIM_MODE_OFF
 }
+images {
+  image: "/assets/images/lighting_blue.png"
+  sprite_trim_mode: SPRITE_TRIM_MODE_OFF
+}
+images {
+  image: "/assets/images/particle_brown.png"
+  sprite_trim_mode: SPRITE_TRIM_MODE_OFF
+}
 animations {
   id: "bee"
   images {
@@ -297,3 +305,6 @@ animations {
 margin: 0
 extrude_borders: 2
 inner_padding: 0
+max_page_width: 0
+max_page_height: 0
+rename_patterns: ""

+ 22 - 22
examples/_main/loader.go

@@ -50,7 +50,7 @@ embedded_components {
   id: "animation/spinner"
   type: "collectionproxy"
   data: "collection: \"/examples/animation/spinner/spinner.collection\"\n"
-  "exclude: false\n"
+  "exclude: true\n"
   ""
   position {
     x: 0.0
@@ -68,7 +68,7 @@ embedded_components {
   id: "basics/message_passing"
   type: "collectionproxy"
   data: "collection: \"/examples/basics/message_passing/message_passing.collection\"\n"
-  "exclude: false\n"
+  "exclude: true\n"
   ""
   position {
     x: 0.0
@@ -104,7 +104,7 @@ embedded_components {
   id: "basics/parent_child"
   type: "collectionproxy"
   data: "collection: \"/examples/basics/parent_child/parent_child.collection\"\n"
-  "exclude: false\n"
+  "exclude: true\n"
   ""
   position {
     x: 0.0
@@ -298,24 +298,6 @@ embedded_components {
     w: 1.0
   }
 }
-embedded_components {
-  id: "animation/spine"
-  type: "collectionproxy"
-  data: "collection: \"/examples/animation/spine/spine.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
-  }
-}
 embedded_components {
   id: "debug/physics"
   type: "collectionproxy"
@@ -482,7 +464,7 @@ embedded_components {
   id: "basics/z_order"
   type: "collectionproxy"
   data: "collection: \"/examples/basics/z_order/z_order.collection\"\n"
-  "exclude: false\n"
+  "exclude: true\n"
   ""
   position {
     x: 0.0
@@ -1072,3 +1054,21 @@ embedded_components {
     w: 1.0
   }
 }
+embedded_components {
+  id: "factory/dynamic"
+  type: "collectionproxy"
+  data: "collection: \"/examples/factory/dynamic/dynamic.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 = {}
 	self.index["basics"] = { "message_passing", "parent_child", "z_order" }
-	self.index["factory"] = { "basic", "bullets" }
+	self.index["factory"] = { "basic", "bullets", "dynamic" }
 	self.index["movement"] = { "simple_move", "follow", "move_to", "move_forward", "movement_speed", "look_at" }
 	self.index["physics"] = { "dynamic", "kinematic", "raycast", "trigger", "hinge_joint", "pendulum", "knockback"}
 	self.index["animation"] = { "spinner", "flipbook", "chained_tween", "basic_tween", "spine", "cursor", "easing" }

+ 62 - 0
examples/factory/dynamic/bullets.collection

@@ -0,0 +1,62 @@
+name: "dynamic_bullets"
+instances {
+  id: "lightning"
+  prototype: "/examples/factory/dynamic/lightning.go"
+  position {
+    x: 0.0
+    y: 0.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
+  }
+}
+instances {
+  id: "flame"
+  prototype: "/examples/factory/dynamic/flame.go"
+  position {
+    x: 0.0
+    y: 0.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
+  }
+}
+instances {
+  id: "rock"
+  prototype: "/examples/factory/dynamic/rock.go"
+  position {
+    x: 0.0
+    y: 0.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
+  }
+}
+scale_along_z: 0

+ 169 - 0
examples/factory/dynamic/dynamic.collection

@@ -0,0 +1,169 @@
+name: "dynamic"
+scale_along_z: 0
+embedded_instances {
+  id: "ship"
+  data: "components {\n"
+  "  id: \"dynamic\"\n"
+  "  component: \"/examples/factory/dynamic/dynamic.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"
+  "  property_decls {\n"
+  "  }\n"
+  "}\n"
+  "embedded_components {\n"
+  "  id: \"bulletfactory\"\n"
+  "  type: \"factory\"\n"
+  "  data: \"prototype: \\\"/examples/factory/dynamic/flame.go\\\"\\n"
+  "load_dynamically: false\\n"
+  "dynamic_prototype: true\\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: \"bullets\"\n"
+  "  type: \"collectionproxy\"\n"
+  "  data: \"collection: \\\"/examples/factory/dynamic/bullets.collection\\\"\\n"
+  "exclude: false\\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: \"sprite\"\n"
+  "  type: \"sprite\"\n"
+  "  data: \"tile_set: \\\"/assets/sprites.atlas\\\"\\n"
+  "default_animation: \\\"playerShip1_red\\\"\\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: 1.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: 116.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: "go1"
+  data: "embedded_components {\n"
+  "  id: \"label1\"\n"
+  "  type: \"label\"\n"
+  "  data: \"size {\\n"
+  "  x: 128.0\\n"
+  "  y: 32.0\\n"
+  "  z: 0.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: 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: false\\n"
+  "text: \\\"Click/spacebar to change bullet\\\"\\n"
+  "font: \\\"/assets/text48.font\\\"\\n"
+  "material: \\\"/builtins/fonts/label.material\\\"\\n"
+  "\"\n"
+  "  position {\n"
+  "    x: 360.0\n"
+  "    y: 20.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"
+  "  scale {\n"
+  "    x: 0.5\n"
+  "    y: 0.5\n"
+  "    z: 1.0\n"
+  "  }\n"
+  "}\n"
+  ""
+  position {
+    x: 0.0
+    y: 0.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
+  }
+}

+ 14 - 0
examples/factory/dynamic/dynamic.md

@@ -0,0 +1,14 @@
+---
+title: Dynamic factories
+brief: This example shows how to change the prototype game object used by a factory component.
+scripts: dynamic.script
+---
+
+This example shows how to change the prototype game object used by a factory component. All prototype bullets are stored in a collection and referenced as a collection proxy. The collection proxy is never loaded, but it will ensure that the bullet prototypes are included in the build even though they are not immediately used by a factory. Another alternative is to load bullet prototypes using Live Update.
+
+ship
+: The red ship at the bottom. Contains:
+  - A *Sprite* component with the spaceship image.
+  - A *Factory* component to spawn bullet game objects. This component has the *Dynamic Protoype* option checked.
+  - A *Collection Proxy* component referencing a collection containing all bullet types
+  - A *Script* component to handle spawning of bullets.

+ 42 - 0
examples/factory/dynamic/dynamic.script

@@ -0,0 +1,42 @@
+function init(self)
+	msg.post(".", "acquire_input_focus")
+
+	-- a list of different bullet prototypes
+	self.bullets = {
+		"/examples/factory/dynamic/flame.goc",
+		"/examples/factory/dynamic/lightning.goc",
+		"/examples/factory/dynamic/rock.goc",
+	}
+	-- the currently used bullet prototype
+	self.bullet_index = 1
+
+	-- shoot one bullet per second
+	-- animate the bullet up 1000 pixels and then delete it
+	timer.delay(0.2, true, function()
+		local id = factory.create("#bulletfactory")
+		local to = go.get_position(id)
+		to.y = to.y + 1000
+		go.animate(id, "position", go.PLAYBACK_ONCE_FORWARD, to, go.EASING_LINEAR, 1.5, 0, function()
+			go.delete(id)
+		end)
+	end)
+end
+
+function on_input(self, action_id, action)
+	if action_id == hash("action") then
+		if action.pressed then
+			-- next bullet prototype, wrap around to the first
+			self.bullet_index = self.bullet_index + 1
+			if self.bullet_index > #self.bullets then
+				self.bullet_index = 1
+			end
+
+			-- unload current prototype
+			factory.unload("#bulletfactory")
+
+			-- set a new prototype
+			local prototype = self.bullets[self.bullet_index]
+			factory.set_prototype("#bulletfactory", prototype)
+		end
+	end
+end

+ 20 - 0
examples/factory/dynamic/flame.go

@@ -0,0 +1,20 @@
+embedded_components {
+  id: "sprite"
+  type: "sprite"
+  data: "tile_set: \"/assets/sprites.atlas\"\n"
+  "default_animation: \"flame\"\n"
+  "material: \"/builtins/materials/sprite.material\"\n"
+  "blend_mode: BLEND_MODE_ALPHA\n"
+  ""
+  position {
+    x: 0.0
+    y: 0.0
+    z: 0.0
+  }
+  rotation {
+    x: 0.0
+    y: 0.0
+    z: 0.0
+    w: 1.0
+  }
+}

+ 20 - 0
examples/factory/dynamic/lightning.go

@@ -0,0 +1,20 @@
+embedded_components {
+  id: "sprite"
+  type: "sprite"
+  data: "tile_set: \"/assets/sprites.atlas\"\n"
+  "default_animation: \"lighting_blue\"\n"
+  "material: \"/builtins/materials/sprite.material\"\n"
+  "blend_mode: BLEND_MODE_ALPHA\n"
+  ""
+  position {
+    x: 0.0
+    y: 0.0
+    z: 0.0
+  }
+  rotation {
+    x: 0.0
+    y: 0.0
+    z: 0.0
+    w: 1.0
+  }
+}

+ 20 - 0
examples/factory/dynamic/rock.go

@@ -0,0 +1,20 @@
+embedded_components {
+  id: "sprite"
+  type: "sprite"
+  data: "tile_set: \"/assets/sprites.atlas\"\n"
+  "default_animation: \"particle_brown\"\n"
+  "material: \"/builtins/materials/sprite.material\"\n"
+  "blend_mode: BLEND_MODE_ALPHA\n"
+  ""
+  position {
+    x: 0.0
+    y: 0.0
+    z: 0.0
+  }
+  rotation {
+    x: 0.0
+    y: 0.0
+    z: 0.0
+    w: 1.0
+  }
+}