[Help] HardonCollider + Advanced Tiled Loader

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
theobjpo
Prole
Posts: 15
Joined: Sat Sep 08, 2012 4:42 am

[Help] HardonCollider + Advanced Tiled Loader

Post by theobjpo »

They aren't working together? I looked at the other problem but it's not close to my problem.
Any help?

Code: Select all

maps = {}
local tx = 0
local ty = 0
local scale = 1
local loader = require "AdvTiledLoader.Loader"
loader.path = "Maps/"
solid = {}
local i = 0
local map = loader.load("btest.tmx")
function maps.load()
	collider = HC(150, on_collide)
	tileLayer = map.layers["Ground"]
	love.graphics.setBackgroundColor(255,255,255)
	for x, y, tile in map("Ground"):iterate() do
		if tile and tile.properties.solid then
			ctile = collider:addRectangle(x, y, 32, 32)
			collider:addToGroup("tiles", ctile)
			collider:setPassive(ctile)
			solid[#solid+1] = {x=x, y=y, tile=tile}
			i = i + 1
		end
	end
end
function maps.update(dt)
	player = collider:addRectangle(x, y, 32, 32)
	collider:update(dt)
end
function on_collide(dt, shape_a, shape_b)
	x = 250
end
function maps.draw()
	ftx, fty = math.floor(tx), math.floor(ty)
	love.graphics.push()
	love.graphics.scale(scale)
	love.graphics.translate(ftx, fty)
	map:autoDrawRange(tx, ty, scale, 50) 
	map:draw()
	--map:setDrawRange(0, 0, love.graphics.getWidth(), love.graphics.getHeight())
	love.graphics.pop()
end
Map Code.

Code: Select all

game = {}
local socket = require "socket"
local entity
local t
local updaterate = 0.1
x, y = 420, 240
local cobble = {}
function game.load()
	udp = socket.udp()
	udp:settimeout(0)
	udp:setpeername(address, port)
	entity = nickname
	local dg = string.format("%s %s $", entity, 'joined')
	udp:send(dg)
	local dg = string.format("%s %s %d %d", entity, 'at', 420, 240)
	udp:send(dg)
	t = 0
	love.graphics.setColor(255,255,255)
end
function on_collide(dt, shape_a, shape_b, mtv_x, mtv_y)
	print("COLLIDED")
end
function game.update(dt)
	collider = HC(100, on_collide)
	t = t + dt
		if love.keyboard.isDown('w') then y = y - ( 20 * dt ) end
		if love.keyboard.isDown('s') then y = y + ( 20 * dt ) end
		if love.keyboard.isDown('a') then x = x - ( 20 * dt ) end
		if love.keyboard.isDown('d') then x = x + ( 20 * dt ) end
			if love.keyboard.isDown('rctrl') then
				for k, v in pairs(solid) do
					print(k, v.x, v.y, v.tile)
				end
			end
	if t > updaterate then
		local dg = string.format("%s %s %f %f", entity, 'at', x, y)
		udp:send(dg)
		local dg = string.format("%s %s $", entity, 'update')
		udp:send(dg)
		t = t - updaterate
	end
	
	repeat
		data, msg = udp:receive()
		if data then
			ent, cmd, parms = data:match("^(%S*) (%S*) (.*)")
			if cmd == 'at' then
				local x, y = parms:match("^(%-?[%d.e]*) (%-?[%d.e]*)$")
				assert(x and y)
				x, y = tonumber(x), tonumber(y)
				world[ent] = {x=x, y=y}
			else
				print("Unknown Command: ", cmd)
			end
		elseif msg ~= 'timeout' then
			alert.load()
			gamestate = "alert"
			alt = { msg = "Lost Connection", x = 1, y = 264, font = "Fonts/clubland.ttf", size = 72 }
		end
	until not data
end
function game.quit()
	local dg = string.format("%s %s $", entity, 'left')
	udp:send(dg)
end
function game.draw()
	love.graphics.setColor(0,0,0)
	for k,v in pairs(world) do
		love.graphics.setColor(0,0,0)
		love.graphics.rectangle("fill", v.x, v.y, 32, 32)
	end
end
Game Code.
theobjpo
Prole
Posts: 15
Joined: Sat Sep 08, 2012 4:42 am

Re: [Help] HardonCollider + Advanced Tiled Loader

Post by theobjpo »

I got it to work but all of them are now solid?
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: [Help] HardonCollider + Advanced Tiled Loader

Post by Kadoba »

It's hard to tell without a .love
theobjpo
Prole
Posts: 15
Joined: Sat Sep 08, 2012 4:42 am

Re: [Help] HardonCollider + Advanced Tiled Loader

Post by theobjpo »

Heres .love
Attachments
Game.love
(693.9 KiB) Downloaded 193 times
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: [Help] HardonCollider + Advanced Tiled Loader

Post by coffee »

theobjpo wrote:Heres .love
You ask for cursor.png but file is named Cursor.png. OSX is sensible to that and fail.

Also stop loading same files each click/release. I had performance glitches that I believe from that. Or else you have another code problems there. Instead of:

Code: Select all

function love.mousereleased( x, y, button )
	cursor = love.graphics.newImage("Images/cursor.png")

Code: Select all

function love.mousepressed( mx, my, button )
	cursor = love.graphics.newImage("Images/cursor_click.png")
do in love.load

Code: Select all

	cursor_click = love.graphics.newImage("Images/cursor_click.png")
	cursor_regular = love.graphics.newImage("Images/cursor.png")
	cursor = cursor_regular
and

Code: Select all

function love.mousereleased( x, y, button )
	cursor = cursor_regular

Code: Select all

function love.mousepressed( mx, my, button )
	cursor = cursor_click
Always load all images in love.load and switch them assigning the handler ("cursor_regular", cursor_normal") to main var ("cursor") whenever you need. Don't load images inside main cycles or will slow down or even crash. If you had to click-fire imagine how bad is load same image on and on...

I don't have knowledge of tiled maps format because I prefer to use custom map formats but I noticed this

Code: Select all

   <property name="solid" value="1"/>
in test.tmx

Code: Select all

	for tilex, tiley, tile in map.layers["Solid"]:iterate() do
in your map routine
Don't know how much is ATL sensible but you could start for align uppercase/lowercase for avoid problems
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: [Help] HardonCollider + Advanced Tiled Loader

Post by Kadoba »

You seem to have a bad habit of creating resources more than once. In your menu draw code you create a new font every draw update. That's what was slowing down the menu. You only need to create fonts once. Similarly you were creating a new rectangle for the player every update in the map update function. This is why your map is so slow and you kept getting collisions. The created rectangles would collide with one another.

Code: Select all

-- Menu draw code

-- Old code
function menu.draw()
	--[[ IP SECTION ]]--
	love.graphics.setColor(0,0,0)
	love.graphics.rectangle("fill", 190, 190, 420, 68)
	love.graphics.setColor(255,255,255)
	love.graphics.rectangle("fill", 200, 200, 400,48)
	love.graphics.setColor(0,0,0)
	love.graphics.print(ip, 218, 212)
	--[[ END IP SEC ]]--
	font = love.graphics.newFont("Fonts/Clubland.ttf", 18)
	love.graphics.setFont(font)
	love.graphics.print(nickname, 5, 0)
	for k,v in pairs(buttons) do
		if v.hover then
			love.graphics.setColor(0, 255, 0)
			love.graphics.print( v.text, v.x, v.y )
		else
			love.graphics.setColor(0, 0, 0)
			love.graphics.print( v.text, v.x, v.y )
		end
	end
end

-- New code
local font = love.graphics.newFont("Fonts/Clubland.ttf", 18)
function menu.draw()
	--[[ IP SECTION ]]--
	love.graphics.setColor(0,0,0)
	love.graphics.rectangle("fill", 190, 190, 420, 68)
	love.graphics.setColor(255,255,255)
	love.graphics.rectangle("fill", 200, 200, 400,48)
	love.graphics.setColor(0,0,0)
	love.graphics.print(ip, 218, 212)
	--[[ END IP SEC ]]--
	love.graphics.setFont(font)
	love.graphics.print(nickname, 5, 0)
	for k,v in pairs(buttons) do
		if v.hover then
			love.graphics.setColor(0, 255, 0)
			love.graphics.print( v.text, v.x, v.y )
		else
			love.graphics.setColor(0, 0, 0)
			love.graphics.print( v.text, v.x, v.y )
		end
	end
end


-- Map Code

--Old Code
function maps.load()
	collider = HC(100, collide)
	tileLayer = map.layers["Ground"]
	love.graphics.setBackgroundColor(255,255,255)
	for tilex, tiley, tile in map.layers["Solid"]:iterate() do
		if tile.properties.solid == 1 then
			-- Create a quad system that uses only the specific tiles.
			ctile = collider:addRectangle(tilex*32, tiley*32, 32, 32) -- 32, 32 square in all listed for SOLID
			collider:addToGroup("tiles", ctile) -- Grouping.. No real point
			collider:setPassive(ctile) -- Passive, so it's a solid object.
			solid_table[#solid_table+1] = {x=tilex, y=tiley, tile=tile}
			i = i + 1
		end
	end
end
function maps.update(dt)
	player = collider:addRectangle(char_x, char_y, 32, 32)
	collider:update(dt)
end

-- New Code
function maps.load()
	collider = HC(100, collide)
	tileLayer = map.layers["Ground"]
	love.graphics.setBackgroundColor(255,255,255)
	for tilex, tiley, tile in map.layers["Solid"]:iterate() do
		if tile.properties.solid == 1 then
			-- Create a quad system that uses only the specific tiles.
			ctile = collider:addRectangle(tilex*32, tiley*32, 32, 32) -- 32, 32 square in all listed for SOLID
			collider:addToGroup("tiles", ctile) -- Grouping.. No real point
			collider:setPassive(ctile) -- Passive, so it's a solid object.
			solid_table[#solid_table+1] = {x=tilex, y=tiley, tile=tile}
			i = i + 1
		end
	end
	player = collider:addRectangle(char_x, char_y, 32, 32)
end
function maps.update(dt)
    player:moveTo(char_x, char_y)
    collider:update(dt)
end


theobjpo
Prole
Posts: 15
Joined: Sat Sep 08, 2012 4:42 am

Re: [Help] HardonCollider + Advanced Tiled Loader

Post by theobjpo »

Thankyou very much, slight problem is that they aren't just 32x32 squares they're 48x48?
Anyways, I fixed most of my code that I didn't need.
I was unfamiliar with the HardonCollider and I didn't know it had moveTo I thought it would just reassign by default.. Thanks again!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests