Jump to content

Rebuilding project with classes


Recoil

Recommended Posts

For me it has been difficult trying to navigate and make changes.  A lot of people are used to the VB6 framework that this project was originally built on, but a lot of .NET hobby devs like me seems to get lost trying to track everything down.  Making any changes, or adding additional features has become difficult.

Since I have already gotten all of the panels drawing to the screen I found a good stopping point to tackle the next part on my list.  I have decided to move the Orion Revamped project to using classes instead of holding everything in structures.  Just by putting a new class for drawing panels and button in I was able to clear out over 2000 lines just out of my ClientGraphics (modGraphics) file.  Not only is this more efficient, it makes things so much easier when I need to make a change to ALL of instances of my new class.

I have taken a look at an old project called Aphelia to see how it was done, and I have also looked at the Prospekt engine to see how it is done there.  Honestly this is not going to be a quick-fix to make the move to classes.  Right now I would like to get some ideas on the most logical approach to getting everything setup first before I even start deleting and making the necessary changes to weed out the structures.

In my general library I have created new folders to help sort all of this out.  Here are a few new classes I have on there:

Public Class Account

    Public Property ID As Integer
    Public Property Login As String
    Public Property Password As String
    Public Property Banned As Boolean
    Public Property Players() As List(Of Player)
    Public Sub New()
        'ID = 
        Login = "user@user.com"
        Password = "password"
        Banned = False
        Players = New List(Of Player)()
    End Sub
End Class

Public Class Player

    ' General
    'Public Property ID As Integer
    Public Property Name As String
    Public Property Classes As Byte
    Public Property Sprite As Integer
    Public Property Level As Byte
    Public Property Exp As Long
    Public Property Access As Byte
    Public Property Pk As Byte
    ' Vitals
    Public Property Vital() As List(Of Vitals)
    ' Stats
    Public Property Stat() As List(Of Stats)
    Public Property Points As Byte
    ' Worn equipment
    Public Property Equipment() As List(Of Equipment)
    ' Position
    Public Property Map As Integer
    Public Property X As Byte
    Public Property Y As Byte
    Public Property Dir As Byte

    ' Server use only
    'Public Property Login As String
    'Public Property Password As String
    Public Property Sex As Byte
    Public Property Inv() As PlayerInv
    Public Property Spell() As Byte

    ' Client use only
    Public Property MaxHp As Long
    Public Property MaxMp As Long
    Public Property MaxSp As Long
    Public Property XOffset As Integer
    Public Property YOffset As Integer
    Public Property Moving As Byte
    Public Property Attacking As Byte
    Public Property Running As Byte
    Public Property AttackTimer As Long
    Public Property MapGetTimer As Long
    Public Property Steps As Byte

    Public Sub New()
        'ID = 0
        Name = "New Player"
        Classes = 1
        Sprite = 1
        Level = 1
        Exp = 0
        Access = 0
        Pk = 0
        'Vitals
        'Stats
        Points = 0
        'Equipment = (0 to MaxEquipmentItems)
        Map = 1
        X = 10
        Y = 15
        Dir = 1
        Sex = 0
        'Inv = (0 to MaxInvItems)
        'Spell = (0 to MaxSpells)
        MaxHp = 10
        MaxMp = 10
        MaxSp = 10
        XOffset = 0
        YOffset = 0
        Moving = 0
        Attacking = 0
        Running = 0
        AttackTimer = 0
        MapGetTimer = 0
        Steps = 0
    End Sub
End Class

On prospekt there are no get/set values under the properties...I'm not sure why either, maybe this a a new thing and doesn't require these statements?  Regardless, I need some guidance on the best approach on creating a model on how to make everything into new classes.

Link to comment
Share on other sites

According to MSDN: "Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property. When you write code for an auto-implemented property, the Visual Basic compiler automatically creates a private field to store the property variable in addition to creating the associated Get and Set procedures."

That is a VB thing, but it doesn't look like you need to worry about gets and sets :D

Link to comment
Share on other sites

Here is what I have listed in my Player class:

Public Property Name As String
    Public Property Classes As Byte
    Public Property Sprite As Integer
    Public Property Level As Byte
    Public Property Exp As Long
    Public Property Access As Byte
    Public Property Pk As Byte
    ' Vitals
    Public Property Vital() As List(Of Vitals)
    ' Stats
    Public Property Stat() As List(Of Stats)
    Public Property Points As Byte
    ' Worn equipment
    Public Property Equipment() As List(Of Equipment)
    ' Position
    Public Property Map As Integer
    Public Property X As Byte
    Public Property Y As Byte
    Public Property Dir As Byte

    Public Property Guild As String

    ' Server use only
    Public Property Login As String
    Public Property Password As String
    Public Property Sex As Byte
    Public Property Inv() As List(Of PlayerInv)
    Public Property Spell() As List(Of Byte)

    ' Client use only
    Public Property MaxHp As Long
    Public Property MaxMp As Long
    Public Property MaxSp As Long
    Public Property XOffset As Integer
    Public Property YOffset As Integer
    Public Property Moving As Byte
    Public Property Attacking As Byte
    Public Property Running As Byte
    Public Property AttackTimer As Long
    Public Property MapGetTimer As Long
    Public Property Steps As Byte

    Public Sub New()
        
    End Sub

Now in Client, modGeneral, this is the only 3 errors I am pulling, everywhere it says Redim it says it requires an array.  I have tried to change the first one below, but it is not working either, and says Error 6 'Vital' is not a member of 'System.Collections.Generic.List(Of OrionRevampedGeneral.Player)'.

Sub startup()

        Dim players() As List(Of Player) = New List(Of Player)() {}
        'Set the Initial picScreen Sizes
        'ReDim Player(0 To MaxPlayers)

        For i = 0 To MaxPlayers
            For x = 0 To Vitals.VitalCount - 1
                ReDim players(i).Vital(x)
            Next
            For x = 0 To Stats.StatCount - 1
                ReDim Player(i).Stat(x)
            Next
            For x = 0 To Equipment.EquipmentCount - 1
                ReDim Player(i).Equipment(x)
            Next

Right now I am only trying to do the players, then I will move on to the rest.

Link to comment
Share on other sites

Lots of changes.

First, after For i = 0 to MaxPlayers add

Players.Add(new Player())

Before we can access someone in the list we have to add them to the list.

Then, remove all of the redim loops after that, we don't redim lists. We redim arrays, plus we want to do that stuff in the Sub New inside of the player class.

Make your Sub new like this

    Public Sub New()
         For x = 0 To Vitals.VitalCount - 1
                Vital.Add(0)
            Next
            For x = 0 To Stats.StatCount - 1
                Stat.Add(0)
            Next
            For x = 0 To Equipment.EquipmentCount - 1
                Equipment.Add(0)
            Next
    End Sub

Finally change your definitions:

    Public Property Vital() As List(Of Vitals)

    Public Property Stat() As List(Of Stats)

    Public Property Equipment() As List(Of Equipment)

As they should all be As List(Of Integer) = new List(Of Integer)

Link to comment
Share on other sites

For some reason it was causing an error with the equipment, so I changed it to this:

Public Sub New()
        For X = 0 To Vitals.VitalCount - 1
            Vital.Add(0)
        Next
        For X = 0 To Stats.StatCount - 1
            Stat.Add(0)
        Next
        For X = 0 To Enumerations.Equipment.EquipmentCount - 1
            Equipment.Add(0)
        Next
    End Sub

And am now getting the error on each of the "X": "Loop control variable cannot be a property or a late-bound indexed array."

Also, my properties look like this:

Public Property Vital() As List(Of Integer) = New List(Of Integer)
Public Property Stat() As List(Of Integer) = New List(Of Integer)
Public Property Equipment() As List(Of Integer) = New List(Of Integer)

Link to comment
Share on other sites

Imports OrionRevampedGeneral.Core
Imports OrionRevampedGeneral.Core.Enumerations
Imports OrionRevampedGeneral.Core.Types

Public Class PlayerCls

    ' General
    Public Property Name As String
    Public Property Classes As Byte
    Public Property Sprite As Integer
    Public Property Level As Byte
    Public Property Exp As Long
    Public Property Access As Byte
    Public Property Pk As Byte
    ' Vitals
    Public Property Vital() As New List(Of Integer)
    ' Stats
    Public Property Stat() As New List(Of Integer)
    Public Property Points As Byte
    ' Worn equipment
    Public Property Equipment() As New List(Of Integer)
    ' Position
    Public Property Map As Integer
    Public Property X As Byte
    Public Property Y As Byte
    Public Property Dir As Byte

    Public Property Guild As String

    ' Server use only
    Public Property Login As String
    Public Property Password As String
    Public Property Sex As Byte
    Public Property Inv As New List(Of PlayerInv)
    Public Property Spell As New List(Of Byte)

    ' Client use only
    Public Property MaxHp As Long
    Public Property MaxMp As Long
    Public Property MaxSp As Long
    Public Property XOffset As Integer
    Public Property YOffset As Integer
    Public Property Moving As Byte
    Public Property Attacking As Byte
    Public Property Running As Byte
    Public Property AttackTimer As Long
    Public Property MapGetTimer As Long
    Public Property Steps As Byte

    Public Sub New()
        For i = 0 To Enumerations.Vitals.VitalCount - 1
            Vital.Add(0)
        Next
        For i = 0 To Enumerations.Stats.StatCount - 1
            Stat.Add(0)
        Next
        For i = 0 To Enumerations.Equipment.EquipmentCount - 1
            Equipment.Add(0)
        Next
    End Sub

End Class

Public Class PlayerInv

    Public Property Num As Byte
    Public Property Value As Long

End Class

Link to comment
Share on other sites

I have commented out stuff instead of deleting it:

For i = 0 To MaxPlayers
            Players.Add(New Player())
            'For x = 0 To Vitals.VitalCount - 1
            '    ReDim players(i).Vital(x)
            'Next
            'For x = 0 To Stats.StatCount - 1
            '    ReDim Player(i).Stat(x)
            'Next
            'For x = 0 To Equipment.EquipmentCount - 1
            '    ReDim Player(i).Equipment(x)
            'Next
            Items.Add(New Item())
            ReDim Items(0 To MaxBank)
            For x = 0 To Stats.StatCount - 1
                ReDim Items(i).AddStat(x)
            Next
            For x = 0 To Stats.StatCount - 1
                ReDim Items(i).StatReq(x)
            Next
        Next

Link to comment
Share on other sites

I think you want to shoot for something like this:

'Init Players
     For i = 0 To MaxPlayers
            Players.Add(New Player())
     Next

'Init Items
     For i = 0 to MaxItems
            Items.Add(new Item())
            'Throw the stuff that was here into the items Sub New method like we did in player.
     Next
     
'Init Spells
'etc

'Init Npcs
'etc


Link to comment
Share on other sites

This is back in Server, modGeneral, Startup.  This is pulling a null value on startup:

For i = 0 To MaxPlayers
            'Banks.Add(New Bank())
            Banks.Item.Add(New PlayerInv())
            '    ReDim Bank.Item(0 To MaxBank)
            '    For x = 0 To Stats.StatCount - 1
            '        ReDim Bank.Item(i).AddStat(x)
            '    Next
            '    For x = 0 To Stats.StatCount - 1
            '        ReDim Bank.Item(i).StatReq(x)
            '    Next
        Next

Here is my bank class:

Imports OrionRevampedGeneral.Core
Public Class Bank
    Public Property Item As New List(Of PlayerInv)
    Public Sub New()
        For i = 0 To Constants.MaxPlayers
            Item.Add(New PlayerInv())
        Next
    End Sub
End Class

Link to comment
Share on other sites

Server->ServerTypes:

Public MapNpcs As New List(Of MapData) 

MapData class

Public Class MapData

    Public Property Npc() As New List(Of Integer)

    Public Sub New()
        
        For i = 0 To Constants.MaxMapNpcs
            Npc.Add(0)
        Next

    End Sub
End Class

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...