cirnofarm/src/weapons.lua

171 lines
2.7 KiB
Lua
Raw Normal View History

2024-04-24 22:53:04 +02:00
--[[pod_format="raw",created="2024-04-24 07:17:14",modified="2024-04-24 20:33:05",revision=11]]
local map_manager = require(make_path("map"))
2024-04-24 11:11:16 +02:00
M = {}
local weapons = {}
local bullets = {}
2024-04-24 22:53:04 +02:00
local particles = {}
2024-04-24 11:11:16 +02:00
weapon = {
name = "Ice Blaster",
rate_of_fire = 0.2,
shoot=function(self)
end
}
2024-04-24 22:53:04 +02:00
particle = {
x=0,
y=0,
life=4,
spriteIndex=81
}
particle.__index = particle
function particle:new(x,y)
local o = setmetatable({}, particle)
o.x=x
o.y=y
return o
end
function particle:update()
self.life-=1
if self.life<0 then
del(particles,self)
end
end
function particle:draw()
spr(self.spriteIndex,self.x,self.y)
end
2024-04-24 11:11:16 +02:00
bullet = {
x=0,
y=0,
dx=0,
dy=0,
spriteIndex=80,
destroy_sprite_index=66,
life=100,
damage=1,
}
2024-04-24 17:06:19 +02:00
bullet.__index = bullet
function bullet:new(x, y, dx, dy)
local o = setmetatable({}, bullet)
o.x = x
o.y = y
o.dx = dx
o.dy = dy
return o
end
2024-04-24 22:53:04 +02:00
function bullet:destroy()
-- Create particle
local p = particle:new(self.x, self.y)
add(particles, p)
del(bullets,self)
end
2024-04-24 17:06:19 +02:00
function bullet:check_collision()
2024-04-24 22:53:04 +02:00
if (map_manager.is_tile_shoot_solid(self.x,self.y)) then
self:destroy()
end
2024-04-24 17:06:19 +02:00
end
function bullet:update()
self.x+=self.dx
self.y+=self.dy
self.life-=1
if self.life<0 then
2024-04-24 22:53:04 +02:00
self:destroy()
end
2024-04-24 17:06:19 +02:00
2024-04-24 22:53:04 +02:00
self:check_collision()
2024-04-24 17:06:19 +02:00
end
function bullet:draw()
spr(self.spriteIndex,self.x,self.y)
end
2024-04-24 11:11:16 +02:00
function M.create_bullet(new_x, new_y, dir_x, dir_y)
-- Calculate the length of the direction vector
local length = sqrt(dir_x^2 + dir_y^2)
-- Normalize the direction vector
local normalized_dir_x = dir_x / length
local normalized_dir_y = dir_y / length
2024-04-24 17:06:19 +02:00
local b = bullet:new(new_x, new_y, normalized_dir_x * 2, normalized_dir_y * 2)
add(bullets, b)
-- add(bullets, {
-- x=new_x,
-- y=new_y,
-- dx=normalized_dir_x * 2,
-- dy=normalized_dir_y * 2,
-- spriteIndex=80,
-- destroy_sprite_index=66,
-- life=100,
-- damage=1,
-- 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
2024-04-24 11:11:16 +02:00
2024-04-24 17:06:19 +02:00
-- self.life-=1
-- if self.life<0 then
-- del(bullets,self)
-- -- TODO: Create particle
-- end
2024-04-24 11:11:16 +02:00
2024-04-24 17:06:19 +02:00
-- self.check_collision(self)
-- end,
-- check_collision=function(self)
-- -- If Collide with wall destroy self and create particle
2024-04-24 11:11:16 +02:00
2024-04-24 17:06:19 +02:00
-- end
-- })
2024-04-24 11:11:16 +02:00
end
function M.init()
end
function M.draw()
for b in all(bullets) do
b:draw()
end
2024-04-24 22:53:04 +02:00
for p in all(particles) do
p:draw()
end
2024-04-24 11:11:16 +02:00
end
function M.update()
for b in all(bullets) do
b:update()
end
2024-04-24 22:53:04 +02:00
for p in all(particles) do
p:update()
end
2024-04-24 11:11:16 +02:00
end
function M.debug_draw()
2024-04-24 22:53:04 +02:00
print(string.format("Bullets: %d Particles: %d", count(bullets), count(particles)), 0,32,1)
2024-04-24 11:11:16 +02:00
end
return M