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

📄 vehicle.vb

📁 一个.Net下用VB编写的用于游戏的人工智能引擎
💻 VB
字号:
Imports SteeringVB.ParamLoader

Public Class Vehicle
    Inherits MovingEntity

    '//a pointer to the world data. So a vehicle can access any obstacle,
    '//path, wall or agent data
    Dim m_pWorld As GameWorld

    '//the steering behavior class
    Dim m_pSteering As SteeringBehavior


    '//some steering behaviors give jerky looking movement. The
    '//following members are used to smooth the vehicle's heading
    Dim m_pHeadingSmoother As Smoother

    '//this vector represents the average of the vehicle's heading
    '//vector smoothed over the last few frames
    Dim m_vSmoothedHeading As Vector2D

    '//when true, smoothing is active
    Dim m_bSmoothingOn As Boolean


    '//keeps a track of the most recent update time. (some of the
    '//steering behaviors make use of this - see Wander)
    Dim m_dTimeElapsed As Double


    '//buffer for the vehicle shape
    Dim m_vecVehicleVB As New ArrayList

    '//fills the buffer with vertex data
    Private Sub InitializeBuffer()
        Dim NumVehicleVerts As Integer = 3

        Dim vehicle() As Vector2D = {New Vector2D(-1.0F, 0.6F), New Vector2D(1.0F, 0.0F), New Vector2D(-1.0F, -0.6F)}

        '//setup the vertex buffers and calculate the bounding radius
        Dim i As Integer
        For i = 0 To NumVehicleVerts - 1
            m_vecVehicleVB.Add(vehicle(i))
        Next
    End Sub

    '//disallow the copying of Vehicle types
    'Vehicle(const Vehicle&);
    'Vehicle& operator=(const Vehicle&);


    Public Sub New(ByVal world As GameWorld, ByVal position As Vector2D, ByVal rotation As Double, ByVal velocity As Vector2D, ByVal mass As Double, ByVal max_force As Double, ByVal max_speed As Double, ByVal max_turn_rate As Double, ByVal scale As Double)
        MyBase.New(position, scale, velocity, max_speed, New Vector2D(Math.Sin(rotation), -Math.Cos(rotation)), mass, New Vector2D(scale, scale), max_turn_rate, max_force)


        m_pWorld = world
        m_vSmoothedHeading = New Vector2D(0, 0)
        m_bSmoothingOn = False
        m_dTimeElapsed = 0.0
        InitializeBuffer()

        '//set up the steering behavior class
        m_pSteering = New SteeringBehavior(Me)

        m_pSteering.ObstacleAvoidanceOn()
        '//set up the smoother
        m_pHeadingSmoother = New Smoother(Prm.NumSamplesForSmoothing, New Vector2D(0.0, 0.0))

    End Sub

    '//updates the vehicle's position and orientation


    '//-------------------------------------------accessor methods
    Public ReadOnly Property Steering() As SteeringBehavior
        Get
            Return m_pSteering
        End Get
    End Property

    Public ReadOnly Property World() As GameWorld
        Get
            Return m_pWorld
        End Get
    End Property


    Public ReadOnly Property SmoothedHeading() As Vector2D
        Get
            Return m_vSmoothedHeading
        End Get
    End Property

    Public ReadOnly Property isSmoothingOn() As Boolean
        Get
            Return m_bSmoothingOn
        End Get
    End Property

    Public Sub SmoothingOn()
        m_bSmoothingOn = True
    End Sub

    Public Sub SmoothingOff()
        m_bSmoothingOn = False
    End Sub

    Public Sub ToggleSmoothing()
        m_bSmoothingOn = Not m_bSmoothingOn
    End Sub

    Public ReadOnly Property TimeElapsed() As Double
        Get
            Return m_dTimeElapsed
        End Get
    End Property


    Public Overrides Sub Update(ByVal time_elapsed As Double)
        '//update the time elapsed
        m_dTimeElapsed = time_elapsed

        '//keep a record of its old position so we can update its cell later
        '//in this method
        Dim OldPos As Vector2D = Pos()


        Dim SteeringForce As Vector2D

        '//calculate the combined force from each steering behavior in the 
        '//vehicle's list
        SteeringForce = m_pSteering.Calculate()

        '//Acceleration = Force/Mass
        Dim acceleration As Vector2D = SteeringForce.Divided(m_dMass)

        '//update velocity
        m_vVelocity.PlusEqual(acceleration.Mutiply(time_elapsed))

        '//make sure vehicle does not exceed maximum velocity
        m_vVelocity.Truncate(m_dMaxSpeed)

        '//update the position
        m_vPos.PlusEqual(m_vVelocity.Mutiply(time_elapsed))

        '//update the heading if the vehicle has a non zero velocity
        If (m_vVelocity.LengthSQ() > 0.00000001) Then
            m_vHeading = Vector2D.Vec2DNormalize(m_vVelocity)
            m_vSide = m_vHeading.Perp()
        End If

        '//EnforceNonPenetrationConstraint(this, World()->Agents());

        '//treat the screen as a toroid
        Vector2D.WrapAround(m_vPos, m_pWorld.cxClient(), m_pWorld.cyClient())

        '//update the vehicle's current cell if space partitioning is turned on
        If (Steering().isSpacePartitioningOn()) Then
            World().CellSpace().UpdateEntity(Me, OldPos)

        End If

        If (isSmoothingOn()) Then
            m_vSmoothedHeading = m_pHeadingSmoother.Update(Heading())
        End If

    End Sub

    Public Overrides Sub Render(ByVal g As Graphics)
        '//a vector to hold the transformed vertices
        Dim m_vecVehicleVBTrans As New ArrayList

        '//render neighboring vehicles in different colors if requested
        Dim p As Pen
        If (m_pWorld.RenderNeighbors()) Then
            If (ID() = 0) Then
                p = Pens.Red
            Else
                If IsTagged() Then
                    p = Pens.Green
                Else
                    p = Pens.Blue
                End If
            End If
        Else
            p = Pens.Blue
        End If


        If (Steering().isInterposeOn()) Then p = Pens.Red

        If (Steering().isHideOn()) Then p = Pens.Green

        If (isSmoothingOn()) Then
            m_vecVehicleVBTrans = Transformations.WorldTransform(m_vecVehicleVB, Pos(), SmoothedHeading(), SmoothedHeading().Perp(), Scale())
        Else
            m_vecVehicleVBTrans = Transformations.WorldTransform(m_vecVehicleVB, Pos(), Heading(), Side(), Scale())
        End If





        Gdi.ClosedShape(g, p, m_vecVehicleVBTrans)

        '//render any visual aids / and or user options
        If (m_pWorld.ViewKeys()) Then Steering().RenderAids()

    End Sub
End Class

⌨️ 快捷键说明

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