Skip to content

Instantly share code, notes, and snippets.

@timothyhahn
Created October 11, 2012 13:15
Show Gist options
  • Save timothyhahn/3872193 to your computer and use it in GitHub Desktop.
Save timothyhahn/3872193 to your computer and use it in GitHub Desktop.
/**
* From Bullet.cpp
*/
//Creates the bullet
Bullet::Bullet() {
setPosition(100,100);
setDirection(NORTH);
setHealth(1);
setMovementRate(10);
setWidth(5);
setHeight(5);
setDamage(10);
setColor(sf::Color(0,0,0));
}
// I removed all the internal "how to move" stuff since it is different for every platform
void Bullet::fire(Direction d) {
move(d);
}
/**
* From Game.cpp
*/
int start() {
std::vector<Bullet> bullets;
while(window.isOpen()) {
while(game_running) {
checkInput();
}
}
void checkInput() {
// Directional inputs are up here, but I removed them
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
b = Bullet();
b.fire();
bullets.push_back(b);
}
}
void getBulletsLater() {
// This isn't in the original code, but it's a way you can "access" bullets later.
for(std::vector<Bullet*>::iterator iter= bullets.begin(); iter!= bullets.end(); ++iter) {
Bullet * bullet_pointer = *checkAgainst; //Depending on what you need, you can pick either a pointer or an object to use
Bullet bullet_object = *next;
bullet_pointer->doSomething();
bullet_object->doOtherThing();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment