Gun's Rate of fire

Blitz3D Forums/Blitz3D Beginners Area/Gun's Rate of fire

Heya, I can't get my head around how to set up a rate of fire for a gun in my tank game. As it stands, my gun fires by creating a sprite at the gun's position then it updates all bullets types by using MoveEntity. I can make some fast bullets but I can't set up a three round burst or even a controlled automatic rate of fire.
Idealy, I'd like to have a cannon that fires at 1 round per second and a machine gun that fires 5-10 rounds per second.

I have been looking into delta timing and tweening, but I haven't used it yet.
I hope to make this a networked game later on as well.

Any advice would be useful and oh-so-dearly-appreciated.



tags: ROF ,gun timing, bullet rate, time, gun

Just set up a timer. Change the rate_of_fire value to see the different speed. As for a burst shot, again a timer. Set one up to allow so many shots, at a certain rate of fire, before introducing a larger pause. Some code to demonstrate simply rate of fire:

Press SPACEBAR to fire.

Graphics3D 800,600,0,2
SetBuffer BackBuffer()

Global master_bullet_mesh = CreateSphere()
HideEntity master_bullet_mesh ; hide so it stays out of view

Type bullet
	Field x,y,z
	Field mesh
	Field speed#
	Field bullet_life_time
	Field bullet_life_timer
End Type

Global rate_of_fire = 300 ; number of milliseconds between each shot.
Global timer

Global camera = CreateCamera()
PositionEntity camera,0,8,-30

Global light = CreateLight()


While Not KeyHit(1)



	If KeyDown(57) Then
		If MilliSecs() > timer + rate_of_fire Then
			create_bullet()
			timer = MilliSecs() ; reset the timer to the current millisecs value.
		End If
	End If
	
	update_bullets()
	
	UpdateWorld
	RenderWorld
	Flip
	
Wend

Function create_bullet()

	b.bullet = New bullet
	
	b\x = 0
	b\y = 0
	b\z = 0
	b\speed = 1
	
	b\bullet_life_time = 2000 ; bullet last 2 seconds (2000 milliseconds)
	b\bullet_life_timer = MilliSecs() ; set the timer to the same value as milliseconds,
									  ; so you can compare the time later, to determine if time is up
	b\mesh = CopyEntity(master_bullet_mesh)
	
End Function

Function update_bullets()

	For b.bullet = Each bullet
	
		MoveEntity b\mesh,0,0,b\speed
	
		;below compares the previously captured millisecond time, (captured in create_bullet function)
		;with the current millisecond time (MilliSecs()) + the bullets life, to see if the allotted
		;time has passed. If so, then delete the bullet and free the mesh.
		; PLEASE NOTE!
		;  This is not the correct way to use bullets. You should always try and recycle them, rather
		;  than create and free meshes. This will slow your code up. Instead, give each bullet an
		;  ALIVE flag. Set it to 0 when the bullet dies. When you need a new bullet, seach the list,
		;  for dead bullets and reset their properties. I am only doing things this way, for
		;  simplicity :o)
		If MilliSecs() > b\bullet_life_timer + b\bullet_life_time Then
			FreeEntity b\mesh
			Delete b.bullet
		End If
		
	Next
	
End Function


This works wonderfully. Thanks ALOT!