aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author1029chris <1029chris@gmail.com>2022-02-02 15:12:58 -0800
committer1029chris <1029chris@gmail.com>2022-02-02 15:12:58 -0800
commitab78a5fc2b109c4d028d1abd197fc1575d9d37ab (patch)
tree9223c0ed805a991cda6a93ce62788989ebe602a4
parent2c30b81ec818341251929d11c59e77cfefc5c3e3 (diff)
Bullet collision stuff
-rw-r--r--bullets.lua14
-rw-r--r--objects.lua2
-rw-r--r--players.lua18
3 files changed, 26 insertions, 8 deletions
diff --git a/bullets.lua b/bullets.lua
index b7b113d..dad5ba4 100644
--- a/bullets.lua
+++ b/bullets.lua
@@ -13,9 +13,23 @@ function addbullet(x, y, velx, vely, evil)
spr(6, bullet.x, bullet.y)
end
+ function bullet.collide(object, bullet)
+ if bullet.x >= object.x and bullet.x <= object.x+object.w and bullet.y >= object.y and bullet.y <= object.y+object.h then
+ cls(4)
+ end
+ end
+
function bullet.update(bullet)
+ --applying velocity
bullet.x += bullet.velx
bullet.y += bullet.vely
+
+ --collision detection
+ if bullet.evil then
+ foreach(players, bullet.collide(bullet))
+ end
+
+ --delete bullet if off screen
if bullet.y > 128 or bullet.y < -8 or bullet.x > 128 or bullet.x < -8 then
del(obj, bullet)
end
diff --git a/objects.lua b/objects.lua
index 2dba154..5284b03 100644
--- a/objects.lua
+++ b/objects.lua
@@ -1,9 +1,11 @@
obj = {}
function updateobjs()
+ foreach(players, function(obj) obj:update() end)
foreach(obj, function(obj) obj:update() end)
end
function drawobjs()
+ foreach(players, function(obj) obj:draw() end)
foreach(obj, function(obj) obj:draw() end)
end \ No newline at end of file
diff --git a/players.lua b/players.lua
index 92c3646..60227fa 100644
--- a/players.lua
+++ b/players.lua
@@ -4,8 +4,11 @@ players = {}
function addplayer()
playercount += 1
local player = {}
+ player.health = 3
player.x = 18
player.y = 60
+ player.w = 8
+ player.h = 8
player.ymov = 0
player.id = playercount
player.shootcooldown = 0.0
@@ -24,15 +27,15 @@ function addplayer()
function player.update(player)
--movement
if btn(0, player.id) then
- player.x -= 2
+ player.x -= 1
elseif btn(1, player.id) then
- player.x += 2
+ player.x += 1
end
if btn(2, player.id) then
- player.y -= 2
+ player.y -= 1
player.ymov = 1
elseif btn(3, player.id) then
- player.y += 2
+ player.y += 1
player.ymov = -1
else
player.ymov = 0
@@ -41,15 +44,14 @@ function addplayer()
player.y = mid(0, player.y, 120)
--shooting
- player.shootcooldown -= 1/30
+ player.shootcooldown -= 1/60
if btn(4, player.id) and player.shootcooldown < 0 then
- addbullet(player.x+3, player.y, 4, 0, false)
+ addbullet(player.x+3, player.y, 2, 0, false)
player.shootcooldown = 0.1
end
end
- add(obj, player)
- add(players, #obj)
+ add(players, player)
end
addplayer() \ No newline at end of file