Improving Types in this Code

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

Duckstab[o](Posted 2005) [#1]



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


boomboommax(Posted 2005) [#2]
thats just not... cricket


Duckstab[o](Posted 2005) [#3]
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 :)


Bot Builder(Posted 2005) [#4]
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.


Dreamora(Posted 2005) [#5]
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 ...


LarsG(Posted 2005) [#6]
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


AntonyWells(Posted 2005) [#7]
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.


Duckstab[o](Posted 2005) [#8]
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


Duckstab[o](Posted 2005) [#9]
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


Sweenie(Posted 2005) [#10]
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()





Duckstab[o](Posted 2005) [#11]
thanks peeps

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


Dreamora(Posted 2005) [#12]
here is the OO way for solving your problem:




Sweenie(Posted 2005) [#13]
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.