Advertisement
Guest User

Untitled

a guest
Sep 14th, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.29 KB | None | 0 0
  1. ----------------
  2. --mainScreen.lua
  3. ----------------
  4.  
  5. --
  6. --mainScreen.lua
  7. --
  8. --The main screen for the game
  9. --set the background back to black
  10. module(...,package.seeall)
  11. local director = require("director")
  12. local localGroup
  13.  
  14. function new()
  15.     localGroup = display.newGroup()
  16.     local background = display.newRect(0,0,900,500)
  17.     background:setFillColor(0,0,0)
  18.     localGroup:insert(background)
  19.     --we need a world
  20.     local ourWorld = require("world")
  21.     localGroup:insert(ourWorld)
  22.     --need multitouch for the joystick
  23.     system.activate( "multitouch" )
  24.     --load joystick class
  25.     local joystickClass = require("joystick")
  26.    
  27.     -- Add A New Joystick
  28.     joystick = joystickClass.newJoystick{
  29.         outerImage = "joystick_bottom.png",     -- Outer Image - Circular - Leave Empty For Default Vector
  30.         outerAlpha = 0.7,                       -- Outer Alpha ( 0 - 1 )
  31.         innerImage = "joystick_middle.png",     -- Inner Image - Circular - Leave Empty For Default Vector
  32.         innerAlpha = 1.0,                       -- Inner Alpha ( 0 - 1 )
  33.         position_x = 630,                       -- X Position Top - From Left Of Screen - Positions Outer Image
  34.         position_y = 340,                       -- Y Position - From Left Of Screen - Positions Outer Image
  35.         onMove = move                           -- Move Event
  36.     }
  37.    
  38.     -- Print Output To Terminal
  39.     local function move( event )
  40.         if(event.joyX~=false and math.abs(event.joyX)>0.2) then
  41.             ourWorld.moveleft(event.joyX*10)
  42.         end
  43.         if(event.joyY~=false and math.abs(event.joyY)>0.2) then
  44.             if(event.joyY>0) then
  45.                 if(ourWorld.abletogodown()==true) then
  46.                     ourWorld.moveup(event.joyY*10)
  47.                 end
  48.             elseif(event.joyY<0) then
  49.                 ourWorld.moveup(event.joyY*10)
  50.             end
  51.         end
  52.         --print( event.joyX , event.joyY , event.joyAngle , event.joyVector )
  53.     end
  54.     return localGroup
  55. end
  56.  
  57. -------------
  58. --world.lua--
  59. -------------
  60. --thanks to p120ph37
  61. --http://developer.anscamobile.com/forum/2011/01/29/object-culling-render-process-when-not-content-area#comment-26658
  62. --
  63. --pull in our constant variables
  64. module(..., package.seeall)
  65. local const= require("constants")
  66. require("tile")
  67. require("sprite")
  68. require("entity")
  69. --need a player
  70. player = entity.new(0,0,100,100,5)
  71. player.settex(display.newImage("player.png",0,0))
  72. --put the constants into local vairables
  73. local TILE_WIDTH,TILE_HEIGHT,DEPTHX,DEPTHY,RUBYLIMIT,IRONLIMIT,SCREENWIDTH,SCREENHEIGHT = unpack(const,0,table.getn(const))
  74. --define the elements
  75. local IRON=0
  76. local BRONZE=1
  77. local COBALT=2
  78. local STEEL=3
  79. local RHODITE=4
  80. local GOLD=5
  81. local PLATNIUM=6
  82. local IRIDIUM=7
  83. local OPAL=8
  84. local AMETHYST=9
  85. local EMERALD=10
  86. local RUBY=11
  87. local SAPPHIRE=12
  88. local TOPAZ=13
  89. local DIAMOND=14
  90. local OBSIDIAN=15
  91. local DIRT=16
  92. local SPACE=17
  93. --tiles are 64x64 in 576x1216 image for 9x19=171 tiles
  94. local map = display.newGroup()
  95. local center = display.newGroup()
  96. player_offsetx=player.getx()
  97. player_offsety=player.gety()+60
  98. print(player.gettex())
  99. center:insert(player.gettex())
  100. map.xTiles = 200
  101. map.yTiles = 400
  102. map.tiles = {} -- array to hold tile ids without actually creating display objects yet
  103. map.tiles.health = {}
  104. map.tileWidth = 40
  105. map.tileHeight = 40
  106. map.tileSheet = sprite.newSpriteSheet("tiles.png", map.tileWidth, map.tileHeight)
  107. map.tileSet = sprite.newSpriteSet(map.tileSheet, 1, 18)
  108. map.xTilesInView = math.ceil((display.viewableContentWidth - 1) / map.tileWidth) + 2
  109. map.yTilesInView = math.ceil((display.viewableContentHeight - 1) / map.tileHeight) + 2
  110. map.xScroll = 0
  111. map.yScroll = 0
  112.  
  113. --populate the group with just enough objects to cover the entire screen
  114. for y = 0, map.yTilesInView - 1 do
  115.         for x = 0, map.xTilesInView - 1 do
  116.                 local tile = sprite.newSprite(map.tileSet)
  117.                 tile:setReferencePoint(display.TopLeftReferencePoint)
  118.                 tile.x = x * map.tileWidth
  119.                 tile.y = y * map.tileHeight
  120.                 tile.isVisible = false -- everything starts hidden
  121.                 map:insert(tile)
  122.         end
  123. end
  124.  
  125. -- iterate over the visible tiles with a callback
  126. function map.forTilesInView(f)
  127.         for y = 0, map.yTilesInView - 1 do
  128.                 for x = 0, map.xTilesInView - 1 do
  129.                   local tx = math.floor(map.xScroll / map.tileWidth) + x;
  130.                   local ty = math.floor(map.yScroll / map.tileHeight) + y;
  131.                         f(map[map.xTilesInView * y + x + 1], tx, ty)
  132.                 end
  133.         end
  134. end
  135.  
  136. -- align and update the display object grid based on the current scroll position
  137. function map.updateDisplay()
  138.   -- align the display object grid
  139.   map.x = -(map.xScroll % map.tileWidth)
  140.   map.y = -(map.yScroll % map.tileHeight)
  141.  
  142.   -- update the tile contents
  143.   map.forTilesInView(function(t, x, y)
  144.     if(x >= 0 and x < map.xTiles and y >= 0 and y < map.yTiles) then
  145.                         -- tile is inside the map
  146.                         t.isVisible = true
  147.                         t.currentFrame = map.tiles[y * map.xTiles + x + 1]
  148.                 else
  149.                         -- tile is off the edge of the map
  150.                         t.isVisible = false
  151.                 end
  152.         end)
  153. end
  154.  
  155. --init random function
  156. math.randomseed(os.time())
  157. math.random()        --bug in first use
  158.  
  159. function maybe(x)
  160.     if(x<0 or x>100) then
  161.         return false
  162.     else
  163.         if(x/5>=math.random(1,100)) then
  164.             return true
  165.         end
  166.         return false
  167.     end
  168. end
  169.  
  170. function distribute(y)
  171.     --this is for the int return
  172.     local rtn=DIRT
  173.     --start the cascade
  174.     if y<10 then
  175.         rtn = SPACE
  176.     elseif y<30 then
  177.         --3% chance of iron
  178.         if maybe(8) then
  179.             rtn=IRON
  180.         end
  181.         if maybe(5) then
  182.             rtn=OPAL
  183.         end
  184.     elseif y<60 then
  185.         if maybe(10) then
  186.             rtn=IRON
  187.         end
  188.         if maybe(1) then
  189.             rtn=OPAL
  190.         end
  191.         if maybe(1) then
  192.             rtn=BRONZE
  193.         end
  194.     elseif y<90 then
  195.         if maybe(10) then
  196.             rtn=IRON
  197.         end
  198.         if maybe(10) then
  199.             rtn=BRONZE
  200.         end
  201.         if maybe(5) then
  202.             rtn=COBALT
  203.         end
  204.         if maybe(3) then
  205.             rtn=OPAL
  206.         end
  207.     elseif y<120 then
  208.         if maybe(20) then
  209.             rtn=IRON
  210.         end
  211.         if maybe(20) then
  212.             rtn=BRONZE
  213.         end
  214.         if maybe(10) then
  215.             rtn=COBALT
  216.         end
  217.         if maybe(5) then
  218.             rtn=OPAL
  219.         end
  220.         if maybe(1) then
  221.             rtn=AMETHYST
  222.         end
  223.     elseif y<150 then
  224.         if maybe(25) then
  225.             rtn=IRON
  226.         end
  227.         if maybe(15) then
  228.             rtn=COBALT
  229.         end
  230.         if maybe(8) then
  231.             rtn=OPAL
  232.         end
  233.         if maybe(5) then
  234.             rtn=AMETHYST
  235.         end
  236.         if maybe(3) then
  237.             rtn=STEEL
  238.         end
  239.     elseif y<180 then
  240.         if maybe(20) then
  241.             rtn=COBALT
  242.         end
  243.         if maybe(10) then
  244.             rtn=STEEL
  245.         end
  246.         if maybe(8) then
  247.             rtn=AMETHYST
  248.         end
  249.         if maybe(1) then
  250.             rtn=EMERALD
  251.         end
  252.         if maybe(1) then
  253.             rtn=RUBY
  254.         end
  255.     elseif y<210 then
  256.         if maybe(20) then
  257.             rtn=STEEL
  258.         end
  259.         if maybe(5) then
  260.             rtn=EMERALD
  261.         end
  262.         if maybe(5) then
  263.             rtn=RUBY
  264.         end
  265.         if maybe(5) then
  266.             rtn=RHODITE
  267.         end
  268.         if maybe(1) then
  269.             rtn=SAPPHIRE
  270.         end
  271.     elseif y<240 then
  272.         if maybe(15) then
  273.             rtn=RHODITE
  274.         end
  275.         if maybe(6) then
  276.             rtn=RUBY
  277.         end
  278.         if maybe(5) then
  279.             rtn=SAPPHIRE
  280.         end
  281.         if maybe(2) then
  282.             rtn=PLATNIUM
  283.         end
  284.         if maybe(1) then
  285.             rtn=GOLD
  286.         end
  287.     elseif y<270 then
  288.         if maybe(12) then
  289.             rtn=GOLD
  290.         end
  291.         if maybe(10) then
  292.             rtn=PLATNIUM
  293.         end
  294.         if maybe(1) then
  295.             rtn=TOPAZ
  296.         end
  297.     elseif y<300 then
  298.         if maybe(20) then
  299.             rtn=GOLD
  300.         end
  301.         if maybe(15) then
  302.             rtn=PLATNIUM
  303.         end
  304.         if maybe(5) then
  305.             rtn=TOPAZ
  306.         end
  307.         if maybe(1) then
  308.             rtn=DIAMOND
  309.         end
  310.     elseif y<330 then
  311.         if maybe(10) then
  312.             rtn=GOLD
  313.         end
  314.         if maybe(10) then
  315.             rtn=PLATNIUM
  316.         end
  317.         if maybe(8) then
  318.             rtn=IRIDIUM
  319.         end
  320.         if maybe(5) then
  321.             rtn=DIAMOND
  322.         end
  323.         if maybe(2) then
  324.             rtn=OBSIDIAN
  325.         end
  326.     elseif y<360 then
  327.         if maybe(15) then
  328.             rtn=DIAMOND
  329.         end
  330.         if maybe(15) then
  331.             rtn=PLATNIUM
  332.         end
  333.         if maybe(10) then
  334.             rtn=IRIDIUM
  335.         end
  336.         if maybe(5) then
  337.             rtn=OBSIDIAN
  338.         end
  339.     else
  340.         if maybe(25) then
  341.             rtn=DIAMOND
  342.         end
  343.         if maybe(25) then
  344.             rtn=PLATNIUM
  345.         end
  346.         if maybe(15) then
  347.             rtn=IRIDIUM
  348.         end
  349.         if maybe(8) then
  350.             rtn=OBSIDIAN
  351.         end
  352.     end
  353.     --give back that number
  354.     return rtn
  355. end
  356.  
  357. local theNumber
  358. local angles = {360,90,180,270}
  359.  
  360. --randomly populate tile ids (normally this data would be loaded from a file)
  361. for y = 0, map.yTiles - 1 do
  362.         for x = 0, map.xTiles - 1 do
  363.           theNumber = distribute(y)+1
  364.           map.tiles[#map.tiles + 1] = theNumber
  365.           --map.tiles[#map.tiles + 1].tile.rotation = angles[math.random(4)]
  366.  
  367.         end
  368. end
  369.  
  370. -- center the map and display the visible tiles
  371. map.xScroll = 0
  372. map.yScroll = 0
  373. map.updateDisplay()
  374.  
  375. --move tile map
  376. local prevX, prevY
  377. function drag(event)
  378.     if event.phase == "began" then
  379.             prevX = event.x
  380.             prevY = event.y
  381.     elseif event.phase == "moved" then
  382.             map.xScroll = map.xScroll + prevX - event.x
  383.             map.yScroll = map.yScroll + prevY - event.y
  384.             map.updateDisplay()
  385.             prevX = event.x
  386.             prevY = event.y
  387.     end
  388. end
  389. --map:addEventListener("touch", drag)
  390.  
  391. function moveup(x)
  392.     --if(map.tiles[math.floor(center.y+x/TILE_HEIGHT) * map.xTiles + math.floor(center.x/TILE_WIDTH)]==SPACE+1) then
  393.     if(center.y>SCREENHEIGHT/2) then
  394.         --player.sety(player.gety()+x)
  395.         map.yScroll = map.yScroll + x
  396.     elseif(center.y<SCREENHEIGHT/2) then
  397.         center.y=center.y+x
  398.     elseif(center.y>map.yTiles*map.tileHeight-(SCREENHEIGHT/2)) then
  399.         center.y=SCREENHEIGHT/2+x
  400.     end
  401.     player_offsety = player_offsety + x
  402.     --lets check if our tile is dirt
  403.     if(tileon() == DIRT+1) then
  404.         if(map.tiles[math.floor(player_offsety/TILE_HEIGHT) * map.xTiles + math.floor(player_offsetx/TILE_WIDTH)+1].health > 0) then
  405.             map.tiles[math.floor(player_offsety/TILE_HEIGHT) * map.xTiles + math.floor(player_offsetx/TILE_WIDTH)+1].health = map.tiles[math.floor(player_offsety/TILE_HEIGHT) * map.xTiles + math.floor(player_offsetx/TILE_WIDTH)+1].health - 1
  406.         else
  407.             map.tiles[math.floor(player_offsety/TILE_HEIGHT) * map.xTiles + math.floor(player_offsetx/TILE_WIDTH)+1] = SPACE+1
  408.         end
  409.     end
  410.     --else
  411.         --print(map.tiles[math.floor(center.y/TILE_HEIGHT) * map.xTiles + math.floor(center.x/TILE_WIDTH)])
  412.     --end
  413.     map.updateDisplay()
  414. end
  415.  
  416. function moveleft(x)
  417.     if(center.x>SCREENWIDTH/2) then
  418.         --player.setx(player.getx()+x)
  419.         map.xScroll = map.xScroll + x
  420.     elseif(center.x<SCREENWIDTH/2) then
  421.         center.x=center.x+x
  422.     elseif(center.x>map.xTiles*map.tileWidth-(SCREENWIDTH/2)) then
  423.         center.x =SCREENWIDTH/2+x
  424.     end
  425.     player_offsetx = player_offsetx + x
  426.     map.updateDisplay()
  427. end
  428.  
  429. function abletogodown()
  430.     --this should take some time
  431.     if(tileon()==DIRT+1) then
  432.         map.tiles[math.floor(player_offsety/TILE_HEIGHT) * map.xTiles + math.floor(player_offsetx/TILE_WIDTH)+1]=SPACE+1
  433.         return true
  434.     end
  435.     return false
  436. end
  437.  
  438. function tick()
  439.     --make the player drop if he isn't on dirt.
  440.     if(tileon()==SPACE+1) then
  441.         if(center.y<SCREENWIDTH/2) then
  442.             center.y = center.y + 10
  443.         else
  444.             map.yScroll=map.yScroll+10
  445.         end
  446.         player_offsety = player_offsety + 10
  447.     end
  448.     --if the player is digging down, account for it.
  449.     --tileahead()
  450. end
  451. Runtime:addEventListener("enterFrame",tick)
  452.  
  453. --this function will get the tile the player is on
  454. function tileon()
  455.     return map.tiles[math.floor(player_offsety/TILE_HEIGHT) * map.xTiles + math.floor(player_offsetx/TILE_WIDTH)+1]
  456. end
  457.  
  458. function tileahead()
  459.     print(map.tiles[math.floor(player_offsety/TILE_HEIGHT) * map.xTiles + math.floor(player_offsetx/TILE_WIDTH)+1])
  460. end
  461. function tilebehind()
  462. end
  463.  
  464. --[[ printing thing
  465. for i=0,map.xTiles do
  466.     for j=0,map.yTiles do
  467.         print(map.tiles[j*map.xTiles+i])
  468.     end
  469.     print("\n")
  470. end
  471. --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement