Difference between revisions of "Love - Using a TMX Map"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(GitHub Repository)
(GitHub Repository)
Line 1: Line 1:
=GitHub Repository=
+
=Advanced Tiled Loader=
Have a look at his GitHub repository: [https://github.com/TRCCompSci/Advanced-Tiled-Loader Advanced-Tiled-Loader]
+
Have a look at his GitHub repository:  
 +
 
 +
[https://github.com/TRCCompSci/Advanced-Tiled-Loader Advanced-Tiled-Loader]
  
 
It was initially an abandoned project, but with a little bit of work is fully working again. It will be suitable for students creating an RPG style game.
 
It was initially an abandoned project, but with a little bit of work is fully working again. It will be suitable for students creating an RPG style game.
  
It will also work with platform games, but their are other options. For example the STI project: [https://github.com/karai17/Simple-Tiled-Implementation STI]
+
You can download the zip file from this link: [https://github.com/TRCCompSci/Advanced-Tiled-Loader/archive/master.zip download]
 +
 
 +
Extract the zip and copy the 'Advanced-Tiled-Loader' folder into your LOVE project folder (same directory as the 'main.lua').
 +
 
 +
In your TMX map, create an object on an object layer called 'Player'.
 +
 
 +
Copy this code for the 'main.lua'
 +
 
 +
<syntaxhighlight lang=lua>
 +
local player = nil
 +
function love.load()
 +
 +
  local loader = require("Advanced-Tiled-Loader.Loader")
 +
  loader.path = "Maps/"  --Change this to wherever your .tmx files are
 +
  map = loader.load("untitled.tmx") --Change this to the name of your mapfile
 +
  tx = 0
 +
  ty = 0
 +
  scale = 2 -- Adjust zoom with this
 +
 
 +
  image=love.graphics.newImage("Maps/test.png")
 +
  for _, layer in pairs(map.layers) do
 +
  if layer.class == "ObjectLayer" then
 +
for _, obj in pairs(layer.objects) do
 +
if obj.name == "Player" then
 +
print(obj.name)
 +
obj.texture = image
 +
map.camera.Object=obj
 +
map.camera:update()
 +
player=obj
 +
end
 +
if obj.type == "Test" then
 +
print(obj.name)
 +
obj.texture = image
 +
end
 +
end
 +
  end
 +
  end
 +
end
 +
 
 +
function love.draw()
 +
  local ftx, fty = math.floor(tx), math.floor(ty)
 +
  love.graphics.push()
 +
  love.graphics.scale(scale)
 +
  love.graphics.translate(ftx, fty)
 +
  map:draw()
 +
  love.graphics.pop()
 +
end
 +
 
 +
function love.update(dt)
 +
local difx = 0
 +
local dify = 0
 +
change = false
 +
 +
if love.keyboard.isDown("up") then
 +
dify=100*dt
 +
change = true
 +
end
 +
if love.keyboard.isDown("down") then
 +
dify = -100*dt
 +
change = true
 +
end
 +
if love.keyboard.isDown("left") then
 +
difx= 100*dt
 +
change = true
 +
end
 +
if love.keyboard.isDown("right") then
 +
difx= -100*dt
 +
change = true
 +
end
 +
 +
player.x = player.x-difx
 +
player.y = player.y-dify
 +
 +
if change==true then
 +
map:forceRedraw()
 +
end
 +
 
 +
end
 +
</syntaxhighlight>
 +
 
 +
 
 +
=Other Options
 +
For example the STI project: [https://github.com/karai17/Simple-Tiled-Implementation STI]

Revision as of 14:34, 18 December 2020

Advanced Tiled Loader

Have a look at his GitHub repository:

Advanced-Tiled-Loader

It was initially an abandoned project, but with a little bit of work is fully working again. It will be suitable for students creating an RPG style game.

You can download the zip file from this link: download

Extract the zip and copy the 'Advanced-Tiled-Loader' folder into your LOVE project folder (same directory as the 'main.lua').

In your TMX map, create an object on an object layer called 'Player'.

Copy this code for the 'main.lua'

local player = nil
function love.load()
 
  local loader = require("Advanced-Tiled-Loader.Loader")
  loader.path = "Maps/"  --Change this to wherever your .tmx files are
  map = loader.load("untitled.tmx") --Change this to the name of your mapfile
  tx = 0
  ty = 0
  scale = 2 -- Adjust zoom with this
  
  image=love.graphics.newImage("Maps/test.png")
  for _, layer in pairs(map.layers) do
   if layer.class == "ObjectLayer" then
		for _, obj in pairs(layer.objects) do
			if obj.name == "Player" then 
				print(obj.name)
				obj.texture = image
				map.camera.Object=obj
				map.camera:update()
				player=obj
			end
			if obj.type == "Test" then 
				print(obj.name)
				obj.texture = image
			end
		end
   end
  end
end

function love.draw()
  local ftx, fty = math.floor(tx), math.floor(ty)
  love.graphics.push()
  love.graphics.scale(scale)
  love.graphics.translate(ftx, fty)
  map:draw()
  love.graphics.pop()
end

function love.update(dt)
	local difx = 0
	local dify = 0
	change = false
	
	if love.keyboard.isDown("up") then 
		dify=100*dt 
		change = true
		end
	if love.keyboard.isDown("down") then 
		dify = -100*dt
		change = true		
		end
	if love.keyboard.isDown("left") then 
		difx= 100*dt 
		change = true
		end
	if love.keyboard.isDown("right") then 
		difx= -100*dt 
		change = true		
		end
	
	player.x = player.x-difx
	player.y = player.y-dify
	
	if change==true then
		map:forceRedraw()
		end

end


=Other Options For example the STI project: STI