Improving Types in this Code

BlitzMax Forums/BlitzMax Beginners Area/Improving Types in this Code

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Something like this maybe?

Strict

Type WClass
	Field Name$
	Field Atk#, Str#, Skl#

	Function Create(_Name$,_Atk#,_Str#,_Skl#)
	 Local newclass:WClass = New WClass
	 newclass.Name$ = _Name$
	 newclass.Atk = _Atk
	 newclass.Str = _Str
	 newclass.Skl = _Skl
	 ListAddLast(WClassList,newclass)
	End Function

	Function Find:WClass(_Name$)
		For Local c:WClass = EachIn WClassList
			If c.Name$ = _Name$ Then Return c
		Next
		Return Null
	End Function

End Type

Type WType
	Field Name$
	Field Speed#, Modif#

	Function Create(_Name$,_Speed#,_Modif#)
	 Local newtype:WType = New WType
	 newtype.Name$ = _Name$
	 newtype.Speed = _Speed
	 newtype.Modif = _Modif
	 ListAddLast(WTypeList,newtype)
	End Function

	Function Find:WType(_Name$)
		For Local t:WType = EachIn WTypeList
			If t.Name$ = _Name$ Then Return t
		Next
		Return Null
	End Function

End Type

Type WElement
	Field Name$
	Field DamageX2$

	Function Create(_Name$,_DamageX2$)
	 Local newelement:WElement = New WElement
	 newelement.Name$ = _Name$
	 newelement.DamageX2 = _DamageX2
	 ListAddLast(WElementList,newelement)
	End Function

	Function Find:WElement(_Name$)
		For Local e:WElement = EachIn WElementList
			If e.Name$ = _Name$ Then Return e
		Next
		Return Null
	End Function

End Type


Type Weapon

 Field WeaponName$
 Field WeaponClass$
 Field WeaponType$
 Field WeaponElement$

 Function Create:Weapon(_Name$,_WClass$,_WType$,_WElement$)
  Local newweapon:Weapon = New Weapon
  newweapon.WeaponName$ = _Name$
  newweapon.WeaponClass$ = _WClass$
  newweapon.WeaponType$ = _WType$
  newweapon.WeaponElement$ = _WElement$
  Return newweapon
 End Function

 Method Print_Weapon()
  Print "Name:"+WeaponName
  Print "Weapon type:" + WeaponClass + " " + WeaponType 
  Print "Atk:" + WClass.Find(WeaponClass).Atk
  Print "Str:" + WClass.Find(WeaponClass).Str
  Print "Skl:" + WClass.Find(WeaponClass).Skl
  Print "Speed:" + WType.Find(WeaponType).Speed
  Print "Modif:" + WType.Find(WeaponType).Modif
  Print "Element:" + WeaponElement
 End Method

End Type

Global WClassList:TList = CreateList()
Global WTypeList:TList = CreateList()
Global WElementList:TList = CreateList()

'Create a list of weapon classes
WClass.Create("Wood",1,1,1)
WClass.Create("Bronz",50,25,2)
WClass.Create("Silver",100,66,5)
WClass.Create("Gold",255,32,9)

'Create a list of weapon types
WType.Create("Sword",5,20)
WType.Create("Dagger",2,12)
WType.Create("Hammer",1,24)

'Create a list of elements
WElement.Create("Fire","Water")
WElement.Create("Water","Fire")
WElement.Create("Wind","Earth")
WElement.Create("Earth","Wind")

'Create some weapons
Local Weapon1:Weapon = Weapon.Create("Blade of wrath","Wood","Sword","Fire")
Local Weapon2:Weapon = Weapon.Create("Hammer of might","Gold","Hammer","Earth")
Local Weapon3:Weapon = Weapon.Create("Watergod's dagger??","Silver","Dagger","Water")

Weapon1.Print_Weapon()
Print ""
Weapon2.Print_Weapon()
Print ""
Weapon3.Print_Weapon()