I’m working on a inventory system for my RPG.
I’m halfway there but need help. Currently when I touch the item added to inventory it leaves a black square, I want it to disappear and a double tap option to drop. Also once a chest is open I want it to show a empty chest, currently it keeps showing the emerald.
displayMode(FULLSCREEN)
function setup()
Chest:init()
rectMode(CENTER)
dir,i=1,1
delay,xs,ys=0,0,0
dx,dy=WIDTH/2,HEIGHT/2
createButtons()
cx,cy=0,0
sx,sy=0,0
dx,dy=0,0
fill(255)
stroke(255)
strokeWidth(1)
colBody:init()
end
function draw()
background(40, 40, 50)
checkButtons()
noFill()
sprite(asset.builtin.Blocks.Glass_Frame,900,100,200,200)
c1.x=c1.x+dx
c1.y=c1.y+dy
sprite(asset.builtin.Planet_Cute.Character_Princess_Girl,c1.x,c1.y)
chest()
end
function touched(t)
Chest:touched(t)
if t.state==BEGAN then
cx,cy=t.x,t.y
end
if t.state==MOVING then
if insideCircle(t.x,t.y) then
dx=(t.x-cx)/10
dy=(t.y-cy)/10
else
cx,cy=0,0
dx,dy=0,0
end
end
if t.state==ENDED then
cc,cy=0,0
dx,dy=0,0
end
if t.state==BEGAN or t.state==MOVING then
for a,b in pairs(bTab) do
b:touched(t)
end
elseif t.state==ENDED then
xs=0
ys=0
end
end
function collide(c) -- this gets called when a collision occurs
Chest:collide(c)
end
function insideCircle(x,y)
-- check if touch is inside of circle or ellipse
a,b=100,100 -- radius of circle or a,b of ellipse
if (x-cx)^2/a^2+(y-cy)^2/b^2 <= 1 then
return true
end
return false
end
Chest = class()
inventorypos=vec2
function Chest:init()
chestClosed=readImage("Planet Cute:Chest Closed")
chestOpen=readImage("Planet Cute:Chest Open")
citems={emerald =(asset.builtin.Planet_Cute.Gem_Blue),emerald2 =(asset.builtin.Planet_Cute.Gem_Blue)}
inventorypos=vec2(950,80)
end
function chest()
if open then
sprite(chestOpen,c2.x,c2.y)
sprite(citems.emerald,c2.x,c2.y+30)
r=true
if ElapsedTime-et>3 then
open=false
end
else
sprite(chestClosed,c2.x,c2.y)
end
if r==true then
sprite(citems.emerald2,inventorypos.x,inventorypos.y,30,30)
else
end
end
function Chest:touched(t)
if t.state==BEGAN and t.x>inventorypos.x-50 and t.x<inventorypos.x+50 and t.y>inventorypos.y-50 and t.y<inventorypos.y+50 then
citems.emerald2=0
print("super power actived")
end
end
function Chest:collide(c) -- this gets called when a collision occurs
if c1.x>c2.x-101 and c1.x<c2.x+101 and c1.y>c2.y-101 and c1.y<c2.y+101 then
open=true -- open the chest
et=ElapsedTime
end
end
colBody = class()
function colBody:init()
physics.gravity(0,0)
c1 = physics.body(CIRCLE,45) -- sprite collision circle
c1.type=DYNAMIC
c1.x=WIDTH/2
c1.y=300
c1.sleepingAllowed=false
c2 = physics.body(CIRCLE,50) -- sprite collision circle
c2.type=STATIC
c2.x=WIDTH/2
c2.y=600
c2.sleepingAllowed=false
end
button=class()
function button:init(x,y,d,xs,ys,n)
self.x=x -- x position
self.y=y -- y position
self.dir=d -- person direction
self.xs=xs*4 -- person speed x
self.ys=ys*4 -- person speed y
self.name=n -- button name
end
function button:draw()
fill(255,0,0)
rect(self.x,self.y,50,50)
fill(255)
text(self.name,self.x,self.y)
end
function button:touched(t)
if t.x>self.x-25 and t.x<self.x+25 and t.y>self.y-25 and t.y<self.y+25 then
dir=self.dir
i=dir
dx=self.xs
dy=self.ys
end
end
function createButtons()
x,y=WIDTH/2,100
bTab={}
table.insert(bTab,button(x-50,y+50,17,-1,1,"u-l"))
table.insert(bTab,button(x,y+50,9,0,1,"up"))
table.insert(bTab,button(x+50,y+50,1,1,1,"u-r"))
table.insert(bTab,button(x-50,y,33,-1,0,"left"))
table.insert(bTab,button(x+50,y,25,1,0,"right"))
table.insert(bTab,button(x-50,y-50,57,-1,-1,"d-l"))
table.insert(bTab,button(x,y-50,49,0,-1,"down"))
table.insert(bTab,button(x+50,y-50,41,1,-1,"d-r"))
end
function checkButtons()
for a,b in pairs(bTab) do
b:draw()
end
end