Advertisement
Guest User

Untitled

a guest
Sep 11th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 28.35 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. --[[    ------------------------------------------------------------
  6.                            Main.lua
  7. --]]    ------------------------------------------------------------
  8.  
  9. -- global screen size constants
  10. bottomY = HEIGHT *.21
  11. trackWidth = WIDTH * .9 -- width of the track at the bottom
  12. horizonY = trackWidth * .55 + bottomY
  13. trackHeight = horizonY - bottomY
  14. trackLength = 100  -- in meters
  15.  
  16. -- main objects
  17. myCar = nil
  18. cars = {}
  19. day = nil
  20. track = nil
  21.  
  22. -- various global variables
  23. speed = 0                 -- for showing to the user
  24. location = 0              -- my location
  25. maxPosition = -1000       -- the location relative to me where the front-most car is
  26. lastCollisionT = -1000    -- last time i collided, for the penalty
  27. gameTime = 0              -- for reporting to the user
  28. startTime = 0             -- the time at which the game started
  29. accAngle = 0              -- gravity.z threshold for accelerating vs breaking
  30. days = 1                  -- which day we're on
  31. carsToPassInit = 150          -- on day 1 we need this many
  32. carsToPass = carsToPassInit   -- how many cars to advance to the next day
  33. gameOver = false
  34. gameStarted = false           -- whether the user has started the game
  35. gamePaused = false
  36. maxSpeed = 150
  37. regularSpeed = 85
  38.  
  39. function setup()
  40.     track = Track()
  41.     day = Day()
  42.     myCar = Car(0,0,0)
  43.     myCar.speed = 120
  44.    
  45.     watch("carsToPass")
  46.     watch("days")
  47.     watch("speed")
  48.     watch("location")
  49.  
  50.     print("Instructions:")
  51.     print("Angle fwd to accelerate")
  52.     print("Angle back to break")
  53.     print("Keep still to maintain speed")
  54.     print("Arrows to steer")
  55.     print("Touch the screen to start")
  56. end
  57.  
  58. function draw()
  59.     rectMode(CORNER)
  60.     ellipseMode(RADIUS)
  61.     speed = myCar.speed
  62.    
  63.     ------------- UPDATE GAME STATE ----------------
  64.     if not gameOver then
  65.         if not gameStarted or gamePaused then
  66.             stoppedUserInputs()
  67.         else
  68.             accelerating = runningUserInputs()     -- start, left, right, brake, etc
  69.            
  70.             -- move track around
  71.             turnTrack(gameTime,track)
  72.                
  73.             -- move the day forward in time
  74.             day:updateMode(gameTime)
  75.        
  76.             if gameStarted then
  77.                 gameTime = ElapsedTime - startTime
  78.                 location = location + DeltaTime*myCar.speed/100
  79.                 spawnNewCars()
  80.                 myCar:updateTireState()
  81.                 day:updateFogEnd(myCar.speed)
  82.             end
  83.        
  84.             -- if we're turning then apply the centrifugal force
  85.             centrifugal = track:centrifugal()
  86.             centrifugal = centrifugal * myCar.speed / 150
  87.             myCar.x = myCar.x + centrifugal/100
  88.             day:moveX(centrifugal*7)
  89.        
  90.             collided = checkForCollisions()
  91.             moveCars()
  92.         end
  93.     end
  94.  
  95.     --------------- DRAW ---------------
  96.     -- field, tracks, sky, controls, etc
  97.     drawBackground()
  98.    
  99.     --draw all cars
  100.     myCar:draw(track,day)
  101.     for i,c in ipairs(cars) do
  102.         if c.y > 0 and c.y <= .9*trackLength and c.real then
  103.             c:draw(track,day)
  104.         end
  105.     end
  106.    
  107.     day:drawFog()    -- has to be drawn last
  108.    
  109.     drawPauseButton()
  110.     drawUserFeedback(accelerating,collided)  -- userfedback at the bottom of the screen
  111. end
  112.  
  113. function moveCars()
  114.     maxPosition = -1000
  115.     for i,c in ipairs(cars) do
  116.         change = -DeltaTime*(myCar.speed-c.speed)/1.3
  117.         beforeY = c.y
  118.         c.y = c.y + change
  119.  
  120.         -- count carsPassed
  121.         if c.real then
  122.             if c.y<0 and beforeY>0 and not day.won then carsToPass = carsToPass - 1 end
  123.             if c.y>0 and beforeY<0 and not day.won then carsToPass = carsToPass + 1 end
  124.         end
  125.  
  126.         if carsToPass == 0 and not day.won then
  127.             day.won = true
  128.             print("Achievement unlocked: Day", days)
  129.         end
  130.  
  131.         -- remove cars
  132.         -- avoid collisions from the back by removing cars
  133.         if c.y>0 and beforeY<0 and collidesWithMyCar(c) then
  134.             table.remove(cars,i)
  135.         -- remove cars that are too far
  136.         elseif c.y < -5 * trackLength or c.y > 2 * trackLength then
  137.             table.remove(cars,i)
  138.         else c:updateTireState() end
  139.  
  140.         maxPosition = math.max(maxPosition,c.y)
  141.     end
  142. end
  143.  
  144. -- user inputs while the game is stopped
  145. function stoppedUserInputs()
  146.     if (CurrentTouch.state == BEGAN or CurrentTouch.state == MOVING ) then
  147.         if not gameStarted then
  148.             -- start game
  149.             gameStarted = true
  150.             startTime = ElapsedTime
  151.             accAngle = Gravity.z + .002
  152.         elseif gamePaused and pausePressed(CurrentTouch.x,CurrentTouch.y) then
  153.             gamePaused = false
  154.             accAngle = Gravity.z + .002
  155.         end
  156.     end
  157. end
  158.  
  159. -- user inputs while the game is running
  160. function runningUserInputs()
  161.     -- handle steering
  162.     if (CurrentTouch.state == BEGAN or CurrentTouch.state == MOVING ) then
  163.         if CurrentTouch.y < bottomY then
  164.             dx = .05
  165.             if day:inSnow() then dx = dx / 2 end
  166.             if CurrentTouch.x > .64 * WIDTH and CurrentTouch.x < .84 *WIDTH then
  167.                 -- go left
  168.                 myCar.x = myCar.x - dx
  169.             elseif CurrentTouch.x > .84 * WIDTH then
  170.                 -- go right
  171.                 myCar.x = myCar.x + dx
  172.             end
  173.         end
  174.        
  175.         if pausePressed(CurrentTouch.x,CurrentTouch.y) then gamePaused = true end
  176.     end
  177.  
  178.     -- handle acceleration
  179.     dz = Gravity.z - accAngle
  180.     if dz < -.03 then
  181.         myCar.speed = math.min(maxSpeed,myCar.speed+1)
  182.         return(1)
  183.     elseif dz > .08 then
  184.         myCar.speed = math.max(0,myCar.speed-3)
  185.         return(2)
  186.     end
  187.     return(3)
  188. end
  189.  
  190. lastPauseT = 0
  191. function pausePressed(x,y)
  192.     if ElapsedTime - lastPauseT < .1 then return(false) end
  193.     if x > WIDTH*.9 and y>HEIGHT*.9 then
  194.         lastPauseT = ElapsedTime
  195.         return(true)
  196.     else return(false) end
  197. end
  198.  
  199. -- turns the track left and right every now and then
  200. lastTurnT = 0
  201. function turnTrack(gameTime,track)
  202.     track:moveToTargetX()
  203.     if gameTime - lastTurnT > 5 then
  204.         track:newTargetX()
  205.         lastTurnT = gameTime
  206.     end
  207. end
  208.  
  209. function checkForCollisions()
  210.     -- with the walls
  211.     maxX = .75
  212.     if myCar.x > maxX or myCar.x < -maxX then
  213.         if myCar.x < 0 then
  214.             myCar.x = -maxX/1.2
  215.         else
  216.             myCar.x = maxX/1.2
  217.         end
  218.         myCar.speed = math.max(0,myCar.speed-maxSpeed/5)
  219.     end
  220.  
  221.     -- with other cars
  222.     for i, car in ipairs(cars) do
  223.         if collidesWithMyCar(car) then
  224.             lastCollisionT = ElapsedTime
  225.             break
  226.         end
  227.     end
  228.  
  229.     -- penalty for colliding for .5 seconds
  230.     if ElapsedTime - lastCollisionT < 1 then
  231.         myCar.speed = regularSpeed / 2
  232.         return(true)
  233.     else
  234.         return(false)
  235.     end
  236. end
  237.  
  238. -- check if this car collides with my car
  239. function collidesWithMyCar(car)
  240.     if not car.real then return(false) end
  241.     dx = math.abs(myCar.x - car.x)
  242.     dy = car.y - myCar.y
  243.     return(dx < 0.35 and dy > 0 and dy < 4)  -- based on car dimensions
  244. end
  245.  
  246. function drawBackground()
  247.     background(0, 0, 0, 255)
  248.  
  249.     -- field, track, sky, clouds
  250.     day:draw(track)
  251.  
  252.     -- botton
  253.     fill(0, 0, 0, 255)
  254.     rect(-5,0,WIDTH+10,bottomY)
  255.  
  256.     -- the left/right controls
  257.     fill(255, 255, 255, 255)
  258.     stroke(255, 255, 255, 255)
  259.     drawArrow(vec2(WIDTH*.82,bottomY*.5),180,WIDTH/750)
  260.     drawArrow(vec2(WIDTH*0.86,bottomY*.5),0,WIDTH/750)  
  261. end
  262.  
  263. function drawArrow(pos,orient,s)
  264.     pushMatrix()
  265.     resetMatrix()
  266.     pushStyle()
  267.     translate(pos.x,pos.y)
  268.     scale(s)
  269.     strokeWidth(13)
  270.     lineCapMode(PROJECT)
  271.     rotate(orient)
  272.     line(0, 0, 70,0)
  273.     line(73, 0, 50, 25)
  274.     line(73, 0,  50, -25)
  275.     popStyle()
  276.     popMatrix()
  277. end
  278.  
  279. function drawPauseButton()
  280.     pushMatrix()
  281.     translate(WIDTH*.93,HEIGHT*.93)
  282.     lineCapMode(PROJECT)
  283.     stroke(214, 214, 214, 255)
  284.     strokeWidth(10)
  285.     line(0,0,0,20)
  286.     line(12,0,12,20)
  287.     popMatrix()
  288. end
  289.  
  290. function drawUserFeedback(accelerating,collided)
  291.     if gameStarted and not gameOver then
  292.         if accelerating == 1 then
  293.             -- show green arrow forward
  294.             stroke(0, 255, 0, 255)
  295.             drawArrow(vec2(WIDTH*.1,bottomY*.25),90,WIDTH/750)
  296.         elseif accelerating == 2 then
  297.             -- show red arrow back
  298.             stroke(255,0,0,255)
  299.             drawArrow(vec2(WIDTH*.1,bottomY*.75),270,WIDTH/750)
  300.         end
  301.     end
  302.  
  303.     if collided then
  304.         strokeWidth(0)
  305.         fill(255, 243, 0, 255)
  306.         ellipse(WIDTH/2,bottomY*.5,bottomY*.25,bottomY*.25)
  307.     end
  308. end
  309.  
  310. -- why? because I was trying to get around the bug
  311. -- with sounds vs random and in the end I just didn't
  312. -- use sound
  313. function random()
  314.     return(math.random())
  315. end
  316.  
  317. --[[    ------------------------------------------------------------
  318.                            Track.lua
  319.  
  320.        Represents the track. Knows which way the horizon is pointing,
  321.        and how to draw tracks (both straight and curved)
  322.  
  323.        Note on coordinates in this class: x is in pixels relative
  324.        to the middle of the screen and y is in pixels in absolute
  325.        terms (from the bottom of the screen)
  326. --]]    ------------------------------------------------------------
  327.  
  328. Track = class()
  329.  
  330. DIRTH = 2 -- threshold at which tracks become straight, a couple of pixels from Width/2
  331.  
  332. function Track:init()
  333.     self.horizonX = 0                     -- which it's pointing now
  334.     self.horizonTargetX = self.horizonX   -- where eventually it will point
  335.  
  336.     -- the coordinates of the track at the bottom
  337.     bottomXR = trackWidth/2
  338.     bottomXL = -bottomXR
  339.     self.bottomL = vec2(bottomXL,bottomY)
  340.     self.bottomR = vec2(bottomXR,bottomY)
  341.  
  342.     -- these are calculated based on horizonX  when updated
  343.     self.horizonL = nil    -- coords of the track at the horizon
  344.     self.horizonR = nil
  345.     self.centerL = nil     -- center of curvature of the tracks
  346.     self.centerR = nil
  347.     self.rL = 0            -- radius of curvature of the tracks
  348.     self.rR = 0
  349.    
  350.     self:setHorizon(self.horizonTargetX)
  351. end
  352.  
  353. -- UPDATING FUNCTIONS
  354.  
  355. -- call this function to change the targetX to a new random position
  356. function Track:newTargetX()
  357.     self.horizonTargetX = math.floor(3*random()-1) * trackWidth/2
  358. end
  359.  
  360. -- call this function to move the horizonX closer to the target
  361. function Track:moveToTargetX()
  362.     dH = math.min(10,math.abs(self.horizonX-self.horizonTargetX))
  363.  
  364.     if self.horizonTargetX < self.horizonX then
  365.         self:setHorizon(self.horizonX-dH)
  366.     elseif self.horizonTargetX > self.horizonX then
  367.         self:setHorizon(self.horizonX+dH)
  368.     end
  369. end
  370.  
  371. -- x in pixels relative to the middle of the screen. negative is to the left
  372. function Track:setHorizon(x)
  373.     self.horizonX = x
  374.  
  375.     if self.horizonX < -DIRTH then
  376.         -- we're turning left
  377.  
  378.         -- left track
  379.         self.rL = .275 * trackWidth^2 / math.abs(self.horizonX)
  380.         self.horizonL = vec2(self.horizonX,horizonY)
  381.         self.centerL = circleCenter(self.bottomL,self.horizonL,self.rL,1,-1)
  382.  
  383.         -- right track
  384.         self.rR = 8 * self.rL
  385.         adj = -self.horizonX * WIDTH/trackWidth*.009 -- due to numerical error?
  386.         self.horizonR = vec2(self.horizonX,horizonY+adj)
  387.         self.centerR = circleCenter(self.bottomR,self.horizonR,self.rR,-1,-1)
  388.     elseif self.horizonX > DIRTH then
  389.         -- we're turning right
  390.  
  391.         -- right track
  392.         self.rR = .275 * trackWidth^2 / math.abs(self.horizonX)
  393.         self.horizonR = vec2(self.horizonX,horizonY)
  394.         self.centerR = circleCenter(self.bottomR,self.horizonR,self.rR,1,1)
  395.  
  396.         -- left track
  397.         self.rL = 8 * self.rR
  398.         adj = self.horizonX * WIDTH/trackWidth*.009  -- due to numerical error?
  399.         self.horizonL = vec2(self.horizonX,horizonY+adj)
  400.         self.centerL = circleCenter(self.bottomL,self.horizonL,self.rL,-1,1)
  401.     end
  402. end
  403.  
  404. -- calculates the center of a circle that passes through bottom and horizon
  405. -- both of which are vec2s and of radius r, returns a vec2
  406. -- ugly math ahead
  407. function circleCenter(bottom,horizon,r,mult1,mult2)
  408.     m = -(bottom.x-horizon.x)/(bottom.y-horizon.y)
  409.     b = (bottom.y+horizon.y-m*(bottom.x+horizon.x))/2
  410.     c1 = (1+m^2)
  411.     c2 = -(bottom.x+horizon.x)*c1
  412.     c3 = horizon.x^2 + (b-horizon.y)^2 - r^2
  413.     centerX = (-c2 + mult2 * math.sqrt(c2^2-4*c1*c3))/(2*c1)
  414.     centerY = mult1*math.sqrt(r^2-(centerX-bottom.x)^2)+bottom.y
  415.     return(vec2(centerX,centerY))
  416. end
  417.  
  418. -- GETTER FUNCTIONS
  419.  
  420. -- the centrifugal force on my car and clouds
  421. function Track:centrifugal()
  422.     return (-self.horizonX / (trackWidth/2))
  423. end
  424.  
  425. -- x coord of left track in pixels relative to Width/2
  426. -- y in metters
  427. function Track:leftAt(y)
  428.     return(self:atHelper(y,self.rL,self.centerL,-1))
  429. end
  430.  
  431. -- x coord of right track in pixels relative to Width/2
  432. -- y in metters
  433. function Track:rightAt(y)
  434.     return(self:atHelper(y,self.rR,self.centerR,1))
  435. end
  436.  
  437. function Track:atHelper(y,r,center,mult)
  438.     s = metersToPixels(y)
  439.     ys = s + bottomY
  440.    
  441.     if self.horizonX < -DIRTH then
  442.         -- turning left
  443.         xs = math.sqrt(r^2 - (ys - center.y)^2) + center.x
  444.         xs = xs - (9+mult*5)*s/trackHeight  -- numerical adjustment
  445.     elseif self.horizonX > DIRTH then
  446.         -- turning right
  447.         xs = -math.sqrt(r^2 - (ys - center.y)^2) + center.x
  448.         xs = xs + (9-mult*5)*s/trackHeight  -- numerical adjustment
  449.     else
  450.         -- going straight
  451.         xs = mult*(1-s/trackHeight) * trackWidth/2
  452.     end
  453.     return(xs)
  454. end
  455.  
  456. -- tracks y in meter (0,trackLength) and returns y in pixels relative to the bottom
  457. -- note that it's not a linear relationship due to perpective
  458. function metersToPixels(y)
  459.     s=y/trackLength
  460.     s = (1-s)^2.5
  461.     return((1-s)*trackHeight)
  462. end
  463.  
  464. function Track:draw()
  465.     pushStyle()
  466.     pushMatrix()
  467.     resetMatrix()
  468.  
  469.     lineCapMode(PROJECT)
  470.     translate(WIDTH/2,0)
  471.     stroke(183, 183, 183, 255)
  472.  
  473.     if math.abs(self.horizonX) <= DIRTH then
  474.         -- we're going straight
  475.         strokeWidth(5)
  476.         line(self.bottomL.x,self.bottomL.y,self.horizonX,horizonY)
  477.         line(self.bottomR.x,self.bottomR.y,self.horizonX,horizonY)
  478.     else
  479.         -- we're turning
  480.         strokeWidth(3)
  481.         noFill()
  482.         ellipse(self.centerL.x,self.centerL.y,self.rL,self.rL)
  483.         ellipse(self.centerR.x,self.centerR.y,self.rR,self.rR)
  484.     end
  485.  
  486.     popStyle()
  487.     popMatrix()
  488. end
  489.  
  490. --[[    ------------------------------------------------------------
  491.                            Day.lua
  492. --]]    ------------------------------------------------------------
  493.  
  494. Day = class()
  495.  
  496. function Day:init()
  497.     self.mode = 1         -- day, night, snow, etc
  498.     self.lastModeT = 0    -- last time we changed modes
  499.     self.clouds = {}
  500.     self.won = false      -- whether we passed all cars for this day
  501.     self.fogEnd = .5      -- for fog animation
  502.     self:makeClouds()
  503. end
  504.  
  505. function Day:makeClouds()
  506.     for i = 0, 5 do
  507.         minY = horizonY + HEIGHT / 15
  508.         maxY = HEIGHT - HEIGHT / 20
  509.         y = minY + random()*(maxY-minY)
  510.         cloud = Cloud(random()*WIDTH,y)
  511.         table.insert(self.clouds,cloud)
  512.     end
  513. end
  514.  
  515. function Day:inSnow()
  516.     return(modes[self.mode].snow)
  517. end
  518.  
  519. -- moves the day mode forward
  520. function Day:updateMode(gameTime)
  521.     if gameTime - self.lastModeT > modes[self.mode].length then -- time to change mode
  522.         self.mode = self.mode%numModes+1
  523.         self.lastModeT = gameTime
  524.         if self.mode == 1 then   -- end of the day
  525.             if carsToPass > 0 then
  526.                 print("GAME OVER")
  527.                 gameOver = true -- global variable
  528.             else
  529.                 carsToPass = carsToPassInit + 40 * days
  530.                 maxSpeed = maxSpeed + 10
  531.                 days = days + 1
  532.                 self.won = false
  533.             end
  534.         end
  535.     end
  536. end
  537.  
  538. -- moves the clounds around
  539. function Day:moveX(dx)
  540.     for i,cloud in ipairs(self.clouds) do
  541.         cloud:moveX(dx)
  542.         -- handle a clound disappearing at the edges
  543.         if cloud.position.x < -200 or cloud.position.x > WIDTH + 200 then
  544.             table.remove(self.clouds,i)
  545.             -- make a new one
  546.             minY = horizonY + HEIGHT / 15
  547.             maxY = HEIGHT - HEIGHT / 20
  548.             y = minY + random()*(maxY-minY)
  549.             x = random()*WIDTH/8
  550.             if dx>0 then x = -x else x = x + WIDTH end  -- start it a little of screen
  551.             cloud = Cloud(x,y)
  552.             table.insert(self.clouds,cloud)
  553.         end
  554.     end
  555. end
  556.  
  557. -- needs track because we need to draw track in between the field and the sky
  558. function Day:draw(track)
  559.     -- green field
  560.     strokeWidth(0)
  561.     fill(modes[self.mode].field)
  562.     rect(-5, bottomY, WIDTH+10, horizonY)
  563.  
  564.     -- track
  565.     track:draw()
  566.  
  567.     -- sky
  568.     strokeWidth(0)
  569.     fill(modes[self.mode].sky)
  570.     rect(-5, horizonY, WIDTH+10, HEIGHT)
  571.  
  572.     -- sunset
  573.     sunsetW = (HEIGHT-horizonY)*.02
  574.     if modes[self.mode].sunseta ~= nil then
  575.         fill(modes[self.mode].sunseta)
  576.         rect(-5,horizonY,WIDTH+10,sunsetW+1)
  577.     end
  578.     if modes[self.mode].sunsetb ~= nil then
  579.         fill(modes[self.mode].sunsetb)
  580.         rect(-5,horizonY+sunsetW,WIDTH+10,sunsetW+1)
  581.     end
  582.     if modes[self.mode].sunsetc ~= nil then
  583.         fill(modes[self.mode].sunsetc)
  584.         rect(-5,horizonY+sunsetW*2,WIDTH+10,sunsetW+1)
  585.     end
  586.  
  587.     -- clouds
  588.     for i,cloud in ipairs(self.clouds) do
  589.         cloud:draw(modes[self.mode].cloud1,modes[self.mode].cloud2)
  590.     end
  591. end
  592.  
  593. function Day:updateFogEnd(speed)
  594.     self.fogEnd = self.fogEnd - speed/20000
  595.     if self.fogEnd < .5 then self.fogEnd = .6 end
  596. end
  597.  
  598. function Day:drawFog()
  599.     strokeWidth(0)
  600.     if modes[self.mode].fog then
  601.         fill(74,74,74,255)
  602.         rect(-5,bottomY+(horizonY-bottomY)*self.fogEnd,WIDTH+10,HEIGHT)
  603.     end
  604. end
  605.  
  606. --[[    ------------------------------------------------------------
  607.                            Car.lua
  608. --]]    ------------------------------------------------------------
  609.  
  610. dayCarColors = {
  611.     color(255, 0, 0, 255),
  612.     color(0, 35, 255, 255),
  613.     color(11, 255, 0, 255),
  614.     color(255, 245, 0, 255)
  615. }
  616.  
  617. nightCarColors = {
  618.     color(255, 0, 0, 255),
  619.     color(255, 0, 188, 255),
  620.     color(255, 0, 188, 255),
  621.     color(255, 0, 0, 255)
  622. }
  623.  
  624. Car = class()
  625.  
  626. function Car:init(x,y,color)
  627.     self.x = x    -- between -1 and 1
  628.     self.y = y    -- from 0 to trackLength in meters
  629.     self.color = color
  630.     self.speed = regularSpeed
  631.     self.real = true  -- unreal cars help with spawning
  632.     self.tireState = true  -- for tire animation
  633.     self.tireStateHelper = 0
  634. end
  635.  
  636. function Car:updateTireState()
  637.     self.tireStateHelper = self.tireStateHelper + self.speed / 60
  638.     if self.tireStateHelper > 5 then
  639.         self.tireStateHelper = 0
  640.         self.tireState = not self.tireState
  641.     end
  642. end
  643.  
  644. function Car:draw(track,day)
  645.     -- coords of the track at this y
  646.     leftX = track:leftAt(self.y)
  647.     rightX = track:rightAt(self.y)
  648.  
  649.     -- my coord at this y
  650.     x = ((1+self.x)*rightX + (1-self.x)*leftX)/2
  651.  
  652.     s = metersToPixels(self.y) -- implemented in the track class
  653.     self:drawHelper(x,s,1-s/trackHeight,self.color,self.tireState,day)
  654. end
  655.  
  656. -- x relative to middle of the screen in pixels, y relative to the bottom of the track
  657. -- scale relative to a car at the bottom of the screen
  658. -- tireState is true or false, for animation
  659. function Car:drawHelper(x,y,s,colorIdx,tireState,day)
  660.     pushMatrix()
  661.     pushStyle()
  662.     resetMatrix()
  663.     translate(WIDTH/2+x,bottomY+y)
  664.     scale(trackWidth*s/90,trackHeight*s/108)
  665.  
  666.     if colorIdx == 0 then -- hack for my car
  667.         thisColor = color(190,190,190,255)
  668.     elseif modes[day.mode].rearlights then
  669.         thisColor = nightCarColors[colorIdx]
  670.     else
  671.         thisColor = dayCarColors[colorIdx]
  672.     end
  673.  
  674.     fill(thisColor)
  675.     stroke(thisColor)
  676.     strokeWidth(2)
  677.  
  678.     if not modes[day.mode].rearlights or colorIdx == 0 then
  679.         -- regular car
  680.         rect(-4,1,8,7) -- body horizontal
  681.         rect(-2,0,4,12) -- body vertical
  682.         rect(-6,8,12,3) -- top wing
  683.         rect(-6,8,3,4) -- left wing
  684.         rect(3,8,3,4) -- right wing
  685.         for i = 0,7 do
  686.             if tireState == (i%2==0) then startX = -8 else startX = -6 end
  687.             rect(startX,i,3,2)
  688.             rect(startX+11,i,3,2)
  689.         end
  690.     else
  691.         -- rear lights
  692.         rect(2,0,5,4)
  693.         rect(-6,0,5,4)
  694.     end
  695.     popMatrix()
  696.     popStyle()
  697. end
  698.  
  699. function spawnNewCars()
  700.     if maxPosition < 65 then -- space the cars apart
  701.         rand1 = random()
  702.         if rand1 < 0.7 then
  703.             -- spawn one car
  704.             x = (math.floor(3*random())-1)/2
  705.             newCar = Car(x,.9*trackLength,randColor())
  706.             table.insert(cars,newCar)
  707.         else
  708.             newCar = Car(0,.9*trackLength,randColor())
  709.             newCar.real = false
  710.             table.insert(cars,newCar)
  711.         end
  712.     end
  713. end
  714.  
  715. -- used above
  716. function randColor()
  717.     ncolors = table.maxn(dayCarColors)
  718.     idx = math.ceil(random()*ncolors)
  719.     return(idx)
  720. end
  721.  
  722. --[[    ------------------------------------------------------------
  723.                            Cloud.lua
  724. --]]    ------------------------------------------------------------
  725. Cloud = class()
  726.  
  727. function Cloud:init(x,y)
  728.     -- you can accept and set parameters here
  729.     self.shapes = {}
  730.     self.position = vec2(x,y)
  731.  
  732.     -- Generate random cloud
  733.     numCircles = 4
  734.     spacing = 20
  735.  
  736.     for i = 1,numCircles do
  737.         x = i * spacing - ((numCircles/2)*spacing)
  738.         y = (random() - 0.5) * 20
  739.         rad = spacing*random()+spacing
  740.         table.insert(self.shapes, {x=x, y=y, r=rad})
  741.     end
  742.  
  743.     self.width = numCircles * spacing + spacing
  744. end
  745.  
  746. function Cloud:moveX(dx)
  747.     self.position.x = self.position.x + dx
  748. end
  749.  
  750. function Cloud:draw(c1,c2)
  751.     pushStyle()
  752.     pushMatrix()
  753.  
  754.     translate(self.position.x, self.position.y)
  755.  
  756.     -- noStroke()
  757.     strokeWidth(-1)
  758.     fill(c2)
  759.  
  760.     for i,s in ipairs(self.shapes) do
  761.         ellipse(s.x, s.y - 5, s.r)
  762.     end
  763.  
  764.     fill(c1)
  765.  
  766.     for i,s in ipairs(self.shapes) do
  767.         ellipse(s.x, s.y + 5, s.r)
  768.     end
  769.  
  770.     popMatrix()
  771.     popStyle()
  772. end
  773.  
  774. --[[    ------------------------------------------------------------
  775.                            DayModes.lua
  776. --]]    ------------------------------------------------------------
  777.  
  778. modes = {
  779.     {     -- morning
  780.         length = 14,
  781.         field = color(0,68,0,255),
  782.         sky = color(24,26,167,255),
  783.         sunseta = nil,
  784.         sunsetb = nil,
  785.         sunsetc = nil,
  786.         cloud1 = color(255,255,255,255),
  787.         cloud2 = color(167,190,221,255),
  788.         rearlights = false,
  789.         snow = false,
  790.         fog = false
  791.     },
  792.  
  793.     {     -- day1
  794.         length = 17,
  795.         field = color(0,68,0,255),
  796.         sky = color(35,38,170,255),
  797.         sunseta = nil,
  798.         sunsetb = nil,
  799.         sunsetc = nil,
  800.         cloud1 = color(255, 255, 255, 255),
  801.         cloud2 = color(167,190,221,255),
  802.         rearlights = false,
  803.         snow = false,
  804.         fog = false
  805.     },
  806.  
  807.     {     -- day2
  808.         length = 17,
  809.         field = color(0,68,0,255),
  810.         sky = color(45,50,184,255),
  811.         sunseta = nil,
  812.         sunsetb = nil,
  813.         sunsetc = nil,
  814.         cloud1 = color(255,255,255,255),
  815.         cloud2 = color(167,190,221,255),
  816.         rearlights = false,
  817.         snow = false,
  818.         fog = false
  819.     },
  820.    
  821.     {     -- snow
  822.         length = 34,
  823.         field = color(255,255,255,255),
  824.         sky = color(45,50,184,255),
  825.         sunseta = nil,
  826.         sunsetb = nil,
  827.         sunsetc = nil,
  828.         cloud1 = color(255,255,255,255),
  829.         cloud2 = color(167,190,221,255),
  830.         rearlights = false,
  831.         snow = true,
  832.         fog = false
  833.     },
  834.    
  835.     {     -- afternoon
  836.         length = 4,
  837.         field = color(20,60,0,255),
  838.         sky = color(24,26,167,255),
  839.         sunseta = nil,
  840.         sunsetb = nil,
  841.         sunsetc = nil,
  842.         cloud1 = color(255,255,255,255),
  843.         cloud2 = color(167,190,221,255),
  844.         rearlights = false,
  845.         snow = false,
  846.         fog = false
  847.     },
  848.  
  849.     {     -- sunset1
  850.         length = 4,
  851.         field = color(20,60,0,255),
  852.         sky = color(24,26,167,255),
  853.         sunseta = color(104,25,154,255),
  854.         sunsetb = color(51,26,163,255),
  855.         cloud1 = color(255,255,255,255),
  856.         cloud2 = color(167,190,221,255),
  857.         sunsetc = nil,
  858.         rearlights = false,
  859.         snow = false,
  860.         fog = false
  861.     },
  862.  
  863.     {     -- sunset2
  864.         length = 4,
  865.         field = color(20,60,0,255),
  866.         sky = color(51,26,163,255),
  867.         sunseta = color(151,25,122,255),
  868.         sunsetb = color(104,25,154,255),
  869.         sunsetc = nil,
  870.         cloud1 = color(255, 255, 255, 255),
  871.         cloud2 = color(226, 220, 199, 255),
  872.         rearlights = false,
  873.         snow = false,
  874.         fog = false
  875.     },
  876.  
  877.     {     -- sunset3
  878.         length = 4,
  879.         field = color(20,60,0,255),
  880.         sky = color(51,26,163,255),
  881.         sunseta = color(167,26,26,255),
  882.         sunsetb = color(151,25,122,255),
  883.         sunsetc = color(104,25,154,255),
  884.         cloud1 = color(255,255,255,255),
  885.         cloud2 = color(212, 202, 169, 255),
  886.         rearlights = false,
  887.         snow = false,
  888.         fog = false
  889.     },
  890.  
  891.     {     -- sunset4
  892.         length = 4,
  893.         field = color(48,56,0,255),
  894.         sky = color(104,25,154,255),
  895.         sunseta = color(163,57,21,255),
  896.         sunsetb = color(167,26,26,255),
  897.         sunsetc = color(151,25,122,255),
  898.         cloud1 = color(230, 230, 230, 255),
  899.         cloud2 = color(193, 168, 107, 255),
  900.         rearlights = false,
  901.         snow = false,
  902.         fog = false
  903.     },
  904.  
  905.     {     -- sunset5
  906.         length = 4,
  907.         field = color(48,56,0,255),
  908.         sky = color(151,25,122,255),
  909.         sunseta = color(181,83,40,255),
  910.         sunsetb = color(163,57,21,255),
  911.         sunsetc = color(167,26,26,255),
  912.         cloud1 = color(204, 204, 204, 255),
  913.         cloud2 = color(251,228,187,255),
  914.         rearlights = false,
  915.         snow = false,
  916.         fog = false
  917.     },
  918.  
  919.     {     -- sunset6
  920.         length = 4,
  921.         field = color(48,56,0,255),
  922.         sky = color(167,26,26,255),
  923.         sunseta = color(162,98,33,255),
  924.         sunsetb = color(181,83,40,255),
  925.         sunsetc = color(163,57,21,255),
  926.         cloud1 = color(179, 179, 179, 255),
  927.         cloud2 = color(255,194,104,255),
  928.         rearlights = false,
  929.         snow = false,
  930.         fog = false
  931.     },
  932.  
  933.     {     -- sunset7
  934.         length = 4,
  935.         field = color(48,56,0,255),
  936.         sky = color(163,57,21,255),
  937.         sunseta = color(134,134,29,255),
  938.         sunsetb = color(162,98,33,255),
  939.         sunsetc = color(181,83,40,255),
  940.         cloud1 = color(156, 156, 156, 255),
  941.         cloud2 = color(254,157,133,255),
  942.         rearlights = false,
  943.         snow = false,
  944.         fog = false
  945.     },
  946.  
  947.     {     -- night1
  948.         length = 34,
  949.         field = color(0,0,0,255),
  950.         sky = color(74,74,74,255),
  951.         sunseta = nil,
  952.         sunsetb = nil,
  953.         sunsetc = nil,
  954.         cloud1 = color(127, 127, 127, 255),
  955.         cloud2 = color(207, 207, 207, 255),
  956.         rearlights = true,
  957.         snow = false,
  958.         fog = false
  959.     },
  960.  
  961.     {     -- fog
  962.         length = 34,
  963.         field = color(74,74,74,255),
  964.         sky = color(74,74,74,255),
  965.         sunseta = nil,
  966.         sunsetb = nil,
  967.         sunsetc = nil,
  968.         cloud1 = color(255,255,255,255),
  969.         cloud2 = color(167,190,221,255),
  970.         rearlights = true,
  971.         snow = false,
  972.         fog = true
  973.     },
  974.  
  975.     {     -- night2
  976.         length = 17,
  977.         field = color(0,0,0,255),
  978.         sky = color(74,74,74,255),
  979.         sunseta = nil,
  980.         sunsetb = nil,
  981.         sunsetc = nil,
  982.         cloud1 = color(127,127,127,255),
  983.         cloud2 = color(207,207,207,255),
  984.         rearlights = true,
  985.         snow = false,
  986.         fog = false
  987.     },
  988.  
  989.     {     -- sunrise
  990.         length = 17,
  991.         field = color(0,0,0,255),
  992.         sky = color(111,111,111,255),
  993.         sunseta = nil,
  994.         sunsetb = nil,
  995.         sunsetc = nil,
  996.         cloud1 = color(159, 159, 159, 255),
  997.         cloud2 = color(200, 200, 200, 255),
  998.         rearlights = false,
  999.         snow = false,
  1000.         fog = false
  1001.     }
  1002. }
  1003.  
  1004. numModes = table.maxn(modes)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement