Added bullets

This commit is contained in:
MaddoScientisto 2024-04-18 23:49:42 +02:00
commit 3f4c6afde3
2 changed files with 103 additions and 51 deletions

View file

@ -1,4 +1,4 @@
--[[pod_format="raw",created="2024-04-14 14:05:11",modified="2024-04-18 17:11:46",revision=281]]
--[[pod_format="raw",created="2024-04-14 14:05:11",modified="2024-04-18 21:49:23",revision=310]]
--include("/cirnofarm/src/actor.lua")
last_coll=0
mouse_debug = true
@ -11,12 +11,45 @@ LAYERS_COUNT=4
LAYERS = {
{index=4, name="background", render=true, render_objects=false, spawn_objects=false},
{index=3, name="solid", render=true, render_objects=false, spawn_objects=false},
{index=2, name="foreground", render=true, render_objects=true, spawn_objects=false},
{index=3, name="solid", render=true, render_objects=true, spawn_objects=false},
{index=2, name="foreground", render=true, render_objects=false, spawn_objects=false},
{index=1, name="objects", render=false, render_objects=false, spawn_objects=true}
}
function create_bullet(new_x,new_y)
add(bullets, {
x=new_x,
y=new_y,
dx=2,
dy=2,
spriteIndex=80,
destroy_sprite_index=66,
life=100,
draw=function(self)
--pset(self.x,self.y,8)
spr(self.spriteIndex,self.x,self.y)
end,
update=function(self)
self.x+=self.dx
self.y+=self.dy
self.life-=1
if self.life<0 then
del(bullets,self)
-- TODO: Create particle
end
self.check_collision(self)
end,
check_collision=function(self)
-- If Collide with wall destroy self and create particle
end
})
end
function _init()
bullets={}
player={}
add(player, {
@ -26,7 +59,7 @@ function _init()
h=16,
speed=0.05,
hflip=false,
spriteIndex=64,
spriteIndex=65,
move_x=0,
move_y=0,
noclip=false,
@ -34,10 +67,15 @@ function _init()
cb=true, -- Collide with world bounds
draw=function(self)
spr(self.spriteIndex,self.x,self.y, self.hflip)
print(string.format("x:%.2f y:%.2f mx:%.2f my:%.2f coll:%d",self.x,self.y,
self.move_x,self.move_y,last_coll),0,0,1)
print(string.format("x:%.2f y:%.2f mx:%.2f my:%.2f bls:%s",self.x,self.y,
self.move_x,self.move_y,count(bullets)),0,0,1)
end,
update=function(self)
update=function(self)
self.move_character(self)
if (btn(4)) then create_bullet(self.x,self.y) end
end,
move_character=function(self)
local hitbox_x = 4
local hitbox_y = 8
@ -50,7 +88,7 @@ function _init()
if (btn(1)) self.move_x += self.speed self.hflip = false
if (btn(2)) self.move_y -= self.speed
if (btn(3)) self.move_y += self.speed
if (check_collision(self.x+self.move_x+hitbox_x,
self.y+hitbox_y,
hitbox_w,
@ -96,6 +134,10 @@ function render_layer(layer)
for p in all(player) do
p:draw()
end
for b in all(bullets) do
b:draw()
end
end
end
@ -110,6 +152,10 @@ function _update()
for p in all(player) do
p:update()
end
for b in all(bullets) do
b:update()
end
end
function check_collision(x,y,w,h)