Advertisement
Exho

Untitled

Jan 4th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. local APP = {}
  2. -- Name that appears on the home screen
  3. APP.PrintName = "Half Bit"
  4. -- Path to the icon, relative to materials directory
  5. APP.Icon = "vgui/gphone/app_image.png"
  6. APP.FPS = 30
  7.  
  8. local isInGame = false
  9. local mat_player = Material( "vgui/gphone/apps/player.png" )
  10. --416 w 234 h
  11. -- Called when your App opens
  12. function APP.Run( objects, screen )
  13. --local objects = gApp["_children_"]
  14. --local screen = gPhone.phoneScreen
  15. gPhone.HideStatusBar()
  16.  
  17. gPhone.RotateToLandscape()
  18. --gPhone.RotateToPortrait()
  19.  
  20. isInGame = true
  21.  
  22. APP.SetUpGame()
  23. end
  24.  
  25. -- Holds the zombies for in game messing
  26. local zombies = {}
  27.  
  28. local zombieCount = 1
  29. local function spawnZombie()
  30. local objects = gApp["_children_"]
  31. local screen = gPhone.phoneScreen
  32. local y = math.random(1,204)
  33. local x = math.random(1,276)
  34. objects[zombieCount] = vgui.Create("DPanel", screen)
  35. objects[zombieCount]:SetSize( 20, 20 )
  36. objects[zombieCount]:SetPos( x, y )
  37. --objects.zombie.Rotation = 0
  38. objects[zombieCount].Paint = function( self, w, h )
  39. --surface.SetDrawColor( color_white )
  40. --surface.SetMaterial( mat_player )
  41. --surface.DrawTexturedRectRotated( w/2, h/2, w, h, objects.Player.Rotation )
  42. draw.RoundedBox(0, 0, 0, w, h, Color(0, 255, 0) )
  43. end
  44.  
  45. table.insert(zombies, objects[zombieCount])
  46. zombieCount = zombieCount + 1
  47. end
  48.  
  49. function APP.SetUpGame()
  50. local objects = gApp["_children_"]
  51. local screen = gPhone.phoneScreen
  52. objects.GameTitle = vgui.Create( "DLabel", screen)
  53. objects.GameTitle:SetText( "Half Bit Alpha" )
  54. objects.GameTitle:SetTextColor( color_white )
  55. objects.GameTitle:SetFont("gPhone_22")
  56. objects.GameTitle:SizeToContents()
  57. objects.GameTitle:SetPos( screen:GetWide()/2 - objects.GameTitle:GetWide()/2, 10 )
  58.  
  59. objects.Player = vgui.Create("DPanel", screen)
  60. objects.Player:SetSize( 20, 20 )
  61. objects.Player:SetPos( screen:GetWide()/2, screen:GetTall()/2 )
  62. objects.Player.Rotation = 0
  63. objects.Player.Paint = function( self, w, h )
  64. surface.SetDrawColor( color_white )
  65. surface.SetMaterial( mat_player )
  66. surface.DrawTexturedRectRotated( w/2, h/2, w, h, objects.Player.Rotation )
  67. end
  68. timer.Create("zambiespawn", 1, 5, spawnZombie)
  69. --objects.Zombie
  70.  
  71. -- Detect shooting
  72. screen.OnMousePressed = function( self, code )
  73. if code == MOUSE_LEFT then
  74. objects.Player.ShouldFire = true
  75. end
  76. end
  77.  
  78. objects.Bullets = {}
  79. end
  80.  
  81. local shotCount = 0
  82. local img_bullet = Material( "vgui/gphone/apps/2d_bullet.png" )
  83. local function createBullet( x, y )
  84. local objects = gApp["_children_"]
  85. local screen = gPhone.phoneScreen
  86.  
  87. local projectile = vgui.Create("DPanel", screen)
  88. projectile:SetSize( 8, 4 )
  89. projectile:SetPos( x, y )
  90. projectile.Rotation = 0
  91. projectile.Paint = function( self, w, h )
  92. draw.RoundedBox(0, 0, 0, w, h, color_white )
  93.  
  94. surface.SetDrawColor( color_white )
  95. surface.SetMaterial( img_bullet )
  96.  
  97. surface.DrawTexturedRectRotated( w/2, h/2, w, w, self.Rotation )
  98. end
  99. projectile.VelocityX = 0
  100. projectile.VelocityY = 0
  101.  
  102. objects.Bullets[shotCount] = projectile
  103. shotCount = shotCount + 1
  104.  
  105. return projectile
  106. end
  107.  
  108. local function shootBullet()
  109. local objects = gApp["_children_"]
  110.  
  111. if objects.Player.ShouldFire == true then
  112. local x, y = objects.Player:GetPos()
  113. local ang = objects.Player.Rotation
  114.  
  115. local bullet = createBullet( x + objects.Player:GetWide(), y )
  116.  
  117. local mX, mY = gPhone.phoneScreen:ScreenToLocal( gui.MouseX(), gui.MouseY() )
  118. local shootAngle = math.atan2((mY - y), (mX - x))
  119. shootAngle = -( math.Clamp(shootAngle, -90, 90) )
  120.  
  121. -- The bullet needs proper velocity for its trajectory
  122. bullet.VelocityX = math.cos(shootAngle) * 5
  123. bullet.VelocityY = math.sin(-shootAngle) * 5
  124. bullet.Rotation = shootAngle
  125.  
  126. objects.Player.ShouldFire = false
  127. end
  128. end
  129.  
  130. local function movePlayer( direction )
  131. local objects = gApp["_children_"]
  132. local screen = gPhone.phoneScreen
  133.  
  134. local x, y = objects.Player:GetPos()
  135. local newX, newY = x, y
  136.  
  137. if direction == "UP" then
  138. newY = newY - 2
  139. elseif direction == "DOWN" then
  140. newY = newY + 2
  141. end
  142.  
  143. if direction == "LEFT" then
  144. newX = newX - 2
  145. elseif direction == "RIGHT" then
  146. newX = newX + 2
  147. end
  148.  
  149. newX = math.Clamp( newX, 0, screen:GetWide() - objects.Player:GetWide() )
  150. newY = math.Clamp( newY, 0, screen:GetTall() - objects.Player:GetTall() )
  151.  
  152. objects.Player:SetPos( newX, newY )
  153. end
  154.  
  155. local function updatePlayerRotation()
  156. local objects = gApp["_children_"]
  157. local screen = gPhone.phoneScreen
  158.  
  159. local mX, mY = screen:ScreenToLocal(gui.MouseX(), gui.MouseY())
  160. local x, y = objects.Player:GetPos()
  161.  
  162. local yDis = mY - y
  163. local xDis = mX - x
  164.  
  165. local ang = math.deg( math.atan2( yDis, xDis ) )
  166.  
  167. objects.Player.Rotation = -ang
  168. end
  169.  
  170.  
  171. local function handleKeys()
  172. if input.IsKeyDown( KEY_W ) then
  173. movePlayer( "UP" )
  174. elseif input.IsKeyDown( KEY_S ) then
  175. movePlayer( "DOWN" )
  176. end
  177.  
  178. if input.IsKeyDown( KEY_A ) then
  179. movePlayer( "LEFT" )
  180. elseif input.IsKeyDown( KEY_D ) then
  181. movePlayer( "RIGHT" )
  182. end
  183.  
  184. end
  185.  
  186. -- Called when the phone screen's Think function is called
  187. function APP.Think( screen )
  188. local objects = gApp["_children_"]
  189. local screen = gPhone.phoneScreen
  190.  
  191. if isInGame then
  192. handleKeys()
  193. updatePlayerRotation()
  194.  
  195. shootBullet()
  196.  
  197. for num, pnl in pairs(objects.Bullets) do
  198. if IsValid(pnl) then
  199. local x, y = pnl:GetPos()
  200. pnl:SetPos(x + pnl.VelocityX, y + pnl.VelocityY)
  201. else
  202. objects.Bullets[num] = nil
  203. end
  204. end
  205. end
  206. end
  207.  
  208. -- Called when the phone screen's Paint function is called
  209. function APP.Paint( screen )
  210. if isInGame then
  211. -- Draw the game board
  212. draw.RoundedBox(2, 0, 0, screen:GetWide(), screen:GetTall(), Color(100, 200, 100))
  213.  
  214. else
  215. -- Not in game
  216. draw.RoundedBox(2, 0, 0, screen:GetWide(), screen:GetTall(), Color(0, 0, 0))
  217. end
  218. end
  219.  
  220. -- You MUST uncomment this line for the app to work. This adds it to the phone
  221. gPhone.AddApp(APP)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement