This commit is contained in:
Mikko Ahlroth 2014-10-19 01:46:27 +03:00
parent 42d482ba8b
commit a50e53d260
10 changed files with 188 additions and 1 deletions

21
chaingunmode.lua Normal file
View file

@ -0,0 +1,21 @@
Class = require 'hump/class'
require 'powerupmode'
require 'settings'
ChaingunMode = Class{
__include = PowerUpMode,
init = function(self)
PowerUpMode.init(self, 10)
end;
activate = function(self, plane)
PowerUpMode.activate(self, plane)
plane.machinegun:setType("chaingun")
end;
deactivate = function(self, plane)
PowerUpMode.deactivate(self, plane)
plane.machinegun:setType("vickers77")
end;
}

View file

@ -0,0 +1,17 @@
Class = require 'hump.class'
require 'entities/powerup'
local CHAINGUNPOWERUP_IMG = love.graphics.newImage("resources/graphics/chaingunpowerup.png")
local CHAINGUNPOWERUP_QUAD = love.graphics.newQuad(0, 0, 32, 32, 32, 32)
ChaingunPowerUp = Class{
__includes = PowerUp,
init = function(self, x, y, level)
PowerUp.init(self, x, y, level, 10)
end;
draw = function(self)
love.graphics.draw(CHAINGUNPOWERUP_IMG, CHAINGUNPOWERUP_QUAD, self.x, self.y, self.angle, 1, 1, 16, 16)
end;
}

View file

@ -10,6 +10,7 @@ PhysicsEntity = Class{
self.physicsType = physics_type
self.main_color = main_color
self.restitution = restitution
self.deleteLater = false
self:initPhysics(self)
end;
@ -37,6 +38,11 @@ PhysicsEntity = Class{
-- Only update if physics is not static
update = function(self, dt)
if self.deleteLater then
self:delete()
return
end
if self.physicsType ~= "static" then
self.x = self.body:getX()
self.y = self.body:getY()

View file

@ -75,9 +75,10 @@ Plane = Class{
end
self.quad = love.graphics.newQuad(0, 0, self.xsize, self.ysize, self.frames[0]:getWidth(), self.frames[0]:getHeight())
self.machinegun = MachineGun(self, "vickers77")
self.machinegun = MachineGun(self, PLANE_DEFAULT_GUN)
self.health = PLANE_HEALTH
self.powerupmode = nil
end;
getGunPosition = function(self)
@ -88,6 +89,11 @@ Plane = Class{
return {x + pos[1], y + pos[2]}
end;
setPowerUpMode = function(self, powerupmode)
self.powerupmode = powerupmode
self.powerupmode:activate(self)
end;
accelerate = function(self, down)
self.accelerating = down
end;
@ -109,6 +115,10 @@ Plane = Class{
PhysicsEntity.update(self, dt)
self.machinegun:update(dt)
if self.powerupmode ~= nil then
self.powerupmode:update(dt)
end
self.x, self.y = self.fixture:getBoundingBox()
self.angle = self.body:getAngle()

33
entities/powerup.lua Normal file
View file

@ -0,0 +1,33 @@
Class = require 'hump.class'
require 'entities/physicsentity'
-- rad per second
local ROTATION_SPEED = math.pi / 2
PowerUp = Class{
__includes = PhysicsEntity,
init = function(self, x, y, level, lifetime)
PhysicsEntity.init(self, x, y, level, "static", 0)
self.angle = 0
self.age = 0
self.lifetime = lifetime
end;
draw = function(self)
-- Noop, implement in child
end;
update = function(self, dt)
self.angle = self.angle + ROTATION_SPEED * dt
if self.angle > math.pi * 2 then
self.angle = self.angle - math.pi * 2
end
self.age = self.age + dt
if self.age > self.lifetime then
self:delete()
end
end;
}

View file

@ -2,6 +2,9 @@ Gamestate = require 'hump.gamestate'
require 'level'
require 'player'
require 'scoreboard'
require 'entities/chaingunpowerup'
require 'entities/plane'
require 'chaingunmode'
level_state = {}
@ -16,6 +19,8 @@ local level_music = nil
local current_level = nil
local paused = false
local POWERUP_POSSIBILITY = 0.001
function level_state:init()
end
@ -33,6 +38,7 @@ function level_state:enter(previous, level_file)
current_level.world:setCallbacks(begin_contact, end_contact, pre_solve, post_solve)
players[1]:setPlane(current_level:getPlane(1))
players[2]:setPlane(current_level:getPlane(2))
-- Set up scoreboards
for i = 1, #players, 1 do
@ -41,6 +47,8 @@ function level_state:enter(previous, level_file)
scoreboards[i] = Scoreboard(x, 20, players[i])
end
ChaingunPowerUp(750, 750, current_level)
end
@ -61,6 +69,15 @@ function level_state:update(dt)
return
end
-- Generate powerups
local r = love.math.random()
if r <= POWERUP_POSSIBILITY then
local x = love.math.random(love.window.getWidth())
local y = love.math.random(love.window.getHeight())
ChaingunPowerUp(x, y, current_level)
end
if not paused then
current_level:updateEntities(dt)
end
@ -110,7 +127,18 @@ function level_state:mousepressed(x, y, button)
end
function begin_contact(a, b, coll)
local aObj = a:getUserData()
local bObj = b:getUserData()
if aObj ~= nil and bObj ~= nil then
if aObj:isinstance(Plane) and bObj:isinstance(ChaingunPowerUp) then
bObj.deleteLater = true
aObj:setPowerUpMode(ChaingunMode())
elseif bObj:isinstance(Plane) and aObj:isinstance(ChaingunPowerUp) then
aObj.deleteLater = true
bObj:setPowerUpMode(ChaingunMode())
end
end
end
function end_contact(a, b, coll)

33
powerupmode.lua Normal file
View file

@ -0,0 +1,33 @@
Class = require 'hump/class'
require 'settings'
PowerUpMode = Class{
init = function(self, duration)
self.age = 0
self.duration = duration
end;
update = function(self, dt)
self.age = self.age + dt
if self.age >= duration then
self:deactivate()
end
end;
draw = function(self, x, y)
local age = self.age / self.duration
local start_color = {0, 255, 0}
local end_color = {255, 0, 0}
love.graphics.setColor(colorSlide(start_color, end_color, age_ratio))
love.graphics.rectangle("fill", self.x, self.y + 60, 50, 5)
end;
activate = function(self, plane)
plane.powerupmode = self
end;
deactivate = function(self, plane)
plane.powerupmode = nil
end;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

38
scoreboard.lua Normal file
View file

@ -0,0 +1,38 @@
Class = require 'hump/class'
require 'settings'
require 'utils'
local SCOREBOARD_FONT = love.graphics.newFont(16)
Scoreboard = Class{
init = function(self, x, y, player)
self.player = player
self.x = x
self.y = y
end;
draw = function(self)
if self.player.plane == nil then
return
end
local origWidth = love.graphics.getLineWidth()
love.graphics.setFont(SCOREBOARD_FONT)
love.graphics.setColor(128, 57, 75, 255)
love.graphics.setLineWidth(3)
love.graphics.rectangle("line", self.x, self.y, SCOREBOARD_WIDTH, SCOREBOARD_HEIGHT)
love.graphics.print(self.player.name, self.x + SCOREBOARD_MARGIN, self.y + SCOREBOARD_MARGIN)
love.graphics.print("Score", self.x + SCOREBOARD_MARGIN, self.y + SCOREBOARD_MARGIN + 20)
love.graphics.printf(self.player.score, self.x + SCOREBOARD_MARGIN, self.y + SCOREBOARD_MARGIN + 20, SCOREBOARD_WIDTH - 2 * SCOREBOARD_MARGIN, "right")
love.graphics.setLineWidth(origWidth)
local health_ratio = self.player.plane.health / PLANE_HEALTH
local start_color = {0, 255, 0}
local end_color = {255, 0, 0}
love.graphics.setColor(colorSlide(start_color, end_color, health_ratio))
love.graphics.rectangle("fill", self.x + SCOREBOARD_MARGIN, self.y + SCOREBOARD_MARGIN + 45, (SCOREBOARD_WIDTH - 2 * SCOREBOARD_MARGIN) * health_ratio, 20)
end;
}

View file

@ -27,6 +27,7 @@ KEYMAP = {
}
PLANE_HEALTH = 1000
PLANE_DEFAULT_GUN = "vickers77"
SCOREBOARD_WIDTH = 250
SCOREBOARD_HEIGHT = 100