Page 1 of 1
Referring to individual sprites of the same time
Posted: Sat Feb 07, 2009 10:42 pm
by philnelson
I'm really banging my head on how to keeping items shown or hidden, like one might find in Nethack or the Mystery Dungeon series. Essentially: everything the player has actually been near is revealed, all else is hidden. I can't figure it out.
Re: Referring to individual sprites of the same time
Posted: Sat Feb 07, 2009 11:05 pm
by Tabasco
If it's something like nethack, you should probably have a tile class and it can have a member for visibility as well as a member for discovery. Setting discovery based on player proximity should be easy, but if you want line of sight based on light, you might be interested in this:
http://en.wikipedia.org/wiki/Bresenham% ... _algorithm
Re: Referring to individual sprites of the same time
Posted: Sat Feb 07, 2009 11:14 pm
by Skofo
Ooh, perhaps when you go through your array of items/sprites (when drawing them or whatever) you can check if they're in the range of your player, and if they are, then you set their "seen" variable or whatever to true, then you only draw the items when their "seen" variable is true.
For example:
Code: Select all
--- Untested code, might've missed some Lua-specific things, but the concept in general should work.
function draw()
for i = 1, table.maxn(items) do -- Go through your array of items (or monsters or whatever)
if math.abs((items[i].y - items[i].x)^2 + (player.y - player.x)^2) <= 32 then -- Use the distance formula to check if the item and player are less than or equal to 32 pixels away
items[i].seen = true -- If they are, set the item's "seen" variable to true
end
if items[i].seen == true then -- Check if the item has been seen
love.graphics.draw(items[i].sprite, items[i].x, items[i].y) -- If it has been, then draw it! =D
end
end
end
If you want to check if there are any walls between and stuff, it might be a little bit more complex.
Re: Referring to individual sprites of the same time
Posted: Sun Feb 08, 2009 12:04 am
by philnelson
Thanks for the ideas, guys, what I'm having the trouble with is changing that setting on the fly. How can something START as invisible and become visible? I generate the tiles like this:
Code: Select all
i = 0
for j=1, map_height, 1 do
for k=1, map_width, 1 do
i = i + 1
x = k
y = j
tiles[i] = {x=x,y=y,type='empty',seen='no'}
end
end
Obviously for walls the type = 'wall'
Does the draw() function re-run itself at an interval? Elsewise I can't see how the Skofo's code would be able to update newly viewed tiles.
Re: Referring to individual sprites of the same time
Posted: Sun Feb 08, 2009 12:08 am
by osgeld
yes everything but function load() loop
Re: Referring to individual sprites of the same time
Posted: Sun Feb 08, 2009 12:09 am
by philnelson
Thanks very much, everyone. This helped me quite a bit. I'll share actual code once I get something working.
Re: Referring to individual sprites of the same time
Posted: Sun Feb 08, 2009 8:54 am
by bartbes
philnelson wrote:Does the draw() function re-run itself at an interval? Elsewise I can't see how the Skofo's code would be able to update newly viewed tiles.
To clarify this, every time update is called, draw should be called too.