Skip to content

Instantly share code, notes, and snippets.

@juaxix
Created July 16, 2012 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juaxix/3123491 to your computer and use it in GitHub Desktop.
Save juaxix/3123491 to your computer and use it in GitHub Desktop.
Wavespark from Processing to Lua (Codea - iPad)
-- 
-- xixgames.com
-- LGPL
-- basis
width  = WIDTH
height = HEIGHT
-- images
buffer   = image(width,height)
snapshot = image(width,height)
-- arrays
levelMap = {}
bonusQueue = {}
grindParticles = {}
checkpointParticles= {}
-- global constants and variables 
MODE_FREE_PLAY = 0
MODE_BONUS = 1
MODE_DISTANCE = 2
MODE_FIVEMINUTE = 3
gameMode = MODE_BONUS
-----
MOUNTAINS = 0
WAVES = 1
HILLS = 2
RAMPS = 3
terrain = WAVES
----
GAME_OVER = 0
GAMEPLAY = 1
TITLE_SCREEN = 2
gamestate = TITLE_SCREEN
----
clock = 0
CLOCK_CYCLE = 60
GAME_SCALE = 0.6
GRAVITY = 0.1 * math.sqrt(GAME_SCALE)
BOOST_GRAVITY = 0.7 * math.sqrt(GAME_SCALE)
BOOST_UP_GRAVITY = 0.2 * math.sqrt(GAME_SCALE)
CAM_MIN_Y = -200
CAM_MAX_Y = 100
MIN_X_SPEED = 4.0
FIRST_CHECKPOINT = 20000
CHECKPOINT_INCREMENT = 2500
nextCheckpoint = 1
lastCheckpoint = 1
checkpoints = 0
camPos = vec3(0,0,0)
playerPos= vec3(0,0,0)
playerVel= vec3(0,0,0)
grounded = false
locked = false
slogging = false
timerAir = 0
timerSlog = 0
maxAir = 0
maxSlog = 0
ouchCount = 0
sweetCount = 0
sweetChain = 0
sweetMax = 0
bonusCount = 0
exclaimString = 0
exclaimColor = color(0)
exclaimTimer = 0
EXCLAIM_TIME = 60
bonusTimer = 0
bonusStack = 0
BONUS_TIME = 120
BONUS_STAGGER = 10
bonusString = ""
AIR_BONUS_TIME = 120
AIR_BONUS_MULT = 2
topSpeed = 0.0
bonuses = 0
distance = 0.0
ready = false
CHECKPOINT_TIME = 60 * 46
gameTimer = 0
lastSegment = 1
level_xpos = 100 * GAME_SCALE
level_ypos = 0
level_lastSlope = 0
paused = false
gameOverTimer = 0
keyTimer = 0
touching = false
function reset()
  ready = false
  level_xpos = 100
  level_ypos = 0
  level_lastSlope = 1
  lastSegment = 1
  clock = 0
  
  playerPos = vec3(0, HEIGHT -200,0)
  playerVel = vec3(0,0,0)
  levelMap = {}
  grindParticles = {}
  checkpointParticles = {}
  grounded = false
  locked = false
  table.insert(levelMap,(vec3(-200, -200,0)))
  for i = 1,20 do
    Particle.addSegment()
  end 
  camPos = vec3(50, 0,0)
  grounded = false
  locked = false
  timerAir = 0
  timerSlog = 0
  maxAir = 0
  maxSlog = 0
  topSpeed = 0
  ouchCount = 0
  sweetCount = 0
  sweetChain = 0
  sweetMax = 0
  distance = 0
  bonuses = 0
  bonusCount = 0
  bonusString = ""
  bonusTimer = 0
  checkpoints = 0
  nextCheckpoint = FIRST_CHECKPOINT
  if(gameMode ~= MODE_FIVEMINUTE) then
    gameTimer = CHECKPOINT_TIME
  else 
        gameTimer = 121*60
  end
  bonusQueue = {}
  gameState = GAMEPLAY
  pause(false)
end
function setup()
  reset()
  
end
function ouch()
  ouchCount = ouchCount + 1
  exclaimString = "Ouch!"
  exclaimColor = color(255, 0, 0)
  exclaimTimer = EXCLAIM_TIME 
end
function sweet()
  sweetCount = sweetCount + 1
  sweetChain = sweetChain + 1
  sweetMax = math.max(sweetMax, sweetChain)
  exclaimString = "Sweet!"
  if(sweetChain > 1)  then
        exclaimString = exclaimString .. "\nx" .. sweetChain
  end
  exclaimColor = color(0, 192, 255)
  exclaimTimer = EXCLAIM_TIME 
  if(sweetChain == 1) then
    awardBonus("Sweet", 200*sweetChain)
  else
    awardBonus("Sweet Chain", 200*sweetChain)   
  end
  if(sweetChain == 5) then
    awardBonus("Dessert", 5000)
  end
end
-- Re-maps a number from one range to another. 
-- Numbers outside the range are not clamped to 0 and 1, because out-of-range values are often intentional and useful.
-- Adapted processing function:
function map(value,istart, istop, ostart, ostop) 
        return (
                ostart + 
                (ostop - ostart) * 
                ((value - istart) / (istop - istart))
        )
end
 
function drawParticles()
  local i = 0
  for i,p in ipairs(grindParticles) do
    p.pos = p.pos + p.vel
    fill(color(255,255, math.random(255)))
    noStroke()
    ellipse(p.pos.x, p.pos.y, 4, 4)
    p.life = p.life - 1
    if(p.life <= 0) then
        table.remove(grindParticles,i)
    end
  end
  for i,p in ipairs(checkpointParticles) do
    p.pos = p.pos + p.vel
    --colorMode(HSB) --??
    fill((clock*30)%255, 128, 255)
    --colorMode(RGB) --?? backing?
    
    noStroke()
    local rad =  map(p.life, p.maxlife, 0, 7, 0)
    ellipse(p.pos.x, p.pos.y, rad, rad)
    p.life = p.life - 1
    if(p.life <= 0) then
        table.remove(checkpointParticles,i)
    end
  end
end
function playerPhysics()
  if(touching) then
    playerVel.y = playerVel.y + BOOST_GRAVITY
    if(playerVel.y <0) then
        playerVel.y = playerVel.y + BOOST_UP_GRAVITY
    end
  else 
        playerVel.y = playerVel.y + GRAVITY
  end
  if(grounded and (playerVel:len() <= MIN_X_SPEED or playerVel.x <= 0)) 
  then
    playerVel:normalize()
    playerVel = playerVel * MIN_X_SPEED
    if(playerVel.x <= 0) then
        playerVel = playerVel * -1
    end
    slogging = true
  else 
        slogging = false
  end
  --find collision segment
  playerPos = playerPos + playerVel
  local levelSegmentIndex = lastSegment+1
--print(levelSegmentIndex.."  "..(#levelMap)..levelMap[1].x..","..playerPos.z)
  while(
        levelSegmentIndex<((#levelMap)) and 
        levelMap[levelSegmentIndex] ~= nil and
        levelMap[levelSegmentIndex].x ~= nil and
        levelMap[levelSegmentIndex].x < playerPos.x
    )do
    --print(levelSegmentIndex)
    levelSegmentIndex = levelSegmentIndex + 1
    
  end
  levelSegmentIndex = levelSegmentIndex - 1
  local origin = levelMap[levelSegmentIndex]
  local segment = levelMap[levelSegmentIndex+1] - origin
  local toPlayer = playerPos - origin
  local c = toPlayer:cross(segment)
  local d = playerVel:cross(segment)
  if((c.z < 0 and d.z < 0) or locked) then
    local proj = toPlayer:dot(segment) / segment:dot(segment)
    playerPos = origin + (segment * proj)
    segment:normalize()
    if(grounded and levelSegmentIndex ~= lastSegment) then
        playerVel = segment * playerVel:len()
    else
    --print("----")
--print(segment)
--print("----")
      if(vec2(segment.x,segment.y):angleBetween(vec2(playerVel.x,playerVel.y)) > math.pi * 0.4 
      --if(segment:angleBetween(playerVel) > math.pi * 0.4 
        and timerAir > 3)
      then
        ouch()
        sweetChain = 0
        if(playerVel:len() > 20) then awardBonus("Pancake", 500) end
      elseif(not grounded) then
          if(   
                        vec2(segment.x,segment.y):angleBetween(vec2(playerVel.x,playerVel.y))
                         < math.pi * 0.2 and 
                        playerVel.y > 12
                ) then 
                        sweet()
          else 
                        sweetChain = 0
          end
      end
      playerVel = segment * segment:dot(playerVel)
    end
    playerVel = playerVel *0.999
    grounded = true
  else 
        grounded = false
  end
  if(grounded and touching) then 
        locked = true
  else 
        locked = false
  end
  lastSegment = levelSegmentIndex
  topSpeed = math.max(topSpeed, playerVel.x)
end
function drawPlayer()
  if(touching) then
    fill(0, 255, clock%5*50)
  else
    fill(0, clock%5*50, 255)
  end
  noStroke()
  ellipse(playerPos.x, playerPos.y, 15, 15)
  if(not slogging) then
    fill(clock%5*25+128, 255, 255)
  else 
        fill(clock%5*25+128-70, 255-70, 255-70)
  end
  ellipse(playerPos.x, playerPos.y, 13, 13)
  if(playerPos.y < camPos.y + 80) then
    local indicator = vec3(camPos.x + width/2 - 40, camPos.y + 70,0)
    noFill()
    stroke(clock%5*25+128, 255, 255, 
        map(playerPos.y, camPos.y + 80, camPos.y, 0, 255)
    )
    strokeWidth(3)
    local to = playerPos - indicator
    to:normalize()
    to = (to *25) + indicator
    line(indicator.x, indicator.y, to.x, to.y)
    local alt_radius = (
        math.floor(map(playerPos.y, camPos.y, camPos.y-height, 0, 50))
    )%50
    stroke(clock%5*25+128, 255, 255, map(alt_radius, 0, 50, 255, 0))
    if(playerPos.y < camPos.y) then
        ellipse(indicator.x, indicator.y, alt_radius, alt_radius)
    end
  end
end
function drawTerrain()
  local levelSegmentIndex = 1
  stroke(color(255, 230, 200))
  strokeWidth(3)
  fill(color(50, 20, 60, 192))
  
  while(levelMap[levelSegmentIndex]~=nil and levelMap[levelSegmentIndex].x < 
    camPos.x)  do
    levelSegmentIndex = levelSegmentIndex + 1
  end
  local terrain_mesh = mesh()
  levelSegmentIndex = levelSegmentIndex - 1
  
  table.insert(terrain_mesh.vertices,levelMap[levelSegmentIndex])
  --vertex(v.x, v.y)
  while(
        levelMap[levelSegmentIndex].x < camPos.x+width and 
        #levelMap > levelSegmentIndex+1
  )
  do
    levelSegmentIndex = levelSegmentIndex + 1
    table.insert(terrain_mesh.vertices,levelMap[levelSegmentIndex])
    --vertex(v.x, v.y)  
  end
  table.insert(terrain_mesh.vertices,vec3(camPos.x+width+50, 600,0))
  table.insert(terrain_mesh.vertices,vec3(camPos.x-50, 600,0))
  --endShape(CLOSE)
  terrain_mesh:draw()
end
function doCheckpoints()
  if(playerPos.x > nextCheckpoint) then
    for i=0,HEIGHT,5 do
      local pos = vec3(nextCheckpoint, camPos.y+i,0)
      local distance = pos:dist(playerPos)
      table.insert(checkpointParticles,
        Particle(pos, 
         Particle.randomVector(5) + playerVel * 3/(distance/100+1),
         120
        )
      )
    end
    awardBonus("Checkpoint "..(checkpoints+1), (checkpoints+4)*250)
    if(gameTimer > 60) then
        awardBonus("Punctuality", math.floor(gameTimer * (FIRST_CHECKPOINT + checkpoints * CHECKPOINT_INCREMENT)/CHECKPOINT_TIME /10))
    else 
        awardBonus("Panic", 2000)
    end
    checkpoints = checkpoints + 1
    lastCheckpoint = nextCheckpoint
    nextCheckpoint = nextCheckpoint + FIRST_CHECKPOINT + checkpoints * CHECKPOINT_INCREMENT
    gameTimer = CHECKPOINT_TIME
    if(playerPos.y < CAM_MIN_Y) then
        awardBonus("Flagpole", 2000)
    end
    
  end
  --colorMode(HSB)
  strokeWidth(5)
  stroke(color((clock*30)%255, 128, 255))
  if(camPos.x+width > nextCheckpoint) then
        line(nextCheckpoint, camPos.y, nextCheckpoint, camPos.y+height)
  end
  local check_distance_limit = 3000
  if(camPos.x+width > nextCheckpoint - check_distance_limit and camPos.x+width < nextCheckpoint) then
    local distto = (nextCheckpoint - (camPos.x+width))/check_distance_limit
    stroke((clock*30)%255, 128, 255, map(distto, 1, 0, 0, 128))
    noFill()
    ellipse(camPos.x+width, camPos.y+height/2, distto*300, distto*300)
    noStroke()
    fill((clock*30)%255, 128, 255, map(distto, 1, 0, 0, 128))
    triangle(camPos.x+width - distto*170, camPos.y+height/2, camPos.x+width - 30 - distto*170, camPos.y+height/2 - 30, camPos.x+width - 30 - distto*170, camPos.y+height/2 + 30)
  end
  --colorMode(RGB)
end
--A triangle is a plane created by connecting three points. The first two arguments specify the first point, the middle two arguments specify the second point, and the last two arguments specify the third point.
function triangle(x1, y1, x2, y2, x3, y3)
        local triangleVertices = triangulate({x1, y1, x2, y2, x3, y3})
        local triangle_mesh = mesh()
        triangle_mesh.vertices = triangleVertices
        triangle_mesh:draw()
end
function pruneSegments()
  while(lastSegment > 10) do
    table.remove(levelMap,0)
    lastSegment = lastSegment - 1
    while(#levelMap < 40) do
      Particle.addSegment()
    end
  end
end
function doTimers()
  if(not grounded) then 
        timerAir = timerAir + 1
  else 
    maxAir = math.max(timerAir, maxAir)
  end
  if(timerAir > AIR_BONUS_TIME) then
        awardBonus("Air", (timerAir-AIR_BONUS_TIME)*AIR_BONUS_MULT)
    timerAir = 0
  end
  if(slogging) then
        timerSlog = timerSlog + 1
  else 
    maxSlog = math.max(timerSlog, maxSlog)
    timerSlog = 0
  end
  fontSize(32)
  font("Georgia")
  textWrapWidth(70)
  --textFont(font, 32)
  textAlign(LEFT)
  if(timerAir > 2) then
    fill(color(0, 192, 255, timerAir/60.0*255 - 128))
    stroke(0)
    text("Air Time: "..(timerAir/60).."."..(timerAir/6)%10, 5, 50)
  else
    fill(color(255, 32, 32, timerSlog/60.0*255 - 128))
    stroke(0)
    text("Slog Time: "..(timerSlog/60) .."." ..(timerSlog/6)%10, 5, 50)
  end
  gameTimer = gameTimer - 1
  exclaimTimer = exclaimTimer - 1
  --textFont(font, 48)
  fontSize(48)
  textAlign(CENTER)
  if(
        gameTimer/60 > CHECKPOINT_TIME/60 - 2 and 
        (gameTimer/3)%3==0 and gameMode ~= MODE_FIVEMINUTE
  ) then
        fill(color(255, 255, 0))
  elseif(gameTimer/60 < 6 and (gameTimer/3)%3==0) then
        fill(color(255, 0, 0))
  else 
        fill(255)
  end
  text(""..(gameTimer/60), width/2, 8)
  if(gameTimer/60 <= 6 and (gameTimer%60)==0) then
        -- timer sound!..
  end
  if(bonusTimer <= 0 and #bonusQueue==0) then
    bonusString = ""
    bonusStack = 0
  end
  
  
  if(#bonusQueue>0 and bonusTimer < BONUS_TIME - BONUS_STAGGER)
  then
        bonusTimer = BONUS_TIME
        --bonus sound!
        --bonusString = bonusString .. bonusQueue:remove(0).."\n"
  end
  textAlign(RIGHT)
  fill(color(255, 255, 255.0/BONUS_TIME*bonusTimer))
  --textFont(font, 24)
  fontSize(24)
  text(bonusString, width - 5, 5)
  
  bonusTimer = bonusTimer - 1
  if(gameTimer <= 0) then gameOver() end
end
function awardBonus(reason, amount)
  if(gameMode == MODE_BONUS or gameMode == MODE_FIVEMINUTE)
  then
    bonusCount = bonusCount + 1
    bonuses = bonuses + amount
    table.insert(bonusQueue,reason .. " Bonus   +"..amount)
    bonusStack = bonusStack + 1
    if(bonusStack == 5) then
        awardBonus("Bonus", 5000)
    end
    
  end
end
function drawProgress()
  stroke(255, 128)
  strokeWidth(2)
  noFill()
  ellipse(WIDTH/2-200, HEIGHT-20, 10, 10) 
  fill(255, 128)
  ellipse(width/2+200, height-20, 10, 10) 
  noStroke()
  fill(color(255, 0, 0, 128))
  local timeGhost = map(gameTimer, CHECKPOINT_TIME, 0, -200, 200)
  ellipse(width/2+timeGhost, height-20, 7, 7) 
  fill(color(128, 255, 255, 128))
  local progress = map(playerPos.x, lastCheckpoint, nextCheckpoint, -200, 200)
  ellipse(width/2+progress, height-20, 7, 7) 
end
function drawExclamation()
  if(exclaimTimer >= EXCLAIM_TIME - 6 and exclaimTimer%2==0) then
        fill(255)
  else 
       -- fill(exclaimColor, 255.0/EXCLAIM_TIME*exclaimTimer)
    fill(255, 0, 0, 255)
  end
  if(exclaimTimer > 0) then
    --textFont(font, 48)
    fontSize(48)
    textAlign(CENTER)
    text(exclaimString, width/2, height/2) 
  end
end
function pause(toPause)
  paused = toPause
  if(paused) then
    --capScreen()
  else 
        -- volumen mute...
  end
end
--- Codea do this for us :)
function capScreen()
        --[[
  snapshot = image(width, height)
  -- processing -> snapshot.loadPixels()
  for(i=0 , pixels.length)
  {
    snapshot.pixels[i] = pixels[i]
  } 
  snapshot.updatePixels()
  ]]--
end
function gameOver()
  keyTimer = 0
  --capScreen()
  gameState = GAME_OVER
  -- game over sound?
  gameOverTimer = 0
end
function titleScreen()
  keyTimer = 0
  gameState = TITLE_SCREEN
  -- draw main menu...
end
function draw()
  background(0)
  strokeWidth(6)
  keyTimer = keyTimer + 1
  if(gameState == GAMEPLAY) then
    if(paused) then
      --image(snapshot, 0, 0) --> processing?
      noStroke()
      fill(0, 32)
      rect(0, 0, width, height)
      fill(255)
      textAlign(CENTER)
      --textFont(font, 48)
      fontSize(48)
      text("Paused", width/2, height/2)
    else
      pushMatrix()
      playerPhysics()
      pruneSegments()
      if(grounded and not slogging) then
        local passBand = map(playerVel:len(), 0, 30, 300, 10000)
        passBand = math.max(passBand, 50)
        local l  = vec2(playerVel.x,playerVel.y):len()*0.2
        
        table.insert(grindParticles,
                Particle(
                        playerPos, 
                        Particle.randomVector(
                         l
                        ),20
                )
        )
      else 
                        camPos.x = playerPos.x - 70
                        
                        camPos.y = math.min(
                            math.max(playerPos.y/2 - HEIGHT/3, CAM_MIN_Y),
                        CAM_MAX_Y)
                        if camPos.y == nil then
                            camPos.y = CAM_MIN_Y
                        end
                        --sprite with background here! :)
              --image(bg, 0, - map(camPos.y, CAM_MIN_Y, CAM_MAX_Y, 0, 1) * (bg.height-height))
      translate(-camPos.x, -camPos.y)
      drawTerrain()  
      drawParticles()
      drawPlayer()
      if(gameMode ~= MODE_FREE_PLAY) then
        if(gameMode ~= MODE_FIVEMINUTE) then
          doCheckpoints()
        end
        translate(camPos.x, camPos.y)
        doTimers()
        if(gameMode ~= MODE_FIVEMINUTE) then
          drawProgress()
        end
        drawExclamation()
        distance = (playerPos.x)/10
        --textFont(font, 32)
        fontSize(32)
        textAlign(LEFT)
        fill(255)
        if(gameMode == MODE_BONUS) then
          text("Score: "..(bonuses), 5, 5)
        elseif(gameMode == MODE_DISTANCE) then
          text("Score: "..math.floor(distance), 5, 5)
        else 
                text("Score: "..math.floor(distance + bonuses), 5, 5)
        end
      end
      popMatrix()
      local fadeTime = 20
      if(clock < fadeTime) then
                        noStroke()
                        fill(0)
                        triangle(0, 0, width, 0, width, height*map(clock, 0, fadeTime, 1, 0))
                triangle(width, height, 0, height, 0, 
                        height*map(clock, 0, fadeTime, 0, 1)
                )
      end
      clock = clock + 1
    end 
   end
  end
  -- check game over now:
  if(gameState == GAME_OVER) then
    --image(snapshot, 0, 0) --> processing
    -- sprite here
    fill(0, 128)
    noStroke()
    rect(0, 0, width, height)
    fill(255)
    --textFont(font, 48)
    fontSize(48)
    gameOverTimer = gameOverTimer + 1
    textAlign(CENTER)
    text("Game Over", width/2,  30+math.max(500-gameOverTimer, 0))
    --textFont(font, 32)
    fontSize(32)
    textAlign(LEFT)
    local score = 0
    if(gameMode == MODE_BONUS) then 
        score = bonuses
    elseif(gameMode == MODE_DISTANCE) then 
        score = math.floor(playerPos.x/10)
    elseif(gameMode == MODE_FIVEMINUTE) then 
        score = math.floor(bonuses + playerPos.x/10)
    end
    text("Distance traveled: "..math.floor(playerPos.x/10 ), 20,
             80+math.max(600-gameOverTimer, 0))
    text("Bonus points earned: "..bonuses, 20, 120+math.max(700-gameOverTimer, 0))
    text("TOTAL SCORE: "..score, 20, 160+math.max(800-gameOverTimer, 0))
    strokeWidth(3)
    stroke(255)
    line(20, 195+math.max(900-gameOverTimer, 0), width-20, 195+math.max(900-gameOverTimer, 0))
    --textFont(font, 24)
    fontSize(24)
    fill(255, (gameOverTimer - 900)*3)
    text("Best Air: "..(maxAir/60).."."..(maxAir/6)%10, 20, 210)
    text("Worst Slog: "..(maxSlog/60)+"."..(maxSlog/6)%10, width/2, 210) 
    fill(255, (gameOverTimer - 950)*3)
    text("Sweet: "..sweetCount, 20, 235)
    text("Ouch: "..ouchCount, width/2, 235)
    fill(255, (gameOverTimer - 1000)*3)
    text("Best Sweet Chain: x"..sweetMax, 20, 260)
    text("Bonuses Earned: "..bonusCount, width/2, 260) 
    fill(255, (gameOverTimer - 1050)*3)
    text("Checkpoints: "..checkpoints, 20, 285)
    text("Top Speed: "..math.floor(topSpeed), width/2, 285) 
    fill(255, (gameOverTimer - 1200)*3)
    textAlign(CENTER)
    text("Press Any Key", width/2, 400)
  elseif(gameState == TITLE_SCREEN) then
    --image(bg, 0, (height-bg.height)/2)
    -- draw BG Sprite in Codea!:
    -- ...
    fill(0, 128)
    noStroke()
        strokeWidth(3)
    rect(-5, 128, width+10, 55)
    fill(255)
    --textFont(font, 48)
    fontSize(48)
    textAlign(CENTER)
    smooth()
    text("Wavespark", width/2, 140)
    fontSize(24)
    --textFont(font, 24)
    text("Select Game Mode:\n\nD - Distance\nB - Bonus\nT - Time Attack\nF - Free Play\n\nP to pause       Q to end game", width/2, 250)
    noSmooth()
  end
end
--[[
function keyPressed()
  if(key == 'p' and gameState == GAMEPLAY) pause(not paused)
  if(key == 'q' and gameState == GAMEPLAY and not paused) gameOver()
  if(gameState == GAME_OVER and gameOverTimer < 2000 and keyTimer > 30)
       { gameOverTimer = 2000 keyTimer = 0}
  if(gameState == GAME_OVER and gameOverTimer > 1200 and keyTimer > 30) titleScreen()
  if(gameState == TITLE_SCREEN and keyTimer > 30)
  then
    if(key == 'f') {gameMode = MODE_FREE_PLAY reset()}
    if(key == 't') {gameMode = MODE_FIVEMINUTE reset()}
    if(key == 'b') {gameMode = MODE_BONUS reset()}
    if(key == 'd') {gameMode = MODE_DISTANCE reset()}
    
  end
end
]]--
function touched(touch)
        if touch.state == ENDED then
                touching = false
                
        else
                touching = true
        end
end
-- Particle class
Particle = class()
-- xixgames.com
-- LGPL
function Particle:init(p, v, lifetime)
    self.pos = p
    self.vel = v
    self.life = lifetime
    self.maxlife = lifetime
    --print(self.pos)
end
function Particle:randomVector(magnitude)
  local dir = math.random(math.pi*2)
if magnitude == nil then magnitude = 5 end
  return vec3(math.cos(dir)*magnitude, math.sin(dir)*magnitude,0)
end
function Particle:addSegment()
  if(terrain == WAVES) then
    level_xpos = level_xpos + 200*GAME_SCALE
    if(level_ypos < 80) then
        level_lastSlope = 30 + math.random(80)
    elseif (level_lastSlope < -10 and math.random(5)<1 and level_ypos < 300) then
        level_lastSlope = math.random(50)+30
    elseif(level_ypos > 350) then
        level_lastSlope = -30
    elseif(level_lastSlope < -70) then
        level_lastSlope = 40
    else 
        level_lastSlope = level_lastSlope - math.random(50)+10
    end
    level_ypos = level_ypos + level_lastSlope
    table.insert(levelMap, vec3(level_xpos , level_ypos,0))
  end
  if(terrain == MOUNTAINS) then
    level_xpos = level_xpos + 100*GAME_SCALE+
        math.floor(math.random(300*GAME_SCALE))
    table.insert(levelMap,vec3(
        level_xpos, 
        math.random(200*GAME_SCALE)+
        math.random(200*GAME_SCALE)+
        80*GAME_SCALE
        ,0)
    ) 
  end
  if(terrain == HILLS) then
    level_xpos = level_xpos + 300*GAME_SCALE
    level_lastSlope = math.random(200)-100 - (level_ypos-250)
    level_ypos = level_ypos + level_lastSlope
    table.insert(levelMap,vec3(level_xpos, level_ypos,0))
  end
  if(terrain == RAMPS) then
    level_xpos = level_xpos + 100*GAME_SCALE
    level_ypos = level_ypos + 100*GAME_SCALE
    table.insert(levelMap,vec3(level_xpos, level_ypos,0)) 
    level_xpos = level_xpos + -0*GAME_SCALE
    level_ypos = level_ypos + -100*GAME_SCALE
    table.insert(levelMap,vec3(level_xpos, level_ypos,0))
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment