Part 1 - Base Types
First
we will need to declare some class types to store the information we
will need like Flies, Fly receptacles, and a Scent object.
Because
the flies and the player we see on screen could be considered actors
that share similar qualities we will declare an Actor class and then
create a Fly class that inherits from the Actor class. We will not
create a player class because the player will not possess any unique
qualities other then what is already provided by the actor class.
- Public Class Actor
- Public Position As Point
- Public Direction As Single = 0
- Public Speed As Single = 1
- Public Size As Single = 6
- End Class
- Public Class Fly
- Inherits Actor
- Public Receptors As New Generic.List(Of Receptor)
- Private mlngLastDirectionChange As Long
- Public Sub Update()
- If Now.Ticks > Me.mlngLastDirectionChange + (TimeSpan.TicksPerSecond \ 2) Then
- Randomize(Now.Ticks)
- Me.Direction = CSng(Rnd() * (Math.PI * 2))
- Me.mlngLastDirectionChange = Now.Ticks
- End If
- End Sub
- Public Sub New(ByVal Position As Point)
- Me.Position = Position
- Me.Speed = 6
- mlngLastDirectionChange = CLng(Rnd() * TimeSpan.TicksPerSecond)
- End Sub
- Public Sub New(ByVal Position As Point, ByVal R As Generic.List(Of Receptor))
- Me.New(Position)
- Me.Receptors = R
- End Sub
- End Class
You
will notice that the Fly class contains a generic collection of
Receptor objects. Receptors are like little antenna that we will use
for detecting any scent that the Receptor may come in contact with. The
flies we will be using for this tutorial will only be using 2 receptors.
- Public Class Fly
- Inherits Actor
- Public Receptors As New Generic.List(Of Receptor)
- Private mlngLastDirectionChange As Long
- Public Sub Update()
- If Now.Ticks > Me.mlngLastDirectionChange + (TimeSpan.TicksPerSecond \ 2) Then
- Randomize(Now.Ticks)
- Me.Direction = CSng(Rnd() * (Math.PI * 2))
- Me.mlngLastDirectionChange = Now.Ticks
- End If
- End Sub
- Public Sub New(ByVal Position As Point)
- Me.Position = Position
- Me.Speed = 6
- mlngLastDirectionChange = CLng(Rnd() * TimeSpan.TicksPerSecond)
- End Sub
- Public Sub New(ByVal Position As Point, ByVal R As Generic.List(Of Receptor))
- Me.New(Position)
- Me.Receptors = R
- End Sub
- End Class
Next
we will define a Scent class that will represent a scent in our
application. The scent class contains properties like Strength which we
will use to determine how large we should draw the scent on screen. It
also contains two other properties DecayRate and DecaySpeed. DecayRate
specifies how much the scent strength will be reduced. And DecaySpeed
will be used to determine how fast to apply the DecayRate. The scent
class also contains a field called Owner. Owner is not required in this
tutorial, but it will be set to reference the player varible we will
define later.
- Public Class Scent
- Public Strength As Single = 25
- Public DecayRate As Single = 8
- Public DecaySpeed As Single = 1
- Public Position As Point
- Public Owner As Actor
- Public Sub New(ByVal Owner As Actor)
- Me.Owner = Owner
- Me.position = Me.Owner.Position
- End Sub
- Public Sub Decay()
- Static LastDecayTime As Long
- Dim TheTime As Long = Now.Ticks
- If TheTime > LastDecayTime + (TimeSpan.TicksPerSecond \ CLng(DecaySpeed)) Then
- Me.Strength = RestrictValue(Me.Strength - Me.DecayRate, 0, Single.MaxValue)
- LastDecayTime = TheTime
- End If
- End Sub
- End Class