Commits

Danny Fritz  committed 0c9f40b

Pushed finished tilemap loader.

  • Participants
  • Parent commits 6f1ab19

Comments (0)

Files changed (4)

File free_tileset_version_10.png

Added
New image
-print("Hello World')
---other unrelated change
+local sprite = require("sprite")
+require "test"
+
+display.setStatusBar( display.HiddenStatusBar )
+
+local screen_height = display.viewableContentHeight
+local screen_width = display.viewableContentWidth
+
+local global_x = 0
+local global_x_delta = nil
+local global_x_start = nil
+local global_x_orig = nil
+local global_y = 0
+local global_y_delta = nil
+local global_y_start = nil
+local global_y_orig = nil
+
+local hiddenFG = false
+
+local tileSheet =
+		sprite.newSpriteSheet("free_tileset_version_10.png",
+		map.tilesets[1].tilewidth,
+		map.tilesets[1].tileheight)
+local tileSet = sprite.newSpriteSet(tileSheet, 1,
+		map.tilesets[1].imagewidth / map.tilesets[1].tilewidth *
+		map.tilesets[1].imageheight / map.tilesets[1].tilewidth)
+local tiles = {}
+tiles.bg = {}
+for i=1, 20 do
+	table.insert(tiles.bg, {})
+	for j=1, 20 do
+		local tile = sprite.newSprite(tileSet)
+		table.insert(tiles.bg[i], tile)
+	end
+end
+tiles.fg = {}
+for i=1, 20 do
+	table.insert(tiles.fg, {})
+	for j=1, 20 do
+		local tile = sprite.newSprite(tileSet)
+		table.insert(tiles.fg[i], tile)
+	end
+end
+
+local function drawMap()
+	for cur_layer=1, #map.layers do
+		local layer = map.layers[cur_layer]
+		for cur_height=1, layer.height do
+			for cur_width=1, layer.width do
+				local tile = nil
+				if layer.name == "FG" then
+					tile = tiles.fg[cur_width][cur_height]
+				else
+					tile = tiles.bg[cur_width][cur_height]
+				end
+				local currentFrame = layer.data[(cur_height-1)*20+cur_width]
+				tile.currentFrame = layer.data[(cur_height-1)*20+cur_width]
+				tile.x = cur_width*32-16 + global_x
+				tile.y = cur_height*32-16 + global_y
+				if currentFrame ~= 0 then
+					tile.isVisible = true
+				else
+					tile.isVisible = false
+				end
+				if tile.x <= 0 or tile.y <= 0 then
+					tile.isVisible = false
+				elseif tile.x >= screen_width or tile.y >= screen_height then
+					tile.isVisible = false
+				end
+				if hiddenFG and layer.name == "FG" then
+					tile.isVisible = false
+				end
+			end
+		end
+	end
+end
+
+local function moveMap(event)
+	if event.phase == "began" then
+		global_x_start, global_y_start = event.xStart, event.yStart
+		global_x_orig, global_y_orig = global_x, global_y
+	elseif event.phase == "moved" or event.phase == "ended" then
+		global_x = global_x_orig - (global_x_start - event.x)
+		global_y = global_y_orig - (global_y_start - event.y)
+	end
+end
+
+local function hideFG(event)
+	hiddenFG = not hiddenFG
+end
+
+Runtime:addEventListener("enterFrame", drawMap)
+Runtime:addEventListener("touch", moveMap)
+Runtime:addEventListener("tap", hideFG)
+map = {
+  version = "1.1",
+  luaversion = "5.1",
+  orientation = "orthogonal",
+  width = 20,
+  height = 20,
+  tilewidth = 32,
+  tileheight = 32,
+  properties = {
+  },
+  tilesets = {
+    {
+      name = "silveira",
+      firstgid = 1,
+      tilewidth = 32,
+      tileheight = 32,
+      spacing = 0,
+      margin = 0,
+      image = "free_tileset_version_10.png",
+      imagewidth = 480,
+      imageheight = 1216,
+      tiles = {
+      }
+    }
+  },
+  layers = {
+    {
+      type = "tilelayer",
+      name = "BG",
+      x = 0,
+      y = 0,
+      width = 20,
+      height = 20,
+      visible = true,
+      opacity = 1,
+      properties = {
+      },
+      encoding = "lua",
+      data = {
+        497, 13, 276, 276, 276, 276, 276, 276, 276, 276, 276, 276, 276, 276, 276, 276, 276, 276, 276, 277,
+        497, 13, 361, 362, 363, 364, 276, 276, 261, 276, 276, 276, 276, 276, 276, 276, 276, 292, 276, 292,
+        497, 13, 376, 377, 378, 379, 276, 276, 276, 276, 276, 276, 276, 277, 276, 276, 276, 276, 276, 276,
+        497, 13, 391, 392, 393, 394, 276, 292, 261, 292, 261, 276, 276, 276, 276, 276, 276, 292, 261, 292,
+        497, 13, 406, 407, 408, 409, 276, 276, 276, 277, 276, 276, 276, 276, 276, 277, 276, 276, 276, 277,
+        497, 13, 421, 422, 423, 424, 292, 261, 292, 261, 276, 276, 276, 276, 276, 292, 276, 276, 276, 276,
+        385, 12, 12, 12, 12, 12, 276, 276, 276, 276, 276, 276, 276, 277, 276, 276, 276, 276, 276, 276,
+        499, 415, 496, 496, 496, 496, 496, 496, 496, 496, 496, 496, 496, 496, 496, 496, 496, 496, 496, 496,
+        276, 276, 276, 276, 276, 276, 276, 276, 276, 277, 276, 276, 526, 276, 529, 277, 276, 276, 276, 276,
+        276, 292, 261, 292, 261, 292, 276, 276, 276, 276, 276, 276, 276, 276, 276, 276, 276, 276, 261, 292,
+        276, 276, 276, 277, 276, 276, 276, 276, 276, 277, 276, 276, 276, 277, 276, 276, 276, 276, 276, 276,
+        291, 292, 261, 276, 276, 276, 276, 292, 276, 276, 261, 292, 261, 276, 276, 292, 292, 292, 261, 276,
+        276, 276, 276, 276, 276, 276, 276, 276, 276, 277, 276, 277, 276, 276, 276, 277, 277, 277, 276, 276,
+        276, 261, 261, 276, 276, 276, 276, 292, 291, 292, 276, 276, 261, 292, 291, 292, 276, 276, 292, 291,
+        276, 276, 276, 277, 276, 277, 276, 276, 276, 276, 276, 276, 276, 276, 276, 276, 276, 277, 276, 276,
+        280, 280, 280, 280, 280, 280, 281, 282, 280, 280, 280, 280, 280, 280, 332, 308, 276, 276, 276, 276,
+        283, 283, 283, 283, 283, 283, 296, 297, 295, 295, 295, 295, 295, 295, 295, 331, 332, 308, 276, 276,
+        295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 331, 332, 332,
+        295, 295, 295, 295, 295, 295, 295, 295, 295, 283, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295,
+        295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295, 295
+      }
+    },
+    {
+      type = "tilelayer",
+      name = "FG",
+      x = 0,
+      y = 0,
+      width = 20,
+      height = 20,
+      visible = true,
+      opacity = 1,
+      properties = {
+      },
+      encoding = "lua",
+      data = {
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 271, 272, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 286, 287, 0, 0, 0, 0, 0, 0, 273, 274, 0, 0, 0, 226, 227, 228,
+        0, 0, 0, 0, 0, 0, 0, 0, 293, 0, 0, 0, 288, 289, 0, 0, 0, 241, 242, 243,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 294, 256, 257, 258,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 279, 0, 294, 188,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 284, 0, 0, 0, 0, 0, 0, 0,
+        0, 284, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570
+      }
+    }
+  }
+}
+<?xml version="1.0" encoding="UTF-8"?>
+<map version="1.0" orientation="orthogonal" width="20" height="20" tilewidth="32" tileheight="32">
+ <tileset firstgid="1" name="silveira" tilewidth="32" tileheight="32">
+  <image source="free_tileset_version_10.png" width="480" height="1216"/>
+ </tileset>
+ <layer name="BG" width="20" height="20">
+  <data encoding="base64" compression="zlib">
+   eJytkkkKwkAQRTtCdioIBtQsbVD33sJcxOEADitXDgdwWLlyOJ7BC/hBC5q2ulOJWTx+qK7+9alOGihVU0o1g/+JQPr1G0MnYApmRk8o8NGGkt8cugBLsBLkcJ2R3wa6BTuwN+aFhvryUR/5HaBHcAJnYR67bu7vCr2BO3gIs2mmtgZV9UvR/b3ABTxLQvpf0Xe98tFGxZ3T3hW3lyy4e3ne0eXb9byhZuaH1rn23M/aH5czYuqhJx/lt3PadapJcuR5/5aDNuh4zhMwZLxjB30wAD0PI8bX1y+FfBNhf1zS3CK8AbUKR7M=
+  </data>
+ </layer>
+ <layer name="FG" width="20" height="20">
+  <data encoding="base64" compression="zlib">
+   eJxjYBgFgxEkD7QDRgFdAD8jA4MAI/XMkwOaJY/HPEGgnBCa/CMgfgzET4gwXxVNrwKQr4gm9hGIPwHxZyLMIwTUQGYDMSMQM1EhnMQZEebuodw4ioEMEX4iRg21gSETA4MREBsDsQkQmwKxGRCbA7EFEFsCsRUQAwAdtgpe
+  </data>
+ </layer>
+</map>