Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Method: Game Object Tables ]]
- -- go through the "enemies" table
- -- backwards, so we can efficiently remove objects, and there will be no skips
- for enum = #enemies, 1, -1 do
- local enemy = enemies[enum]
- -- all of your enemy creation/moving code
- -- go through whatever table holds the bullets
- for bnum = #bullets, 1, -1 do
- local bullet = bullets[bnum]
- -- check if they are touching
- -- for this, I assume that everything takes up only one character
- -- use whatever method fits you, however
- if bullet.x == enemy.x
- and bullet.y == enemy.y then
- -- destroy both the enemy and the bullet
- -- by removing them from their respective tables
- table.remove(enemies, enum)
- table.remove(bullets, bnum)
- break
- -- alternatively, you could use a health system for enemies.
- --[[
- if enemy.health > 0 then
- enemy.health = enemy.health - 1
- else
- -- the removing code above
- end
- --]]
- end
- end
- end
- --[[ Method: Game Object Grid ]]
- -- go through all of the grid squares
- for y=1, #grid do
- for x=1, #grid[y] do
- -- we assume that each grid space has a "type" property that tells us what's there
- if grid[y][x].type == 'enemy' then
- -- for this, we assume that the player is shooting bullets from the bottom
- -- therefore, any bullets will be below us
- -- so we check that space, which is at x, y + 1
- if grid[y + 1] and grid[y + 1][x] and grid[y + 1][x].type == 'bullet' then
- -- then, erase both the enemy and the bullet from the grid if they are both in the right position
- grid[y][x] = nil
- grid[y + 1][x] = nil
- break
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment