Referring to individual sprites of the same time

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
philnelson
Prole
Posts: 48
Joined: Sun Feb 01, 2009 3:32 am

Referring to individual sprites of the same time

Post 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.
User avatar
Tabasco
Citizen
Posts: 72
Joined: Tue Dec 02, 2008 5:04 pm

Re: Referring to individual sprites of the same time

Post 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
User avatar
Skofo
Party member
Posts: 146
Joined: Mon Dec 22, 2008 10:55 pm

Re: Referring to individual sprites of the same time

Post 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.
Last edited by Skofo on Sun Feb 08, 2009 12:48 am, edited 1 time in total.
Working on: Viator
Need a 64-bit Debian package for LÖVE? Here it is!
philnelson
Prole
Posts: 48
Joined: Sun Feb 01, 2009 3:32 am

Re: Referring to individual sprites of the same time

Post 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.
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

Re: Referring to individual sprites of the same time

Post by osgeld »

yes everything but function load() loop
philnelson
Prole
Posts: 48
Joined: Sun Feb 01, 2009 3:32 am

Re: Referring to individual sprites of the same time

Post by philnelson »

Thanks very much, everyone. This helped me quite a bit. I'll share actual code once I get something working.
User avatar
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

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 20 guests