2015년 11월 21일 토요일

corona sdk 굴착소년 쿵 같은거 어떻게 만들까 고민하기(2탄)

앞에서 만든것을 다른 라이브러리를 이용해서 만들어 봤습니다.

라이브러리 이름은 Million Tile Engine 입니다.

기존의 이미지 타일 모두 재사용해서 비슷하게 만들었습니다.

차이는 이번에는 물리엔진을 사용하였습니다. 충돌 점프등 사용이 상당히 편합니다.

완성된 모습은 아래와 같습니다.


코드는 기존보다 더 간단해 졌습니다.
Million Tile Engine 은 타일 변환등이 간단해서 구현이 처리가 간단하였습니다. 하지만 카메라처리등은 좀 더 복잡하였습니다.
참고로 tiled에서 물리처리를 위해 physics true처리를 해야 합니다.
엔진은 아래 링크에서 받으면 되고
https://forums.coronalabs.com/topic/33119-million-tile-engine-beta-release/
작업된 아래 샘플 코드는 아래 링크에서 받으면 됩니다.
https://drive.google.com/file/d/0B9vAKDzHthQIMEhrRklGTXU0RkE/view?usp=sharing


--------------------------------------------------------------------------------
display.setStatusBar(display.HiddenStatusBar)


--------------------------------------------------------------------------------
-- Localize
--------------------------------------------------------------------------------
local mte = require('mte').createMTE()        --Load and instantiate MTE.
local physics = require ("physics")
local LD = require("lib.LD_LoaderG2")


local myLevel = {}
local math_ceil = math.ceil
local math_abs = math.abs
local table_insert = table.insert
local map
local player = {}
local dir = 0
local drill = false
local gravity = 1
local speed = 60
local drillOffset = 30
local drillObject
local printMemUsageCount = 0

--------------------------------------------------------------------------------
-- Functions
--------------------------------------------------------------------------------
local distanceBetween = function(x1, y1, x2, y2) return (((x2 - x1) ^ 2) + ((y2 - y1) ^ 2)) ^ 0.5 end
local getPointsAlongLine = function(x1, y1, x2, y2, d) local points = {} local diffX = x2 - x1 local diffY = y2 - y1 local distBetween = math_ceil(distanceBetween(x1, y1, x2, y2) / d) local x, y = x1, y1 local addX, addY = diffX / distBetween, diffY / distBetween for i = 1, distBetween do table_insert(points, {x, y}) x, y = x + addX, y + addY end return points end
local clamp = function(v, l, h) return (v < l and l) or (v > h and h) or v end

--------------------------------------------------------------------------------
-- Physics
--------------------------------------------------------------------------------
physics.start()
physics.setGravity(0, 9.8)
mte.enableBox2DPhysics()

--------------------------------------------------------------------------------
-- Load Map
--------------------------------------------------------------------------------

mte.loadMap("resource/map1.tmx")            --Load a map.
myLevel = LD:loadLevel("LDloadresource")

--------------------------------------------------------------------------------
-- Physics
--------------------------------------------------------------------------------
--physics.setDrawMode( "hybrid" )

--------------------------------------------------------------------------------
-- Engine dependny code
--------------------------------------------------------------------------------
------------------------------------
local function spawnObject(pname,px,py,passetName,physicsTrue)
------------------------------------
 local function mteSpriteSetup(object,pname,px,py)
  local setup = {
   kind = "sprite", 
   layer = mte.getSpriteLayer(1), 
   levelPosX = px, 
   levelPosY = py,
   name = pname
   }
  mte.addSprite(object, setup)
 end

 local objProps = 
 {
  name  = pname, 
  x  = px,
  y  = py,
  xScale  =  1,
  yScale  =  1,
  assetName = passetName,
 }
 -- For LD
 -- createObject param nil need not remove object.
 -- That is changed.
 local object = myLevel:createObject(nil, objProps).view 
 -- For Map Engine
 mteSpriteSetup(object,pname,px,py);
 if( physicsTrue ~= nil ) then
  if( physicsTrue == true ) then
   physics.addBody(object, "dynamic", { density=10.0, friction=0.1, bounce=0.1 })
   object.isFixedRotation = true
  end
 end
 return object
end

local function removeObject(displayObject)
 -- body
 mte.removeSprite(displayObject, true)
 -- For Dust map.layer["Object Layer 1"]:remove(displayObject)
end

local function getTileInfo( screenX, screenY, layer )
 local loc = mte.convert("levelPosToLoc", screenX, screenY, layer)
 print("Tile Loc",loc.x, loc.y)
 local tile = mte.getTileObj(loc.x, loc.y, layer)
 -- For Dust 
 --map.layer["Tile Layer 1"].tileByPixels(screenX, screenY)
 return tile
end

local function engineLoop(track)
 -- For MTE
 --mte.setCamera({sprite = track, scale = 1})
 mte.update() --Required to process the camera and display the map.
 mte.debug()  --Displays onscreen position/fps/memory text.

 -- For Dust 
 --map.updateView()
end

local function getTileId(tile)
 local id = tile.index
 return id
end

local function getTileXY(tile)
 local x = tile.locX
 local y = tile.locY
 return x , y
end

local function removeTile( tileX, tileY )
 mte.updateTile({locX = tileX, locY = tileY, layer = 1, tile = 0})
 -- For Dust
 --map.layer["Tile Layer 1"].lockTileErased(tile.tileX, tile.tileY)
end

local function setCamera(track)
 mte.constrainCamera({})
 mte.setCamera({sprite = track, scale = 1})
 mte.setCameraFocus(track)
 -- For Dust map.setCameraFocus(track,true,true)
end

--------------------------------------------------------------------------------
-- Engine dependny code END
--------------------------------------------------------------------------------





------------------------------------
local monitorMem = function()
------------------------------------
 collectgarbage()
 print( "MemUsage: " .. collectgarbage("count") )
 local textMem = system.getInfo( "textureMemoryUsed" ) / 1000000
 print( "TexMem: " .. textMem )
end


------------------------------------
------------------------------------
------------------------------------
------------------------------------
local gameLoop = function(event)
------------------------------------
------------------------------------
------------------------------------
------------------------------------
 local vx,vy = player:getLinearVelocity()
 player.prevX, player.prevY = player.x, player.y
 player.xVel = dir * speed
 player.yVel = vy
 --print(player.x, player.y, player.xVel, player.yVel)

 player:setLinearVelocity(player.xVel, player.yVel)
 if( player.yVel < 0.01 ) then
  player.isGrounded = true
 else
  player.isGrounded = false
 end
 
 engineLoop(player)

 printMemUsageCount = printMemUsageCount + 1;
 if( printMemUsageCount > 100 ) then
  printMemUsageCount = 0
  monitorMem()
 end
end

------------------------------------
local function upPressed(event)
------------------------------------
 if( drill == true ) then
  return
 end
 print("U")

 local vx,vy = player:getLinearVelocity()
 dx = vx
 dy = -(speed*3)
 player:setLinearVelocity(vx,dy)
 dir=0
end

------------------------------------
local function leftPressed(event)
------------------------------------
 if( drill == true ) then
  return
 end
 print("L")
 dir = -1
end

------------------------------------
local function rightPressed(event)
------------------------------------
 if( drill == true ) then
  return
 end
 print("R")
 dir = 1
end

------------------------------------
local function btnReleased(event)
------------------------------------
 print("release")
 dir = 0
end

------------------------------------
local function drillTimerCB()
------------------------------------
print("Drill",drillObject.x, drillObject.y)
 local tile = getTileInfo( drillObject.x, drillObject.y, 1 )
 if( tile == nil ) then
  removeObject(drillObject)
  drillObject=nil
  drill = false
  return
 end
 local x,y = getTileXY(tile)
 print(x,y)
 removeTile(x,y)
 removeObject(drillObject)
 drillObject=nil
 drill = false
end

------------------------------------
local function drillPressed(event)
------------------------------------
 print("Drill")
 if( drill == true or player.isGrounded == false ) then
  return
 end
 drill = true
 dir = 0
 drillObject = spawnObject("drill",player.x,player.y+drillOffset,"drill_down_png")

 timer.performWithDelay(500, drillTimerCB )
end

------------------------------------
local function drillRelease(event)
------------------------------------
 
end

------------------------------------
local function drillRPressed(event)
------------------------------------
 print("Drill")
 if( drill == true or player.isGrounded == false ) then
  return
 end
 drill = true
 dir = 0
 drillObject = spawnObject("drill",player.x+drillOffset,player.y,"drill_right_png")

 timer.performWithDelay(500, drillTimerCB )
end

------------------------------------
local function drillRRelease(event)
------------------------------------
 
end

------------------------------------
local function drillLPressed(event)
------------------------------------
 print("Drill")
 if( drill == true or player.isGrounded == false ) then
  return
 end
 drill = true
 dir = 0
 drillObject = spawnObject("drill",player.x-drillOffset,player.y,"drill_left_png")

 timer.performWithDelay(500, drillTimerCB )
end

------------------------------------
local function drillLRelease(event)
------------------------------------
 
end

------------------------------------
local function spawnPlayerObject()
------------------------------------
 player = spawnObject("dog",200,200,"dog_brown_png_1",true)
 player.xVel, player.yVel = 0, 0 -- Initial X and Y velocities 
 player.YgroundOffset = 10
 player.XgroundOffset = 10
end
--------------------------------------------------------------------------------

Runtime:addEventListener("enterFrame", gameLoop)

spawnPlayerObject()

setCamera(player)

btnLeft = myLevel:getLayerObject("layer1","btnL")
btnLeft.onPress = leftPressed
btnLeft.onRelease = btnReleased
btnRight = myLevel:getLayerObject("layer1","btnR")
btnRight.onPress = rightPressed
btnRight.onRelease = btnReleased
btnUp = myLevel:getLayerObject("layer1","btnU")
btnUp.onPress = upPressed
btnUp.onRelease = btnReleased
btnDrill = myLevel:getLayerObject("layer1","btnDrill")
btnDrill.onPress = drillPressed
btnDrill.onRelease = drillRelease
btnDrillR = myLevel:getLayerObject("layer1","btnDrillR")
btnDrillR.onPress = drillRPressed
btnDrillR.onRelease = drillRRelease
btnDrillL = myLevel:getLayerObject("layer1","btnDrillL")
btnDrillL.onPress = drillLPressed
btnDrillL.onRelease = drillLRelease

댓글 없음:

댓글 쓰기