Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local APP = {}
- -- Name that appears on the home screen
- APP.PrintName = "Half Bit"
- -- Path to the icon, relative to materials directory
- APP.Icon = "vgui/gphone/app_image.png"
- APP.FPS = 30
- local isInGame = false
- local mat_player = Material( "vgui/gphone/apps/player.png" )
- --416 w 234 h
- -- Called when your App opens
- function APP.Run( objects, screen )
- --local objects = gApp["_children_"]
- --local screen = gPhone.phoneScreen
- gPhone.HideStatusBar()
- gPhone.RotateToLandscape()
- --gPhone.RotateToPortrait()
- isInGame = true
- APP.SetUpGame()
- end
- -- Holds the zombies for in game messing
- local zombies = {}
- local zombieCount = 1
- local function spawnZombie()
- local objects = gApp["_children_"]
- local screen = gPhone.phoneScreen
- local y = math.random(1,204)
- local x = math.random(1,276)
- objects[zombieCount] = vgui.Create("DPanel", screen)
- objects[zombieCount]:SetSize( 20, 20 )
- objects[zombieCount]:SetPos( x, y )
- --objects.zombie.Rotation = 0
- objects[zombieCount].Paint = function( self, w, h )
- --surface.SetDrawColor( color_white )
- --surface.SetMaterial( mat_player )
- --surface.DrawTexturedRectRotated( w/2, h/2, w, h, objects.Player.Rotation )
- draw.RoundedBox(0, 0, 0, w, h, Color(0, 255, 0) )
- end
- table.insert(zombies, objects[zombieCount])
- zombieCount = zombieCount + 1
- end
- function APP.SetUpGame()
- local objects = gApp["_children_"]
- local screen = gPhone.phoneScreen
- objects.GameTitle = vgui.Create( "DLabel", screen)
- objects.GameTitle:SetText( "Half Bit Alpha" )
- objects.GameTitle:SetTextColor( color_white )
- objects.GameTitle:SetFont("gPhone_22")
- objects.GameTitle:SizeToContents()
- objects.GameTitle:SetPos( screen:GetWide()/2 - objects.GameTitle:GetWide()/2, 10 )
- objects.Player = vgui.Create("DPanel", screen)
- objects.Player:SetSize( 20, 20 )
- objects.Player:SetPos( screen:GetWide()/2, screen:GetTall()/2 )
- objects.Player.Rotation = 0
- objects.Player.Paint = function( self, w, h )
- surface.SetDrawColor( color_white )
- surface.SetMaterial( mat_player )
- surface.DrawTexturedRectRotated( w/2, h/2, w, h, objects.Player.Rotation )
- end
- timer.Create("zambiespawn", 1, 5, spawnZombie)
- --objects.Zombie
- -- Detect shooting
- screen.OnMousePressed = function( self, code )
- if code == MOUSE_LEFT then
- objects.Player.ShouldFire = true
- end
- end
- objects.Bullets = {}
- end
- local shotCount = 0
- local img_bullet = Material( "vgui/gphone/apps/2d_bullet.png" )
- local function createBullet( x, y )
- local objects = gApp["_children_"]
- local screen = gPhone.phoneScreen
- local projectile = vgui.Create("DPanel", screen)
- projectile:SetSize( 8, 4 )
- projectile:SetPos( x, y )
- projectile.Rotation = 0
- projectile.Paint = function( self, w, h )
- draw.RoundedBox(0, 0, 0, w, h, color_white )
- surface.SetDrawColor( color_white )
- surface.SetMaterial( img_bullet )
- surface.DrawTexturedRectRotated( w/2, h/2, w, w, self.Rotation )
- end
- projectile.VelocityX = 0
- projectile.VelocityY = 0
- objects.Bullets[shotCount] = projectile
- shotCount = shotCount + 1
- return projectile
- end
- local function shootBullet()
- local objects = gApp["_children_"]
- if objects.Player.ShouldFire == true then
- local x, y = objects.Player:GetPos()
- local ang = objects.Player.Rotation
- local bullet = createBullet( x + objects.Player:GetWide(), y )
- local mX, mY = gPhone.phoneScreen:ScreenToLocal( gui.MouseX(), gui.MouseY() )
- local shootAngle = math.atan2((mY - y), (mX - x))
- shootAngle = -( math.Clamp(shootAngle, -90, 90) )
- -- The bullet needs proper velocity for its trajectory
- bullet.VelocityX = math.cos(shootAngle) * 5
- bullet.VelocityY = math.sin(-shootAngle) * 5
- bullet.Rotation = shootAngle
- objects.Player.ShouldFire = false
- end
- end
- local function movePlayer( direction )
- local objects = gApp["_children_"]
- local screen = gPhone.phoneScreen
- local x, y = objects.Player:GetPos()
- local newX, newY = x, y
- if direction == "UP" then
- newY = newY - 2
- elseif direction == "DOWN" then
- newY = newY + 2
- end
- if direction == "LEFT" then
- newX = newX - 2
- elseif direction == "RIGHT" then
- newX = newX + 2
- end
- newX = math.Clamp( newX, 0, screen:GetWide() - objects.Player:GetWide() )
- newY = math.Clamp( newY, 0, screen:GetTall() - objects.Player:GetTall() )
- objects.Player:SetPos( newX, newY )
- end
- local function updatePlayerRotation()
- local objects = gApp["_children_"]
- local screen = gPhone.phoneScreen
- local mX, mY = screen:ScreenToLocal(gui.MouseX(), gui.MouseY())
- local x, y = objects.Player:GetPos()
- local yDis = mY - y
- local xDis = mX - x
- local ang = math.deg( math.atan2( yDis, xDis ) )
- objects.Player.Rotation = -ang
- end
- local function handleKeys()
- if input.IsKeyDown( KEY_W ) then
- movePlayer( "UP" )
- elseif input.IsKeyDown( KEY_S ) then
- movePlayer( "DOWN" )
- end
- if input.IsKeyDown( KEY_A ) then
- movePlayer( "LEFT" )
- elseif input.IsKeyDown( KEY_D ) then
- movePlayer( "RIGHT" )
- end
- end
- -- Called when the phone screen's Think function is called
- function APP.Think( screen )
- local objects = gApp["_children_"]
- local screen = gPhone.phoneScreen
- if isInGame then
- handleKeys()
- updatePlayerRotation()
- shootBullet()
- for num, pnl in pairs(objects.Bullets) do
- if IsValid(pnl) then
- local x, y = pnl:GetPos()
- pnl:SetPos(x + pnl.VelocityX, y + pnl.VelocityY)
- else
- objects.Bullets[num] = nil
- end
- end
- end
- end
- -- Called when the phone screen's Paint function is called
- function APP.Paint( screen )
- if isInGame then
- -- Draw the game board
- draw.RoundedBox(2, 0, 0, screen:GetWide(), screen:GetTall(), Color(100, 200, 100))
- else
- -- Not in game
- draw.RoundedBox(2, 0, 0, screen:GetWide(), screen:GetTall(), Color(0, 0, 0))
- end
- end
- -- You MUST uncomment this line for the app to work. This adds it to the phone
- gPhone.AddApp(APP)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement