Compare Method for z-sorting

BlitzMax Forums/BlitzMax Programming/Compare Method for z-sorting

verfum(Posted 2007) [#1]
Hi, I'm using an overriding Compare Method which someone on here used quite a while ago, I can get it to sort objects created by the type but I cant get it to sort different types, here I'm creating a Player and Enemy Types, the player z-sorts with the other player, but not with the enemy?
Strict

Graphics 640,480,0

Global entity_list:TList = CreateList()  'Create a list for our player and enemy

' Player Class --------------------------------
Type Player
	Field x:Float,y:Float
	Field image:TImage

	Function Create:Player(x,y,anim_file:String)
		Local ent:Player = New Player
		ent.x = x
		ent.y = y
		ent.image = LoadAnimImage(anim_file,128,128,0,1)

		entity_list.AddLast(ent)	'Add the player to the list
		Return ent
	End Function
	
	Method Update()
		
		If KeyDown(KEY_RIGHT)
			x = x +1
		EndIf
		If KeyDown(KEY_LEFT)
			x = x -1
		EndIf
		If KeyDown(KEY_DOWN)
			y = y +1
		EndIf
		If KeyDown(KEY_UP)
			y = y -1
		EndIf
		
			
		DrawImage image,x,y,0
		

	End Method
	
	Method Compare( other:Object )
		
		If Player(other)
			Return Sgn(y - Player(other).y)
		EndIf
		Return -1

	End Method

End Type

' Enemy Class ------------------------------------------
Type Enemy
	Field x:Float,y:Float
	Field image:TImage
	
	Function Create:Enemy(x,y,anim_file:String)
		Local ent:Enemy = New Enemy
		ent.x = x
		ent.y = y
		ent.image = LoadAnimImage(anim_file,256,256,0,1)
		
		entity_list.AddLast(ent)	'Add the enemy to the list
		Return ent
	End Function
		
	Method Update()
		
		DrawImage image,x,y,0
		
	End Method
	
	Method Compare( other:Object )
		
		If Enemy(other)
			Return Sgn(y - Enemy(other).y)
		EndIf
		Return -1

	End Method

	
End Type

'Create our Player instance
Local player1:Player = Player.Create(50,80,"Media/warrior.bmp")
Local player2:Player = Player.Create(100,50,"Media/warrior.bmp")
'Create our Enemy instances
Local forest_monster:Enemy = Enemy.Create(100,100,"Media/forest_monster.bmp")


'MAIN LOOP ----------------
While Not KeyHit(KEY_ESCAPE)
	Cls 
	
	SortList(entity_list)
	
	For Local p:Player = EachIn entity_list  'Update our player
		p.Update()
	Next
	For Local e:Enemy = EachIn entity_list	 'Update our enemy
		e.Update()
	Next

' IGNORE THIS, this is to make one player move slower than the other.
	player1.x :+ (KeyDown(KEY_RIGHT) - KeyDown(KEY_LEFT))*2
	player1.y :+ (KeyDown(KEY_DOWN) - KeyDown(KEY_UP))*2

	Flip
Wend


Thanks for any help.


Suco-X(Posted 2007) [#2]
Hi
Can't run code but this should help you:


I think you are on the wrong way. Something like this is more logical:



Mfg Suco


TaskMaster(Posted 2007) [#3]
Edit: Whoops, looks like I was a day late and a dollar short. :)

You probably need to make the types both extend a common base type.

Type Base
End Type

Type Player Extends Base
End Type

Type Enemy Extends Base
End Type

Then put the common stuff like x,y,image in the base and put the compare function in the base type.


verfum(Posted 2007) [#4]
Thanks guys, it was the base type which was causing issues, I needed to put the method in the Entity and change the updates in the loop to
Entity.Update().