Improving Types in this Code

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

here is the OO way for solving your problem:

Strict
Type METAL
	Field _name:String
	Field _attack:Float
	Field _strength:Float
	Field _skill:Float
	
	Function create:METAL (name:String,atk:Float,str:Float,skl:Float)
		Local t:METAL		= New METAL
		t._name			= name
		t._attack		= atk
		t._strength		= str
		t._skill			= skl
		Return t
	End Function
End Type

Field WEAPON
	Field _name:String
	Field _speed:Int
	Field _modifier:Int
	Field _type:String
	
	Field _class:METAL
	Field _element:ELEMENT
	
	Function create:WEAPON (name:String,speed:Int,modifier:Int)
		Local t:WEAPON	= New WEAPON
		t._name			= name
		t._speed			= speed
		t._modifier		= modifier
		Return t
	End Function
	
	Method set_propertiers (class:METAL,element:ELEMENT)
		_class			= class
		_element			= element
	End Method
	
	Method print_weapon()
		Print _class.name + " " + _type + " of " + _element.name
	End Method
End Type

Type SWORD Extends WEAPON
	Method New ()
		_type		= "sword"
	End Method
End Type

Type DAGGER Extends WEAPON
	Method New ()
		_type		= "dagger"
	End Method
End Type

Type HAMMER Extends WEAPON
	Method New ()
		_type		= "hammer"
	End Method
End Type

Type ELEMENT
	Field _name:String
	Field _damage:String
	
	Function create:ELEMENT (name:String,damage:String)
		Local t:ELEMENT	= New ELEMENT
		t._name			= name
		t._damage		= damage
		Return t
	End Function
End Type

Local bronze:METAL			= METAL.create ("bronze",50,25,2)
Local fire:ELEMENT			= ELEMENT.create ("fire","X2 to Ice")
Local bronze_firesword:WEAPON 	= SWORD.create ("bronze firesword",5,20)
bronze_firesword.set_properties (bronze,fire)

Local silver:METAL			= METAL.create ("silver",100,66,5)
Local ice:ELEMENT				= ELEMENT.create ("ice","X2 to Fire")
Local silver_icedagger:WEAPON 	= DAGGER.create ("silver icedagger",5,20)
silver_icedagger.set_properties (silver,ice)

bronze_firesword.Print_Weapon()
silver_icedagger.print_Weapon()