Torna al Thread
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Input
Imports Microsoft.Xna.Framework.Graphics
Public Class clsGame
Inherits Microsoft.Xna.Framework.Game
'DATA GRAPHICS
Private Graphics As GraphicsDeviceManager
Private SBatch As SpriteBatch
'GENERAL OBJECT
Private Bar_Life As clsGameObject
'OBJECT SHIP, ENEMY
Private ship As clsGameObject
Private enemy As clsGameObject
'SETTING SHIP
Private ShipSpeed As Integer = 10
'OBJECT WEAPON
Private SingleWeapon As clsGameObject
Private DoubleWeapon As clsGameObject
Private laser As clsGameObject
Private Shield As clsGameObject
'SETTING WEAPON
Private SingleWeaponSpeed As Integer = 30
'DATA GAME
Private GAME_PAUSE As Boolean = False
Private CAN_FIRE As Boolean = False
Sub New()
My.Settings.Reload()
Me.Graphics = New GraphicsDeviceManager(Me)
Me.Graphics.IsFullScreen = False
Me.Graphics.PreferMultiSampling = True
Me.Graphics.PreferredBackBufferHeight = 600
Me.Graphics.PreferredBackBufferWidth = 600
Me.Graphics.SynchronizeWithVerticalRetrace = True
If My.Settings.HDPROFILE Then
Me.Graphics.GraphicsProfile = GraphicsProfile.HiDef
Else
Me.Graphics.GraphicsProfile = GraphicsProfile.Reach
End If
Me.Graphics.ApplyChanges()
Me.IsMouseVisible = True
Me.IsFixedTimeStep = True
Me.Window.Title = "Air Combat Missione " & currentMission
Me.Window.AllowUserResizing = False
Me.TargetElapsedTime = TimeSpan.FromSeconds(1.0 / 60.0)
Me.Content.RootDirectory = "Content"
End Sub
Protected Overrides Sub Initialize()
SBatch = New SpriteBatch(Me.Graphics.GraphicsDevice)
MyBase.Initialize()
End Sub
Protected Overrides Sub LoadContent()
MyBase.LoadContent()
NewShip(texture_ship, Me.GraphicsDevice.Viewport.Width / 2, Me.GraphicsDevice.Viewport.Height / 2, 0.8, 100, 100)
End Sub
Protected Overrides Sub UnloadContent()
MyBase.UnloadContent()
End Sub
Private Sub NewShip(ByVal TexturePath As String, ByVal PositionX As Integer, ByVal PositionY As Integer, ByVal Scale As Single, ByVal Life As Integer, ByVal Damage As Integer)
ship = New clsGameObject(GetTexture(TexturePath))
ship.Position = New Vector2(PositionX, PositionY)
ship.Scale = Scale
ship.Life = Life
ship.Damage = Damage
End Sub
Private Sub NewEnemy(ByVal TextureName As String, ByVal PositionX As Integer, ByVal PositionY As Integer, ByVal Scale As Single, ByVal Life As Integer, ByVal Damage As Integer)
enemy = New clsGameObject(GetTexture(TextureName))
enemy.Position = New Vector2(PositionX, PositionY)
enemy.Scale = Scale
enemy.Life = Life
enemy.Damage = Damage
End Sub
Private Sub NewSingleWeapon(ByVal TexturePath As String, ByVal PositionX As Integer, ByVal PositionY As Integer, ByVal Scale As Single, ByVal Life As Integer, ByVal Damage As Integer)
SingleWeapon = New clsGameObject(GetTexture(TexturePath))
SingleWeapon.Position = New Vector2(PositionX, PositionY)
SingleWeapon.Scale = Scale
SingleWeapon.Life = Life
SingleWeapon.Damage = Damage
End Sub
Private Sub NewDoubleWeapon(ByVal TexturePath As String, ByVal PositionX As Integer, ByVal PositionY As Integer, ByVal Scale As Single, ByVal Life As Integer, ByVal Damage As Integer)
DoubleWeapon = New clsGameObject(GetTexture(TexturePath))
DoubleWeapon.Position = New Vector2(PositionX, PositionY)
DoubleWeapon.Scale = Scale
DoubleWeapon.Life = Life
DoubleWeapon.Damage = Damage
End Sub
Private Function GetTexture(ByVal TexturePath As String) As Texture2D
Try
Return Texture2D.FromStream(Me.Graphics.GraphicsDevice, System.IO.File.OpenRead(TexturePath))
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
End Try
End Function
Private Sub CaptureInput()
Dim KeyState As KeyboardState = Keyboard.GetState
If KeyState.IsKeyDown(My.Settings.COMMAND_UP) Then
If ship.Position.Y >= 50 Then ship.Position += New Vector2(0, -ShipSpeed)
ElseIf KeyState.IsKeyDown(My.Settings.COMMAND_DOWN) Then
If ship.Position.Y <= Me.GraphicsDevice.Viewport.Height - 50 Then ship.Position += New Vector2(0, ShipSpeed)
End If
If KeyState.IsKeyDown(My.Settings.COMMAND_LEFT) Then
If ship.Position.X >= 50 Then ship.Position += New Vector2(-ShipSpeed, 0)
ElseIf KeyState.IsKeyDown(My.Settings.COMMAND_RIGHT) Then
If ship.Position.X <= Me.GraphicsDevice.Viewport.Width - 50 Then ship.Position += New Vector2(ShipSpeed, 0)
End If
If KeyState.IsKeyDown(My.Settings.COMMAND_FIRE) Then
CAN_FIRE = True
ElseIf KeyState.IsKeyUp(My.Settings.COMMAND_FIRE) Then
CAN_FIRE = False
End If
If KeyState.IsKeyDown(My.Settings.COMMAND_PAUSE) Then
If GAME_PAUSE Then
GAME_PAUSE = False
Else
GAME_PAUSE = True
End If
End If
End Sub
Private Sub Fire()
NewSingleWeapon(texture_single_weapon, ship.Position.X, ship.Position.Y, 1.0, 0, 100)
SingleWeapon.Position += New Vector2(0, -SingleWeaponSpeed)
End Sub
Private Function GetBound(ByVal obj As clsGameObject) As Rectangle
Return New Rectangle(obj.Position.X - (obj.Sprite.Width / 2), obj.Position.Y - (obj.Sprite.Height / 2), obj.Sprite.Width, obj.Sprite.Height)
End Function
Protected Overrides Sub Draw(ByVal GameTime As GameTime)
Me.Graphics.GraphicsDevice.Clear(Color.CornflowerBlue)
SBatch.Begin()
ship.Draw(SBatch)
If Not SingleWeapon Is Nothing Then SingleWeapon.Draw(SBatch)
SBatch.End()
MyBase.Draw(GameTime)
End Sub
Protected Overrides Sub Update(ByVal GameTime As GameTime)
CaptureInput()
If CAN_FIRE Then Fire()
MyBase.Update(GameTime)
End Sub
End Class