Attribute VB_Name = "Character1"
'// Programmed by Ervin Louie B. Pe Benito
'// Character1.bas
'***** Main Character *****
Option Explicit
Public Type User
    Moved As Boolean    'If User Has Recently Moved
    Sprite(1 To 8) As Direct3DTexture8   'Used Sprite
    AnimMax As Byte 'Max Amount of Frames On Sprite
    Direction As Integer   'Direction User Is Facing
    AnimCount As Single 'Current Animation Frame Count
    
    MapX As Long   'X location on screen
    MapY As Long   'Y location on screen

End Type

Public Type fightChar
    Sprite(1 To 4) As Direct3DTexture8
    AnimCount As Single
    Status As Integer
    MapX As Long 'x location on map
    MapY As Long 'y location on map
End Type

Const Speed = 8

Public Userchar As User
Public UserFightChar As fightChar
Public EnemyChar(0 To 4) As fightChar

Private BattleInitiator As Integer
'+++++++++ used in credits screen
Public Sub autoAnimate0()
    If Autoframecount = 2 Then
        Dev.SetTexture 0, Userchar.Sprite(1)
    Else
        Dev.SetTexture 0, Userchar.Sprite(2)
    End If
End Sub
Public Sub autoAnimate1()

    If Autoframecount = 2 Then
        Dev.SetTexture 0, Userchar.Sprite(3)
    Else
        Dev.SetTexture 0, Userchar.Sprite(4)
    End If
End Sub
'+++++++++++++++++++++++++++++++++
Public Sub Animate0()
    If Userchar.AnimCount = 2 Then
        Dev.SetTexture 0, Userchar.Sprite(1)
    Else
        Dev.SetTexture 0, Userchar.Sprite(2)
    End If
End Sub
'moveright
Public Sub Animate1()
    If Userchar.AnimCount = 2 Then
        Dev.SetTexture 0, Userchar.Sprite(3)
    Else
        Dev.SetTexture 0, Userchar.Sprite(4)
    End If
End Sub
'moveleft
Public Sub Animate2()
    If Userchar.AnimCount = 2 Then
        Dev.SetTexture 0, Userchar.Sprite(5)
    Else
        Dev.SetTexture 0, Userchar.Sprite(6)
    End If
End Sub
'moveup
Public Sub Animate3()
    If Userchar.AnimCount = 2 Then
        Dev.SetTexture 0, Userchar.Sprite(7)
    Else
        Dev.SetTexture 0, Userchar.Sprite(8)
    End If
End Sub

Public Sub CharMoveDown(iMapNum As Integer)
Dim y As Integer
    If Userchar.MapY + Speed < GameMapScreenHeight Then
        Userchar.MapY = Userchar.MapY + Speed
        WalkingSound
    End If
    CheckIfWalkable 0, iMapNum
End Sub
Public Sub CharMoveUp(iMapNum As Integer)
    If Userchar.MapY - Speed > GameMapScreenY Then
        Userchar.MapY = Userchar.MapY - Speed
        WalkingSound
    End If
    CheckIfWalkable 2, iMapNum
End Sub
Public Sub CharMoveRight(iMapNum As Integer)
    If Userchar.MapX + Speed < GameMapScreenWidth Then
        Userchar.MapX = Userchar.MapX + Speed
        WalkingSound
    End If
    CheckIfWalkable 1, iMapNum
End Sub
Public Sub CharMoveLeft(iMapNum As Integer)
    If Userchar.MapX - Speed > GameMapScreenX Then
        Userchar.MapX = Userchar.MapX - Speed
        WalkingSound
    End If
    CheckIfWalkable 3, iMapNum
End Sub
'Main Character
Public Sub UserFightAnimate(Optional iStatus As Integer = 0)
Static framecount, framewait As Integer
    framewait = 5
    If iStatus = 0 Or iStatus = 3 Or iStatus = 4 Then
        If framecount > framewait Then
            UserFightChar.AnimCount = UserFightChar.AnimCount + 1
            If Userchar.AnimCount > 2 Then
                Userchar.AnimCount = 1
            Else
                Userchar.AnimCount = Userchar.AnimCount + 1
            End If
            framecount = 1
        Else
            framecount = framecount + 1
        End If
    
        If Userchar.AnimCount >= 2 Then
            Dev.SetTexture 0, UserFightChar.Sprite(1)
            Set_Vertex GameMapScreenX + 192, GameMapScreenY + 160, 64, 32, 16777215, 16777215, 16777215, 16777215
            Dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex(0), Len(Vertex(0))
        Else
            Dev.SetTexture 0, UserFightChar.Sprite(2)
            Set_Vertex GameMapScreenX + 192, GameMapScreenY + 160, 64, 32, 16777215, 16777215, 16777215, 16777215
            Dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex(0), Len(Vertex(0))
        End If
        'if status is lifeup
        If iStatus = 3 Then
            LifeUp GameMapScreenX + 192, GameMapScreenY + 128
            Sound_Play 7
        'graphic for miss
        ElseIf iStatus = 4 Then
            Missed GameMapScreenX + 192, GameMapScreenY + 128
            Sound_Play 8
        End If
    ElseIf iStatus = 1 Then
        Dev.SetTexture 0, UserFightChar.Sprite(3)
        Set_Vertex GameMapScreenX + 192, GameMapScreenY + 160, 64, 32, 16777215, 16777215, 16777215, 16777215
        Dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex(0), Len(Vertex(0))
    ElseIf iStatus = 2 Then
        Dev.SetTexture 0, UserFightChar.Sprite(4)
        Set_Vertex GameMapScreenX + 192, GameMapScreenY + 160, 64, 32, 16777215, 16777215, 16777215, 16777215
        Dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex(0), Len(Vertex(0))
        If iEnemy = 0 Then
            Sound_Play 3
        ElseIf iEnemy = 1 Then
            Sound_Play 2
        ElseIf iEnemy = 2 Then
            Sound_Play 3
        ElseIf iEnemy = 3 Then
            Sound_Play 4
        ElseIf iEnemy = 4 Then
            Sound_Play 2
        End If
    End If
End Sub

'Enemy Characters
Public Sub EnemyFightAnimate(iNum As Integer, Optional iStatus As Integer = 0)
Static framewait, framecount As Integer
    framewait = 8
If iNum < 2 Then
    If iStatus = 0 Or iStatus = 3 Or iStatus = 4 Then
        If framecount > framewait Then
            EnemyChar(iNum).AnimCount = EnemyChar(iNum).AnimCount + 1
            If EnemyChar(iNum).AnimCount > 3 Then
                EnemyChar(iNum).AnimCount = 1
            Else
                EnemyChar(iNum).AnimCount = EnemyChar(iNum).AnimCount + 1
            End If
            framecount = 1
        Else
            framecount = framecount + 1
        End If

        If EnemyChar(iNum).AnimCount >= 2 Then
            Dev.SetTexture 0, EnemyChar(iNum).Sprite(2)
            Set_Vertex GameMapScreenWidth - 224, GameMapScreenY + 160, 64, 32, 16777215, 16777215, 16777215, 16777215
            Dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex(0), Len(Vertex(0))
        Else
            Dev.SetTexture 0, EnemyChar(iNum).Sprite(1)
            Set_Vertex GameMapScreenWidth - 224, GameMapScreenY + 160, 64, 32, 16777215, 16777215, 16777215, 16777215
            Dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex(0), Len(Vertex(0))
        End If
        
        If iStatus = 3 Then
            LifeUp GameMapScreenWidth - 192, GameMapScreenY + 128
            Sound_Play 7
        ElseIf iStatus = 4 Then
            Missed GameMapScreenWidth - 196, GameMapScreenY + 128
            Sound_Play 8
        End If
        
    ElseIf iStatus = 1 Then
        Dev.SetTexture 0, EnemyChar(iNum).Sprite(3)
        Set_Vertex GameMapScreenWidth - 224, GameMapScreenY + 160, 64, 32, 16777215, 16777215, 16777215, 16777215
        Dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex(0), Len(Vertex(0))
        
    ElseIf iStatus = 2 Then
        Dev.SetTexture 0, EnemyChar(iNum).Sprite(4)
        Set_Vertex GameMapScreenWidth - 224, GameMapScreenY + 160, 64, 32, 16777215, 16777215, 16777215, 16777215
        Dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex(0), Len(Vertex(0))
        Sound_Play 2
    End If
Else
        If iStatus = 0 Or iStatus = 3 Or iStatus = 4 Then
        If framecount > framewait Then
            EnemyChar(iNum).AnimCount = EnemyChar(iNum).AnimCount + 1
            If EnemyChar(iNum).AnimCount > 3 Then
                EnemyChar(iNum).AnimCount = 1
            Else
                EnemyChar(iNum).AnimCount = EnemyChar(iNum).AnimCount + 1
            End If
            framecount = 1
        Else
            framecount = framecount + 1
        End If

        If EnemyChar(iNum).AnimCount >= 2 Then
            Dev.SetTexture 0, EnemyChar(iNum).Sprite(2)
            Set_Vertex GameMapScreenWidth - 224, GameMapScreenY + 160, 64, 64, 16777215, 16777215, 16777215, 16777215
            Dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex(0), Len(Vertex(0))
        Else
            Dev.SetTexture 0, EnemyChar(iNum).Sprite(1)
            Set_Vertex GameMapScreenWidth - 224, GameMapScreenY + 160, 64, 64, 16777215, 16777215, 16777215, 16777215
            Dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex(0), Len(Vertex(0))
        End If
        If iStatus = 3 Then
            LifeUp GameMapScreenWidth - 192, GameMapScreenY + 128
            Sound_Play 7
        ElseIf iStatus = 4 Then
            Missed GameMapScreenWidth - 196, GameMapScreenY + 128
            Sound_Play 8
        End If
        
    ElseIf iStatus = 1 Then
        Dev.SetTexture 0, EnemyChar(iNum).Sprite(3)
        Set_Vertex GameMapScreenWidth - 224, GameMapScreenY + 160, 64, 64, 16777215, 16777215, 16777215, 16777215
        Dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex(0), Len(Vertex(0))

    ElseIf iStatus = 2 Then
        Dev.SetTexture 0, EnemyChar(iNum).Sprite(4)
        Set_Vertex GameMapScreenWidth - 224, GameMapScreenY + 160, 64, 64, 16777215, 16777215, 16777215, 16777215
        Dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex(0), Len(Vertex(0))
        Sound_Play 2
    End If
End If
End Sub
Public Sub DestroyChar()
Dim x As Integer
    For x = 1 To 8
        Set Userchar.Sprite(x) = Nothing
    Next
    For x = 1 To 4
        Set UserFightChar.Sprite(x) = Nothing
        Set EnemyChar(0).Sprite(x) = Nothing
        Set EnemyChar(1).Sprite(x) = Nothing
        Set EnemyChar(2).Sprite(x) = Nothing
        Set EnemyChar(3).Sprite(x) = Nothing
        Set EnemyChar(4).Sprite(x) = Nothing
    Next
End Sub

Public Function BattleDeterminer(Optional iNum As Integer = 60) As Boolean
    BattleInitiator = CInt(Rnd() * iNum) + 1
    If BattleInitiator = 1 Then
        BattleDeterminer = True
    Else
        BattleDeterminer = False
    End If
End Function

Public Sub WalkingSound()
Static iWalk As Integer
    If iWalk = 1 Then
        Sound_Play 5
        iWalk = 2
    Else
        Sound_Play 6
        iWalk = 1
    End If
End Sub

Public Sub LifeUp(x, y As Long)
    Dev.SetTexture 0, Misc(27)
    Set_Vertex x, y, 32, 32, 16777215, 12777215, 16777215, 16777215
    Dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex(0), Len(Vertex(0))
End Sub

Public Sub Missed(x, y As Long)
    Dev.SetTexture 0, Misc(32)
    Set_Vertex x, y, 32, 32, 16777215, 12777215, 16777215, 16777215
    Dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex(0), Len(Vertex(0))
End Sub

Public Function HitDeterminer(lAgilityOfWillBeHit As Long) As Boolean
Dim itemp As Integer
    itemp = CInt(Rnd() * 12) + 1
    If lAgilityOfWillBeHit < 20 Then
        If itemp < 2 Then
            HitDeterminer = False
        Else
            HitDeterminer = True
        End If
    ElseIf lAgilityOfWillBeHit < 30 Then
        If itemp < 4 Then
            HitDeterminer = False
        Else
            HitDeterminer = True
        End If
    ElseIf lAgilityOfWillBeHit < 40 Then
        If itemp < 6 Then
            HitDeterminer = False
        Else
            HitDeterminer = True
        End If
    ElseIf lAgilityOfWillBeHit < 50 Then
        If itemp < 7 Then
            HitDeterminer = False
        Else
            HitDeterminer = True
        End If
    ElseIf lAgilityOfWillBeHit < 60 Then
        If itemp < 8 Then
            HitDeterminer = False
        Else
            HitDeterminer = True
        End If
    ElseIf lAgilityOfWillBeHit < 70 Then
        If itemp < 9 Then
            HitDeterminer = False
        Else
            HitDeterminer = True
        End If
    ElseIf lAgilityOfWillBeHit < 80 Then
        If itemp < 10 Then
            HitDeterminer = False
        Else
            HitDeterminer = True
        End If
    ElseIf lAgilityOfWillBeHit < 90 Then
        If itemp < 12 Then
            HitDeterminer = False
        Else
            HitDeterminer = True
        End If
    End If
End Function
'if FirstMoveDeterminer = true then the main character moves first
Public Function FirstMoveDeterminer() As Boolean
    If PlayerStats.Dexterity < tempstats.Dexterity Or iEnemy = 4 Then
        FirstMoveDeterminer = False
    Else
        FirstMoveDeterminer = True
    End If
End Function
'if RetreatDeterminer = true then the main character could escape
Public Function RetreatDeterminer() As Boolean
Dim lNum As Long
    If PlayerStats.Dexterity < tempstats.Dexterity Then
        lNum = CLng(Rnd() * 12) - 3
    Else
        lNum = CLng(Rnd() * 12) + 2
    End If
    If lNum < 7 Then
        RetreatDeterminer = False
    Else
        RetreatDeterminer = True
    End If
End Function
'Determines the amount of damage taken... varies accordingly because of
'attacker's damage and the ammount of armor of being attacked
Public Function DamageTakenDeterminer(lAttackerDamage As Long, lBeingAttackedArmor) As Long
    If (lAttackerDamage - lBeingAttackedArmor) < 1 Then
        DamageTakenDeterminer = 0
    Else
        DamageTakenDeterminer = lAttackerDamage - lBeingAttackedArmor
    End If
End Function

