How to check if i need to generate more land?

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
Jhawsh
Prole
Posts: 4
Joined: Mon Oct 15, 2012 1:11 pm
Contact:

How to check if i need to generate more land?

Post by Jhawsh »

Okay heres the deal, trying to create a simple parallax game.

Currently i have it if u click left it goes left and when your in 300px of the edge of the box its moves the background instead of the player. How can i detect if i need to generate more land?

https://love2d.org/imgmirrur/Ds8q2yR.gif

player.lua

Code: Select all

function InitPlayer()
	player = love.graphics.newImage("images/player.png")
	playerwidth = 32
	playerheight = 32
	playerx = 500
	playery = 359
end

function UpdatePlayer(dt, map_w)
	if love.keyboard.isDown("left") then
		if playerx+playerwidth <= 300 then
			for i = 1, map_w/64 do
				GroundMap[i] = GroundMap[i] + 75 * dt
			end
				for i = 1, #TreeMap do
					TreeMap[i][2] = TreeMap[i][2] + 75 * dt
				end
			else
				player = love.graphics.newImage("images/playerl.png")
				playerx = playerx - 75 * dt
		end
	end 
	if love.keyboard.isDown("right") then
		if playerx+playerwidth >= map_w-300 then
			for i = 1, map_w/64 do
				GroundMap[i] = GroundMap[i] - 75 * dt
			end
			for i = 1, #TreeMap do
				TreeMap[i][2] = TreeMap[i][2] - 75 * dt
			end
		else
			player = love.graphics.newImage("images/playerr.png")
			playerx = playerx + 75 * dt
		end
	end
	if love.keyboard.isDown("down") then
		player = love.graphics.newImage("images/player.png")
	end
end

function DrawPlayer()
	love.graphics.draw(player, playerx, playery)
end
ground.lua

Code: Select all

function InitGround(map_w)
	grass = love.graphics.newImage("images/grass.png")
	GroundMap = {}
	for i = 1, map_w/64 do
		GroundMap[i] = i*64-64
	end
end

function UpdateGround()

end

function DrawGround(map_w)
	for x = 1, map_w/64 do
		love.graphics.draw(grass, GroundMap[x], 388)
	end
end
Attachments
Game.zip
(24.91 KiB) Downloaded 97 times
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: How to check if i need to generate more land?

Post by davisdude »

Here's what I would do:

Code: Select all

function UpdateGround( x, y, w, h ) -- x and y are the lower left corner of the view-able screen. w is the width of the screen, h is the height. 
     if not GroundMap[math.floor( x / 64 )] then
          GroudnMap[math.floor( x / 64 )] = math.floor( x / 64 ) * 64 - 64
     elseif not GroundMap[math.ceil( x / 64 )] then
          GroudnMap[math.ceil( x / 64 )] = math.ceil( x / 64 ) * 64 - 64
     end
     if not GroundMap[math.floor( ( x + w ) / 64 )] then
          GroudnMap[math.floor( ( x + w ) / 64 )] = math.floor( ( x + w ) / 64 ) * 64 - 64
     elseif not GroundMap[math.ceil( ( x + w )/ 64 )] then
          GroudnMap[math.ceil( ( x + w ) / 64 )] = math.ceil( ( x + w ) / 64 ) * 64 - 64
     end
end
By the way, I would recommend not having the variables so hard-coded. That's just me, though. :)
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Jhawsh
Prole
Posts: 4
Joined: Mon Oct 15, 2012 1:11 pm
Contact:

Re: How to check if i need to generate more land?

Post by Jhawsh »

davisdude wrote:Here's what I would do:

Code: Select all

function UpdateGround( x, y, w, h ) -- x and y are the lower left corner of the view-able screen. w is the width of the screen, h is the height. 
     if not GroundMap[math.floor( x / 64 )] then
          GroudnMap[math.floor( x / 64 )] = math.floor( x / 64 ) * 64 - 64
     elseif not GroundMap[math.ceil( x / 64 )] then
          GroudnMap[math.ceil( x / 64 )] = math.ceil( x / 64 ) * 64 - 64
     end
     if not GroundMap[math.floor( ( x + w ) / 64 )] then
          GroudnMap[math.floor( ( x + w ) / 64 )] = math.floor( ( x + w ) / 64 ) * 64 - 64
     elseif not GroundMap[math.ceil( ( x + w )/ 64 )] then
          GroudnMap[math.ceil( ( x + w ) / 64 )] = math.ceil( ( x + w ) / 64 ) * 64 - 64
     end
end
By the way, I would recommend not having the variables so hard-coded. That's just me, though. :)
Cant seem to get this working - UpdateGround(0, 388, map_h, map_w)

Not too sure why
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: How to check if i need to generate more land?

Post by davisdude »

You would need to use player.x - half the screen width, player.y - half the screen height. By the way, I can't test it since you're still using 0.8.0. :P
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

Users browsing this forum: No registered users and 87 guests