This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KingofGamesYami's profile picture

Hitboxes not functioning

Started by KingofGamesYami, 11 December 2014 - 11:44 PM
KingofGamesYami #1
Posted 12 December 2014 - 12:44 AM
It is somehow impossible to die in this hitbox testing thingy I did. I'm not sure how, or why it's not registering, but it's not.
Spoiler

local baddies = {}

local maxx, maxy = term.getSize()

for i = 1, maxx, 3 do
	baddies[ #baddies + 1 ] = {x = i, y = math.random( 1, maxy / 2 ), v = math.random( 1, 3 ) }
end

local player = { x = maxx/2, y = maxy - 3 }

function drawPlayer()
	term.setBackgroundColor( colors.green )
	term.setCursorPos( player.x, player.y )
	term.write( " " )
end

local id
while true do
	id = os.startTimer( 0.01 )
	term.setBackgroundColor( colors.black )
	term.clear()
	drawPlayer()
	term.setBackgroundColor( colors.red )
	for _, bad in ipairs( baddies ) do
		local doesMove = math.random( 0, bad.v ) ~= 0
		bad.y = bad.y + (doesMove and 1 or 0)
		if bad.y > maxy then
			bad.y = 1
			bad.x = bad.x + math.random( -1, 1 )
		end
		term.setCursorPos( bad.x, bad.y )
		term.write( " " )
		if bad.y == player.y and bad.x == player.x then
			term.setBackgroundColor( colors.red )
			term.clear()
			term.setTextColor( colors.black )
			term.setCursorPos( 1, 1 )
			print( "You lose!" )
			error()
		end
	end
	while true do
		local event = {os.pullEvent()}
		if event[ 1 ] == 'timer' and event[ 2 ] == id then
			break
		elseif event[ 1 ] == 'key' and event[ 2 ] == keys.right then
			player.x = player.x + 1
			if player.x > maxx then
				player.x = maxx
			end
		elseif event[ 1 ] == 'key' and event[ 2 ] == keys.left then
			player.x = player.x - 1
			if player.x < 1 then
				player.x = 1
			end
		end
	end
end
Agent Silence #2
Posted 12 December 2014 - 01:46 AM
Go to the sides
Bomb Bloke #3
Posted 12 December 2014 - 06:15 AM
local player = { x = math.floor(maxx/2), y = math.floor(maxy - 3) }
KingofGamesYami #4
Posted 12 December 2014 - 02:15 PM
Go to the sides

What?
local player = { x = math.floor(maxx/2), y = math.floor(maxy - 3) }
Yeah, that's a line of code… but I doubt it causes the problem.
AgentE382 #5
Posted 12 December 2014 - 09:23 PM
You'll want to check for collisions every time anything moves. I suggest moving that code into a function, then calling it after each enemy movement, and each player movement. Right now, your code only checks every .1 seconds.

EDIT:

Go to the sides

What?
local player = { x = math.floor(maxx/2), y = math.floor(maxy - 3) }
Yeah, that's a line of code… but I doubt it causes the problem.
LOL, this is actually why it's likely not working. Replace your line 9 with Bomb Bloke's version. One or both of the terminal sizes is likely not divisible by two, so you've got non-integer player coordinates. None of your enemies will be at the same coordinate as the player, ever.
Edited on 12 December 2014 - 08:24 PM
theoriginalbit #6
Posted 13 December 2014 - 07:01 AM
Bomb bloke is definitely correct, 51/2 is 25.5 which renders as 25, but will never line up with the enemies. as a side point, you should probably consider changing the structure of your code to be a little more logical, perhaps a basic game loop could assist you


while true do
  gatherInput()
  updateGame()
  renderGame()
end