Destruction of Extended Types

BlitzMax Forums/BlitzMax Beginners Area/Destruction of Extended Types

Berbank(Posted 2004) [#1]
Hi,

I've written this (see below) and I'm sure it could be written better. In particular I think there are better ways of detroying extended classes (some way to do it from the Base class that cleas up all the counters?). Thanks to anyone who can help with this.


'Set Up Graphics Window

Const G_WIDTH = 800
Const G_HEIGHT = 600

Graphics G_WIDTH,G_HEIGHT,16,85


' Define a type for a visual

Type Visual

	' Maintain List
	Global VCount
	Global VisualItemList:TList 
	'Global angRotate#

	Field x#,y#
	Field alpha#
	Field rotation
	Field gfxHandle
	
	Function Create:Visual(tx,ty)
         v:Visual = New Visual
		v.SetXY(tx,ty)
	End Function
	
	Method SetXY:Visual(tx,ty)
		x = tx
         y = ty
	End Method	
	
	Function UpdateAll:Visual ()
         If VisualItemList = Null Then Return
         For v:Visual= EachIn VisualItemList 
             v.Update
             v.Draw
         Next
	End Function
	
	Method New ()
         If VisualItemList = Null Then VisualItemList = New TList
         VisualItemList.AddLast Self
         VCount = VCount + 1
		alpha# = 1
    End Method
	
	Method Destroy ()
         VisualItemList.Remove Self
         VCount = VCount - 1
	End Method
	
	Method Update()
		DrawText "UPDATE BASE", MouseX(), MouseY() + 40
	End Method
	
	Method Draw()
		SetBlend ALPHABLEND
		SetAlpha alpha#
		'angRotate = angRotate + 0.1
		'SetTransform angRotate,1,2
		DrawOval x,y,10,10
		
	End Method
	
End Type

Type Particle Extends Visual

	Global PCount

	Field dx#,dy#
	
	Function Create:Particle(tx,ty)
		p:Particle = New Particle
		p.SetXY(tx,ty)
		p.SetParticle()
	End Function
	
	Method SetParticle()
		dx# = (Rndfloat#() - 0.5) * 5
		dy# = (Rndfloat#() - 0.5) * 5
         PCount = PCount + 1		
	End method
	
	Method Update()
		x = x + dx#
		y = y + dy#
		UpdateLife()
		DrawText "UPDATE PARTICLE", MouseX(), MouseY() + 60
		Super.Update()
	End Method
	
	Method UpdateLife()
		If alpha# <= 0
         	PCount = PCount - 1
			Super.Destroy
		endif
		alpha# = alpha# - 0.005
	End Method

End Type

Type BounceParticle Extends Particle

	Global BCount
	
	Function Create:Particle(tx,ty)
		b:BounceParticle = New BounceParticle
		b.SetXY(tx,ty)
		b.SetParticle()
         BCount = BCount + 1
	End Function
	
	Method Update()
		If x + dx# > G_WIDTH Or x + dx# < 0
			dx# = -dx#
		endif
		If y + dy# > G_HEIGHT Or y + dy# < 0
			dy# = -dy#
		EndIf	
		alpha# = alpha# - 0.005
		If alpha# <= 0
         	BCount = BCount - 1
		EndIf
		DrawText "UPDATE BOUNCE", MouseX(), MouseY() + 80	
		Super.Update()
	End Method

End Type

While KeyDown(KEY_ESCAPE) = False

	Cls
	
	SetColor 255, 255, 255
	DrawText "X", MouseX(), MouseY() 
	
	If MouseDown(1)
		If Rand(0,1) = 0
			BounceParticle.Create(MouseX(),MouseY())
		else
			Particle.Create(MouseX(),MouseY())
		endif
	EndIf
	
	Visual.UpdateAll()
	SetBlend SOLIDBLEND
	SetAlpha 1
	DrawText "Visuals   : " + Visual.VCount , 20, 20
	DrawText "Particles : " + Particle.PCount , 20, 40
	DrawText "Bouncers  : " + BounceParticle.BCount , 20, 60
	
	FlushMem
	Flip

Wend

End