cirnofarm/src/weapons.lua

134 lines
No EOL
2.2 KiB
Lua

--[[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"))
local Actor = require(make_path("actor2"))
--local actor = require("actor2")
M = {}
local weapons = {}
-- local particles = {}
weapon = {
name = "Ice Blaster",
rate_of_fire = 0.2,
shoot=function(self)
end
}
Particle = Actor:new()
Particle.life=4
Particle.spriteIndex=81
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
Bullet = Actor:new()
--function Bullet:new()
--
--end
--Bullet.dx=0
--Bullet.dy=0
Bullet.spriteIndex = 80
Bullet.damage=1
function Bullet:new(x,y,dir_x,dir_y)
local obj = Actor:new(x,y)
obj.dx = dir_x
obj.dy = dir_y
-- 'obj' has 'Actor' as a prototype at the moment
-- which isn't what we want so we just change it.
return setmetatable(obj, {__index=self})
end
function Bullet:destroy()
-- Create particle
local p = Particle:new(self.x, self.y)
add(particles, p)
del(bullets,self)
end
function Bullet:check_collision()
if (map_manager.is_tile_shoot_solid(self.x,self.y)) then
self:destroy()
end
end
function Bullet:update()
self.x+=self.dx
self.y+=self.dy
self.life-=1
if self.life<0 then
self:destroy()
end
self:check_collision()
end
function Bullet:draw()
spr(self.spriteIndex,self.x,self.y)
end
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
local b = Bullet:new(new_x, new_y, normalized_dir_x * 2, normalized_dir_y * 2)
--b.dx = normalized_dir_x * 2
--b.dy = normalized_dir_y * 2
add(bullets, b)
end
function M.init()
end
function M.draw()
--for b in all(bullets) do
-- b:draw()
--end
-- for p in all(particles) do
-- p:draw()
-- end
end
function M.update()
--for b in all(bullets) do
-- b:update()
--end
-- for p in all(particles) do
-- p:update()
-- end
end
function M.debug_draw()
print(string.format("Bullets: %d", count(bullets)), 0,32,1)
end
return M