Referring to individual sprites of the same time
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 48
- Joined: Sun Feb 01, 2009 3:32 am
Referring to individual sprites of the same time
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
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
http://en.wikipedia.org/wiki/Bresenham% ... _algorithm
Re: Referring to individual sprites of the same time
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:
If you want to check if there are any walls between and stuff, it might be a little bit more complex.
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
Last edited by Skofo on Sun Feb 08, 2009 12:48 am, edited 1 time in total.
-
- Prole
- Posts: 48
- Joined: Sun Feb 01, 2009 3:32 am
Re: Referring to individual sprites of the same time
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:
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.
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
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
yes everything but function load() loop
-
- Prole
- Posts: 48
- Joined: Sun Feb 01, 2009 3:32 am
Re: Referring to individual sprites of the same time
Thanks very much, everyone. This helped me quite a bit. I'll share actual code once I get something working.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Referring to individual sprites of the same time
To clarify this, every time update is called, draw should be called too.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.
Who is online
Users browsing this forum: Ahrefs [Bot] and 2 guests