mirror of
https://gitlab.com/MaddoScientisto/cirnofarm.git
synced 2026-06-10 18:35:54 +00:00
Refactoring and weapons class
This commit is contained in:
parent
bd2fbb6a4d
commit
c33488fabd
4 changed files with 240 additions and 134 deletions
|
|
@ -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)
|
||||
|
|
|
|||
68
src/game.lua
68
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
|
||||
117
src/weapons.lua
Normal file
117
src/weapons.lua
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue