Permalink
Browse files

misc. cleanup / extraction

  • Loading branch information...
1 parent 2750e9b commit 9cdb4b859a71ee3350630ad79415032ea904e3f1 @Textmode Textmode committed Aug 27, 2012
Showing with 110 additions and 49 deletions.
  1. +85 −0 entity.lua
  2. +20 −46 ship.lua
  3. +2 −2 space.lua
  4. +3 −1 weapon.lua
View
@@ -0,0 +1,85 @@
+-------------------------------------------------------------------------
+-- [entity.lua]
+-- entity
+-------------------------------------------------------------------------
+local _M = {_NAME = "entity", _TYPE = 'module'}
+local _MT = {__index = _M}
+-------------------------------------------------------------------------
+
+local count = 0
+
+
+-- Handles some texture properties
+local function set_texture(self, texture)
+ self.texture = texture
+ self.width = texture:getWidth()
+ self.height = texture:getHeight()
+end
+
+-------------------------------------------------------------------------
+
+function _M.new(t)
+ t = t or {}
+ local e = {}
+
+ e._TYPE = 'entity'
+ e.kind = t.kind or 'thing'
+ e.name = t.name or string.format('entity#%d', count)
+ e.id = count
+
+ e.pos_x = t.pos_x or 0
+ e.pos_y = t.pos_y or 0
+ e.dir_x = t.dir_x or 0
+ e.dir_y = t.dir_y or 0
+
+ e.speed = t.speed or 0
+ e.damage = t.damage or 0
+ e.radius = t.radius or 0
+
+
+ -- Handles the texture, width, and height fields
+ assert(t.texture, "No texture defined") -- needed for rendering
+ set_texture(e, t.texture)
+
+ count = count + 1
+
+ return setmetatable(e, _MT)
+end
+
+-------------------------------------------------------------------------
+
+function _M:think(dt)
+
+end
+
+-------------------------------------------------------------------------
+
+function _M:update(dt)
+ local dv = self.speed * dt
+ self.x = self.x + (self.dir_x * dv)
+ self.y = self.y + (self.dir_y * dv)
+
+ self:think(dt)
+end
+
+-- returns the distance between this entity, and the given entity.
+function _M: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
+
+-- tests if this entity, and the given entity, have collided.
+function _M:testcollision(e)
+ return self:distance(e) <= self.radius+e.radius
+end
+
+function _M:draw()
+ love.graphics.draw(self.texture, self.pos_x, self.pos_y)
+end
+
+-------------------------------------------------------------------------
+if _VERSION == "Lua 5.1" then _G[_M._NAME] = _M end
+
+return _M
+
View
@@ -1,51 +1,37 @@
-require "lib.gamestate"
-require "weapon"
+local Gamestate = require "lib.gamestate"
+local Bullet = require "weapon"
+local entity = require "entity"
-local ship = {}
-local _MT = {__index = ship }
+local _M = {} -- Module
+local _MT = {__index = _M } -- Metatable
+local _MMT = {__index = entity} -- Module Metatable
+local ship = setmetatable(_M, _MMT)
-- sound effects, all one of them
local SFX_Explosion = love.audio.newSource("sfx/Explosion.wav", "static")
--- Handles some texture properties
-local function set_texture(self, texture)
- self.texture = texture
- self.width = texture:getWidth()
- self.height = texture:getHeight()
-end
-
-
-- creates a new entity
function ship.new(t)
t = t or {}
- local e = {}
+ local e = entity.new(t)
- e._TYPE = 'ship'
- e.pos_x = t.pos_x or 0
- e.pos_y = t.pos_y or 0
- e.dir_x = t.dir_x or 0
- e.dir_y = t.dir_y or 0
+ e.kind = 'ship'
- e.name = t.name or 'ship'
+ e.name = t.name or string.format('ship#%d', e.id)
e.shieldmax = t.shieldmax or 100
- e.shield = t.shield or e.shieldmax
- e.speed = t.speed or 100
- e.damage = t.damage or e.shieldmax *.3
- e.state = t.state or 'alive'
- print(e.state)
- assert(type(e.state) == 'string', "er, didn't we just ste this?")
-
- -- Handles the texture, width, and height fields
- assert(t.texture, "No texture defined") -- needed for rendering
- set_texture(e, t.texture)
+ e.shield = t.shield or e.shieldmax
+ e.speed = t.speed or 100
+ e.damage = t.damage or e.shieldmax *.3
+ e.state = t.state or 'alive'
+
e.radius = t.radius or e.height
- e.npc = t.npc == nil and true or t.npc
+ e.npc = t.npc == nil and true or t.npc
e.dir_timer = t.dir_timer or 0
- e.entities = t.entities or {}
- e.shooting = false
- e.shot_rate = t.shot_rate or 0.2 -- time between shots in seconds.
+ e.entities = t.entities or {}
+ e.shooting = false
+ e.shot_rate = t.shot_rate or 0.2 -- time between shots in seconds.
e.shot_timer = 0
return setmetatable(e, _MT)
@@ -57,7 +43,7 @@ function ship:draw()
love.graphics.draw(entity.texture, entity.pos_x, entity.pos_y)
end
- love.graphics.draw(self.texture, self.pos_x, self.pos_y)
+ entity.draw(self)
end
-- updates the entity according the to time passed (in seconds)
@@ -132,18 +118,6 @@ local function round(val, decimal)
end
end
--- returns the distance between this entity, and the given entity.
-function ship: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
-
--- tests if this entity, and the given entity, have collided.
-function ship:testcollision(e)
- return self:distance(e) <= self.radius+e.radius
-end
-
-- handles various collision related events.
-- mostly simply handles self Vs. Level terrain collision atm.
function ship:docollision(level, dt) -- this is new
View
@@ -86,7 +86,7 @@ function state:enter()
self.level.entities = self.enemies
-- allows easy addition of entities to a level
self.level.addentity = function(self, ent)
- assert(ent and ent._TYPE == 'ship', "Can only add entities")
+ assert(ent and ent._TYPE == 'entity', "Can only add entities")
self.entities[#self.entities+1] = ent
end
@@ -113,7 +113,7 @@ end
-- Allows easy addition of Entities to the Level
function state:addentity(e)
- assert(e and e._TYPE == 'ship', "Can only add entities")
+ assert(e and e._TYPE == 'entity', "Can only add entities")
self.enemies[#self.enemies+1] = e
end
View
@@ -38,4 +38,6 @@ end
-- tests if this entity, and the given entity, have collided.
function Bullet:testcollision(e)
return self:distance(e) <= self.radius+e.radius
-end
+end
+
+return Bullet

0 comments on commit 9cdb4b8

Please sign in to comment.