Movable object

This commit is contained in:
MaddoScientisto 2024-04-26 14:24:25 +02:00
commit 5e667fd6d2
6 changed files with 97 additions and 17 deletions

View file

@ -8,7 +8,9 @@ Actor = {
function Actor:new(new_x,new_y)
local o = {
x=new_x,
y=new_y,
y=new_y,
w=16,
h=16
}
return setmetatable(o, {__index=self})

View file

@ -9,7 +9,7 @@ local Actor = require(make_path("actor2"))
-- return barrel
Barrel = Actor:new()
local Barrel = Actor:new()
Barrel.spriteIndex = 3

View file

@ -56,6 +56,7 @@ local mouse_debug = require(make_path("mouse_debug"))
local map_manager = require(make_path("map"))
local weapons_manager = require(make_path("weapons"))
local Barrel = require(make_path("barrel"))
local Strawberry = require(make_path("strawberry"))
tile_width = 16
tile_height = 16
@ -104,7 +105,7 @@ function _draw()
mouse_debug.draw(4, tile_width, tile_height)
weapons_manager.debug_draw()
print(string.format("Actors: %d", count(actors)),0,32+8)
end
@ -121,7 +122,11 @@ function spawn_objects()
if (tile == 3) then
local b = Barrel:new(x*tile_width,y*tile_height)
add(actors,b)
elseif(tile == 72) then
local s = Strawberry:new(x*tile_width,y*tile_height)
add(actors,s)
end
end
end

72
src/movable_actor.lua Normal file
View file

@ -0,0 +1,72 @@
local Actor = require(make_path("actor2"))
local map_manager = require(make_path("map"))
local MovableActor = Actor:new()
MovableActor.speed=0.05
MovableActor.hflip=false
MovableActor.move_x=0
MovableActor.move_y=0
MovableActor.noclip=false
MovableActor.collide_map=true
MovableActor.collide_actors=true
MovableActor.collide_bounds=true
function MovableActor:draw()
spr(self.spriteIndex,self.x,self.y, self.hflip)
end
function MovableActor:update()
self:move()
end
function MovableActor:move()
local hitbox_x = 4
local hitbox_y = 8
local hitbox_w = 6
local hitbox_h = 8
if (self.collide_map) then
if (check_collision(self.x+self.move_x+hitbox_x,
self.y+hitbox_y,
hitbox_w,
hitbox_h)) then
if (not self.noclip) then self.move_x = 0 end
end
if (check_collision(self.x+hitbox_x,
self.y+self.move_y+hitbox_y,
hitbox_w,
hitbox_h)) then
if (not self.noclip) then self.move_y = 0 end
end
end
self.move_x *= 0.95
self.move_y *= 0.95
self.x += self.move_x
self.y += self.move_y
end
function check_collision(x,y,w,h)
local collide = false
for i=x,x+w,w do
if (map_manager.is_tile_solid(i,y) or map_manager.is_tile_solid(i,y+h)) then
collide = collide or true
end
end
for i=y,y+h do
if (map_manager.is_tile_solid(x,i) or map_manager.is_tile_solid(x+w,i)) then
collide = collide or true
end
end
return collide
end
return MovableActor

View file

@ -1,8 +1,9 @@
--[[pod_format="raw",created="2024-04-22 19:32:54",modified="2024-04-23 20:21:06",revision=6]]
M = {}
local MovableActor = require(make_path("movable_actor"))
local Strawberry = MovableActor:new()
Strawberry.spriteIndex = 72
Strawberry.collide_map=false
Strawberry.collide_actors=false
Strawberry.collide_bounds=false
function M.func()
print("inside module.func()")
end
return M
return Strawberry