From c33488fabdfff40ed6c95ccb6570202d3f493ff8 Mon Sep 17 00:00:00 2001 From: Marco Giacomelli Date: Wed, 24 Apr 2024 11:11:16 +0200 Subject: [PATCH] Refactoring and weapons class --- cirnofarm.p64 | 96 ++++++--------------------------------- src/cirno.lua | 93 +++++++++++++++++++------------------- src/game.lua | 68 +++++++++++++++++++++++++--- src/weapons.lua | 117 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 240 insertions(+), 134 deletions(-) create mode 100644 src/weapons.lua diff --git a/cirnofarm.p64 b/cirnofarm.p64 index 5e14bd4..1bb29e5 100644 --- a/cirnofarm.p64 +++ b/cirnofarm.p64 @@ -6,7 +6,7 @@ version 2 :: sfx/ :: gfx/0.gfx b64$LS1bW3BvZCxjcmVhdGVkPSIyMDI0LTA0LTA0IDA3OjE5OjM0Iixtb2RpZmllZD0iMjAyNC0w -NC0yMyAyMTowNTo0NSIscmV2aXNpb249NjIzXV1sejQAbgYAAOA6AADzFHtbMF09e2JtcD1weHUA +NC0yNCAwOToxMTowMyIscmV2aXNpb249NjQ4XV1sejQAbgYAAOA6AADzFHtbMF09e2JtcD1weHUA QyAQEATw8CxmbGFncz0wLHBhbl94CADaeT0wLHpvb209MTF9LDIA8QivGyPeA34D3iPeA-4JE24D ThNuI04DfhMA-wOOAz4DTgMuI34jLgOeA-4UAy5eAB3wCfBFTYAtJi1QHWYdBSAdhg0VAB2WHRUN pgQAww0VAA2WHQUQvQXwE1gAHzFYABXwKlBEgBRPHxRQDxSPHwRALxRPHyRADxQfH0QODxkEQA8U @@ -38,11 +38,11 @@ EAfwBAcQByAHMAeAQgAdkAgAOAAIAAgwCAYAgRAIABhQGFAYCgAEFgA-OAAINwEfDzIA-------- ------------------------------------w1A9MTF9fQ== :: gfx/.info.pod b64$LS1bW3BvZCxjcmVhdGVkPSIyMDI0LTA0LTA0IDA3OjE5OjMzIixtb2RpZmllZD0iMjAyNC0w -NC0yMyAyMTowNTo0NSIsc3RvcmVkPSIyMDI0LTA0LTA0IDA3OjE5OjMzIl1dbHo0AAQAAAADAAAA +NC0yNCAwOToxMTowMyIsc3RvcmVkPSIyMDI0LTA0LTA0IDA3OjE5OjMzIl1dbHo0AAQAAAADAAAA MG5pbA== :: map/0.map b64$LS1bW3BvZCxjcmVhdGVkPSIyMDI0LTA0LTA0IDA3OjE5OjMzIixtb2RpZmllZD0iMjAyNC0w -NC0yMyAyMTowNTo0NSIscmV2aXNpb249NjMzXV1sejQAagIAALFBAADwCHt7Ym1wPXVzZXJkYXRh +NC0yNCAwOToxMTowMyIscmV2aXNpb249NjU4XV1sejQAagIAALFBAADwCHt7Ym1wPXVzZXJkYXRh KCJpMTYiLDMyAwAvIjABAP--jD80ODABAF4vNDABAAs-MTMw9AL--1wOBAMOIAAPAQBGEzMEAAgB AC8zMAEATi80OBgADw4kAA8BADkOaAAPAQD----------8vxGSIpLGhpZGRlbj1mYWxzZSxuYW1l PSJvYmplY3RzIixwYW5feD0tOTAKAPIBeT0tMTEwLHRpbGVfaD0xNgoAEHcKAI96b29tPTF9LGsQ @@ -56,11 +56,11 @@ ACIPAQA3HzgEACIPgAE3HzgEACMPAQA1LzE4BAAjDwEANS8xOAQAIw8BADUvMTgEACMPAQA1LzE4 BAAjD9cg-----------FTGJhY2vXIA9uEBFQbT0xfX0= :: map/.info.pod b64$LS1bW3BvZCxjcmVhdGVkPSIyMDI0LTA0LTA0IDA3OjE5OjMzIixtb2RpZmllZD0iMjAyNC0w -NC0yMyAyMTowNTo0NSIsc3RvcmVkPSIyMDI0LTA0LTA0IDA3OjE5OjMzIl1dbHo0AAQAAAADAAAA +NC0yNCAwOToxMTowMyIsc3RvcmVkPSIyMDI0LTA0LTA0IDA3OjE5OjMzIl1dbHo0AAQAAAADAAAA MG5pbA== :: sfx/0.sfx b64$LS1bW3BvZCxjcmVhdGVkPSIyMDI0LTA0LTA0IDA3OjE5OjM0Iixtb2RpZmllZD0iMjAyNC0w -NC0yMyAyMTowNTo0NSIscmV2aXNpb249NTMyXV1sejQARgEAAGYIAADwJ3B4dQADKAAAAwAED0AQ +NC0yNCAwOToxMTowMyIscmV2aXNpb249NTU3XV1sejQARgEAAGYIAADwJ3B4dQADKAAAAwAED0AQ Ag4AAaABIAKgDgAPEAAN8MoBAgMEBQYHAA--kAgJCgsPDA8NDw4PDxAA8AANDxEPEg8TDxQPFQ8W DxcTAPEBDxgPGQ8aDxsPHA8dDx4PHxQA8QAgDyEPIg8jDyQPJQ8mDycUAPEAKA8pDyoPKw8sDy0P Lg8vFADxADAPMQ8yDzMPNA81DzYPNxQA-wU4DzkPOg87DzwPPQ8_Dz8AD--w-wEA6-InWgEQBg8g @@ -69,86 +69,16 @@ L-AAMAD--4If-wEAzPEd6A9AAA1ADxcACxoIBggQAgMQBQAGAAgASRcAJggJUP--CfEX-gn8F-4J 8HArAF-_sPBwBAgA-9wf-wEAl1D-----Hw== :: sfx/.info.pod b64$LS1bW3BvZCxjcmVhdGVkPSIyMDI0LTA0LTA0IDA3OjE5OjMzIixtb2RpZmllZD0iMjAyNC0w -NC0yMyAyMTowNTo0NSIsc3RvcmVkPSIyMDI0LTA0LTA0IDA3OjE5OjMzIl1dbHo0AAQAAAADAAAA +NC0yNCAwOToxMTowMyIsc3RvcmVkPSIyMDI0LTA0LTA0IDA3OjE5OjMzIl1dbHo0AAQAAAADAAAA MG5pbA== :: main.lua ---[[pod_format="raw",created="2024-04-04 07:19:33",modified="2024-04-23 21:05:45",revision=712]] ---cd("/cirnofarm/src") ---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 +--[[pod_format="raw",created="2024-04-04 07:19:33",modified="2024-04-24 09:11:03",revision=754]] +include("/cirnofarm/src/game.lua") :: .info.pod b64$LS1bW3BvZCxjcmVhdGVkPSIyMDI0LTA0LTE2IDE5OjQyOjIyIixtb2RpZmllZD0iMjAyNC0w -NC0yMyAyMTowNTo0NSIscnVudGltZT02LHN0b3JlZD0iMjAyNC0wNC0xNiAxOTo0MDowNSIsd29y -a3NwYWNlcz17e2xvY2F0aW9uPSJtYWluLmx1YSM1MiIsd29ya3NwYWNlX2luZGV4PTF9LHtsb2Nh -dGlvbj0iZ2Z4LzAuZ2Z4Iix3b3Jrc3BhY2VfaW5kZXg9Mn0se2xvY2F0aW9uPSJtYXAvMC5tYXAi -LHdvcmtzcGFjZV9pbmRleD0zfSx7bG9jYXRpb249InNmeC8wLnNmeCIsd29ya3NwYWNlX2luZGV4 -PTR9fV1dbHo0AAQAAAADAAAAMG5pbA== +NC0yNCAwOToxMTowMyIscnVudGltZT02LHN0b3JlZD0iMjAyNC0wNC0xNiAxOTo0MDowNSIsd29y +a3NwYWNlcz17e2xvY2F0aW9uPSJtYWluLmx1YSMxIix3b3Jrc3BhY2VfaW5kZXg9MX0se2xvY2F0 +aW9uPSJnZngvMC5nZngiLHdvcmtzcGFjZV9pbmRleD0yfSx7bG9jYXRpb249Im1hcC8wLm1hcCIs +d29ya3NwYWNlX2luZGV4PTN9LHtsb2NhdGlvbj0ic2Z4LzAuc2Z4Iix3b3Jrc3BhY2VfaW5kZXg9 +NH19XV1sejQABAAAAAMAAAAwbmls :: [eoc] diff --git a/src/cirno.lua b/src/cirno.lua index 8290769..c34b3a7 100644 --- a/src/cirno.lua +++ b/src/cirno.lua @@ -1,6 +1,8 @@ --[[pod_format="raw",created="2024-04-14 14:05:11",modified="2024-04-23 21:05:44",revision=395]] --include("/cirnofarm/src/actor.lua") +local weapons_manager = require(make_path("weapons")) + last_coll=0 mouse_debug = true w=480 @@ -15,45 +17,45 @@ LAYERS = { {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 - local length = sqrt(dir_x^2 + dir_y^2) +-- -- 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 +-- -- Normalize the direction vector +-- local normalized_dir_x = dir_x / length +-- local normalized_dir_y = dir_y / length - 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, - 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 +-- 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, +-- 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.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 +-- self.check_collision(self) +-- end, +-- check_collision=function(self) +-- -- If Collide with wall destroy self and create particle - end - }) -end +-- end +-- }) +-- end function draw_crosshair(self) local radius = 20 -- Adjust the radius of the crosshair as needed @@ -69,7 +71,7 @@ end function cirno_init() - bullets={} + --bullets={} player={} add(player, { @@ -88,8 +90,8 @@ function cirno_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 blts:%s",self.x,self.y, - self.move_x,self.move_y,count(bullets)),0,0,1) + print(string.format("x:%.2f y:%.2f mx:%.2f my:%.2f",self.x,self.y, + self.move_x,self.move_y),0,0,1) draw_crosshair(self) --print(string.format("dir_x:%.4f dir_y:%.4f",self. @@ -97,10 +99,10 @@ function cirno_init() update=function(self) self.move_character(self) - -- Shoot bullet + -- Shoot bullet with Z if (btn(4)) then --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, move_character=function(self) @@ -153,7 +155,7 @@ function cirno_draw() print(string.format("%.4f %dfps",stat(1),stat(7)),2,16,5) - + weapons_manager.debug_draw() end function render_layer(layer) @@ -167,9 +169,11 @@ function render_layer(layer) p:draw() end - for b in all(bullets) do - b:draw() - end + weapons_manager.draw() + + -- for b in all(bullets) do + -- b:draw() + -- end end end @@ -185,9 +189,8 @@ function cirno_update() p:update() end - for b in all(bullets) do - b:update() - end + weapons_manager.update() + end function check_collision(x,y,w,h) diff --git a/src/game.lua b/src/game.lua index 56094a8..fa94fbd 100644 --- a/src/game.lua +++ b/src/game.lua @@ -1,15 +1,71 @@ ---[[pod_format="raw",created="2024-04-14 13:41:07",modified="2024-04-14 14:05:57",revision=2]] -include("cirno.lua") +--cd("/cirnofarm/src") +--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() + cirno.init() end function _update() - cirno_update() + cirno.update() end function _draw() - cls(0) - cirno_draw() + cirno.draw() + mouse_debug.draw(3, tile_width, tile_height) end \ No newline at end of file diff --git a/src/weapons.lua b/src/weapons.lua new file mode 100644 index 0000000..aaeb949 --- /dev/null +++ b/src/weapons.lua @@ -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 \ No newline at end of file