Archived

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

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

This topic is 5780 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
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
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
quote:
Original post by Dwarf with Axe
That doesn''t change anything...

I get the same results..



Dwarf with Axe, post the code again with all the ''='' in conditional statements changed to ''=='', just for everyones sanity




Slightly shrimpy smell == unsafe breadbin

Share this post


Link to post
Share on other sites
Okay, now I can see the lasers on the screen, but they don't move... They just appear and sit there...

CWeapon.h
    
#ifndef CWEAPON_H
#define CWEAPON_H
#endif

#define MAX_BULLETS 50

struct bullet_t {
float x, y;
float ypos;
bool active;
};

class CWeapon {
public:
bullet_t bullet[MAX_BULLETS];
float speed;
float lastFireTime;
float fireDelay;
virtual int Fire(float x, float y);
virtual int Update(); //Loop through all the bullets

virtual int DrawBullet(bullet_t bullet);
virtual int Init();
};


CWeapon.cpp

     
#define MAX_Y 50.0f

//The three most changed functions

#define BULLET_SPEED 0.6f
#define BULLET_FADE 0.05f

#define MAX_BULLETS 50
//- - - - - - - - - - - - - - - - - -


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

CWeapon::DrawBullet(bullet_t bullet)
{
//glPushMatrix();

glDisable(GL_TEXTURE_2D);
glColor3f(0.0f, 1.0f, 1.0f);
glBegin(GL_LINES);
glVertex3f(bullet.x, bullet.y, 0.1f);
glVertex3f(bullet.x, bullet.y+1.0f, 0.1f);
glEnd();
glEnable(GL_TEXTURE_2D);
//glPopMatrix();

return 0;
}

CWeapon::Init()
{
for(int loop=0; loop<MAX_BULLETS; loop++){
bullet[loop].x = 0;
bullet[loop].y = 0;
bullet[loop].ypos = 0.1f;
bullet[loop].active = false;
}
return 0;
}

CWeapon::Update()
{
for(int loop=0; loop<MAX_BULLETS; loop++){
if(bullet[loop].active == true){

glPushMatrix();
glTranslatef(0.0f, bullet[loop].ypos, 0.0f);
DrawBullet(bullet[loop]);
glPopMatrix();
}
bullet[loop].ypos+=this->speed;
}
return 0;
}


I call player.weapon.Fire(player.x, player.y) everytime the laser key is hit and call weapon.Update() everyframe...

What is wrong?

Thanks,
~Dwarf

Edited by - Dwarf with Axe on February 18, 2002 2:59:03 PM

Share this post


Link to post
Share on other sites