Advertisement
Guest User

Untitled

a guest
Jun 9th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.83 KB | None | 0 0
  1.  
  2. ENT_PLAYER = 0
  3. ENT_BULLET = 1
  4. ENT_ENEMY = 2
  5.  
  6. function initPlayer()
  7.  
  8.     playerX = 0
  9.     playerY = 0
  10.  
  11.     playerTopSpeed = 30
  12.     playerSpeedX = 0
  13.     playerSpeedY = 0
  14.     playerAccelRate = 3000
  15.     playerFriction = 6
  16.     playerNextShot = 0
  17.  
  18.     player = spawnPlayer()
  19.  
  20. end
  21.  
  22. function init()
  23.  
  24.     playing = false
  25.     limitX = 175
  26.  
  27.  
  28.     print("BULLET TYPE: " .. ENT_BULLET)
  29.  
  30. end
  31.  
  32. function drawScrollBG(x,y,w,h,s,tex)
  33.  
  34.     _r.quad(0,0,x,y,
  35.         w,0,s+x,0+y,
  36.         w,h,s+x,s+y,
  37.         0,h,0+x,s+y,
  38.         tex)
  39.  
  40. end
  41.  
  42. function drawBG(w,h)
  43.  
  44.     _r.color(0,0,0,1)
  45.     _r.rect(0,0,w,h)
  46.  
  47.     _r.color(1,1,1,1,1)
  48.     drawScrollBG(0,time*-.01,w,h,.5,_t.clouds)
  49.  
  50.     _r.color(.2,.2,.2,1,1)
  51.     drawScrollBG(0,time*-.2,w,h,.6,_t.stars)
  52.  
  53.     _r.color(.4,.4,.4,1,1)
  54.     drawScrollBG(0,time*-.04,w,h,.7,_t.stars)
  55.  
  56. end
  57.  
  58. function toCoords(x,y)
  59.  
  60.     return x + _width/2, y + _height-32
  61.  
  62. end
  63.  
  64. function playerCoords()
  65.  
  66.     return toCoords(playerX, playerY)
  67.  
  68. end
  69.  
  70. function draw()
  71.  
  72.     _width, _height = _r.size()
  73.     local w,h = _r.size()
  74.  
  75.     local sx = 0
  76.     local sy = 0
  77.  
  78.     _r.layer(0)
  79.     drawBG(w,h)
  80.  
  81.     _r.layer(1)
  82.     _r.color(1,1,1,1,0)
  83.  
  84.     if not playing then
  85.         return
  86.     end
  87.  
  88.     local x,y = playerCoords()
  89.     drawEdicts()
  90.  
  91. end
  92.  
  93. function think(time, dt)
  94.     game.time = time
  95.  
  96.     if not playing then return end
  97.  
  98.     tickEdicts(time, dt)
  99. end
  100.  
  101. function startGame()
  102.  
  103.     if playing then return end
  104.  
  105.     edicts = {}
  106.  
  107.     ch_music = sound.play(_s.music)
  108.     sound.setLooping(ch_music, true)
  109.     sound.setVolume(ch_music, .2)
  110.  
  111.     playing = true
  112.  
  113.     initPlayer()
  114.  
  115. end
  116.  
  117. function stopGame()
  118.  
  119.     if not playing then return false end
  120.  
  121.     sound.stop(ch_music)
  122.  
  123.     playing = false
  124.     return true
  125.  
  126. end
  127.  
  128. function onMousePressed(x,y,b,focus)
  129.  
  130. end
  131.  
  132. function onMouseReleased(x,y,b,focus)
  133.  
  134. end
  135.  
  136. function onMouseMoved(x,y,dx,dy,focus)
  137.  
  138. end
  139.  
  140. function onFocusGained(e)
  141.  
  142. end
  143.  
  144. function onFocusLost(e)
  145.     stopGame()
  146. end
  147.  
  148. function onKeyPressed(k)
  149.  
  150.     print(_keynames[k] .. " is down")
  151.  
  152.     if k == _keys.Z then
  153.         if not startGame() then
  154.             k_shoot = true
  155.         end
  156.     end
  157.  
  158.     if k == _keys.LEFT then k_left = true end
  159.     if k == _keys.RIGHT then k_right = true end
  160.     if k == _keys.UP then k_up = true end
  161.     if k == _keys.DOWN then k_down = true end
  162.  
  163. end
  164.  
  165. function onKeyReleased(k)
  166.     print(_keynames[k] .. " is up")
  167.  
  168.     if k == _keys.LEFT then k_left = false end
  169.     if k == _keys.RIGHT then k_right = false end
  170.     if k == _keys.UP then k_up = false end
  171.     if k == _keys.DOWN then k_down = false end
  172.  
  173.     if k == _keys.Z then
  174.         k_shoot = false
  175.     end
  176.  
  177. end
  178.  
  179. function edict(type)
  180.     local e = { type = type }
  181.     table.insert(edicts, e)
  182.     return e
  183. end
  184.  
  185. function drawEdicts()
  186.     for _,e in pairs( edicts ) do
  187.         if e.draw then e:draw() end
  188.     end
  189. end
  190.  
  191. function tickEdicts(time, dt)
  192.     for i=#edicts, 1, -1 do
  193.         if edicts[i].think then edicts[i]:think(dt) end
  194.         if edicts[i].dead then table.remove(edicts, i) end
  195.     end
  196. end
  197.  
  198. function edictsByType(t)
  199.     local out = {}
  200.     for _,e in pairs( edicts ) do
  201.         if e.type == t then table.insert(out, e) end
  202.     end
  203.     return out
  204. end
  205.  
  206. function spawnBullet(x,y,vx,vy,life)
  207.     local e = edict(ENT_BULLET)
  208.     e.x = x
  209.     e.y = y
  210.     e.vx = vx
  211.     e.vy = vy
  212.     e.life = life or 3
  213.     e.think = function(e,dt)
  214.         e.x = e.x + e.vx * dt
  215.         e.y = e.y + e.vy * dt
  216.         e.life = e.life - dt
  217.         if e.life <= 0 then e.dead = true end
  218.     end
  219.     e.draw = function(e)
  220.         _r.color(1,1,1,1,2)
  221.         _r.sprite(e.x, e.y, 24, 24, 0, e.tex)
  222.     end
  223.  
  224.     local ch_bullet = sound.play(_s.shoot)
  225.     sound.setVolume(ch_bullet, .3)
  226.  
  227.     local result = host.bulletSpawned(x,y)
  228.     print( tostring(result) )
  229.  
  230.     return e
  231. end
  232.  
  233. function spawnPlayer(x,y)
  234.     local e = edict(ENT_PLAYER)
  235.     e.x = x or 0
  236.     e.y = y or 0
  237.     e.vx = 0
  238.     e.vy = 0
  239.     e.ax = 0
  240.     e.ay = 0
  241.     e.nextshoot = 0
  242.     e.altshoot = false
  243.     e.think = function(e,dt)
  244.  
  245.         local move_horiz = 0
  246.         if k_left then move_horiz = -1
  247.         elseif k_right then move_horiz = 1 end
  248.  
  249.         local move_vert = 0
  250.         if k_up then move_vert = -1
  251.         elseif k_down then move_vert = 1 end
  252.  
  253.         e.vx = e.vx + playerAccelRate * move_horiz * dt
  254.         e.vx = e.vx * math.exp(dt * -playerFriction)
  255.  
  256.         e.vy = e.vy + playerAccelRate * move_vert * dt
  257.         e.vy = e.vy * math.exp(dt * -playerFriction)
  258.  
  259.         e.x = e.x + dt * e.vx
  260.         e.y = e.y + dt * e.vy
  261.  
  262.         if e.x > limitX then
  263.             e.x = limitX
  264.             e.vx = e.vx * -.5
  265.         end
  266.  
  267.         if e.x < -limitX then
  268.             e.x = -limitX
  269.             e.vx = e.vx * -.5
  270.         end
  271.  
  272.         if e.y > 0 then
  273.             e.y = 0
  274.             e.vy = e.vy * -.5
  275.         end
  276.  
  277.         if k_shoot and e.nextshoot < time then
  278.             local x,y = toCoords( e.x, e.y )
  279.             local width = 12
  280.  
  281.             if e.altshoot then
  282.                 x = x - width
  283.                 e.altshoot = false
  284.             else
  285.                 x = x + width
  286.                 e.altshoot = true
  287.             end
  288.  
  289.             local b = spawnBullet(x,y,0,-800)
  290.             b.tex = _t.shoot
  291.  
  292.             e.nextshoot = time + .1
  293.         end
  294.  
  295.     end
  296.     e.draw = function(e)
  297.         local r = -e.vx / 1200
  298.         local x,y = toCoords(e.x, e.y)
  299.         _r.sprite(x,y,64,64,r,math.fmod(time, .1) > .05 and _t.ship0 or _t.ship1)
  300.     end
  301.  
  302.     return e
  303. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement