Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Powerups! they...kinda work.

  • Loading branch information...
commit f640c3cf783869265385390bf1f7b3c985b74b2b 1 parent e11d0a0
@Textmode Textmode authored
Showing with 123 additions and 18 deletions.
  1. +21 −6 entity.lua
  2. +73 −0 powerup.lua
  3. +7 −4 ship.lua
  4. +22 −8 space.lua
View
27 entity.lua
@@ -11,12 +11,13 @@ local SFX_Explosion = love.audio.newSource("sfx/Explosion.wav", "static")
-------------------------------------------------------------------------
-- Handles some texture properties
-local function set_texture(self, texture)
+function _M.set_texture(self, texture)
self.texture = texture
self.width = texture:getWidth()
self.height = texture:getHeight()
end
+local generic_icon = love.graphics.newImage("gfx/AutoTurret.png")
-------------------------------------------------------------------------
function _M.new(t)
@@ -37,10 +38,8 @@ function _M.new(t)
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)
+ _M.set_texture(e, t.texture or generic_icon)
count = count + 1
@@ -57,8 +56,8 @@ 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.pos_x = self.pos_x + (self.dir_x * dv)
+ self.pos_y = self.pos_y + (self.dir_y * dv)
self:think(dt)
end
@@ -83,6 +82,7 @@ end
-- and tests / triggers death if appropriate
function _M:dohit(n)
n = n or 1
+ assert(n > 0, "Cannot hurt for negative damage")
local shield = self.shield - n
shield = shield >= 0 and shield or 0
@@ -105,6 +105,21 @@ function _M:die()
self.dir_y = 0
end
+function _M:heal(n)
+ n = n or 1
+ assert(n > 0, "Cannot heal for negative health")
+
+ local shield = self.shield
+ shield = shield + n
+ shield = shield >= 0 and shield or 0
+ shield = shield <= self.shieldmax and shield or self.shieldmax
+ self.shield = shield
+end
+
+function _M:collidewith(e, dt)
+
+end
+
-------------------------------------------------------------------------
if _VERSION == "Lua 5.1" then _G[_M._NAME] = _M end
View
73 powerup.lua
@@ -0,0 +1,73 @@
+-------------------------------------------------------------------------
+-- [powerup.lua]
+-- powerup
+-------------------------------------------------------------------------
+local entity = require "entity"
+
+local _M = {_NAME = "powerup", _TYPE = 'module'}
+local _MT = {__index = _M}
+local _MMT = {__index = entity}
+setmetatable(_M, _MMT)
+-------------------------------------------------------------------------
+local Image = love.graphics.newImage
+icons = {
+ generic = Image "gfx/RepairPack.png";
+ heal10 = Image "gfx/RepairPack.png";
+ heal30 = Image "gfx/RepairPack2.png";
+ heal60 = Image "gfx/RepairPack3.png";
+ heal100 = Image "gfx/RepairPack4.png";
+
+}
+
+function _M.new(t)
+ t = t or {}
+ p = entity.new(t)
+
+ p.kind = 'powerup'
+
+ p.faction = t.faction or 'player'
+ p.effect = t.effect or 'heal10'
+
+ _M.set_texture(p, icons[p.effect])
+
+ return setmetatable(p, _MT)
+end
+
+function _M:testcollision(e)
+ if e.npc and not self.faction == 'npc' then
+ return false
+ else
+ return entity.testcollision(self, e)
+ end
+end
+
+function _M:dohit()
+ self:die()
+end
+
+function _M:doeffect(e)
+ if self.effect == 'heal10' then
+ e:heal(e.shieldmax*.1)
+ elseif self.effect == 'heal30' then
+ e:heal(e.shieldmax*.3)
+ elseif self.effect == 'heal60' then
+ e:heal(e.shieldmax*.6)
+ elseif self.effect == 'heal100' then
+ e:heal(e.shieldmax)
+ else
+ print("Unknown powerup effect", self.effect)
+ end
+
+end
+
+function _M:collidewith(e, dt)
+ self:doeffect(e)
+ self:dohit()
+end
+
+
+-------------------------------------------------------------------------
+if _VERSION == "Lua 5.1" then _G[_M._NAME] = _M end
+
+return _M
+
View
11 ship.lua
@@ -154,14 +154,17 @@ end
-- handles the death of the entity.
function ship:die()
entity.die(self)
-
- if not self.npc then
- Gamestate.space.level.scrolling = false
- end
end
function ship:shoot()
self.entities[#self.entities+1] = Bullet.new(self)
end
+
+function ship:collidewith(e, dt)
+ if e.kind == 'ship' then
+ self:dohit(e.damage*dt)
+ end
+end
+
-------------------------------------------------------------------------
return ship
View
30 space.lua
@@ -1,6 +1,7 @@
-- requires
local Gamestate = require "lib.gamestate"
local Ship = require "ship"
+local powerup = require "powerup"
-- create and register this gamestate
local state = Gamestate.new()
@@ -34,9 +35,11 @@ state.player = Ship.new {name = 'player';
function state.player:die(...)
Ship.die(self, ...)
+
state.endtimer = 3
state.endtype = 'death'
state.music:stop()
+ state.level.scrolling = false
end
state.enemies = {}
@@ -80,10 +83,12 @@ function state:enter()
-- Reset some player ship defaults
self.player.pos_x = 64
self.player.pos_y = 320
- self.player.shieldmax = 100 -- and this
- self.player.shield = self.player.shieldmax
+ self.player.shieldmax = 100 -- and this
+ self.player.shield = self.player.shieldmax
+ self.player.state = 'alive'
self.timer = 0
self.endtimer = false
+ self.level.scrolling = true
-- reset the level
self.level.x = -800
@@ -144,15 +149,24 @@ function state:update(dt)
self.timer = 0
self:addentity(Ship.new{
name = string.format("Enemy#%03d", #self.enemies);
- pos_x = math.random(0, 800/2);
- pos_y = math.random(0, 600/2);
+ pos_x = math.random(SCREEN_WIDTH/2, SCREEN_WIDTH);
+ pos_y = math.random(0, SCREEN_HEIGHT);
texture = Enemy2;
})
+ self:addentity(powerup.new{
+ pos_x = math.random(SCREEN_WIDTH/2, SCREEN_WIDTH);
+ pos_y = math.random(0, SCREEN_HEIGHT);
+ })
end
-- Loop level
if level.x > level.width * 32 then
level.x = -800
+
+ self:addentity(powerup.new{
+ pos_x = math.random(SCREEN_WIDTH/2, SCREEN_WIDTH);
+ pos_y = math.random(0, SCREEN_HEIGHT);
+ })
end
--Randomize tile when you are past it
@@ -175,16 +189,16 @@ function state:update(dt)
-- Ideally, the player would be a normal entity, for now we
--special-case them
if player:testcollision(ship) then
- player:dohit(ship.damage*dt)
- ship:dohit(player.damage*dt)
+ player:collidewith(ship, dt)
+ ship:collidewith(player, dt)
end
-- with a mutual test, each entity only has to be tested against
-- the entities that come after it.
for j=i+1,#enemies do
oship = enemies[j]
if ship:testcollision(oship) then
- ship:dohit(oship.damage*dt)
- oship:dohit(ship.damage*dt)
+ ship:collidewith(ship, dt)
+ oship:collidewith(player, dt)
end
end
-- as a crude hack, we'll simply remove entities that are dead.
Please sign in to comment.
Something went wrong with that request. Please try again.