2015년 7월 24일 금요일

corona sdk 타워 디펜스 소스 분석 (하)




타워 디펜스
http://www.tandgapps.co.uk/resources/tutorial-tower-defense-for-ios/
소스
http://www.tandgapps.co.uk/wp-content/uploads/2013/02/TowerDefense.zip



타워의 총알 자동 발사

사용자 터치 핸들러인 levelTouched 함수에서 시작됩니다.
levelRects[i]:addEventListener("touch", levelTouched)
levelTouched->startTowers
1.2초 반복 실행 핸들러입니다.
towerTimers[tower] = timer.performWithDelay(1200,autoNow,0)

해당 함수에서는 현재타워와 각각의 적들의 각도와 거리를 계산합니다.
local angleBetween = mCeil(mAtan2( (y - yPos), (x - xPos) ) * 180 / mPi) + 90
local sqDistance = ((x-xPos)*(x-xPos))+((y-yPos)*(y-yPos)) --a2+b2=c2
local dist = mSqrt(sqDistance); --Sqaure root it.

거리가 특정 거리보다가까우면 총알을 발사합니다.
--If the enemy is close we shoot.
if dist <= 80 and dist >= 0 or dist >= -80 and dist <= 0 then
--Rotate the tower..
towerArray[tower].rotation = angleBetween-180

--Fire the weapon..

--Break out the loop if we have fired.
break;
end

총알 발사의 경우 원을 만들어서 일정시간 후에는 사라지게 합니다.
여기에서는 크게 의미가 있는것 같지는 않습니다. "weapon" 을 충돌체크에서 사용하게 됩니다.
--Fire the weapon..
local shot = display.newCircle(0,3,3)
shot.x = xPos; shot.y = yPos; 
shot:setFillColor(240,0,0)
shot.name = "weapon"; shot.rotation = angleBetween+90;
physics.addBody(shot, { isSensor = true } )
weaponGroup:insert(shot)

local function kill () timer.performWithDelay(2, function() if shot ~= nil then display.remove(shot); shot = nil; end; end,1);end
towerTrans[tower] = transition.to(shot, {time = distTime, x = x, y = y, onComplete = kill})







댓글 없음:

댓글 쓰기