Kingdaro

Enemy Destruction via Bullet in CC

Mar 30th, 2013
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.71 KB | None | 0 0
  1. --[[ Method: Game Object Tables ]]
  2. -- go through the "enemies" table
  3. -- backwards, so we can efficiently remove objects, and there will be no skips
  4. for enum = #enemies, 1, -1 do
  5.   local enemy = enemies[enum]
  6.  
  7.   -- all of your enemy creation/moving code
  8.  
  9.   -- go through whatever table holds the bullets
  10.   for bnum = #bullets, 1, -1 do
  11.     local bullet = bullets[bnum]
  12.  
  13.     -- check if they are touching
  14.     -- for this, I assume that everything takes up only one character
  15.     -- use whatever method fits you, however
  16.     if bullet.x == enemy.x
  17.     and bullet.y == enemy.y then
  18.       -- destroy both the enemy and the bullet
  19.       -- by removing them from their respective tables
  20.       table.remove(enemies, enum)
  21.       table.remove(bullets, bnum)
  22.       break
  23.  
  24.       -- alternatively, you could use a health system for enemies.
  25.       --[[
  26.       if enemy.health > 0 then
  27.         enemy.health = enemy.health - 1
  28.       else
  29.         -- the removing code above
  30.       end
  31.       --]]
  32.     end
  33.   end
  34. end
  35.  
  36. --[[ Method: Game Object Grid ]]
  37. -- go through all of the grid squares
  38. for y=1, #grid do
  39.   for x=1, #grid[y] do
  40.     -- we assume that each grid space has a "type" property that tells us what's there
  41.     if grid[y][x].type == 'enemy' then
  42.       -- for this, we assume that the player is shooting bullets from the bottom
  43.       -- therefore, any bullets will be below us
  44.       -- so we check that space, which is at x, y + 1
  45.       if grid[y + 1] and grid[y + 1][x] and grid[y + 1][x].type == 'bullet' then
  46.         -- then, erase both the enemy and the bullet from the grid if they are both in the right position
  47.         grid[y][x] = nil
  48.         grid[y + 1][x] = nil
  49.         break
  50.       end
  51.     end
  52.   end
  53. end
Advertisement
Add Comment
Please, Sign In to add comment