Improving Types in this Code

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




Type Bronze
	Field Name$="Bronze"
	Field Atk#=50
	Field Str#=25
	Field Skl#=2
End Type

Type Silver
	Field Name$="Silver"
	Field Atk#=100
	Field Str#=66
	Field Skl#=5
End Type

Type Gold
	Field Name$="Gold"
	Field Atk#=255
	Field Str#=32
	Field Skl#=9
End Type

Type Sword
	Field Name$="Sword"
	Field Speed=5
	Field Modif=20
End Type

Type Dagger
	Field Name$="Dagger"
	Field Speed=2
	Field Modif=12
End Type

Type Hammer
	Field Name$="Hammer"
	Field Speed=1
	Field Modif=24
End Type

Type Fire
	Field Name$="Fire"
	Field Dama$="X2 to Ice"
End Type

Type Ice
	Field Name$="Ice"
	Field Dama$="X2 to Fire"
End Type

Type Water
	Field Name$="Air"
	Field Dama$="X2 to Earth"
End Type

Type Earth
	Field Name$="Earth"
	Field Dama$="X2 to Air"
End Type

Type Weapon1

	Field WClass:Bronze=New Bronze
	Field WType:Sword=New Sword
	Field WElem:Fire=New Fire
	
	Method Print_Weapon()
	Print Wclass.Name$+" "+WType.name$+" of "+WElem.Name$
	End Method
	
End Type

Type Weapon2

	Field WClass:Silver=New Silver
	Field WType:Dagger=New Dagger
	Field WElem:ice=New ice
	
	Method Print_Weapon()
	Print Wclass.Name$+" "+WType.name$+" of "+WElem.Name$
	End Method
	
End Type


A:Weapon1=New Weapon1
a.Print_Weapon()
B:Weapon2=New Weapon2
B.print_Weapon()



Are there anyways the you could Change The WClass to include different types withouts Having To have to code as in weapon1 and weapon2

for instance having the Wclass Field but adding a type later

thats just not... cricket

I want my types to be more dynamic instead of linear
inheritance is nice would be better if you had a stacked option where same field are added together without having to create a method every time

@Thanks Cyanide :)

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

Type WType
	Field Name$
	Field Speed
	Field Modif
End Type

Type WElement
	Field Name$
	Field DamageX2:WElement
End Type

'make an instance for each of your original types using these with global variables.
I would probably do:
Type Weapon
	Field ClassName$, TypeName$, ElementName$
	Field Atk#, Str#, Skl#
	Field Speed, Modif
	Field DamageX2$
End Type


Although, either method could work, and either method could be good, however for different situations.

Sorry but you just have not understood the concept of class and object.

You create a new class for which nothing but a new object is needed ...

gold etc is only a name and does not need its own class. just its own value in the type "material" or similar ...

perhaps create one (big) type for all weapons.. you should have all available fields for every weapon in there...
fields which aren't available to that weapon, will only point to null (or whatever you fancy)
it can also link to a damage type (element type?), or you can calculate this during runtime.. (which I think you'll have to do anyways)
ie. pseudo code:
function calc_damage(perhaps an optional type as parameter)

select element type
fire ; dmg = weapon_max + armor_type + extra_fire_dmg + some_random_value
earth; dmg = weapon_max + armor_type + extra_earth_dmg + some_random_value
... etc
endselect
... more stuff goes here.. :p
endfunction


something along those lines?!?

ps: I really don't know if any of this made any sense to you, because my mind started to drift abit there.. :p

Type Weapon
   field Name:string
   field atk!,str!,skl!

   method fireWeapon()
        'A universal function to fire any weapon.
   end method

end Type

Type Bronze Extends Weapon

    method New()
       Name = "Bronze"
       str=20
       skl=30
       atk=20
    end method

   method FireWeapon()
        'but for special weapons you can circumvent the base fire weapon method
   end method

end Type

type sword extends weapon
 
   method new()
     name="blah."
     skl = 20 'etc
   end method
 
   method fireWeapon()
       super.fireWeapon()
       'or simply extend it by calling it using the super keyword.
   end method

end type




now you can share different weapons, as they are from the same base.

Temp:Weapon = New Bronze 'will compile fine.

You can then convert weapons to and from base/child classes to obtain their specific functions.

Temp:Weapon = new bronze
Brn:Bronze = Bronze( Temp )

'bronze is not a function above, but a cast.

The Types I posted are for a mock up of what i need

They Will All hold anything From 5 to 10 Fields of A collection of 40

The original idea was to create one object that linked to 3 objects From a selection of 100+

I was trying to create a method where you wouldnt have to
CaseSelect and object could be added without any problems ie the concept of OO

The Weapon Types were going to be predifined collections of its lesser type and made private so nobody could access these

I know it looks like the types are to similar but im trying to get the basics working before I put in the Time

Think Diablo 2 Weapons
I want to be able to add/remove sockets add/remove abilities

I konw you can have lists that are contained within Types Which are themselves contained by list

these are the principals im using

which i would have said was very OO

thanks for you help peeps

Btw Should Take into account that my programming Knowledge Is under 2 years

I have learnt everything I know by my self never had one lesson in programming unless you count Turtle* :)

Science/Maths is me thing so I think in a logical way which isnt always the best

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()




thanks peeps

@sweenie that looks just like to sor t of thing me need :)

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()



Doing it my way you can keep all class,type and element definitions in an external file which you read in at gamestart instead of hardcoding everything into the executable.
Makes it more easy to modify.