I’m trying to replicate the resident evil style of inventory based on blocks and space management. I have some code i made, but is full of bugs and doesn’t support double sized blocks. Any help is appreciated.
--# Main
function setup()
displayMode(FULLSCREEN)
single = Brick(450,450,100,100,"*")
single2 = Brick(550,550,100,100)
s3 = Brick(550,450,100,100)
s4 = Brick(450,550,100,100)
double = Brick(600,600,200,100)
GRID = {
Grid(200,200),
Grid(300,200),
Grid(200,300),
Grid(300,300),
}
end
function draw()
background(0, 0, 0, 255)
stroke(255)
strokeWidth(2)
--rectMode(CENTER)
single:draw()
single2:draw()
s3:draw()
s4:draw()
--double:draw()
for pieces = 1,#GRID do
GRID[pieces]:draw()
end
end
function touched(t)
single:touched(t)
single2:touched(t)
s3:touched(t)
s4:touched(t)
double:touched(t)
end
--# Brick
Brick = class()
function Brick:init(x,y,w,h,symbol,size)
self.x = x
self.y = y
self.origin_x = self.x
self.origin_y = y
self.size = size or 1
self.w = 100 *self.size
self.h = h
self.symbol = symbol or "&"
end
function Brick:draw()
pushStyle()
fill(127)
rectMode(CORNER)
rect(self.x,self.y,self.w,self.h)
fill(0)
text(self.symbol,self.x+self.w/2,self.y+self.h/2)
self.canSnap = true
popStyle()
end
function Brick:touched(t)
if t.x >= self.x - self.w/2 and
t.x <= self.x + self.w/2 and
t.y >= self.y - self.h/2 and
t.y <= self.y + self.h/2 and
t.state == MOVING then
self.x = CurrentTouch.x
self.y = CurrentTouch.y
end
for pieces = 1,#GRID do
if self.x + self.w/2 > GRID[pieces].x - GRID[pieces].w/2 and
self.x - self.w/2 < GRID[pieces].x + GRID[pieces].w/2 and
self.y + self.h/2 > GRID[pieces].y - GRID[pieces].h/2 and
self.y - self.h/2 < GRID[pieces].y + GRID[pieces].h/2 and
self.canSnap and
GRID[pieces].full ~= true and
t.state == ENDED then
GRID[pieces].full = true
self.x = GRID[pieces].x
self.y = GRID[pieces].y
else
GRID[pieces].full = false
--self.x = self.origin_x
--self.y = self.origin_y
end
if self.w > GRID[pieces].w then
self.canSnap = false
end
end
end
--# GRID
Grid = class()
function Grid:init(x,y)
self.x = x
self.y = y
self.w = 100
self.h = 100
self.full = false
end
function Grid:draw()
fill(0, 0, 0, 0)
rectMode(CORNER)
rect(self.x,self.y,self.w,self.h)
end
--[[
if you have the title of a longer work,
--]]