Kaynağa Gözat

Added get and set tile example

Björn Ritzl 4 yıl önce
ebeveyn
işleme
23a86b4d69

+ 0 - 0
examples/tilemap/collisions/tiles.tilesource → assets/tiles/tiles.tilesource


+ 0 - 0
examples/tilemap/collisions/tilesource.png → assets/tiles/tilesource.png


+ 18 - 0
examples/_main/loader.go

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

@@ -113,7 +113,7 @@ function init(self)
 	self.index["collection"] = { "proxy", "splash" }
 	self.index["sprite"] = { "size", "tint" }
 	self.index["file"] = { "sys_save_load" }
-	self.index["tilemap"] = { "collisions" }
+	self.index["tilemap"] = { "collisions", "get_set_tile" }
 	local categories = {}
 	for k,_ in pairs(self.index) do
 		categories[#categories + 1] = k

+ 1 - 0
examples/sound/fade_in_out/fade_in_out.collection

@@ -100,6 +100,7 @@ embedded_instances {
   "gain: 1.0\\n"
   "pan: 0.0\\n"
   "speed: 1.0\\n"
+  "loopcount: 0\\n"
   "\"\n"
   "  position {\n"
   "    x: 0.0\n"

+ 1 - 0
examples/sound/music/music.collection

@@ -100,6 +100,7 @@ embedded_instances {
   "gain: 1.0\\n"
   "pan: 0.0\\n"
   "speed: 1.0\\n"
+  "loopcount: 0\\n"
   "\"\n"
   "  position {\n"
   "    x: 0.0\n"

+ 1 - 1
examples/tilemap/collisions/level.tilemap

@@ -1,4 +1,4 @@
-tile_set: "/examples/tilemap/collisions/tiles.tilesource"
+tile_set: "/assets/tiles/tiles.tilesource"
 layers {
   id: "layer1"
   z: 0.0

+ 106 - 0
examples/tilemap/get_set_tile/get_set_tile.collection

@@ -0,0 +1,106 @@
+name: "default"
+scale_along_z: 0
+embedded_instances {
+  id: "go"
+  data: "components {\n"
+  "  id: \"level\"\n"
+  "  component: \"/examples/tilemap/get_set_tile/level.tilemap\"\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"
+  "components {\n"
+  "  id: \"get_set_tile\"\n"
+  "  component: \"/examples/tilemap/get_set_tile/get_set_tile.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: \"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: 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: false\\n"
+  "text: \\\"\\\"\\n"
+  "font: \\\"/assets/text24.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"
+  "}\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
+  }
+}

+ 11 - 0
examples/tilemap/get_set_tile/get_set_tile.md

@@ -0,0 +1,11 @@
+---
+title: Get and set tiles
+brief: This example shows how to get and set tiles of a tilemap
+scripts: get_set_tiles.script
+---
+
+This example uses a tilemap and a script file to read mouse input to place tiles or read information about the tile the mouse is hovering.
+
+---
+
+Tilemap graphics made by [Kenney](https://www.kenney.nl).

+ 31 - 0
examples/tilemap/get_set_tile/get_set_tile.script

@@ -0,0 +1,31 @@
+local TILE_SIZE = 64
+
+-- helper function check if a tile coordinate is within the bounds of the tilemap
+local function within_bounds(x, y)
+	local bx, by, bw, bh = tilemap.get_bounds("#level")
+	return x >= bx and y >= by and x < (bx + bw) and y < (by + bh)
+end
+
+function init(self)
+	msg.post(".", "acquire_input_focus")
+end
+
+function on_input(self, action_id, action)
+	local tile_x = math.ceil(action.x / TILE_SIZE)
+	local tile_y = math.ceil(action.y / TILE_SIZE)
+
+	if within_bounds(tile_x, tile_y) then
+		-- click to place a flower
+		if action_id == hash("touch") and action.pressed then
+			tilemap.set_tile("#level", "layer1", tile_x, tile_y, 77)
+		end
+
+		-- show tile info
+		local tile = tilemap.get_tile("#level", "layer1", tile_x, tile_y)
+		local text = ("x: %d y: %d tile: %d"):format(tile_x, tile_y, tile)
+		label.set_text("#label", text)
+	else
+		local text = ("x: %d y: %d out of bounds"):format(tile_x, tile_y)
+		label.set_text("#label", text)
+	end
+end

+ 267 - 0
examples/tilemap/get_set_tile/level.tilemap

@@ -0,0 +1,267 @@
+tile_set: "/assets/tiles/tiles.tilesource"
+layers {
+  id: "layer1"
+  z: 0.0
+  is_visible: 1
+  cell {
+    x: 1
+    y: 2
+    tile: 28
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 2
+    y: 2
+    tile: 2
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 3
+    y: 2
+    tile: 2
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 4
+    y: 2
+    tile: 2
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 5
+    y: 2
+    tile: 2
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 6
+    y: 2
+    tile: 2
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 7
+    y: 2
+    tile: 2
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 8
+    y: 2
+    tile: 2
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 9
+    y: 2
+    tile: 27
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 1
+    y: 3
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 9
+    y: 3
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 1
+    y: 4
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 9
+    y: 4
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 1
+    y: 5
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 3
+    y: 5
+    tile: 8
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 9
+    y: 5
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 1
+    y: 6
+    tile: 22
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 4
+    y: 6
+    tile: 5
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 5
+    y: 6
+    tile: 6
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 6
+    y: 6
+    tile: 7
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 8
+    y: 6
+    tile: 8
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 9
+    y: 6
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 1
+    y: 7
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 6
+    y: 7
+    tile: 5
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 7
+    y: 7
+    tile: 7
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 9
+    y: 7
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 1
+    y: 8
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 9
+    y: 8
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 1
+    y: 9
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 2
+    y: 9
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 3
+    y: 9
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 4
+    y: 9
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 5
+    y: 9
+    tile: 44
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 6
+    y: 9
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 7
+    y: 9
+    tile: 22
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 8
+    y: 9
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+  cell {
+    x: 9
+    y: 9
+    tile: 0
+    h_flip: 0
+    v_flip: 0
+  }
+}
+material: "/builtins/materials/tile_map.material"
+blend_mode: BLEND_MODE_ALPHA