Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Dwarf with Axe

Bullet system code review (check it out and help me fix it)

This topic is 5963 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Please! Here''s my code friends, and it''s bad. I''m trying to set up a manager for all of my bullets that I will fire from my spaceship. Well, here it goes:
  

#define BULLET_SPEED 0.006f
#define BULLET_FADE 0.05f

#define MAX_BULLETS 5
//- - - - - - - - - - - - - - - - - -


CWeapon::Fire(float startx, float starty)
{	
	for(int loop=0; loop<MAX_BULLETS; loop++)
	{
		if(bullet[loop].active = false){
			bullet[loop].startx = startx;
			bullet[loop].starty = starty;
			this->DrawBullet(startx, starty);
			bullet[loop].active = true;
		}
	}
	return 0;
}

CWeapon::Init()
{
	for(int i=0; i<MAX_BULLETS; i++){
		this->bullet[i].active = false;
	}	

	this->speed = BULLET_SPEED;
	this->fade = BULLET_FADE;
	return 0;
}

CWeapon::DrawBullet(float startx, float starty)
{
	glDisable(GL_TEXTURE_2D);
	glBegin(GL_LINES);
	glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(startx, starty, 0.1f);
		glVertex3f(startx, starty+3.0f, 0.1f);
	glEnd();
	glEnable(GL_TEXTURE_2D);
	return 0;
}
	
CWeapon::Update()
{
	for(int loop=0; loop<MAX_BULLETS; loop++){
		if(bullet[loop].active = true){
			glTranslatef(0.0f, this->speed, 0.0f);
			this->DrawBullet(this->bullet[loop].startx, this->bullet[loop].starty);
		}
}
	return 0;
}

  
I''m just trying to be able to call a Fire() function when I hit the control key (which is in another file) and call Update() everyframe to loop through and kill and activate and unactivate the bullets. Know what I mean? It''s for a cheap little galaxian ripoff. Thanks for the help so far, and oh yeah, I know I forgot some parts of the Update() function, like how to encorporate the fade and stuff. Thanks! ~Dwarf

Share this post


Link to post
Share on other sites
Advertisement
I guess it might help if you knew what was in CWeapon.h (included in the above file):

  
struct bullet_t {
float x, y;
float startx, starty;
bool active;
};

class CWeapon {
public:
bullet_t bullet[MAX_BULLETS];
int current;
float speed;
float fade;
virtual int Fire(float startx, float starty);
virtual int Update(); //Loop through all the bullets

virtual int DrawBullet(float startx, float starty);
virtual int Init();
};


thanks again.

Share this post


Link to post
Share on other sites
On the contrary: While the bullet is in flight, bullet.active = true. While it is not in use, bullet.active = false, therefore , when bullet.active = false, I can use it.

Share this post


Link to post
Share on other sites
I maintain what I said.

(bullet[loop].active = false) is == false
Your if bloc is never executed.

(bullet[loop].active = true) is == true
Your if bloc is always executed.




Edited by - Fruny on February 16, 2002 1:46:56 AM

Share this post


Link to post
Share on other sites
Dwarf With Axe:
Listen to Fruny. ^,^

= assigns a value
== tests to see if the lefthand value is equal to the righthand
value.

if(a=b) would just assign b to a
if(a==b) asks whether a has the same value as b

^_^

-Hyatus
"The more you know..."

Share this post


Link to post
Share on other sites
Ive managed to clean it up a little bit, I think.

  
#define BULLET_SPEED 0.6f
#define BULLET_FADE 0.05f

#define MAX_BULLETS 5
//- - - - - - - - - - - - - - - - - -




//Loops through all of the bullets and checks to see if they are

//active. If they are then it will draw them.

CWeapon::Fire(float x, float y)
{
for(int loop=0;loop<MAX_BULLETS; loop++){
if(this->bullet[loop].active=false){
this->bullet[loop].active=true;
this->bullet[loop].x = x;
this->bullet[loop].y = y;
}
}
return 0;
}

CWeapon::Init()
{
for(int i=0; i<MAX_BULLETS; i++){
this->bullet[i].active = false;
this->bullet[i].ypos = 0;
this->bullet[i].life = 10.0f;
}

this->speed = BULLET_SPEED;
this->fade = BULLET_FADE;
return 0;
}

CWeapon::DrawBullet(int loop)
{
glPushMatrix();
glDisable(GL_TEXTURE_2D);
glTranslatef(0.0f, this->bullet[loop].ypos, 0.0f);
glBegin(GL_LINES);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(this->bullet[loop].x, this->bullet[loop].y, 0.1f);
glVertex3f(this->bullet[loop].x, this->bullet[loop].y+1.0f, 0.1f);
glEnd();
glEnable(GL_TEXTURE_2D);
glPopMatrix();
return 0;
}

CWeapon::Update()
{
for(int loop=0; loop<MAX_BULLETS; loop++){
if(bullet[loop].active = true){
if(this->bullet[loop].life<=0){
this->bullet[loop].active=false;
}
this->bullet[loop].ypos+=this->speed;
this->DrawBullet(loop);
}
this->bullet[loop].life-=this->fade;
}
return 0;
}
[source]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!