Browse Source

updated hitboxes and collision

master
NCLanceman 8 months ago
parent
commit
a3e8611a11
  1. 21
      blaster.lua
  2. 25
      entities/enemy.lua
  3. 25
      entities/player.lua

21
blaster.lua

@ -10,6 +10,8 @@ function Blaster:new(x, y, owner)
self.color = { 1, 0, 0 } self.color = { 1, 0, 0 }
self.destroy = false self.destroy = false
self.owner = owner self.owner = owner
self.bounds = { self.x, (self.x + self.width), self.y, (self.y + self.height) }
end end
function Blaster:draw() function Blaster:draw()
@ -23,22 +25,25 @@ function Blaster:update(dt)
elseif self.owner == "enemy" then elseif self.owner == "enemy" then
self.y = self.y + self.speed * dt self.y = self.y + self.speed * dt
end end
self.bounds = { self.x, (self.x + self.width), self.y, (self.y + self.height) }
end end
function Blaster:checkCollision(obj) function Blaster:checkCollision(obj)
local self_left = self.x local self_left = self.bounds[1]
local self_right = self.x + self.width local self_right = self.bounds[2]
local self_top = self.y local self_top = self.bounds[3]
local self_bottom = self.y + self.height local self_bottom = self.bounds[4]
local obj_left = obj.x local obj_left = obj.bounds[1]
local obj_right = obj.x + obj.width local obj_right = obj.bounds[2]
local obj_top = obj.y local obj_top = obj.bounds[3]
local obj_bottom = obj.y + obj.height local obj_bottom = obj.bounds[4]
if (self_right > obj_left) and (self_left < obj_right) and (self_bottom > obj_top) and (self_top < obj_bottom) then if (self_right > obj_left) and (self_left < obj_right) and (self_bottom > obj_top) and (self_top < obj_bottom) then
self.destroy = true self.destroy = true
obj.health = obj.health - 25 obj.health = obj.health - 25
--other blaster logic here --other blaster logic here
print("Collision! ", self.owner, " bullet struck ", obj.type)
end end
end end

25
entities/enemy.lua

@ -3,22 +3,26 @@ require("blaster")
Enemy = Object:extend() Enemy = Object:extend()
local fireTime = love.timer.getTime() local fireTime = love.timer.getTime()
local listOfBullets local enemyBullets
function Enemy:new(x, y, radius) function Enemy:new(x, y, radius)
self.origin = { x, y } self.origin = { x, y }
self.image = love.graphics.newImage("/assets/enemy/e-fighter.png") self.image = love.graphics.newImage("/assets/enemy/e-fighter.png")
self.x = x self.x = x
self.y = y self.y = y
self.width = self.image:getWidth() self.width = self.image:getWidth()
self.height = self.image:getHeight() self.height = self.image:getHeight()
self.type = "enemy"
self.health = 10 self.health = 10
self.move_radius = radius self.move_radius = radius
self.speed = 100 self.speed = 100
self.fireRate = 2 self.fireRate = 2
self.destroy = false self.destroy = false
listOfBullets = {} self.bounds = { (self.x + 18), (self.x + 44), (self.y + 20), (self.y + 44) }
enemyBullets = {}
end end
function Enemy:draw() function Enemy:draw()
@ -26,29 +30,30 @@ function Enemy:draw()
--love.graphics.rectangle("line", self.x, self.y, self.width, self.height) --love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
love.graphics.draw(self.image, self.x, self.y, 0, -1) love.graphics.draw(self.image, self.x, self.y, 0, -1)
for _, v in ipairs(listOfBullets) do for _, v in ipairs(enemyBullets) do
--love.graphics.circle("fill", v.x, v.y, v.radius) --love.graphics.circle("fill", v.x, v.y, v.radius)
v:draw() v:draw()
end end
end end
function Enemy:update(dt, player) function Enemy:update(dt, player)
local bounds = { self.origin[1] - self.move_radius, self.origin[1] + self.move_radius } local game_bounds = { self.origin[1] - self.move_radius, self.origin[1] + self.move_radius }
local window_height = love.graphics.getHeight() local window_height = love.graphics.getHeight()
self.x = self.x + self.speed * dt self.x = self.x + self.speed * dt
self.bounds = { (self.x + 18), (self.x + 44), (self.y + 20), (self.y + 44) }
--movement --movement
if self.x > bounds[2] then if self.x > game_bounds[2] then
self.speed = -self.speed self.speed = -self.speed
elseif self.x < bounds[1] then elseif self.x < game_bounds[1] then
self.speed = -self.speed self.speed = -self.speed
end end
--firing! --firing!
self:fire() self:fire()
for i, v in ipairs(listOfBullets) do for i, v in ipairs(enemyBullets) do
v:update(dt) v:update(dt)
if v.y > window_height then if v.y > window_height then
@ -56,8 +61,8 @@ function Enemy:update(dt, player)
end end
if v.destroy == true then if v.destroy == true then
print("Enemy Bullet Removed! Bullets in table remaining: ", #listOfBullets) print("Enemy Bullet Removed! Bullets in table remaining: ", #enemyBullets)
table.remove(listOfBullets, i) table.remove(enemyBullets, i)
end end
v:checkCollision(player) v:checkCollision(player)
@ -71,7 +76,7 @@ end
--shoot at the player a variable number of seconds --shoot at the player a variable number of seconds
function Enemy:fire() function Enemy:fire()
if love.timer.getTime() - fireTime > self.fireRate then if love.timer.getTime() - fireTime > self.fireRate then
table.insert(listOfBullets, Blaster(self.x, self.y, "enemy")) table.insert(enemyBullets, Blaster(self.x, self.y, "enemy"))
fireTime = love.timer.getTime() fireTime = love.timer.getTime()
end end
end end

25
entities/player.lua

@ -1,7 +1,7 @@
--! file: player.lua --! file: player.lua
Player = Object:extend() Player = Object:extend()
require("blaster") require("blaster")
local listOfBullets local playerBullets
function Player:new(x, y) function Player:new(x, y)
self.image = { self.image = {
@ -15,10 +15,16 @@ function Player:new(x, y)
self.y = y self.y = y
self.width = self.image[1][2]:getWidth() self.width = self.image[1][2]:getWidth()
self.height = self.image[1][2]:getHeight() self.height = self.image[1][2]:getHeight()
self.type = "player"
self.health = 100 self.health = 100
self.speed = 200 self.speed = 200
listOfBullets = {} --bounds of the hitbox
--(left, right, top, bottom)
self.bounds = { (self.x + 8), (self.x + 38), (self.y + 10), (self.y + 36) }
playerBullets = {}
end end
function Player:update(dt, enemies) function Player:update(dt, enemies)
@ -30,16 +36,19 @@ function Player:update(dt, enemies)
self.x = self.x + self.speed * dt self.x = self.x + self.speed * dt
end end
--update bounds
self.bounds = { (self.x + 8), (self.x + 38), (self.y + 10), (self.y + 36) }
--bullets! --bullets!
for i, v in ipairs(listOfBullets) do for i, v in ipairs(playerBullets) do
v:update(dt) v:update(dt)
if v.y < 0 then if v.y < 0 then
v.destroy = true v.destroy = true
end end
if v.destroy == true then if v.destroy == true then
table.remove(listOfBullets, i) table.remove(playerBullets, i)
print("Bullet Destroyed! Bullets in Table: ", #listOfBullets) print("Bullet Destroyed! Bullets in Table: ", #playerBullets)
end end
--recieve the list of enemies and check to see if the bullets hit --recieve the list of enemies and check to see if the bullets hit
for _, j in ipairs(enemies) do for _, j in ipairs(enemies) do
@ -55,9 +64,9 @@ function Player:draw()
love.graphics.draw(fullHealth, self.x, self.y) love.graphics.draw(fullHealth, self.x, self.y)
for _, v in ipairs(listOfBullets) do for _, v in ipairs(playerBullets) do
--love.graphics.setColor(v.color) --love.graphics.setColor(v.color)
love.graphics.circle("fill", v.x, v.y, v.radius) v:draw()
end end
--love.graphics.setColor(1, 1, 1) --love.graphics.setColor(1, 1, 1)
end end
@ -70,6 +79,6 @@ function Player:keyPressed(key)
--Shooting controls --Shooting controls
if key == "space" then if key == "space" then
--pew pew --pew pew
table.insert(listOfBullets, Blaster(fire_origin_x, fire_origin_y, "player")) table.insert(playerBullets, Blaster(fire_origin_x, fire_origin_y, "player"))
end end
end end

Loading…
Cancel
Save