루아(LUA)/마이크로스튜디오_예제
마우스를 따라다니는 원의 충돌 현상 구현
JK77
2022. 1. 10. 05:19
--[[ 저는 문과이며 코딩입문자로 제 모든 글과 코딩에 오류가 있을 수 있음을 알려드립니다. 로블록스와 마이크로스튜디오로 학습 또는 교육용 게임을 만들어 보는 것이 2022년 목표입니다. 2022.1.1.

방문: 독학_YouTube, 독학_Cafe
-- ]] -- 주석 끝.
하나의 프로젝트를 끝냈습니다. 마이크로 스튜디오에서 마이크로스크립트로 작성된 Circle Hitbox의 코드를 루아코드로 변경해 보았습니다. 이번 과정을 끝내면서 루아의 클래스에 대한 개념을 조금이나마 이해할 수 있었습니다.
아래링크를 클릭하시면 마우스를 따라오는 원이 중앙에 있는 하얀색 원과 만나면 색이 빨간색에서 녹색으로 바뀌는 것을 볼 수 있습니다.
http://lotusjk77.com/wp-content/game/circle1/index.html
A circle following a mouse_2022.1.9. (copy)
lotusjk77.com
클래스의 개념이 이해하기 어려워 3일 정도 읽었던 부분을 계속 반복해서 읽었더니 조금 이해가 된 것 같기도 합니다. 유튜브에 기록영상을 올려야 하는데 귀찮네요 ㅋ
일단 코드만 올려둡니다.
CircleHit = {x=0,y=0,size=20,color="rgb(255,255,255)",
new = function(self, o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end,
setPos = function (self, x, y)
self.x = x
self.y = y
end,
collidedWith = function(self, c)
local collided = false
if math.sqrt((self.x - c.x)^2 + (self.y - c.y)^2) < self.size/2 + self.size/2 then
collided = true
end
return collided
end,
drawR = function (self)
screen:drawRound(self.x,self.y,self.size,self.size,self.color)
end
}
init = function()
-- c1 = CircleHit:new({color="rgb(255,255,255)"})
c1 = CircleHit:new()
c2 = CircleHit:new()
end
update = function()
c2:setPos(mouse.x, mouse.y)
if c2:collidedWith(c1) then
c2.color = ("rgb(0,255,0)")
else
c2.color = ("rgb(255,0,0)")
end
end
draw = function()
screen:clear()
c1:drawR()
c2:drawR()
end
new = function(self, o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end,
setPos = function (self, x, y)
self.x = x
self.y = y
end,
collidedWith = function(self, c)
local collided = false
if math.sqrt((self.x - c.x)^2 + (self.y - c.y)^2) < self.size/2 + self.size/2 then
collided = true
end
return collided
end,
drawR = function (self)
screen:drawRound(self.x,self.y,self.size,self.size,self.color)
end
}
init = function()
-- c1 = CircleHit:new({color="rgb(255,255,255)"})
c1 = CircleHit:new()
c2 = CircleHit:new()
end
update = function()
c2:setPos(mouse.x, mouse.y)
if c2:collidedWith(c1) then
c2.color = ("rgb(0,255,0)")
else
c2.color = ("rgb(255,0,0)")
end
end
draw = function()
screen:clear()
c1:drawR()
c2:drawR()
end