Tile based RPG

Show off your games, demos and other (playable) creations.
Post Reply
User avatar
zugamifk
Prole
Posts: 28
Joined: Sun Jul 26, 2009 4:01 pm

Tile based RPG

Post by zugamifk »

Hello all, I've been working on a tile based RPG for the past couple weeks, trying out LOVE2D and learning Lua. I must say, I love this engine. It runs very smoothly and Lua is a wonderful break from C, which is what I'm used to.

Anyway, I've reached a point I can't quite pass. How do I implement collisions in a smooth scrolling tile based RPG? I've tried marking some tiles as "uncrossable", but because the game moves the map rather than the player it doesn't seem to work. Has anyone worked on something similar to this before? Are there any alternatives?

Here's my game if anyone's interested in seeing it: http://www.mediafire.com/download.php?3njjgmtvioz
Neolitik
Citizen
Posts: 55
Joined: Sun Jun 28, 2009 3:13 pm

Re: Tile based RPG

Post by Neolitik »

hello

You can use Rectangle Colision System :

Code of the Wiki ; By athanazio 04:58, 11 April 2009 (UTC)

function colide(x1,y1,width1,height1,x2,y2,width2,height2)
return not (
(x2 > (x1 + width1 )) or
(x1 > (x2 + width2 )) or
(y2 > (y1 + height1)) or
(y1 > (y2 + height2)) )
end

i'm use it in the : Jump 'n Run Test tread .
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Tile based RPG

Post by Jasoco »

Not bad. It's nice how fast LÖVE runs. No slowdown at all.

What do you plan on doing with it? Is it really an "RPG" though? Or will it eventually be?

I'm working on an engine for a Zelda/Mana/Evermore/Densetsu style adventure game myself. So far I have working enemies, projectiles and weapons for the player/enemies such as a melee weapon, arrows, bombs and a boomerang that works exactly like in Zelda. (Where it flies out, and homes back into the player when it rebounds) Next up I'm gonna work on a hookshot. Once I get a scripting system in place I'm gonna post a thread for it.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Tile based RPG

Post by Jasoco »

To answer your question on collisions, here's how I do it.

My game uses a grid based two-dimensional array for the tile data.

Code: Select all

function overlap(x1,y1,w1,h1,x2,y2,w2,h2)
	if (x2 >= x1 and x2 <= (x1 + w1)) and (y2 >= y1 and y2 <= (y1 + h1)) then tl = true else tl = false end
	if (x2+w2 >= x1 and x2+w2 <= (x1 + w1)) and (y2 >= y1 and y2 <= (y1 + h1)) then tr = true else tr = false end
	if (x2 >= x1 and x2 <= (x1 + w1)) and (y2+h2 >= y1 and y2+h2 <= (y1 + h1)) then bl = true else bl = false end
	if (x2+w2 >= x1 and x2+w2 <= (x1 + w1)) and (y2+h2 >= y1 and y2+h2 <= (y1 + h1)) then br = true else br = false end
	
	if tl and tr and bl and br then return 2 elseif tl or tr or bl or br then return true else return false end
end

function checkCollision(x,y,w)
	cpx, cpy = math.floor(x / 32), math.floor(y / 32)
	if pushTime > 0 then pushingX, pushingY = cpx, cpy else pushingX, pushingY = -1, -1 end
	if mapHit[cpx][cpy] == "x" or (mapHit[cpx][cpy] == "w" and w ~= 2) then
		return true
	else
		return false
	end
end
The overlap function is used for checking if one shape is inside another. (Rectangles only right now) Pass the x, y, width and height of the first box then the x, y, width and height of the second, in my case, the x and y of the player or enemy or projectile and their width and heights and check it against the x, y, width and height of another enemy, projectile or the player if it happens to belong to an enemy.

And the checkCollision function is used for the rest. This checks the position of the player against the tile hit map of the level/map. I also use various collision types. Like impassible by anything, or it might be water or a bush or a waist high wall. In my example, an x means impassible for anything and w means it is water or low walls so projectiles can still go through (over) it but characters cannot walk through. Basically you'd pass a 2 if the thing you are checking it for is a projectile and anything else (Like a 1 for an enemy or a 0 for a player) 32 is the width and height of each tile on my grid.

Then when moving an object such as the player:

Code: Select all

kLeft = love.keyboard.isDown(love.key_left)
kRight = love.keyboard.isDown(love.key_right)
kUp = love.keyboard.isDown(love.key_up)
kDown = love.keyboard.isDown(love.key_down)

if kLeft and kRight == false then 
	tpx = playerX - playerSpeed * dt
	if checkCollision(tpx, playerY+16) == false and checkCollision(tpx, playerY + 31) == false then
		playerX = tpx
	else
		playerX = (math.floor(tpx / 32)+1) * 32
	end
	playerFacing = 1
end
if kRight and kLeft == false then 
	tpx = playerX + playerSpeed * dt
	if checkCollision(tpx+32, playerY+16) == false and checkCollision(tpx+32, playerY + 31) == false then
		playerX = tpx
	else
		playerX = (math.floor(tpx / 32)) * 32
	end
	playerFacing = 3
end
if kUp and kDown == false then
	tpy = playerY - playerSpeed * dt
	if checkCollision(playerX+4, tpy+16) == false and checkCollision((playerX+24), tpy+16) == false then
		playerY = tpy
	else
		playerY = ((math.floor(tpy / 32)+1) * 32) - 16
	end
	playerFacing = 2
end
if kDown and kUp == false then
	tpy = playerY + playerSpeed * dt
	if checkCollision(playerX + 4, tpy + 32) == false and checkCollision(playerX + 24, tpy + 32) == false then
		playerY = tpy
	else
		playerY = (math.floor(tpy / 32)) * 32
	end
	playerFacing = 4
end
This is just how I do it of course. Everyone will do it differently. See, the tpx or tpy variables are calculated first, then a collision is checked against the new variable and if it isn't collided then the player/enemy's X or Y location is updated. I also put the arrow key status into a variable ahead of time to save code and typing for later checks. But that's only because of the complexity of my game.

I would't use this code outright, rather study it and see how it works, then implement your own method based on how your games variables work in engine.
User avatar
zugamifk
Prole
Posts: 28
Joined: Sun Jul 26, 2009 4:01 pm

Re: Tile based RPG

Post by zugamifk »

Thanks for the replies, it seems the rectangle collision sort of thing works perfectly with what I was trying to do.

Jasoco: for now it's really just an experiment. I'm trying to design a huge open world with as little required map design as possible. Essentially, it will precedurally generated except for the actual geology of the land and some of the larger cities. This means I can focus most of my map designing on places people will actually spend time and let the wilderness feel more random and dynamic.

So for the time being, all it really is is a field with a couple of plants in it. You can plant seeds, and when I have most of the plant types in I'll probably make it a little farming game at first to see how it works before expanding the world and adding cities and such. I called it an 'RPG", but I know there's debate around the meaning of that term. It's not going to be Zelda, but I'm not adding things like characters stats or leveling either.

I guess we'll see how it turns out. I'm enjoying this engine, the fact that it runs so smoothly is something that's probably going to be a very important to my game.
Post Reply

Who is online

Users browsing this forum: No registered users and 59 guests