In this episode, we continue with the floaters. Among some strange new things that we'll have to deal with concerning the floaters is how to set the properties of the floater using the editor. Other things like using the floater arraylist and file I/O should be common knowledge by now. First, we will only be concerning ourselves with getting multiple floaters. This should be simple by now, but I'll show it again (as always). Declaration:
Dim Floaters As ArrayList 'Holds all of the floaters for this level. |
Floater = New LevFloater(New Rectangle(420, 400, FLOATERSIZE, FLOATERSIZE), 5, 5, 16, 16, PeriodicDesignation.CosineFxn, PeriodicDesignation.SineFxn) 'Information about the floater. The width and height are all determined by the floatersize constant. |
Floaters = New ArrayList(10) Floaters.Add(New LevFloater(New Rectangle(420, 400, FLOATERSIZE, FLOATERSIZE), 5, 5, 16, 16, PeriodicDesignation.CosineFxn, PeriodicDesignation.SineFxn)) Floaters.Add(New LevFloater(New Rectangle(550, 200, FLOATERSIZE, FLOATERSIZE), 5, 5, 16, 16, PeriodicDesignation.SineFxn, PeriodicDesignation.CosineFxn)) 'Information about the floater. The width and height are all determined by the floatersize constant. |
For Each Floater In Floaters GFX.DrawImage(FloatBmp, Floater.Loc.X, Floater.Loc.Y) 'Draw the floater to the display. There's only one frame, so no selection involved. Next |
Dim LP As Integer For LP = 0 To Floaters.Count - 1 Floater = DirectCast(Floaters(LP), LevFloater) 'Since we are changing properties of the floater, and since floater is a structure, we have to reassign the 'newly modified floater back to the array. Select Case Floater.FunctionHorizontal 'Looking at the function that we have designated, we choose the proper periodic function for setting to dX and dY. Case PeriodicDesignation.CosineFxn dX = Convert.ToInt32(Floater.RadispeedX * Math.Cos(Convert.ToDouble(FloatTick) * Floater.FrequencyX / Me.FREQUENCYDIVIDER)) Case PeriodicDesignation.SineFxn dX = Convert.ToInt32(Floater.RadispeedX * Math.Sin(Convert.ToDouble(FloatTick) * Floater.FrequencyX / Me.FREQUENCYDIVIDER)) Case PeriodicDesignation.Stable dX = 0 End Select Select Case Floater.FunctionVertical 'Looking at the function that we have designated, we choose the proper periodic function for setting to dX and dY. Case PeriodicDesignation.CosineFxn dY = Convert.ToInt32(Floater.RadispeedY * Math.Cos(Convert.ToDouble(FloatTick) * Floater.FrequencyY / Me.FREQUENCYDIVIDER)) Case PeriodicDesignation.SineFxn dY = Convert.ToInt32(Floater.RadispeedY * Math.Sin(Convert.ToDouble(FloatTick) * Floater.FrequencyY / Me.FREQUENCYDIVIDER)) Case PeriodicDesignation.Stable dY = 0 End Select Floater.Offset(dX, dY) 'Move the floater using our offset subroutine. Floaters(LP) = Floater 'Return the floater that we've modified to the array. Next |
For Each Floater In Floaters If Floater.Loc.IntersectsWith(PlayerLoc) Then IsGoner = True 'The player has touched the floater. Lose a life. PlayerVeloc = SBARINITIALVELOCITY End If Next |
Private Sub FloaterEd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.ComboBox1.Items.AddRange(System.Enum.GetNames(PeriodicDesignation.Stable.GetType())) 'Adds periodic functions to combobox1. Me.ComboBox2.Items.AddRange(System.Enum.GetNames(PeriodicDesignation.Stable.GetType())) 'Also to combobox2. End Sub |
Private Flot As LevFloater 'The floater that will be returned by this prompt form. Public ReadOnly Property Floater() As LevFloater Get Return Flot End Get End Property |
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim HFxn As PeriodicDesignation = CType(ComboBox1.SelectedIndex, PeriodicDesignation) Dim VFxn As PeriodicDesignation = CType(ComboBox2.SelectedIndex, PeriodicDesignation) 'Get the periodic functions to use. Dim RSpdX As Integer = Convert.ToInt32(Me.NumericUpDown1.Value) Dim RSpdY As Integer = Convert.ToInt32(Me.NumericUpDown2.Value) Dim FreqX As Integer = Convert.ToInt32(Me.NumericUpDown3.Value) Dim FreqY As Integer = Convert.ToInt32(Me.NumericUpDown4.Value) 'Get the radispeeds and frequencies. Flot = New LevFloater(Rectangle.Empty, RSpdX, RSpdY, FreqX, FreqY, HFxn, VFxn) 'Filled in all properties except for the rectangle. Me.Close() 'Close the form when OK is clicked, like all dialogs do. End Sub |
ElseIf e.KeyCode = Keys.F Then If Not IsMouseDown Then MDObject = "F"c End If |
Case "F"c Clock.Enabled = False 'Lighten the processing load while we set up our floater. Dim Fm As FloaterEd = New FloaterEd Fm.ShowDialog() 'After the showdialog, the floater's final six properties will be set. 'But, the rectangle will be empty. Dim Fltr As LevFloater = Fm.Floater 'Copy the floater to our variable. Fltr.Loc = New Rectangle(Mu.X, Mu.Y, FLOATERSIZE, FLOATERSIZE) 'Set the rectangle. The size is fixed, so we just make the rectangle like we did for the coin. Floaters.Insert(0, Fltr) 'And this final floater will be added. 'The movement of the floater after it is added to the form is not the exact movement that it will have 'when it is loaded from file, because the value of FloatTick is not always 0 when the floater is created, and its 'movement depends on the value of floattick with the periodic function. Clock.Enabled = True 'Continue game. |
'Floaters are written to file. BW.Write(Floaters.Count) For Each Floater In Floaters BW.Write(Floater.Loc.Left) BW.Write(Floater.Loc.Top) BW.Write(Floater.RadispeedX) BW.Write(Floater.RadispeedY) BW.Write(Floater.FrequencyX) BW.Write(Floater.FrequencyY) BW.Write(Floater.FunctionHorizontal) BW.Write(Floater.FunctionVertical) Next |
Floaters = New ArrayList(BR.ReadInt32()) For LV = 1 To Floaters.Capacity Floaters.Add(New LevFloater(New Rectangle(BR.ReadInt32(), BR.ReadInt32(), FLOATERSIZE, FLOATERSIZE), _ BR.ReadInt32(), BR.ReadInt32(), BR.ReadInt32(), BR.ReadInt32(), _ CType(BR.ReadInt32(), PeriodicDesignation), CType(BR.ReadInt32(), PeriodicDesignation))) Next Floaters.Capacity = 20 |
Floaters = New ArrayList(10) Floaters.Add(New LevFloater(New Rectangle(420, 400, FLOATERSIZE, FLOATERSIZE), 5, 5, 16, 16, PeriodicDesignation.CosineFxn, PeriodicDesignation.SineFxn)) Floaters.Add(New LevFloater(New Rectangle(550, 200, FLOATERSIZE, FLOATERSIZE), 5, 5, 16, 16, PeriodicDesignation.SineFxn, PeriodicDesignation.CosineFxn)) |