wip stuffs

This commit is contained in:
Paul Liverman 2015-03-27 19:27:43 -07:00
parent ec879fbbf3
commit 0dffba3258
3 changed files with 85 additions and 40 deletions

23
src/Bullet.lua Normal file
View File

@ -0,0 +1,23 @@
local defaultSpeed = 540/3*2
local function Bullet(x, y, w, h, speed, color)
self = {}
self.x = x or 0
self.y = y or lg.getHeight() / 2
self.w = w or 13
self.h = h or 3
if not speed then speed = defaultSpeed end
--self.speed = speed or player.speed
self.update = function(self, dt)
self.x = self.x + speed * dt
end
self.color = color or {0, 0, 255}
return self
end
return Bullet

View File

@ -11,7 +11,10 @@ local lg = love.graphics
local defaultSpeed = 960/3
local function Enemy(x, y, w, h, color, update)
local cron = require "lib.cron"
local Bullet = require "Bullet"
local function Enemy(bullets, x, y, w, h, color, update)
self = {}
self.w = w or 10
@ -22,14 +25,33 @@ local function Enemy(x, y, w, h, color, update)
self.color = color or {255, 0, 0}
self.update = update or function(self, dt)
local fire = cron.every(1/12, function()
print("Fired bullet")
table.insert(bullets, Bullet(self.x, self.y, false, false, false, {255, 0, 0}))
end)
--[[self.update = update or function(self, dt)
self.x = self.x - defaultSpeed * dt
fire:update(dt)
print("What?")
end]]
self.update = function(self, dt)
if update then
update(self, dt)
else
self.x = self.x - defaultSpeed * dt
end
fire:update(dt)
end
--[[self.fire = function(self, dt)
crony:update(dt)
end]]
return self
end
local function EnemyGroup(enemiesTable, enemyType, movementType, placement, amount)
local function EnemyGroup(bullets, enemiesTable, enemyType, movementType, placement, amount)
if not enemiesTable then
enemiesTable = {}
end
@ -63,7 +85,7 @@ local function EnemyGroup(enemiesTable, enemyType, movementType, placement, amou
y = math.random(5, lg.getHeight() - 5)
end
--table.insert stuff
table.insert(enemiesTable, Enemy(x, y, w, h, color, update))
table.insert(enemiesTable, Enemy(bullets, x, y, w, h, color, update))
end
--[[
EnemyGroup creates groups, based on a movement type (straight movement, sin movement, others),

View File

@ -7,12 +7,14 @@ local cron = require "lib.cron"
local inspect = require "lib.inspect"
local Spawn = require "EnemySpawns"
local Bullet = require "Bullet"
local enemies = {}
local bullets = {}
Spawn.CreateEnemyGroup(enemies, false, "randomSpeed", "random", "random")
Spawn.CreateEnemyGroup(bullets, enemies, false, "randomSpeed", "random", "random")
local whaaaa = cron.every(1, function() Spawn.CreateEnemyGroup(enemies, false, "randomSpeed", "random", "random") end)
local enemySpawner = cron.every(1, function() Spawn.CreateEnemyGroup(bullets, enemies, false, "randomSpeed", "random", "random") end)
local function outOfBounds(e)
-- I decided to only check if gone far enough left, regardless of entry / exit,
@ -37,47 +39,27 @@ local player = {
speed = 540/3
}
local bullets = {}
local function Bullet(x, y, w, h, speed, color)
self = {}
self.x = x or 0
self.y = y or lg.getHeight() / 2
self.w = w or 13
self.h = h or 3
if not speed then speed = player.speed * 2 end
--self.speed = speed or player.speed
self.update = function(self, dt)
self.x = self.x + speed * dt
end
self.color = color or {0, 0, 255}
return self
end
local bulletFiring
local function fireBullet()
if lk.isDown(' ') then
table.insert(bullets, Bullet(player.x, player.y, false, false, player.v.x + player.speed * 2))
table.insert(bullets, Bullet(player.x, player.y))--, false, false, player.v.x * 20 + player.speed * 2))
bulletFiring = cron.after(1/12, fireBullet)
end
end
function love.update(dt)
whaaaa:update(dt)
--[[ --this version would end up outside of bounds because #enemies was only calculated once
for i=1,#enemies do
enemies[i]:update(dt)
if outOfBounds(enemies[i]) then
table.remove(enemies, i)
i = i - 1 --so we don't skip one!
end
end
local function checkAABB(A, B)
if not A or not B then return end -- because fuck everything
return A.x - A.w / 2 < B.x + B.w / 2 and B.x - B.w / 2 < A.x + A.w / 2 and A.y - A.h / 2 < B.y + B.h / 2 and B.y - B.h / 2 < A.y + A.h / 2
--[[
this.x <= rect.x + rect.width &&
rect.x <= this.x + this.width &&
this.y <= rect.y + rect.height &&
rect.y <= this.y + this.height);
]]
end
function love.update(dt)
enemySpawner:update(dt)
for i=#bullets,1,-1 do
bullets[i]:update(dt)
if bullets[i].x > lg.getWidth() + bullets[i].w / 2 then
@ -87,7 +69,8 @@ function love.update(dt)
end
for i=#enemies,1,-1 do
enemies[i]:update(dt)
if outOfBounds(enemies[i]) then
--e.x < e.w / 2
if enemies[i].x < enemies[i].w / 2 then
table.remove(enemies, i)
i = i + 1 --so we don't skip one!
end
@ -97,11 +80,13 @@ function love.update(dt)
elseif lk.isDown('s') then
player.v.y = player.v.y + player.speed * dt
end
--[[
if lk.isDown('a') then
player.v.x = player.v.x - player.speed * dt
elseif lk.isDown('d') then
player.v.x = player.v.x + player.speed * dt
end
--]]
player.v.x = player.v.x * 0.9
player.v.y = player.v.y * 0.9
player.x = player.x + player.v.x
@ -122,6 +107,21 @@ function love.update(dt)
end
if bulletFiring then bulletFiring:update(dt) end
--check collisions with bullets and enemies
--checkAABB
for i=#enemies,1,-1 do
for j=#bullets,1,-1 do
if checkAABB(enemies[i], bullets[j]) then
table.remove(enemies, i)
table.remove(bullets, j)
-- fuck it, if it works with duplicated checks, I don't care
--i = i + 1
--j = j + 1
--print(i, #enemies, j, #bullets)
end
end
end
end
function love.draw()