Post

 Resources 

Console

Fart Sniffer


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.

  1. Public Class Actor
  2.    Public Position As Point
  3.    Public Direction As Single = 0
  4.    Public Speed As Single = 1
  5.    Public Size As Single = 6
  6. End Class


  1. Public Class Fly
  2.    Inherits Actor
  3.    Public Receptors As New Generic.List(Of Receptor)
  4.    Private mlngLastDirectionChange As Long
  5.    Public Sub Update()
  6.       If Now.Ticks > Me.mlngLastDirectionChange + (TimeSpan.TicksPerSecond \ 2) Then
  7.          Randomize(Now.Ticks)
  8.          Me.Direction = CSng(Rnd() * (Math.PI * 2))
  9.          Me.mlngLastDirectionChange = Now.Ticks
  10.       End If
  11.    End Sub
  12.    Public Sub New(ByVal Position As Point)
  13.       Me.Position = Position
  14.       Me.Speed = 6
  15.       mlngLastDirectionChange = CLng(Rnd() * TimeSpan.TicksPerSecond)
  16.    End Sub
  17.    Public Sub New(ByVal Position As Point, ByVal R As Generic.List(Of Receptor))
  18.       Me.New(Position)
  19.       Me.Receptors = R
  20.    End Sub
  21. 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.

  1. Public Class Fly
  2.    Inherits Actor
  3.    Public Receptors As New Generic.List(Of Receptor)
  4.    Private mlngLastDirectionChange As Long
  5.    Public Sub Update()
  6.       If Now.Ticks > Me.mlngLastDirectionChange + (TimeSpan.TicksPerSecond \ 2) Then
  7.          Randomize(Now.Ticks)
  8.          Me.Direction = CSng(Rnd() * (Math.PI * 2))
  9.          Me.mlngLastDirectionChange = Now.Ticks
  10.       End If
  11.    End Sub
  12.    Public Sub New(ByVal Position As Point)
  13.       Me.Position = Position
  14.       Me.Speed = 6
  15.       mlngLastDirectionChange = CLng(Rnd() * TimeSpan.TicksPerSecond)
  16.    End Sub
  17.    Public Sub New(ByVal Position As Point, ByVal R As Generic.List(Of Receptor))
  18.       Me.New(Position)
  19.       Me.Receptors = R
  20.    End Sub
  21. 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.

  1. Public Class Scent
  2.    Public Strength As Single = 25
  3.    Public DecayRate As Single = 8
  4.    Public DecaySpeed As Single = 1
  5.    Public Position As Point
  6.    Public Owner As Actor
  7.    Public Sub New(ByVal Owner As Actor)
  8.       Me.Owner = Owner
  9.       Me.position = Me.Owner.Position
  10.    End Sub
  11.    Public Sub Decay()
  12.       Static LastDecayTime As Long
  13.       Dim TheTime As Long = Now.Ticks
  14.       If TheTime > LastDecayTime + (TimeSpan.TicksPerSecond \ CLng(DecaySpeed)) Then
  15.          Me.Strength = RestrictValue(Me.Strength - Me.DecayRate, 0, Single.MaxValue)
  16.          LastDecayTime = TheTime
  17.       End If
  18.    End Sub   
  19. End Class




Next: Part 2 - Declaring Variables | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11

Post a Comment


Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.
There have been 40 visitors within the last 20 minutes
RSS News Feed