Advertisement
Juaxix

Abudction code for Codea 1/4 - Main

Nov 3rd, 2011
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.36 KB | None | 0 0
  1. -- Main code
  2.  
  3.  
  4. -- Use this function to perform your initial setup
  5. function setup()
  6.     ships   = {}
  7.     persons = {}
  8.     time    = 0
  9.     seedSpd = 0.6 -- people generation speed
  10.     bgLines = StreamingLines()
  11.     bgLines.spawnRate = 1
  12.     saves   = 0
  13.     score   = 0
  14.     maxpers = 3
  15.     print("Use the UFO to get people to a safe planet")
  16. end
  17.  
  18. function generatePerson()
  19.     local l= math.random(1,3)
  20.     local x,y,a = 0,0,0
  21.     if l == 1 then
  22.         -- left
  23.         x = (WIDTH/2)-math.random(111,123)
  24.         y = math.random(111,123)
  25.         a = 45
  26.     elseif l == 2 then
  27.         -- right
  28.         x = (WIDTH/2) + math.random(179,217)
  29.         y = math.random(111,123)
  30.         a = 315
  31.     else
  32.         -- center
  33.         x = math.random((WIDTH/2)-11,(WIDTH/2)+11)
  34.         y = math.random(200,223)
  35.     end
  36.     local person = Person(x,y,a)
  37.     table.insert(persons,person)
  38.     sound(SOUND_EXPLODE)
  39. end
  40.  
  41. function updatePersons()
  42.     for i,person in ipairs(persons) do
  43.         local ship = ships[person.shipId]
  44.         if person.state == PERSON_SHIP then
  45.              -- move the person with the ship
  46.             if ship == nil then
  47.                 person.state = PERSON_FALL
  48.             else
  49.                 person.x = ship.x - 6
  50.                 person.y = ship.y - 23
  51.                 if math.abs(person.x - (WIDTH/2) -6)<66 and
  52.                     math.abs(person.y-(HEIGHT-133))<33
  53.                 then
  54.                     person.state = PERSON_SAFE
  55.                     person.timeToGo = 6
  56.                     sound(SOUND_HIT)
  57.                 end
  58.             end
  59.         elseif person.state == PERSON_SAFE then
  60.             person.timeToGo = person.timeToGo - 1/32
  61.             if person.timeToGo<=0 then
  62.                 person.state = PERSON_BYE
  63.                 person.timeToGo = 3
  64.                 score = score + person.points
  65.                 sound(SOUND_PICKUP)
  66.                 saves = saves + 1
  67.             end
  68.         elseif person.state == PERSON_BYE then
  69.             person.timeToGo = person.timeToGo - 1/32
  70.             if person.timeToGo<=0 then
  71.                 table.remove(persons, i)
  72.             end
  73.         elseif person.state == PERSON_BORN then
  74.             person.timeToGo = person.timeToGo - 1/32
  75.             if person.timeToGo<=0 then
  76.                 person.state = PERSON_LOST
  77.             end
  78.         elseif person.state == PERSON_LEVI then
  79.             local goDown = false
  80.             if ship == nil then
  81.                 goDown = true
  82.             elseif ship.isFiring then
  83.                 if not(math.abs(ship.x - person.x)<23)  then
  84.                     goDown = true
  85.                 end
  86.             else
  87.                 goDown = true
  88.             end
  89.             if goDown then
  90.                 person.state = PERSON_FALL
  91.                 person.y = person.y - 1.2
  92.             end
  93.         elseif person.state == PERSON_FALL then
  94.             person.y = person.y - 1
  95.             if person.y < 111 then
  96.                 if person.x > ((WIDTH/2)+ 222) or
  97.                    person.x < ((WIDTH/2)- 222) then
  98.                     -- die
  99.                     person.state = PERSON_KILL
  100.                     person.timeToGo = 2.3
  101.                     score = score - person.points
  102.                     saves = saves - 1
  103.                     sound(SOUND_SHOOT)
  104.                 else
  105.                     person.state = PERSON_LOST
  106.                 end
  107.             end
  108.         elseif person.state == PERSON_KILL then
  109.             person.timeToGo = person.timeToGo - 1/32
  110.             if person.timeToGo<=0 then
  111.                 table.remove(persons,i)
  112.             end
  113.         end
  114.     end -- for persons
  115. end
  116.  
  117. function checkAbduction(k,ship)
  118.     for i,person in ipairs(persons) do
  119.         if person.state == PERSON_LOST or person.state == PERSON_LEVI or
  120.            person.state == PERSON_FALL
  121.         then
  122.          if math.abs(ship.x - person.x)<23 and person.y<(HEIGHT/2)+33 then
  123.             --print ("abduction")
  124.             person.shipId = k
  125.             person.y = person.y + 0.6
  126.             if person.angle ~= 0 then
  127.                 person.angle = 0
  128.             end
  129.             person.state = PERSON_LEVI --levitation
  130.             if math.abs(ship.y - person.y)<23 then
  131.                 person.state = PERSON_SHIP
  132.                 sound(SOUND_BLIT)
  133.             end
  134.          end
  135.         end
  136.     end
  137. end
  138.  
  139. -- This function gets called once every frame
  140. function draw()
  141.     for k,ship in pairs(ships) do
  142.      if ship.isFiring==true then
  143.         checkAbduction(k,ship)
  144.      end
  145.     end
  146.     updatePersons()
  147.     if time <= 0 and table.maxn(persons)<maxpers then
  148.         time = seedSpd * 100
  149.         if table.maxn(persons)<3 then
  150.             generatePerson()
  151.         end
  152.     else
  153.         time = time - seedSpd
  154.     end
  155.     background(0, 0, 0, 255)
  156.     bgLines:update()
  157.     bgLines:draw()
  158.     -- planet
  159.     ellipse(WIDTH/2,0,HEIGHT/2,HEIGHT/2)
  160.     sprite("SpaceCute:Planet", WIDTH/2, HEIGHT - 200)
  161.     for i,p in ipairs(persons) do
  162.         p:draw()
  163.     end
  164.     for k,ship in pairs(ships) do
  165.         ship:draw()
  166.     end
  167.     noSmooth()
  168.     stroke(255, 255, 255, 255)
  169.     strokeWidth(2)
  170.     number(6, HEIGHT - 6, score, 10)
  171.     sprite("Planet Cute:Star",WIDTH-45,HEIGHT - 10,60)
  172.     number(WIDTH - 33, HEIGHT - 10, "x"..saves, 8)
  173. end
  174.  
  175. function touched(touch)
  176.    -- if touch.tapCount ~= 1 then return nil end
  177.     if ships[touch.id]==nil then
  178.         ships[touch.id] = Ship(WIDTH/2,HEIGHT-111)
  179.     end
  180.     if touch.x>23 and touch.x< WIDTH - 23 then
  181.         ships[touch.id].x = touch.x
  182.     end
  183.     if touch.y >279 then
  184.         ships[touch.id].y = touch.y
  185.     else
  186.         ships[touch.id].y = 279
  187.     end
  188.     if touch.state == BEGAN or touch.state == MOVING  then
  189.         if touch.y < 444 then
  190.             ships[touch.id].isFiring = true
  191.         else
  192.             ships[touch.id].isFiring = false
  193.         end
  194.     elseif touch.state == ENDED then
  195.         --ships[touch.id].isFiring = false
  196.         ships[touch.id] = nil
  197.     end
  198.     --print (touch.y)
  199.    
  200. end
  201.  
  202. -----------------------------------
  203. -- Functions for drawing numbers --
  204. -----------------------------------
  205.  
  206.  
  207. -- Draw a number. x, y is top left
  208. function number(x, y, n, w)
  209.     l = string.len(n)
  210.     for i = 1, l do
  211.         drawDigit(x + ((i - 1) * (w * 1.5)), y, string.sub(n, i, i), w)
  212.     end
  213. end
  214.  
  215. -- Draw a single digit
  216. function drawDigit(x, y, n, w)
  217.     h = 2 * w
  218.     if string.match(n, "1") then
  219.         line(x + (w / 2), y, x + (w / 2), y - h)
  220.     elseif string.match(n, "2") then
  221.         line(x, y, x + w, y)
  222.         line(x + w, y, x + w, y - (h / 2))
  223.         line(x + w, y - (h / 2), x, y - (h / 2))
  224.         line(x, y - (h / 2), x, y - h)
  225.         line(x, y - h, x + w, y - h)
  226.     elseif string.match(n, "3") then
  227.         line(x, y, x + w, y)
  228.         line(x + w, y, x + w, y - h)
  229.         line(x + w, y - h, x, y - h)
  230.         line(x, y - (h / 2), x + w, y - (h / 2))
  231.     elseif string.match(n, "4") then
  232.         line(x, y, x, y - (h / 2))
  233.         line(x, y - (h / 2), x + w, y - (h / 2))
  234.         line(x + w, y, x + w, y - h)
  235.     elseif string.match(n, "5") then
  236.         line(x + w, y, x, y)
  237.         line(x, y, x, y - (h / 2))
  238.         line(x, y - (h / 2), x + w, y - (h / 2))
  239.         line(x + w, y - (h / 2), x + w, y - h)
  240.         line(x + w, y - h, x, y - h)
  241.     elseif string.match(n, "6") then
  242.         line(x + w, y, x, y)
  243.         line(x, y, x, y - h)
  244.         line(x, y - h, x + w, y - h)
  245.         line(x + w, y - h, x + w, y - (h / 2))
  246.         line(x + w, y - (h / 2), x, y - (h / 2))
  247.     elseif string.match(n, "7") then
  248.         line(x, y, x + w, y)
  249.         line(x + w, y, x + w, y - h)
  250.     elseif string.match(n, "8") then
  251.         line(x, y, x + w, y)
  252.         line(x + w, y, x + w, y - h)
  253.         line(x + w, y - h, x, y - h)
  254.         line(x, y - h, x, y)
  255.         line(x, y - (h / 2), x + w, y - (h / 2))
  256.     elseif string.match(n, "9") then
  257.         line(x + w, y - (h / 2), x, y - (h / 2))
  258.         line(x, y - (h / 2), x, y)
  259.         line(x, y, x + w, y)
  260.         line(x + w, y, x + w, y - h)
  261.         line(x + w, y - h, x, y - h)
  262.     elseif string.match(n, "0") then
  263.         line(x, y, x + w, y)
  264.         line(x + w, y, x + w, y - h)
  265.         line(x + w, y - h, x, y - h)
  266.         line(x, y - h, x, y)
  267.     elseif string.match(n, "x") then
  268.         line(x, y - (w / 3), x + w, y - (h + 1))
  269.         line(x + w, y - (w / 3), x, y - (h + 1))
  270.     end
  271. end
  272.  
  273.  
  274.  
  275.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement