TGC Codebase Backup



variable bullet code by Visigoth

23rd Oct 2005 14:15
Summary

Bullet code to generate bullets that can fire at variable speeds and rate of fire. Uses a type for bullets and vectors for timing



Description

I am creating a game that simulates airsoft(bb guns, basically), so I needed a bullet code that would allow me to change rate of fire and speed easily, as well as other variables, like weight of bb, color, size, etc. After a bunch of tries, I came up with this. Firstly, I create a bullet type that stores all the data about the bullet. Then, I create an array that stores the bullet type. The actual shooting part is where it gets a little tricky. First, after you click the mouse button, the prog checks to see if the bullet is alive, and if so, it fires the bullet. The timing is done using vectors. The second bullet isn't fired until the first bullet travels X amount of distance. By changing this distance, the rate of fire can be changed. Alos, by changing the speed field in the bullet type changes the velocity of the bullet. I can easily implement this into a game to allow for different gun types, just by creating bullet types for different guns, or, by changing the field values during run time when you use different guns. Problems still to be worked out: collision. Collision before the bullet travels X distance. Aiming. But, this should all be doable.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    REM Project: bullet test
REM Created: 10/22/2005 3:28:44 PM
REM Author: Mike Shihrer
REM ***** Main Source File *****
REM

REM Type declarations
TYPE bullet
   ID as integer
   fired as boolean
   speed as float
   size as float
   weight as float
   vector_length as float
   color as integer
   start_x_pos as float
   start_y_pos as float
   start_z_pos as float
   alive as boolean
   glow as boolean
ENDTYPE

REM Globals

REM Variables declarations

num_bullets as integer
bullet_inc as integer
current_bullet as integer
distance_value as float
kill_value as float
Xi as integer
Xc as integer
Xd as integer
length_vector1 as float
length_vector2 as float

Xi = 0
num_bullets = 100
current_bullet = 0
distance_value = 12
kill_value = 100
Dim AK_bullet(num_bullets) as bullet

REM initialize the fields in the bullet array
For bullet_inc = 100 to num_bullets + 100
   AK_bullet(Xi).ID = bullet_inc
   AK_bullet(Xi).alive = 1
   AK_bullet(Xi).size = .1
   AK_bullet(Xi).speed = .1
   make object sphere AK_bullet(Xi).ID, AK_bullet(Xi).size
   hide object Ak_bullet(Xi).ID
   Inc Xi
Next bullet_inc


REM Main Loop

do

REM check for already fired bullets, and set the next available one to fire
If mouseclick() = 2 and AK_bullet(current_bullet).fired = 0 and AK_bullet(current_bullet).alive = 1
   Ak_bullet(current_bullet).fired = 1
   position object AK_bullet(current_bullet).ID, 0 ,-2 ,8
   show object AK_bullet(current_bullet).ID
   AK_Bullet(current_bullet).start_x_pos = camera position x()
   AK_Bullet(current_bullet).start_y_pos = camera position y()
   AK_Bullet(current_bullet).start_z_pos = camera position z()
EndIf

REM this function returns the distance from the bullets starting point to its current position
length_vector1# = threeD_distance(AK_Bullet(current_bullet).start_x_pos, AK_Bullet(current_bullet).start_y_pos, AK_Bullet(current_bullet).start_z_pos, object position x(AK_bullet(current_bullet).ID), object position y(AK_bullet(current_bullet).ID), object position z(AK_bullet(current_bullet).ID))

REM move each bullet that has the .fired field set to 1

For Xc = 0 to num_bullets
length_vector2# = threeD_distance(AK_Bullet(Xc).start_x_pos, AK_Bullet(Xc).start_y_pos, AK_Bullet(Xc).start_z_pos, object position x(AK_bullet(Xc).ID), object position y(AK_bullet(Xc).ID), object position z(AK_bullet(Xc).ID))
AK_bullet(Xc).vector_length = length_vector2#
If Ak_bullet(Xc).fired = 1 and Ak_bullet(Xc).alive = 1
   move object AK_bullet(Xc).ID ,AK_bullet(Xc).speed
EndIf

Next Xc


REM checks to see if mouse is pressed, distance from last bullet fired, and object number less than total number of bullets
REM lowering the distance value increases the rate of fire

If mouseclick() = 2 and length_vector1# > distance_value and current_bullet < num_bullets
   Inc current_bullet
Endif
text 0,0,str$(length_vector1#)

REM Check to see if bullet should be killed
For Xd = 0 to num_bullets
   If AK_bullet(Xd).vector_length > kill_value
      AK_bullet(Xd).alive = 0
      hide object AK_bullet(Xd).ID
   EndIf
Next Xd


loop


REM borrowed this function from the Vector tutorial
REm use it to calulate the distance from the fired bullets to their original starting point
REM this is then used to figure out when the next bullet is going to be fired. Use for RATE OF FIRE
FUNCTION threeD_distance(x1# AS float, y1# AS float, z1# AS float, x2# AS float, y2# AS float, z2# AS float)

     null = make vector3(1)
     set vector3 1, x1# - x2#, y1# - y2#, z1# - z2#
     length_vector# = length vector3(1)
     null = delete vector3(1)

ENDFUNCTION length_vector#