Refactoring and weapons class

This commit is contained in:
Marco Giacomelli 2024-04-24 11:11:16 +02:00
commit c33488fabd
4 changed files with 240 additions and 134 deletions

View file

@ -6,7 +6,7 @@ version 2
:: sfx/ :: sfx/
:: gfx/0.gfx :: gfx/0.gfx
b64$LS1bW3BvZCxjcmVhdGVkPSIyMDI0LTA0LTA0IDA3OjE5OjM0Iixtb2RpZmllZD0iMjAyNC0w b64$LS1bW3BvZCxjcmVhdGVkPSIyMDI0LTA0LTA0IDA3OjE5OjM0Iixtb2RpZmllZD0iMjAyNC0w
NC0yMyAyMTowNTo0NSIscmV2aXNpb249NjIzXV1sejQAbgYAAOA6AADzFHtbMF09e2JtcD1weHUA NC0yNCAwOToxMTowMyIscmV2aXNpb249NjQ4XV1sejQAbgYAAOA6AADzFHtbMF09e2JtcD1weHUA
QyAQEATw8CxmbGFncz0wLHBhbl94CADaeT0wLHpvb209MTF9LDIA8QivGyPeA34D3iPeA-4JE24D QyAQEATw8CxmbGFncz0wLHBhbl94CADaeT0wLHpvb209MTF9LDIA8QivGyPeA34D3iPeA-4JE24D
ThNuI04DfhMA-wOOAz4DTgMuI34jLgOeA-4UAy5eAB3wCfBFTYAtJi1QHWYdBSAdhg0VAB2WHRUN ThNuI04DfhMA-wOOAz4DTgMuI34jLgOeA-4UAy5eAB3wCfBFTYAtJi1QHWYdBSAdhg0VAB2WHRUN
pgQAww0VAA2WHQUQvQXwE1gAHzFYABXwKlBEgBRPHxRQDxSPHwRALxRPHyRADxQfH0QODxkEQA8U pgQAww0VAA2WHQUQvQXwE1gAHzFYABXwKlBEgBRPHxRQDxSPHwRALxRPHyRADxQfH0QODxkEQA8U
@ -38,11 +38,11 @@ EAfwBAcQByAHMAeAQgAdkAgAOAAIAAgwCAYAgRAIABhQGFAYCgAEFgA-OAAINwEfDzIA--------
------------------------------------w1A9MTF9fQ== ------------------------------------w1A9MTF9fQ==
:: gfx/.info.pod :: gfx/.info.pod
b64$LS1bW3BvZCxjcmVhdGVkPSIyMDI0L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PHxQA8QAgDyEPIg8jDyQPJQ8mDycUAPEAKA8pDyoPKw8sDy0P
Lg8vFADxADAPMQ8yDzMPNA81DzYPNxQA-wU4DzkPOg87DzwPPQ8_Dz8AD--w-wEA6-InWgEQBg8g Lg8vFADxADAPMQ8yDzMPNA81DzYPNxQA-wU4DzkPOg87DzwPPQ8_Dz8AD--w-wEA6-InWgEQBg8g
@ -69,86 +69,16 @@ L-AAMAD--4If-wEAzPEd6A9AAA1ADxcACxoIBggQAgMQBQAGAAgASRcAJggJUP--CfEX-gn8F-4J
8HArAF-_sPBwBAgA-9wf-wEAl1D-----Hw== 8HArAF-_sPBwBAgA-9wf-wEAl1D-----Hw==
:: sfx/.info.pod :: sfx/.info.pod
b64$LS1bW3BvZCxjcmVhdGVkPSIyMDI0LTA0LTA0IDA3OjE5OjMzIixtb2RpZmllZD0iMjAyNC0w b64$LS1bW3BvZCxjcmVhdGVkPSIyMDI0LTA0LTA0IDA3OjE5OjMzIixtb2RpZmllZD0iMjAyNC0w
NC0yMyAyMTowNTo0NSIsc3RvcmVkPSIyMDI0LTA0LTA0IDA3OjE5OjMzIl1dbHo0AAQAAAADAAAA NC0yNCAwOToxMTowMyIsc3RvcmVkPSIyMDI0LTA0LTA0IDA3OjE5OjMzIl1dbHo0AAQAAAADAAAA
MG5pbA== MG5pbA==
:: main.lua :: main.lua
--[[pod_format="raw",created="2024-04-04 07:19:33",modified="2024-04-23 21:05:45",revision=712]] --[[pod_format="raw",created="2024-04-04 07:19:33",modified="2024-04-24 09:11:03",revision=754]]
--cd("/cirnofarm/src") include("/cirnofarm/src/game.lua")
--include("/cirnofarm/src/cirno.lua")
function make_path(name)
local base_path = "/cirnofarm/src/" -- Change this when releasing
return base_path .. name
end
local _modules = {}
function loadfile (filename)
local src = fetch(filename)
if (type(src) ~= "string") then
notify("could not include "..filename)
stop()
return
end
-- https://www.lua.org/manual/5.4/manual.html#pdf-load
-- chunk name (for error reporting), mode ("t" for text only -- no binary chunk loading), _ENV upvalue
-- @ is a special character that tells debugger the string is a filename
local func,err = load(src, "@"..filename, "t", _ENV)
-- syntax error while loading
if (not func) then
send_message(3, {event="report_error", content = "*syntax error"})
send_message(3, {event="report_error", content = tostr(err)})
stop()
return
end
return func
end
function require(name)
local already_imported = _modules[name]
if already_imported ~= nil then
return already_imported
end
local filename = fullpath(name:gsub ('%.', '/') ..'.lua')
local func = loadfile (filename)
local module = func(name)
_modules[name]=module
return module
end
--local strawberry = require(make_path("strawberry"))
--strawberry.func()
local cirno = require(make_path("cirno"))
local mouse_debug = require(make_path("mouse_debug"))
tile_width = 16
tile_height = 16
function _init()
cirno.init()
end
function _update()
cirno.update()
end
function _draw()
cirno.draw()
mouse_debug.draw(3, tile_width, tile_height)
end
:: .info.pod :: .info.pod
b64$LS1bW3BvZCxjcmVhdGVkPSIyMDI0LTA0LTE2IDE5OjQyOjIyIixtb2RpZmllZD0iMjAyNC0w b64$LS1bW3BvZCxjcmVhdGVkPSIyMDI0LTA0LTE2IDE5OjQyOjIyIixtb2RpZmllZD0iMjAyNC0w
NC0yMyAyMTowNTo0NSIscnVudGltZT02LHN0b3JlZD0iMjAyNC0wNC0xNiAxOTo0MDowNSIsd29y NC0yNCAwOToxMTowMyIscnVudGltZT02LHN0b3JlZD0iMjAyNC0wNC0xNiAxOTo0MDowNSIsd29y
a3NwYWNlcz17e2xvY2F0aW9uPSJtYWluLmx1YSM1MiIsd29ya3NwYWNlX2luZGV4PTF9LHtsb2Nh a3NwYWNlcz17e2xvY2F0aW9uPSJtYWluLmx1YSMxIix3b3Jrc3BhY2VfaW5kZXg9MX0se2xvY2F0
dGlvbj0iZ2Z4LzAuZ2Z4Iix3b3Jrc3BhY2VfaW5kZXg9Mn0se2xvY2F0aW9uPSJtYXAvMC5tYXAi aW9uPSJnZngvMC5nZngiLHdvcmtzcGFjZV9pbmRleD0yfSx7bG9jYXRpb249Im1hcC8wLm1hcCIs
LHdvcmtzcGFjZV9pbmRleD0zfSx7bG9jYXRpb249InNmeC8wLnNmeCIsd29ya3NwYWNlX2luZGV4 d29ya3NwYWNlX2luZGV4PTN9LHtsb2NhdGlvbj0ic2Z4LzAuc2Z4Iix3b3Jrc3BhY2VfaW5kZXg9
PTR9fV1dbHo0AAQAAAADAAAAMG5pbA== NH19XV1sejQABAAAAAMAAAAwbmls
:: [eoc] :: [eoc]

View file

@ -1,6 +1,8 @@
--[[pod_format="raw",created="2024-04-14 14:05:11",modified="2024-04-23 21:05:44",revision=395]] --[[pod_format="raw",created="2024-04-14 14:05:11",modified="2024-04-23 21:05:44",revision=395]]
--include("/cirnofarm/src/actor.lua") --include("/cirnofarm/src/actor.lua")
local weapons_manager = require(make_path("weapons"))
last_coll=0 last_coll=0
mouse_debug = true mouse_debug = true
w=480 w=480
@ -15,45 +17,45 @@ LAYERS = {
{index=1, name="objects", render=false, render_objects=false, spawn_objects=true} {index=1, name="objects", render=false, render_objects=false, spawn_objects=true}
} }
function create_bullet(new_x, new_y, dir_x, dir_y) -- function create_bullet(new_x, new_y, dir_x, dir_y)
-- Calculate the length of the direction vector -- -- Calculate the length of the direction vector
local length = sqrt(dir_x^2 + dir_y^2) -- local length = sqrt(dir_x^2 + dir_y^2)
-- Normalize the direction vector -- -- Normalize the direction vector
local normalized_dir_x = dir_x / length -- local normalized_dir_x = dir_x / length
local normalized_dir_y = dir_y / length -- local normalized_dir_y = dir_y / length
add(bullets, { -- add(bullets, {
x=new_x, -- x=new_x,
y=new_y, -- y=new_y,
dx=normalized_dir_x * 2, -- dx=normalized_dir_x * 2,
dy=normalized_dir_y * 2, -- dy=normalized_dir_y * 2,
spriteIndex=80, -- spriteIndex=80,
destroy_sprite_index=66, -- destroy_sprite_index=66,
life=100, -- life=100,
draw=function(self) -- draw=function(self)
--pset(self.x,self.y,8) -- --pset(self.x,self.y,8)
spr(self.spriteIndex,self.x,self.y) -- spr(self.spriteIndex,self.x,self.y)
end, -- end,
update=function(self) -- update=function(self)
self.x+=self.dx -- self.x+=self.dx
self.y+=self.dy -- self.y+=self.dy
self.life-=1 -- self.life-=1
if self.life<0 then -- if self.life<0 then
del(bullets,self) -- del(bullets,self)
-- TODO: Create particle -- -- TODO: Create particle
end -- end
self.check_collision(self) -- self.check_collision(self)
end, -- end,
check_collision=function(self) -- check_collision=function(self)
-- If Collide with wall destroy self and create particle -- -- If Collide with wall destroy self and create particle
end -- end
}) -- })
end -- end
function draw_crosshair(self) function draw_crosshair(self)
local radius = 20 -- Adjust the radius of the crosshair as needed local radius = 20 -- Adjust the radius of the crosshair as needed
@ -69,7 +71,7 @@ end
function cirno_init() function cirno_init()
bullets={} --bullets={}
player={} player={}
add(player, { add(player, {
@ -88,8 +90,8 @@ function cirno_init()
cb=true, -- Collide with world bounds cb=true, -- Collide with world bounds
draw=function(self) draw=function(self)
spr(self.spriteIndex,self.x,self.y, self.hflip) spr(self.spriteIndex,self.x,self.y, self.hflip)
print(string.format("x:%.2f y:%.2f mx:%.2f my:%.2f blts:%s",self.x,self.y, print(string.format("x:%.2f y:%.2f mx:%.2f my:%.2f",self.x,self.y,
self.move_x,self.move_y,count(bullets)),0,0,1) self.move_x,self.move_y),0,0,1)
draw_crosshair(self) draw_crosshair(self)
--print(string.format("dir_x:%.4f dir_y:%.4f",self. --print(string.format("dir_x:%.4f dir_y:%.4f",self.
@ -97,10 +99,10 @@ function cirno_init()
update=function(self) update=function(self)
self.move_character(self) self.move_character(self)
-- Shoot bullet -- Shoot bullet with Z
if (btn(4)) then if (btn(4)) then
--create_bullet(self.x,self.y,self.move_x,self.move_Y) --create_bullet(self.x,self.y,self.move_x,self.move_Y)
create_bullet(self.x+8,self.y+8,self.move_x,self.move_y) weapons_manager.create_bullet(self.x+8,self.y+8,self.move_x,self.move_y)
end end
end, end,
move_character=function(self) move_character=function(self)
@ -153,7 +155,7 @@ function cirno_draw()
print(string.format("%.4f %dfps",stat(1),stat(7)),2,16,5) print(string.format("%.4f %dfps",stat(1),stat(7)),2,16,5)
weapons_manager.debug_draw()
end end
function render_layer(layer) function render_layer(layer)
@ -167,9 +169,11 @@ function render_layer(layer)
p:draw() p:draw()
end end
for b in all(bullets) do weapons_manager.draw()
b:draw()
end -- for b in all(bullets) do
-- b:draw()
-- end
end end
end end
@ -185,9 +189,8 @@ function cirno_update()
p:update() p:update()
end end
for b in all(bullets) do weapons_manager.update()
b:update()
end
end end
function check_collision(x,y,w,h) function check_collision(x,y,w,h)

View file

@ -1,15 +1,71 @@
--[[pod_format="raw",created="2024-04-14 13:41:07",modified="2024-04-14 14:05:57",revision=2]] --cd("/cirnofarm/src")
include("cirno.lua") --include("/cirnofarm/src/cirno.lua")
function make_path(name)
local base_path = "/cirnofarm/src/" -- Change this when releasing
return base_path .. name
end
local _modules = {}
function loadfile (filename)
local src = fetch(filename)
if (type(src) ~= "string") then
notify("could not include "..filename)
stop()
return
end
-- https://www.lua.org/manual/5.4/manual.html#pdf-load
-- chunk name (for error reporting), mode ("t" for text only -- no binary chunk loading), _ENV upvalue
-- @ is a special character that tells debugger the string is a filename
local func,err = load(src, "@"..filename, "t", _ENV)
-- syntax error while loading
if (not func) then
send_message(3, {event="report_error", content = "*syntax error"})
send_message(3, {event="report_error", content = tostr(err)})
stop()
return
end
return func
end
function require(name)
local already_imported = _modules[name]
if already_imported ~= nil then
return already_imported
end
local filename = fullpath(name:gsub ('%.', '/') ..'.lua')
local func = loadfile (filename)
local module = func(name)
_modules[name]=module
return module
end
--local strawberry = require(make_path("strawberry"))
--strawberry.func()
local cirno = require(make_path("cirno"))
local mouse_debug = require(make_path("mouse_debug"))
tile_width = 16
tile_height = 16
function _init() function _init()
cirno_init() cirno.init()
end end
function _update() function _update()
cirno_update() cirno.update()
end end
function _draw() function _draw()
cls(0) cirno.draw()
cirno_draw() mouse_debug.draw(3, tile_width, tile_height)
end end

117
src/weapons.lua Normal file
View file

@ -0,0 +1,117 @@
--[[pod_format="raw",created="2024-04-24 07:17:14",modified="2024-04-24 08:16:10",revision=9]]
M = {}
local weapons = {}
local bullets = {}
weapon = {
name = "Ice Blaster",
rate_of_fire = 0.2,
shoot=function(self)
end
}
bullet = {
x=0,
y=0,
dx=0,
dy=0,
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
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
}
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
b = bullet
b.life = 100
b.x = new_x
b.y = new_y
b.dx = normalized_dir_x * 2
b.dy = 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
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 M.init()
end
function M.draw()
for b in all(bullets) do
b:draw()
end
end
function M.update()
for b in all(bullets) do
b:update()
end
end
function M.debug_draw()
print(string.format("Bullets: %d", count(bullets)), 0,32,1)
end
return M