Refactored code

This commit is contained in:
MaddoScientisto 2024-04-25 23:07:49 +02:00
commit 4407ce108b
8 changed files with 128 additions and 95 deletions

24
src/actor2.lua Normal file
View file

@ -0,0 +1,24 @@
--M = {}
actor = {}
actor.__index = actor
function actor:new(x,y)
local o = setmetatable({}, actor)
o.x=x
o.y=y
o.life=100
o.spriteIndex=0
return o
end
function actor:update()
end
function actor:draw()
spr(self.spriteIndex,self.x,self.y)
end
--M.actor = actor
return actor

33
src/barrel.lua Normal file
View file

@ -0,0 +1,33 @@
--local actor = require("actor2")
-- barrel = setmetatable({}, {__index = actor})
-- function barrel:new(actor)
-- local instance = setmetatable({
-- x = 0,
-- })
-- end
-- return barrel
barrel = {}
barrel.__index = barrel
function barrel:new(x,y)
local o = setmetatable({}, barrel)
o.x=x
o.y=y
o.life=100
o.spriteIndex=0
return o
end
function barrel:update()
end
function barrel:draw()
spr(self.spriteIndex,self.x,self.y)
end
--M.actor = actor
return barrel

View file

@ -11,52 +11,6 @@ h=300
asdf = {top=0, side=1}
LAYERS_COUNT=4
LAYERS = {
{index=4, name="background", render=true, render_objects=false, spawn_objects=false},
{index=3, name="solid", render=true, render_objects=true, spawn_objects=false},
{index=2, name="foreground", render=true, render_objects=false, spawn_objects=false},
{index=1, name="objects", render=false, render_objects=false, spawn_objects=true}
}
-- 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)
-- -- 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
-- 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 draw_crosshair(self)
local radius = 20 -- Adjust the radius of the crosshair as needed
@ -146,52 +100,26 @@ function cirno_init()
end
function cirno_draw()
cls(0)
--cls(0)
--camera(x - 240, y - 135)
foreach(LAYERS, render_layer)
--drawMap()
--debug_mouse()
for p in all(player) do
p:draw()
end
print(string.format("%.4f %dfps",stat(1),stat(7)),2,16,5)
weapons_manager.debug_draw()
end
function render_layer(layer)
if (layer.render) then
map(fetch"map/0.map"[layer.index].bmp)
end
if (layer.render_objects) then
-- Render all objects here
for p in all(player) do
p:draw()
end
weapons_manager.draw()
-- for b in all(bullets) do
-- b:draw()
-- end
end
end
function drawMap()
for i=LAYERS_COUNT,1,-1 do
map(fetch"map/0.map"[i].bmp)
end
end
function cirno_update()
for p in all(player) do
p:update()
end
weapons_manager.update()
end
function check_collision(x,y,w,h)

View file

@ -53,6 +53,8 @@ end
local cirno = require(make_path("cirno"))
local mouse_debug = require(make_path("mouse_debug"))
local map_manager = require(make_path("map"))
local weapons_manager = require(make_path("weapons"))
tile_width = 16
tile_height = 16
@ -63,9 +65,41 @@ end
function _update()
cirno.update()
weapons_manager.update()
end
LAYERS = {
{index=4, name="background", render=true, render_objects=false, spawn_objects=false},
{index=3, name="solid", render=true, render_objects=true, spawn_objects=false},
{index=2, name="foreground", render=true, render_objects=false, spawn_objects=false},
{index=1, name="objects", render=false, render_objects=false, spawn_objects=true}
}
function _draw()
cirno.draw()
cls(0)
foreach(LAYERS, render_layer)
mouse_debug.draw(4, tile_width, tile_height)
weapons_manager.debug_draw()
end
function render_layer(layer)
if (layer.render) then
-- todo move function in map manager
map(fetch("map/".. map_manager.get_current_map() .. ".map")[layer.index].bmp)
end
if (layer.render_objects) then
-- Render all objects here
cirno.draw()
weapons_manager.draw()
-- for b in all(bullets) do
-- b:draw()
-- end
end
end

View file

@ -1,3 +1,5 @@
--local player = require(make_path("cirno"))
M = {}
local tile_width = 16
@ -28,6 +30,14 @@ function M.is_tile_shoot_solid(x,y)
return (tile_flags & 2) ~= 0
end
function M.get_current_map()
return current_map
end
function M.get_layer_tile(x,y,layer)
return get_layer_tile(x,y,layer)
end
-- function M.can_move(x,y)
-- return is_tile(0,x,y)
-- end

View file

@ -1,4 +1,5 @@
--[[pod_format="raw",created="2024-04-22 19:55:03",modified="2024-04-22 20:14:14",revision=9]]
local map_manager = require(make_path("map"))
M = {}
function debug_mouse(layers_count, tile_width, tile_height)
@ -28,7 +29,7 @@ function debug_mouse(layers_count, tile_width, tile_height)
print(string.format("%d (%d)",tile_y,my),mx+x_offset,my+y_offset+8,9)
for i=layers_count,1,-1 do
local sprite = get_layer_tile(tile_x,tile_y,i)
local sprite = map_manager.get_layer_tile(tile_x,tile_y,i)
local flag = fget(sprite)
print(sprite,mx+x_offset+(i*16),my+y_offset+8*2,10)
print(flag,mx+x_offset+(i*16),my+y_offset+8+8*2,10)

View file

@ -1,5 +1,6 @@
--[[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("actor2")
M = {}