PageRenderTime 4500ms CodeModel.GetById 50ms RepoModel.GetById 2ms app.codeStats 0ms

/SDK/Solutions/VB/Exercise3/MyAnimal.vb

#
Visual Basic | 209 lines | 160 code | 28 blank | 21 comment | 2 complexity | 3ff486d9d0be773e45c5d98e6db13769 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. <Assembly: OrganismClass("Exercise3.MyAnimal")>
  2. <Assembly: AuthorInformation("Your Name", "someone@microsoft.com")>
  3. < _
  4. Carnivore(False), _
  5. MatureSize(36), _
  6. AnimalSkin(AnimalSkinFamily.Beetle), _
  7. MarkingColor(KnownColor.Red), _
  8. MaximumEnergyPoints(20), _
  9. EatingSpeedPointsAttribute(0), _
  10. AttackDamagePointsAttribute(12), _
  11. DefendDamagePointsAttribute(12), _
  12. MaximumSpeedPointsAttribute(16), _
  13. CamouflagePointsAttribute(10), _
  14. EyesightPointsAttribute(20) _
  15. > _
  16. Public Class MyAnimal : Inherits Animal
  17. Dim targetPlant As PlantState = Nothing ' The current plant we're going after
  18. Protected Overloads Overrides Sub Initialize()
  19. ' TODO: Add Initialization logic here
  20. AddHandler Idle, AddressOf MyAnimal_Idle
  21. AddHandler Load, AddressOf MyAnimal_Load
  22. AddHandler Attacked, AddressOf MyAnimal_Attacked
  23. AddHandler MoveCompleted, AddressOf MyAnimal_MoveCompleted
  24. End Sub
  25. ' First event fired on an organism each turn
  26. Sub MyAnimal_Load(ByVal sender As Object, ByVal e As LoadEventArgs)
  27. Try
  28. If Not (targetPlant Is Nothing) Then
  29. ' See if our target plant still exists (it may have died)
  30. ' LookFor returns null if it isn't found
  31. targetPlant = CType(LookFor(targetPlant), PlantState)
  32. If (targetPlant Is Nothing) Then
  33. ' WriteTrace is the best way to debug your creatures.
  34. WriteTrace("Target plant disappeared.")
  35. End If
  36. End If
  37. Catch exc As Exception
  38. WriteTrace(exc.ToString())
  39. End Try
  40. End Sub
  41. ' Fired if we're being attacked
  42. Sub MyAnimal_Attacked(ByVal sender As Object, ByVal e As AttackedEventArgs)
  43. If (e.Attacker.IsAlive) Then
  44. Dim TheAttacker As AnimalState = e.Attacker
  45. BeginDefending(TheAttacker) 'defend against the attacker
  46. WriteTrace("Run away to some random point")
  47. Dim x As Integer = OrganismRandom.Next(0, WorldWidth - 1)
  48. Dim y As Integer = OrganismRandom.Next(0, WorldHeight - 1)
  49. BeginMoving(New MovementVector(New Point(x, y), 10))
  50. End If
  51. End Sub
  52. ' Fired when we've finished moving.
  53. Sub MyAnimal_MoveCompleted(ByVal sender As Object, ByVal e As MoveCompletedEventArgs)
  54. ' Reset the antenna value
  55. Antennas.AntennaValue = 0
  56. ' If we've stopped because something is blocking us...
  57. If (e.Reason = ReasonForStop.Blocked) Then
  58. WriteTrace("Something's blocking my way.")
  59. If (TypeOf e.BlockingOrganism Is AnimalState) Then
  60. Dim blockingAnimal As AnimalState = CType(e.BlockingOrganism, AnimalState)
  61. If (blockingAnimal.AnimalSpecies.IsSameSpecies(Me.Species)) Then
  62. ' Signal to our friend to move out of our way.
  63. WriteTrace("One of my friends is blocking my way. I'll ask him to move.")
  64. Antennas.AntennaValue = 13
  65. End If
  66. End If
  67. End If
  68. End Sub
  69. ' Fired after all other events are fired during a turn
  70. Sub MyAnimal_Idle(ByVal sender As Object, ByVal e As IdleEventArgs)
  71. Try
  72. ' Reproduce as often as possible
  73. If (CanReproduce) Then
  74. BeginReproduction(Nothing)
  75. End If
  76. ' If we can eat and we have a target plant, eat
  77. If (CanEat) Then
  78. WriteTrace("Hungry.")
  79. If Not (IsEating) Then
  80. WriteTrace("Not eating: Have target plant?")
  81. If Not (targetPlant Is Nothing) Then
  82. WriteTrace("Yes, Have target plant already.")
  83. If (WithinEatingRange(targetPlant)) Then
  84. WriteTrace("Within Range, Start eating.")
  85. BeginEating(targetPlant)
  86. If (IsMoving) Then
  87. WriteTrace("Stop while eating.")
  88. StopMoving()
  89. End If
  90. Else
  91. If Not (IsMoving) Then
  92. WriteTrace("Move to Target Plant")
  93. BeginMoving(New MovementVector(targetPlant.Position, 2))
  94. End If
  95. End If
  96. Else
  97. WriteTrace("Don't have target plant.")
  98. If Not (ScanForTargetPlant()) Then
  99. If Not (IsMoving) Then
  100. WriteTrace("No plant found, so pick a random point and move there")
  101. Dim RandomX As Integer = OrganismRandom.Next(0, WorldWidth - 1)
  102. Dim RandomY As Integer = OrganismRandom.Next(0, WorldHeight - 1)
  103. BeginMoving(New MovementVector(New Point(RandomX, RandomY), 2))
  104. Else
  105. WriteTrace("Moving and Looking...")
  106. End If
  107. End If
  108. End If
  109. Else
  110. WriteTrace("Eating.")
  111. If (IsMoving) Then
  112. WriteTrace("Stop moving while eating.")
  113. StopMoving()
  114. End If
  115. End If
  116. Else
  117. WriteTrace("Full: do nothing.")
  118. If (IsMoving) Then
  119. StopMoving()
  120. End If
  121. End If
  122. ShouldIMoveForMyFriend()
  123. Catch exc As Exception
  124. WriteTrace(exc.ToString())
  125. End Try
  126. End Sub
  127. ' Looks for target plants, and starts moving towards the first one it finds
  128. Function ScanForTargetPlant() As Boolean
  129. Try
  130. Dim foundCreatures As System.Collections.ArrayList = Scan()
  131. If (foundCreatures.Count > 0) Then
  132. Dim orgState As OrganismState
  133. ' Always move after closest plant or defend closest creature if there is one
  134. For Each orgState In foundCreatures
  135. If (TypeOf orgState Is PlantState) Then
  136. targetPlant = CType(orgState, PlantState)
  137. BeginMoving(New MovementVector(orgState.Position, 2))
  138. Return True
  139. End If
  140. Next
  141. End If
  142. Catch exc As Exception
  143. WriteTrace(exc.ToString())
  144. End Try
  145. Return False
  146. End Function
  147. ' Routine to move out of the way when blocking our friends.
  148. Sub ShouldIMoveForMyFriend()
  149. Try
  150. Dim foundAnimals As System.Collections.ArrayList = Scan()
  151. If (foundAnimals.Count > 0) Then
  152. Dim orgState As OrganismState
  153. For Each orgState In foundAnimals
  154. If (TypeOf orgState Is AnimalState) Then
  155. Dim visibleAnimal As AnimalState = CType(orgState, AnimalState)
  156. ' Only move if the animal is one of our friends (IsSameSpecies).
  157. If (visibleAnimal.Species.IsSameSpecies(Me.Species)) Then
  158. ' If the animal's antenna value is 13, it means they're blocked (see the MoveCompletedEvent method).
  159. If (visibleAnimal.Antennas.AntennaValue = 13) Then
  160. ' We're blocking our friend, so we should move.
  161. WriteTrace("I'm blocking one of my friends. I should move.")
  162. Dim newX As Integer = Me.Position.X - (visibleAnimal.Position.X - Me.Position.X)
  163. Dim newY As Integer = Me.Position.Y - (visibleAnimal.Position.Y - Me.Position.Y)
  164. BeginMoving(New MovementVector(New Point(newX, newY), 2))
  165. Return
  166. End If
  167. End If
  168. End If
  169. Next
  170. End If
  171. Catch exc As Exception
  172. WriteTrace(exc.ToString())
  173. End Try
  174. End Sub
  175. Public Overloads Overrides Sub SerializeAnimal(ByVal m As MemoryStream)
  176. ' TODO: Add Serialization logic here
  177. End Sub
  178. Public Overloads Overrides Sub DeserializeAnimal(ByVal m As MemoryStream)
  179. ' TODO: Add Deserialization logic here
  180. End Sub
  181. End Class