Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

slowly genericising bullets...

  • Loading branch information...
commit 12022a819e364ea235b5871ca3e9ec03d843101f 1 parent b130bb8
@Textmode Textmode authored
Showing with 25 additions and 26 deletions.
  1. +6 −2 ship.lua
  2. +19 −24 weapon.lua
View
8 ship.lua
@@ -72,7 +72,7 @@ function ship:update(dt, level)
end
-- Update entities
for i, entity in next, self.entities do
- if entity.type == "bullet" then
+ if entity.kind == "bullet" then
-- Move bullet bullet
entity.pos_x = entity.pos_x + entity.speed * dt
-- Remove off-screen bullets
@@ -159,8 +159,12 @@ function ship:die()
end
end
+function ship:addentity(e)
+ self.entities[#self.entities+1] = e
+end
+
function ship:shoot()
- self.entities[#self.entities+1] = Bullet.new(self)
+ self:addentity(Bullet.new(self))
end
function ship:collidewith(e, dt)
View
43 weapon.lua
@@ -1,9 +1,10 @@
-Bullet = {
- speed = 200;
- radius = 1;
- damage = 10;
-}
-local MT = {__index = Bullet}
+local entity = require "entity"
+
+local _M = {}
+local _MT = {__index = _M}
+local _MMT = {__index = entity}
+setmetatable(_M, _MMT)
+Bullet = _M
-- graphics
local GFX_Bullet = love.graphics.newImage("gfx/Bullet.png")
@@ -16,29 +17,23 @@ end
--- Create new bullet
--@param `t` should always be the ship firing the bullet
-function Bullet.new(t)
- local e = {}
+function Bullet.new(owner, t)
t = t or {}
+ local e = entity.new()
- e.type = "bullet"
- e.pos_x = t.pos_x
- e.pos_y = t.pos_y
+ e.type = 'projectile'
+ e.kind = 'bullet'
+
+ e.pos_x = t.pos_x or owner.pos_x or 0
+ e.pos_y = t.pos_y or owner.pos_y or 0
+ e.speed = t.speed or 200;
+ e.radius = t.radius or 1;
set_texture(e, GFX_Bullet)
- return setmetatable(e, MT)
-end
-
--- returns the distance between this entity, and the given entity.
-function Bullet:distance(e)
- local a = math.abs(self.pos_x - e.pos_x)^2
- local b = math.abs(self.pos_y - e.pos_y)^2
- return math.sqrt(a+b)
-end
+ e.damage = t.damage or 10;
--- tests if this entity, and the given entity, have collided.
-function Bullet:testcollision(e)
- return self:distance(e) <= self.radius+e.radius
+ return setmetatable(e, _MT)
end
-return Bullet
+return _M
Please sign in to comment.
Something went wrong with that request. Please try again.