How to better deal with Block based games

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
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

How to better deal with Block based games

Post by NoreoAlles »

Hey, i have a game in wich i create a map with a forloop wich goes through a table like this one:

Code: Select all

  blocks = {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,1,1,1,1,1,0,0,0,0,
              0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,
              1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
              1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,
              1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
              1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
              1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
            }
So, the problem is that my player needs to be grounded and ungrounded with the
beginCon and endCon world callbacks, wich ends with the player being ungrounded if theyre between to blocks while still the ground.
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: How to better deal with Block based games

Post by milon »

What is the problem you're trying to solve? That's not clear to me so far.

I'm also not sure what you mean by grounded/ungrounded, and I'm not familiar with the callbacks you're referring to. Are you using a library, or are you referring to functions you've written yourself? Either way, please provide more detail.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: How to better deal with Block based games

Post by NoreoAlles »

milon wrote: Tue Apr 12, 2022 8:04 pm What is the problem you're trying to solve? That's not clear to me so far.

I'm also not sure what you mean by grounded/ungrounded, and I'm not familiar with the callbacks you're referring to. Are you using a library, or are you referring to functions you've written yourself? Either way, please provide more detail.
I am not using libraries, the callbacks are here.
The player is grounded if the beginCon callback collision normals on the y-Axsis are bigger then zero and the player is ungrounded when the same happens but in the endCon function. The problem that i have is, that when the player is inbetween two blocks and is more on one then the other, it gets ungrounded, while the other cube doesnt ground it.
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
togFox
Party member
Posts: 779
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: How to better deal with Block based games

Post by togFox »

I think we're talking about transitioning between discrete locations.

You're misusing the physics engine. You can't expect an object to travel on a platform, from block to block and between blocks, never leaving the platform, and expect Endcontact to fire.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
BrotSagtMist
Party member
Posts: 614
Joined: Fri Aug 06, 2021 10:30 pm

Re: How to better deal with Block based games

Post by BrotSagtMist »

If you change your block table into a table of tables you can skip all this loop stuff and directly use the map:

Code: Select all

blocks = {{0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
             {0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},--etc...
            }

if blocks[math.floor(Playerx/blocksize)][math.floor(Playery/blocksize)]==0 then
 move()
else 
 colide()
end
obey
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to better deal with Block based games

Post by darkfrei »

Code: Select all

map = {
	{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,1,1,1,1,1,0,0,0,0,},
	{0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,},
	{1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,},
	{1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,},
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
	{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,},
}

where every tile is

Code: Select all

map[y][x]
and the line is

Code: Select all

map[y]
Draw the map:

Code: Select all

tileSize = 32
for y, xs in ipairs (map) do
	for x, value in ipairs (xs) do
		if value == 1 then
			love.graphics.rectangle ('fill', (x-1)*tileSize, (y-1)*tileSize, tileSize, tileSize)
		end
	end
end
If the map has any gaps then use pairs instead of ipairs.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: How to better deal with Block based games

Post by NoreoAlles »

darkfrei wrote: Wed Apr 13, 2022 8:40 pm

Code: Select all

map = {
	{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,1,1,1,1,1,0,0,0,0,},
	{0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,},
	{1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,},
	{1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,},
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
	{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,},
}

where every tile is

Code: Select all

map[y][x]
and the line is

Code: Select all

map[y]
Draw the map:

Code: Select all

tileSize = 32
for y, xs in ipairs (map) do
	for x, value in ipairs (xs) do
		if value == 1 then
			love.graphics.rectangle ('fill', (x-1)*tileSize, (y-1)*tileSize, tileSize, tileSize)
		end
	end
end
If the map has any gaps then use pairs instead of ipairs.
Thanks, thats way cleaner.
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: How to better deal with Block based games

Post by milon »

You can also order things as map[x][y], which defines your map in terms of columns (map[x]) rather than rows. Use whatever works best for you.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to better deal with Block based games

Post by darkfrei »

milon wrote: Thu Apr 14, 2022 1:02 pm You can also order things as map[x][y], which defines your map in terms of columns (map[x]) rather than rows. Use whatever works best for you.
Both systems are pretty same except that the one of them is native to "notepad writing". But you must remember the the Y is before than X.
Yes, you can write/use some transpose function and use it as you want.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], pgimeno and 45 guests