A tank with 5 turrets (types)

Blitz3D Forums/Blitz3D Programming/A tank with 5 turrets (types)

JP: Yes, that is a more sensible way of keeping everything in a single type. Feel a bit of a plonker having missed that. :P As I said, I'm a bit rusty and see you have subtracted 1 when dimensioning a blitz array with a constant. I wasn't sure if blitz arrays worked this way or not so glad you put me straight on that, too. ;)

mpausi: this is one method of allowing tanks to have different amounts of turrets:

	Type turretType
		Field rotation#
		Field elevation#
		Field nextLink.turretType
	End Type
	
	Type tankType
		Field tankName$
		Field totalTurrets%
		Field turretList.turretType
	End Type
	
	; Create a tank.
	tank.tankType = New tankType
	tank\tankName$ = "Mega Crusher"

	; Add 5 turrets with random rotation and elevation.
	For n = 1 To 5
		add_turret(tank, Rand(360), Rand(45))
	Next

	; Display current tank settings.
	print_tank(tank)
	Print
	Print "Press a key to change rotation & elevation"
	Print "of turret 3 ..."	
	Print
	WaitKey()

	; Now change the rotation and elevation of turret 3.
	this.turretType = get_turret(tank, 3)
	this\rotation = 123
	this\elevation = 456
	
	; Display new tank settings.
	print_tank(tank)	
	WaitKey()

	; Kill the tank.
	free_tank(tank)

	End


;	
; Add a new turret to the specified tank.
;
Function add_turret%(tank.tankType, rotation#=0, elevation#=0)

	; Create a new turret type.
	newTurret.turretType = New turretType
	
	; Set initial turret settings.
	newTurret\rotation = rotation
	newTurret\elevation = elevation
	
	; Add turret to tank's linked list.
	newTurret\nextLink = tank\turretList
	tank\turretList = newTurret
	
	; Increment the total count of turrets belonging to this tank.
	tank\totalTurrets = tank\totalTurrets + 1
	Return tank\totalTurrets
	
End Function


;
; Return a pointer to the specified tank turret. 
;
Function get_turret.turretType(tank.tankType, turretNo%)

	; Return NULL pointer if turretNo is invalid.
	If (turretNo < 1) Or (turretNo > tank\totalTurrets) Then Return Null
	
	; Walk along turret linked list until we reach the requested turret.
	this.turretType = tank\turretList
	For n = tank\totalTurrets-1 To turretNo Step -1
		this = this\nextLink
	Next
	
	; Return pointer to the requested turret type.
	Return this

End Function


;
; free all memory used by a tank type.
;
Function free_tank(tank.tankType)

	; Free all the turrets.
	this.turretType = tank\turretList
	While this <> Null
		nextTurret.turretType = this\nextLink
		Delete this
		this = nextTurret
	Wend

	; Free actual tank.
	Delete tank

End Function


;
; Display settings of specified tank.
;
Function print_tank(tank.tankType)

	Print "Tank Name : " + tank\tankName$
	
	For n = 1 To tank\totalTurrets
		this.turretType = get_turret(tank, n)
		Print "Turret " + n + " : rotation=" + this\rotation + " elevation=" + this\elevation
	Next
	
End Function


Slenkar:

I worked out how to do it by creating a type instance for each turret so they are destructible



I'm a bit confused now - I thought you wanted to keep everything contained within a sigle type?!