⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 steeringbehavior.vb

📁 一个.Net下用VB编写的用于游戏的人工智能引擎
💻 VB
📖 第 1 页 / 共 5 页
字号:
        Dim theta As Double = Utils.RandFloat() * Utils.TwoPi

        '//create a vector to a target position on the wander circle
        m_vWanderTarget = New Vector2D(m_dWanderRadius * Math.Cos(theta), m_dWanderRadius * Math.Sin(theta))

        '//create a Path
        m_pPath = New _Path
        m_pPath.LoopOn()
    End Sub

    '//calculates and sums the steering forces from any active behaviors
    '//----------------------- Calculate --------------------------------------
    '//
    '//  calculates the accumulated steering force according to the method set
    '//  in m_SummingMethod
    '//------------------------------------------------------------------------
    Public Function Calculate() As Vector2D
        '//reset the steering force
        m_vSteeringForce.Zero()

        '//use space partitioning to calculate the neighbours of this vehicle
        '//if switched on. If not, use the standard tagging system
        If Not isSpacePartitioningOn() Then
            '//tag neighbors if any of the following 3 group behaviors are switched on
            If IsOn(behavior_type.separation) Or IsOn(behavior_type.allignment) Or IsOn(behavior_type.cohesion) Then
                m_pVehicle.World.TagVehiclesWithinViewRange(m_pVehicle, m_dViewDistance)
            End If
        Else
            '//calculate neighbours in cell-space if any of the following 3 group
            '//behaviors are switched on
            If IsOn(behavior_type.separation) Or IsOn(behavior_type.allignment) Or IsOn(behavior_type.cohesion) Then
                m_pVehicle.World().CellSpace().CalculateNeighbors(m_pVehicle.Pos(), m_dViewDistance)
            End If
        End If
        Select Case m_SummingMethod
            Case summing_method.weighted_average

                m_vSteeringForce = CalculateWeightedSum()

            Case summing_method.prioritized

                m_vSteeringForce = CalculatePrioritized()

            Case summing_method.dithered

                m_vSteeringForce = CalculateDithered()

            Case Else
                m_vSteeringForce = New Vector2D(0, 0)

        End Select

        Return m_vSteeringForce
    End Function

    '//calculates the component of the steering force that is parallel
    '//with the vehicle heading
    '//------------------------- ForwardComponent -----------------------------
    '//
    '//  returns the forward oomponent of the steering force
    '//------------------------------------------------------------------------
    Public Function ForwardComponent() As Double
        Return m_pVehicle.Heading().Dot(m_vSteeringForce)
    End Function

    '//calculates the component of the steering force that is perpendicuar
    '//with the vehicle heading
    '//--------------------------- SideComponent ------------------------------
    '//  returns the side component of the steering force
    '//------------------------------------------------------------------------
    Public Function SideComponent() As Double
        Return m_pVehicle.Side().Dot(m_vSteeringForce)
    End Function



    '//renders visual aids and info for seeing how each behavior is
    '//calculated
    Public Sub RenderAids()

    End Sub

    Public Sub SetTarget(ByVal t As Vector2D)
        m_vTarget = t
    End Sub

    Public Sub SetTargetAgent1(ByVal Agent As Vehicle)
        m_pTargetAgent1 = Agent
    End Sub

    Public Sub SetTargetAgent2(ByVal Agent As Vehicle)
        m_pTargetAgent2 = Agent
    End Sub

    Public Property Offset() As Vector2D
        Get
            Return m_vOffset
        End Get
        Set(ByVal Value As Vector2D)
            m_vOffset = Value
        End Set
    End Property

    Public Sub SetPath(ByVal new_path As ArrayList)
        m_pPath.SetPath(new_path)
    End Sub

    Public Sub CreateRandomPath(ByVal num_waypoints As Integer, ByVal mx As Integer, ByVal my As Integer, ByVal cx As Integer, ByVal cy As Integer)
        m_pPath.CreateRandomPath(num_waypoints, mx, my, cx, cy)
    End Sub

    Public ReadOnly Property Force() As Vector2D
        Get
            Return m_vSteeringForce
        End Get
    End Property

    Public Sub ToggleSpacePartitioningOnOff()
        m_bCellSpaceOn = Not m_bCellSpaceOn
    End Sub
    Public ReadOnly Property isSpacePartitioningOn() As Boolean
        Get
            Return m_bCellSpaceOn
        End Get
    End Property

    Public Sub SetSummingMethod(ByVal sm As summing_method)
        m_SummingMethod = sm
    End Sub

    Public Sub FleeOn()
        m_iFlags = m_iFlags Or behavior_type.flee
    End Sub

    Public Sub SeekOn()
        m_iFlags = m_iFlags Or behavior_type.seek
    End Sub

    Public Sub ArriveOn()
        m_iFlags = m_iFlags Or behavior_type.arrive
    End Sub

    Public Sub WanderOn()
        m_iFlags = m_iFlags Or behavior_type.wander
    End Sub

    Public Sub PursuitOn(ByVal v As Vehicle)
        m_iFlags = m_iFlags Or behavior_type.pursuit
        m_pTargetAgent1 = v
    End Sub

    Public Sub EvadeOn(ByVal v As Vehicle)
        m_iFlags = m_iFlags Or behavior_type.evade
        m_pTargetAgent1 = v
    End Sub

    Public Sub CohesionOn()
        m_iFlags = m_iFlags Or behavior_type.cohesion
    End Sub

    Public Sub SeparationOn()
        m_iFlags = m_iFlags Or behavior_type.separation
    End Sub

    Public Sub AlignmentOn()
        m_iFlags = m_iFlags Or behavior_type.allignment
    End Sub

    Public Sub ObstacleAvoidanceOn()
        m_iFlags = m_iFlags Or behavior_type.obstacle_avoidance
    End Sub

    Public Sub WallAvoidanceOn()
        m_iFlags = m_iFlags Or behavior_type.wall_avoidance
    End Sub

    Public Sub FollowPathOn()
        m_iFlags = m_iFlags Or behavior_type.follow_path
    End Sub

    Public Sub InterposeOn(ByVal v1 As Vehicle, ByVal v2 As Vehicle)
        m_iFlags = m_iFlags Or behavior_type.interpose
        m_pTargetAgent1 = v1
        m_pTargetAgent2 = v2
    End Sub

    Public Sub HideOn(ByVal v As Vehicle)
        m_iFlags = m_iFlags Or behavior_type.hide
        m_pTargetAgent1 = v
    End Sub

    Public Sub OffsetPursuitOn(ByVal v1 As Vehicle, ByVal offset As Vector2D)
        m_iFlags = m_iFlags Or behavior_type.offset_pursuit
        m_vOffset = offset
        m_pTargetAgent1 = v1
    End Sub

    Public Sub FlockingOn()
        CohesionOn()
        AlignmentOn()
        SeparationOn()
        WanderOn()
    End Sub

    Public Sub FleeOff()
        If IsOn(behavior_type.flee) Then m_iFlags = m_iFlags Xor behavior_type.flee
    End Sub

    Public Sub SeekOff()
        If IsOn(behavior_type.seek) Then m_iFlags = m_iFlags Xor behavior_type.seek
    End Sub

    Public Sub ArriveOff()
        If IsOn(behavior_type.arrive) Then m_iFlags = m_iFlags Xor behavior_type.arrive
    End Sub

    Public Sub WanderOff()
        If IsOn(behavior_type.wander) Then m_iFlags = m_iFlags Xor behavior_type.wander
    End Sub

    Public Sub PursuitOff()
        If IsOn(behavior_type.pursuit) Then m_iFlags = m_iFlags Xor behavior_type.pursuit
    End Sub

    Public Sub EvadeOff()
        If IsOn(behavior_type.evade) Then m_iFlags = m_iFlags Xor behavior_type.evade
    End Sub

    Public Sub CohesionOff()
        If IsOn(behavior_type.cohesion) Then m_iFlags = m_iFlags Xor behavior_type.cohesion
    End Sub

    Public Sub SeparationOff()
        If IsOn(behavior_type.separation) Then m_iFlags = m_iFlags Xor behavior_type.separation
    End Sub

    Public Sub AlignmentOff()
        If IsOn(behavior_type.allignment) Then m_iFlags = m_iFlags Xor behavior_type.allignment
    End Sub

    Public Sub ObstacleAvoidanceOff()
        If IsOn(behavior_type.obstacle_avoidance) Then
            m_iFlags = m_iFlags Xor behavior_type.obstacle_avoidance
        End If
    End Sub

    Public Sub WallAvoidanceOff()
        If IsOn(behavior_type.wall_avoidance) Then m_iFlags = m_iFlags Xor behavior_type.wall_avoidance
    End Sub

    Public Sub FollowPathOff()
        If IsOn(behavior_type.follow_path) Then m_iFlags = m_iFlags Xor behavior_type.follow_path
    End Sub

    Public Sub InterposeOff()
        If IsOn(behavior_type.interpose) Then m_iFlags = m_iFlags Xor behavior_type.interpose
    End Sub

    Public Sub HideOff()
        If IsOn(behavior_type.hide) Then m_iFlags = m_iFlags Xor behavior_type.hide
    End Sub

    Public Sub OffsetPursuitOff()
        If IsOn(behavior_type.offset_pursuit) Then m_iFlags = m_iFlags Xor behavior_type.offset_pursuit
    End Sub

    Public Sub FlockingOff()
        CohesionOff()
        AlignmentOff()
        SeparationOff()
        WanderOff()
    End Sub

    Public Function isFleeOn() As Boolean
        Return IsOn(behavior_type.flee)
    End Function

    Public Function isSeekOn()
        Return IsOn(behavior_type.seek)
    End Function

    Public Function isArriveOn() As Boolean
        Return IsOn(behavior_type.arrive)
    End Function

    Public Function isWanderOn() As Boolean
        Return IsOn(behavior_type.wander)
    End Function

    Public Function isPursuitOn() As Boolean
        Return IsOn(behavior_type.pursuit)
    End Function

    Public Function isEvadeOn() As Boolean
        Return IsOn(behavior_type.evade)
    End Function

    Public Function isCohesionOn() As Boolean
        Return IsOn(behavior_type.cohesion)
    End Function

    Public Function isSeparationOn() As Boolean
        Return IsOn(behavior_type.separation)
    End Function

    Public Function isAlignmentOn() As Boolean
        Return IsOn(behavior_type.allignment

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -