if(is_valid()) {
remove_me();
- Explosion* explosion = new Explosion(get_bbox().get_middle());
+ auto explosion = std::make_shared<Explosion>(get_bbox().get_middle());
Sector::current()->add_object(explosion);
}
py += MUZZLE_Y;
SoundManager::current()->play("sounds/dartfire.wav", get_pos());
- Sector::current()->add_object(new Dart(Vector(px, py), dir, this));
+ Sector::current()->add_object(std::make_shared<Dart>(Vector(px, py), dir, this));
state = IDLE;
sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
}
}
try {
- GameObject *game_object;
- BadGuy *bad_guy;
+ GameObjectPtr game_object;
Vector spawnpoint;
Rectf object_bbox;
if (game_object == NULL)
throw std::runtime_error("Creating " + badguy + " object failed.");
- bad_guy = dynamic_cast<BadGuy *> (game_object);
- if (bad_guy == NULL)
- throw std::runtime_error(badguy + " is not a badguy.");
+ BadGuy& bad_guy = dynamic_cast<BadGuy&>(*game_object);
- object_bbox = bad_guy->get_bbox ();
+ object_bbox = bad_guy.get_bbox();
- if (type == "dropper") {
- spawnpoint = get_anchor_pos (get_bbox (), ANCHOR_BOTTOM);
- spawnpoint.x -= 0.5 * object_bbox.get_width ();
+ if (type == "dropper")
+ {
+ spawnpoint = get_anchor_pos (get_bbox(), ANCHOR_BOTTOM);
+ spawnpoint.x -= 0.5 * object_bbox.get_width();
}
- else if ((type == "cannon") || (type == "rocketlauncher")) {
- spawnpoint = get_pos (); /* top-left corner of the cannon */
+ else if ((type == "cannon") || (type == "rocketlauncher"))
+ {
+ spawnpoint = get_pos(); /* top-left corner of the cannon */
if (launchdir == LEFT)
- spawnpoint.x -= object_bbox.get_width () + 1;
+ spawnpoint.x -= object_bbox.get_width() + 1;
else
- spawnpoint.x += get_bbox ().get_width () + 1;
+ spawnpoint.x += get_bbox().get_width() + 1;
}
/* Now we set the real spawn position */
- bad_guy->set_pos (spawnpoint);
+ bad_guy.set_pos(spawnpoint);
/* We don't want to count dispensed badguys in level stats */
- if(bad_guy->countMe)
- bad_guy->countMe = false;
+ if(bad_guy.countMe)
+ bad_guy.countMe = false;
- Sector::current()->add_object(bad_guy);
- } catch(std::exception& e) {
+ Sector::current()->add_object(game_object);
+ } catch(const std::exception& e) {
log_warning << "Error dispensing badguy: " << e.what() << std::endl;
return;
}
{
SoundManager::current()->play("sounds/sizzle.ogg", get_pos());
sprite->set_action("fade", 1);
- Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", bbox.get_middle(), ANCHOR_MIDDLE, Vector(0, -150), Vector(0,0), LAYER_BACKGROUNDTILES+2));
+ Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/smoke.sprite",
+ "default",
+ bbox.get_middle(), ANCHOR_MIDDLE,
+ Vector(0, -150), Vector(0,0), LAYER_BACKGROUNDTILES+2));
set_group(COLGROUP_DISABLED);
// start dead-script
Vector ppos = bbox.get_middle();
Vector pspeed = Vector(gameRandom.randf(-10, 10), 150);
Vector paccel = Vector(0,0);
- Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
+ Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/smoke.sprite",
+ "default",
+ ppos, ANCHOR_MIDDLE, pspeed, paccel,
+ LAYER_OBJECTS-1));
puff_timer.start(gameRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
normal_propeller_speed = gameRandom.randf(0.95, 1.05);
sprite->set_action("dying", 1);
glow_sprite->set_action("dying", 1);
- std::vector<TreeWillOWisp*>::iterator iter;
- for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
- TreeWillOWisp *willo = *iter;
- willo->vanish();
+ for(auto iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
+ TreeWillOWisp& willo = **iter;
+ willo.vanish();
}
run_dead_script();
}
if(suck_timer.check()) {
Color col = glow_sprite->get_color();
SoundManager::current()->play("sounds/tree_suck.ogg", get_pos());
- std::vector<TreeWillOWisp*>::iterator iter;
- for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
- TreeWillOWisp *willo = *iter;
- if(willo->get_color() == col) {
- willo->start_sucking(get_bbox().get_middle() + SUCK_TARGET_OFFSET + Vector(gameRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD), gameRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD)));
+ for(auto iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
+ TreeWillOWisp& willo = **iter;
+ if(willo.get_color() == col) {
+ willo.start_sucking(get_bbox().get_middle() + SUCK_TARGET_OFFSET + Vector(gameRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD), gameRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD)));
}
}
mystate = STATE_SUCKING;
if(willowisp_timer.check()) {
if(willowisps.size() < WILLOWISP_COUNT) {
Vector pos = Vector(bbox.get_width() / 2, bbox.get_height() / 2 + willo_spawn_y + WILLOWISP_TOP_OFFSET);
- TreeWillOWisp *willowisp
- = new TreeWillOWisp(this, pos, 200 + willo_radius, willo_speed);
+ auto willowisp = std::make_shared<TreeWillOWisp>(this, pos, 200 + willo_radius, willo_speed);
Sector::current()->add_object(willowisp);
willowisps.push_back(willowisp);
/* TODO indicate root with an animation */
Player* player = get_nearest_player();
if (player) {
- Root* root = new Root(Vector(player->get_bbox().get_left(), get_bbox().get_bottom()+ROOT_TOP_OFFSET));
+ auto root = std::make_shared<Root>(Vector(player->get_bbox().get_left(), get_bbox().get_bottom()+ROOT_TOP_OFFSET));
Sector::current()->add_object(root);
}
}
if ((mystate == STATE_SUCKING) && (willowisp->was_sucked)) {
mystate = STATE_IDLE;
}
- willowisps.erase(std::find(willowisps.begin(), willowisps.end(), willowisp));
+ willowisps.erase(std::find_if(willowisps.begin(), willowisps.end(),
+ [willowisp](const std::shared_ptr<TreeWillOWisp>& lhs)
+ {
+ return lhs.get() == willowisp;
+ }));
}
void
void
GhostTree::spawn_lantern() {
- Lantern* lantern = new Lantern(get_bbox().get_middle() + SUCK_TARGET_OFFSET);
+ auto lantern = std::make_shared<Lantern>(get_bbox().get_middle() + SUCK_TARGET_OFFSET);
Sector::current()->add_object(lantern);
}
Lantern* suck_lantern; /**< Lantern that is currently being sucked in */
- std::vector<TreeWillOWisp*> willowisps;
+ std::vector<std::shared_ptr<TreeWillOWisp> > willowisps;
bool is_color_deadly(Color color) const;
void spawn_lantern();
if(is_valid()) {
remove_me();
- Sector::current()->add_object(new Explosion(get_bbox().get_middle()));
- Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, -40)));
+ Sector::current()->add_object(std::make_shared<Explosion>(get_bbox().get_middle()));
+ Sector::current()->add_object(std::make_shared<CoinExplode>(get_pos() + Vector (0, -40)));
}
run_dead_script();
}
if(is_valid()) {
remove_me();
- Explosion* explosion = new Explosion(get_bbox().get_middle());
+ auto explosion = std::make_shared<Explosion>(get_bbox().get_middle());
Sector::current()->add_object(explosion);
}
{
SoundManager::current()->play("sounds/sizzle.ogg", get_pos());
sprite->set_action("fade", 1);
- Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", bbox.get_middle(), ANCHOR_MIDDLE, Vector(0, -150), Vector(0,0), LAYER_BACKGROUNDTILES+2));
+ Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/smoke.sprite",
+ "default",
+ bbox.get_middle(), ANCHOR_MIDDLE,
+ Vector(0, -150), Vector(0,0),
+ LAYER_BACKGROUNDTILES+2));
set_group(COLGROUP_DISABLED);
// start dead-script
// check if we see a fire bullet
Sector* sector = Sector::current();
for (Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) {
- Bullet* bullet = dynamic_cast<Bullet*>(*i);
+ Bullet* bullet = dynamic_cast<Bullet*>(i->get());
if (!bullet) continue;
if (bullet->get_type() != FIRE_BONUS) continue;
if (can_see(*bullet)) wants_to_flee = true;
Vector ppos = bbox.get_middle();
Vector pspeed = Vector(0, -150);
Vector paccel = Vector(0,0);
- Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_BACKGROUNDTILES+2));
+ Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/smoke.sprite",
+ "default", ppos, ANCHOR_MIDDLE,
+ pspeed, paccel,
+ LAYER_BACKGROUNDTILES+2));
// extinguish the flame
sprite->set_action(dir == LEFT ? "extinguish-left" : "extinguish-right", 1);
physic.set_velocity_y(0);
float vy = -sin(angle) * THROW_VELOCITY;
SoundManager::current()->play("sounds/dartfire.wav", get_pos());
- Sector::current()->add_object(new MoleRock(Vector(px, py), Vector(vx, vy), this));
+ Sector::current()->add_object(std::make_shared<MoleRock>(Vector(px, py), Vector(vx, vy), this));
}
void
}
if(is_valid()) {
remove_me();
- Sector::current()->add_object(new Bomb(get_pos(), dir, sprite_name ));
+ Sector::current()->add_object(std::make_shared<Bomb>(get_pos(), dir, sprite_name));
}
kill_squished(object);
return true;
{
if(is_valid()) {
remove_me();
- Explosion* explosion = new Explosion(get_bbox().get_middle());
+ auto explosion = std::make_shared<Explosion>(get_bbox().get_middle());
Sector::current()->add_object(explosion);
}
Vector stumpy_pos = get_pos();
stumpy_pos.x += 20;
stumpy_pos.y += 25;
- Stumpy* stumpy = new Stumpy(stumpy_pos, dir);
+ auto stumpy = std::make_shared<Stumpy>(stumpy_pos, dir);
remove_me();
Sector::current()->add_object(stumpy);
float vy = -cos(angle)*velocity;
Vector pspeed = Vector(vx, vy);
Vector paccel = Vector(0, 100);
- Sector::current()->add_object(new SpriteParticle("images/objects/particles/leaf.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
+ Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/leaf.sprite",
+ "default",
+ ppos, ANCHOR_MIDDLE,
+ pspeed, paccel,
+ LAYER_OBJECTS-1));
}
// spawn PoisonIvy
Vector leaf1_pos(stumpy_pos.x - POISONIVY_WIDTH - 1, stumpy_pos.y - POISONIVY_Y_OFFSET);
Rectf leaf1_bbox(leaf1_pos.x, leaf1_pos.y, leaf1_pos.x + POISONIVY_WIDTH, leaf1_pos.y + POISONIVY_HEIGHT);
if (Sector::current()->is_free_of_movingstatics(leaf1_bbox, this)) {
- PoisonIvy* leaf1 = new PoisonIvy(leaf1_bbox.p1, LEFT);
+ auto leaf1 = std::make_shared<PoisonIvy>(leaf1_bbox.p1, LEFT);
leaf1->countMe = false;
Sector::current()->add_object(leaf1);
}
Vector leaf2_pos(stumpy_pos.x + sprite->get_current_hitbox_width() + 1, stumpy_pos.y - POISONIVY_Y_OFFSET);
Rectf leaf2_bbox(leaf2_pos.x, leaf2_pos.y, leaf2_pos.x + POISONIVY_WIDTH, leaf2_pos.y + POISONIVY_HEIGHT);
if (Sector::current()->is_free_of_movingstatics(leaf2_bbox, this)) {
- PoisonIvy* leaf2 = new PoisonIvy(leaf2_bbox.p1, RIGHT);
+ auto leaf2 = std::make_shared<PoisonIvy>(leaf2_bbox.p1, RIGHT);
leaf2->countMe = false;
Sector::current()->add_object(leaf2);
}
void
Owl::initialize()
{
- GameObject *game_object;
-
physic.set_velocity_x(dir == LEFT ? -FLYING_SPEED : FLYING_SPEED);
physic.enable_gravity(false);
sprite->set_action(dir == LEFT ? "left" : "right");
- game_object = ObjectFactory::instance().create(carried_obj_name, get_pos(), dir);
- if (game_object == NULL) {
+ auto game_object = ObjectFactory::instance().create(carried_obj_name, get_pos(), dir);
+ if (game_object == NULL)
+ {
log_fatal << "Creating \"" << carried_obj_name << "\" object failed." << std::endl;
- return;
}
-
- carried_object = dynamic_cast<Portable *> (game_object);
- if (carried_object == NULL) {
- log_warning << "Object is not portable: " << carried_obj_name << std::endl;
- delete game_object;
- return;
+ else
+ {
+ carried_object = dynamic_cast<Portable*>(game_object.get());
+ if (carried_object == NULL)
+ {
+ log_warning << "Object is not portable: " << carried_obj_name << std::endl;
+ }
+ else
+ {
+ Sector::current()->add_object(game_object);
+ }
}
-
- Sector::current ()->add_object (game_object);
-} /* void initialize */
+}
bool
Owl::is_above_player (void)
float vy = -cos(angle)*velocity;
Vector pspeed = Vector(vx, vy);
Vector paccel = Vector(0, 100);
- Sector::current()->add_object(new SpriteParticle("images/objects/particles/poisonivy.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
+ Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/poisonivy.sprite",
+ "default",
+ ppos, ANCHOR_MIDDLE,
+ pspeed, paccel,
+ LAYER_OBJECTS-1));
}
kill_squished(object);
return true;
}
void
-ShortFuse::explode (void)
+ShortFuse::explode()
{
- if (!is_valid ())
+ if (!is_valid())
return;
- Explosion *explosion = new Explosion (get_bbox ().get_middle ());
+ auto explosion = std::make_shared<Explosion>(get_bbox ().get_middle());
- explosion->hurts (false);
- explosion->pushes (true);
- Sector::current()->add_object (explosion);
+ explosion->hurts(false);
+ explosion->pushes(true);
+ Sector::current()->add_object(explosion);
- run_dead_script ();
- remove_me ();
+ run_dead_script();
+ remove_me();
}
bool
void
SkyDive::explode (void)
{
- if (!is_valid ())
+ if (!is_valid())
return;
- Explosion *explosion = new Explosion (get_anchor_pos (bbox, ANCHOR_BOTTOM));
+ auto explosion = std::make_shared<Explosion>(get_anchor_pos (bbox, ANCHOR_BOTTOM));
- explosion->hurts (true);
- explosion->pushes (false);
- Sector::current()->add_object (explosion);
+ explosion->hurts(true);
+ explosion->pushes(false);
+ Sector::current()->add_object(explosion);
remove_me ();
} /* void explode */
set_state (STATE_FALLING);
/* Create a new snowball where the snowman's head was */
- SnowBall* snowball = new SnowBall(snowball_pos, dir, dead_script);
+ auto snowball = std::make_shared<SnowBall>(snowball_pos, dir, dead_script);
Sector::current()->add_object(snowball);
}
float vy = -cos(angle)*velocity;
Vector pspeed = Vector(vx, vy);
Vector paccel = Vector(0, 100);
- Sector::current()->add_object(new SpriteParticle("images/objects/particles/bark.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
+ Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/bark.sprite",
+ "default",
+ ppos, ANCHOR_MIDDLE,
+ pspeed, paccel,
+ LAYER_OBJECTS-1));
}
return true;
Sector* sector = Sector::current();
for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
i != sector->gameobjects.end(); ++i) {
- YetiStalactite* stalactite = dynamic_cast<YetiStalactite*> (*i);
+ YetiStalactite* stalactite = dynamic_cast<YetiStalactite*>(i->get());
if(stalactite && stalactite->is_hanging()) {
float distancex;
if (hit_points >= 3) {
void
JoystickManager::set_joy_controls(Controller::Control id, bool value)
{
- if (m_joystick_config.jump_with_up_joy &&
+ if (m_joystick_config.jump_with_up_joy &&
id == Controller::UP)
{
parent->get_controller()->set_control(Controller::JUMP, value);
BicyclePlatform::update(float elapsed_time)
{
if (!slave) {
- Sector::current()->add_object(new BicyclePlatform(this));
+ Sector::current()->add_object(std::make_shared<BicyclePlatform>(this));
return;
}
if (!master) {
{
Sector* sector = Sector::current();
sector->add_object(
- new BrokenBrick(sprite->clone(), get_pos(), Vector(-100, -400)));
+ std::make_shared<BrokenBrick>(sprite->clone(), get_pos(), Vector(-100, -400)));
sector->add_object(
- new BrokenBrick(sprite->clone(), get_pos() + Vector(0, 16),
- Vector(-150, -300)));
+ std::make_shared<BrokenBrick>(sprite->clone(), get_pos() + Vector(0, 16),
+ Vector(-150, -300)));
sector->add_object(
- new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 0),
- Vector(100, -400)));
+ std::make_shared<BrokenBrick>(sprite->clone(), get_pos() + Vector(16, 0),
+ Vector(100, -400)));
sector->add_object(
- new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 16),
- Vector(150, -300)));
+ std::make_shared<BrokenBrick>(sprite->clone(), get_pos() + Vector(16, 16),
+ Vector(150, -300)));
remove_me();
}
BonusBlock::BonusBlock(const Vector& pos, int data) :
Block(SpriteManager::current()->create("images/objects/bonus_block/bonusblock.sprite")),
contents(),
- object(0),
+ object(),
hit_counter(1),
sprite_name(),
script(),
//object = new Trampoline(get_pos(), false); //needed if this is to be moved to custom
break;
case 8: contents = CONTENT_CUSTOM;
- object = new Trampoline(get_pos(), true);
+ object = std::make_shared<Trampoline>(get_pos(), true);
break;
case 9: contents = CONTENT_CUSTOM;
- object = new Rock(get_pos(), "images/objects/rock/rock.sprite");
+ object = std::make_shared<Rock>(get_pos(), "images/objects/rock/rock.sprite");
break;
case 10: contents = CONTENT_RAIN; break;
case 11: contents = CONTENT_EXPLODE; break;
case 12: contents = CONTENT_CUSTOM;
- object = new PowerUp(get_pos(), "images/powerups/potions/red-potion.sprite");
+ object = std::make_shared<PowerUp>(get_pos(), "images/powerups/potions/red-potion.sprite");
break;
default:
log_warning << "Invalid box contents" << std::endl;
}
} else {
if(contents == CONTENT_CUSTOM) {
- GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
- object = dynamic_cast<MovingObject*> (game_object);
+ GameObjectPtr game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
+ object = std::dynamic_pointer_cast<MovingObject>(game_object);
if(object == 0)
throw std::runtime_error(
"Only MovingObjects are allowed inside BonusBlocks");
BonusBlock::~BonusBlock()
{
- delete object;
}
void
switch(contents) {
case CONTENT_COIN:
{
- Sector::current()->add_object(new BouncyCoin(get_pos(), true));
+ Sector::current()->add_object(std::make_shared<BouncyCoin>(get_pos(), true));
player->get_status()->add_coins(1);
if (hit_counter != 0)
Sector::current()->get_level()->stats.coins++;
case CONTENT_FIREGROW:
{
if(player->get_status()->bonus == NO_BONUS) {
- SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
+ auto riser = std::make_shared<SpecialRiser>(get_pos(), std::make_shared<GrowUp>(direction));
sector->add_object(riser);
} else {
- SpecialRiser* riser = new SpecialRiser(
- get_pos(), new Flower(FIRE_BONUS));
+ auto riser = std::make_shared<SpecialRiser>(
+ get_pos(), std::make_shared<Flower>(FIRE_BONUS));
sector->add_object(riser);
}
SoundManager::current()->play("sounds/upgrade.wav");
case CONTENT_ICEGROW:
{
if(player->get_status()->bonus == NO_BONUS) {
- SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
+ auto riser = std::make_shared<SpecialRiser>(get_pos(), std::make_shared<GrowUp>(direction));
sector->add_object(riser);
} else {
- SpecialRiser* riser = new SpecialRiser(
- get_pos(), new Flower(ICE_BONUS));
+ auto riser = std::make_shared<SpecialRiser>(
+ get_pos(), std::make_shared<Flower>(ICE_BONUS));
sector->add_object(riser);
}
SoundManager::current()->play("sounds/upgrade.wav");
case CONTENT_STAR:
{
- sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
+ sector->add_object(std::make_shared<Star>(get_pos() + Vector(0, -32), direction));
SoundManager::current()->play("sounds/upgrade.wav");
break;
}
case CONTENT_1UP:
{
- sector->add_object(new OneUp(get_pos(), direction));
+ sector->add_object(std::make_shared<OneUp>(get_pos(), direction));
SoundManager::current()->play("sounds/upgrade.wav");
break;
}
case CONTENT_CUSTOM:
{
- SpecialRiser* riser = new SpecialRiser(get_pos(), object);
+ auto riser = std::make_shared<SpecialRiser>(get_pos(), object);
object = 0;
sector->add_object(riser);
SoundManager::current()->play("sounds/upgrade.wav");
}
case CONTENT_TRAMPOLINE:
{
- SpecialRiser* riser = new SpecialRiser(get_pos(), new Trampoline(get_pos(), false));
+ auto riser = std::make_shared<SpecialRiser>(get_pos(), std::make_shared<Trampoline>(get_pos(), false));
sector->add_object(riser);
SoundManager::current()->play("sounds/upgrade.wav");
break;
case CONTENT_RAIN:
{
hit_counter = 1; // multiple hits of coin rain is not allowed
- Sector::current()->add_object(new CoinRain(get_pos(), true));
+ Sector::current()->add_object(std::make_shared<CoinRain>(get_pos(), true));
SoundManager::current()->play("sounds/upgrade.wav");
break;
}
case CONTENT_EXPLODE:
{
hit_counter = 1; // multiple hits of coin explode is not allowed
- Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, -40)));
+ Sector::current()->add_object(std::make_shared<CoinExplode>(get_pos() + Vector (0, -40)));
SoundManager::current()->play("sounds/upgrade.wav");
break;
}
case CONTENT_FIREGROW:
{
- sector->add_object(new PowerUp(get_pos() + Vector(0, 32), "images/powerups/fireflower/fireflower.sprite"));
+ sector->add_object(std::make_shared<PowerUp>(get_pos() + Vector(0, 32), "images/powerups/fireflower/fireflower.sprite"));
SoundManager::current()->play("sounds/upgrade.wav");
countdown = true;
break;
case CONTENT_ICEGROW:
{
- sector->add_object(new PowerUp(get_pos() + Vector(0, 32), "images/powerups/iceflower/iceflower.sprite"));
+ sector->add_object(std::make_shared<PowerUp>(get_pos() + Vector(0, 32), "images/powerups/iceflower/iceflower.sprite"));
SoundManager::current()->play("sounds/upgrade.wav");
countdown = true;
break;
case CONTENT_STAR:
{
- sector->add_object(new Star(get_pos() + Vector(0, 32), direction));
+ sector->add_object(std::make_shared<Star>(get_pos() + Vector(0, 32), direction));
SoundManager::current()->play("sounds/upgrade.wav");
countdown = true;
break;
case CONTENT_1UP:
{
- sector->add_object(new OneUp(get_pos(), DOWN));
+ sector->add_object(std::make_shared<OneUp>(get_pos(), DOWN));
SoundManager::current()->play("sounds/upgrade.wav");
countdown = true;
break;
case CONTENT_EXPLODE:
{
hit_counter = 1; // multiple hits of coin explode is not allowed
- Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, 40)));
+ Sector::current()->add_object(std::make_shared<CoinExplode>(get_pos() + Vector (0, 40)));
SoundManager::current()->play("sounds/upgrade.wav");
countdown = true;
break;
public:
Contents contents;
- MovingObject* object;
+ std::shared_ptr<MovingObject> object;
int hit_counter;
void draw(DrawingContext& context);
Sector* sector = Sector::current();
Player& player_one = *(sector->player);
if(coin_counter > 0) {
- sector->add_object(new BouncyCoin(get_pos(),true));
+ sector->add_object(std::make_shared<BouncyCoin>(get_pos(), true));
coin_counter--;
player_one.get_status()->add_coins(1);
if(coin_counter == 0)
Vector ppos = bbox.get_middle();
Vector pspeed = Vector(0, -150);
Vector paccel = Vector(0,0);
- Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_BACKGROUNDTILES+2));
+ Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/smoke.sprite",
+ "default",
+ ppos, ANCHOR_MIDDLE,
+ pspeed, paccel,
+ LAYER_BACKGROUNDTILES+2));
}
bool
SoundManager::current()->manage_source(soundSource);
*/
Sector::current()->player->get_status()->add_coins(1);
- Sector::current()->add_object(new BouncyCoin(get_pos()));
+ Sector::current()->add_object(std::make_shared<BouncyCoin>(get_pos()));
Sector::current()->get_level()->stats.coins++;
remove_me();
}
int mag = 100; // madnitude that coins are to be thrown
int rand = 30; // max variation to be subtracted from magnitide
- Sector::current()->add_object(new HeavyCoin(position, Vector (2.5,-4.5)*(mag-gameRandom.rand(rand))));
- Sector::current()->add_object(new HeavyCoin(position, Vector (2,-5)*(mag-gameRandom.rand(rand))));
- Sector::current()->add_object(new HeavyCoin(position, Vector (1.5,-5.5)*(mag-gameRandom.rand(rand))));
- Sector::current()->add_object(new HeavyCoin(position, Vector (1,-6)*(mag+gameRandom.rand(rand))));
- Sector::current()->add_object(new HeavyCoin(position, Vector (0.5,-6.5)*(mag-gameRandom.rand(rand))));
- Sector::current()->add_object(new HeavyCoin(position, Vector (-2.5,-4.5)*(mag-gameRandom.rand(rand))));
- Sector::current()->add_object(new HeavyCoin(position, Vector (-2,-5)*(mag-gameRandom.rand(rand))));
- Sector::current()->add_object(new HeavyCoin(position, Vector (-1.5,-5.5)*(mag-gameRandom.rand(rand))));
- Sector::current()->add_object(new HeavyCoin(position, Vector (-1,-6)*(mag+gameRandom.rand(rand))));
- Sector::current()->add_object(new HeavyCoin(position, Vector (-0.5,-6.5)*(mag-gameRandom.rand(rand))));
+ Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (2.5,-4.5)*(mag-gameRandom.rand(rand))));
+ Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (2,-5)*(mag-gameRandom.rand(rand))));
+ Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (1.5,-5.5)*(mag-gameRandom.rand(rand))));
+ Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (1,-6)*(mag+gameRandom.rand(rand))));
+ Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (0.5,-6.5)*(mag-gameRandom.rand(rand))));
+ Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (-2.5,-4.5)*(mag-gameRandom.rand(rand))));
+ Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (-2,-5)*(mag-gameRandom.rand(rand))));
+ Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (-1.5,-5.5)*(mag-gameRandom.rand(rand))));
+ Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (-1,-6)*(mag+gameRandom.rand(rand))));
+ Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (-0.5,-6.5)*(mag-gameRandom.rand(rand))));
remove_me();
}
} // then the first collectable coin drops from one of ten random positions
else if (counter==0){
drop = gameRandom.rand(10);
- Sector::current()->add_object(new HeavyCoin(Vector (position.x+32*((drop<5)?-drop-1:drop-4),-32), Vector (0,0)));
+ Sector::current()->add_object(std::make_shared<HeavyCoin>(Vector (position.x+32*((drop<5)?-drop-1:drop-4),-32), Vector (0,0)));
counter++;
timer.start(DROP_TIME);
} // finally the remainder of the coins drop in a determined but appears to be a random order
if(counter<10){
drop += 7;
if(drop >= 10) drop -=10;
- Sector::current()->add_object(new HeavyCoin(Vector (position.x+32*((drop<5)?-drop-1:drop-4),-32), Vector (0,0)));
+ Sector::current()->add_object(std::make_shared<HeavyCoin>(Vector (position.x+32*((drop<5)?-drop-1:drop-4),-32), Vector (0,0)));
counter++;
timer.start(DROP_TIME);
}else{
{
EndSequence::starting();
endsequence_timer.start(7.3f * ScreenManager::current()->get_speed());
- Sector::current()->add_object(new Fireworks());
+ Sector::current()->add_object(std::make_shared<Fireworks>());
}
void
float vy = -cos(angle)*velocity;
Vector pspeed = Vector(vx, vy);
Vector paccel = Vector(0, 1000);
- Sector::current()->add_object(new SpriteParticle("images/objects/particles/reset.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
+ Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/reset.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
}
if( sprite_name.find("vbell", 0) == std::string::npos ) {
//float green = 0.9;
(void) red;
(void) green;
- sector->add_object(new Particles(pos, 0, 360, Vector(140, 140),
- Vector(0, 0), 45, Color(red, green, 0), 3, 1.3f,
- LAYER_FOREGROUND1+1));
+ sector->add_object(std::make_shared<Particles>(pos, 0, 360, Vector(140, 140),
+ Vector(0, 0), 45, Color(red, green, 0), 3, 1.3f,
+ LAYER_FOREGROUND1+1));
SoundManager::current()->play("sounds/fireworks.wav");
timer.start(graphicsRandom.randf(1.0, 1.5));
}
// throw some particles, bigger and more for large icecrusher
for(int j = 0; j < 9; j++)
{
- Sector::current()->add_object(
- new Particles(Vector(get_bbox().p2.x - j*8 - 4, get_bbox().p2.y),
- 0, 90-5*j, Vector(140, -380), Vector(0, 300),
- 1, Color(.6f, .6f, .6f), 5, 1.8f, LAYER_OBJECTS+1));
- Sector::current()->add_object(
- new Particles(Vector(get_bbox().p1.x + j*8 + 4, get_bbox().p2.y),
- 270+5*j, 360, Vector(140, -380), Vector(0, 300),
- 1, Color(.6f, .6f, .6f), 5, 1.8f, LAYER_OBJECTS+1));
+ Sector::current()->add_object(std::make_shared<Particles>(
+ Vector(get_bbox().p2.x - j*8 - 4, get_bbox().p2.y),
+ 0, 90-5*j, Vector(140, -380), Vector(0, 300),
+ 1, Color(.6f, .6f, .6f), 5, 1.8f, LAYER_OBJECTS+1));
+ Sector::current()->add_object(std::make_shared<Particles>(
+ Vector(get_bbox().p1.x + j*8 + 4, get_bbox().p2.y),
+ 270+5*j, 360, Vector(140, -380), Vector(0, 300),
+ 1, Color(.6f, .6f, .6f), 5, 1.8f, LAYER_OBJECTS+1));
}
}
else {
// throw some particles
for(int j = 0; j < 5; j++)
{
- Sector::current()->add_object(
- new Particles(Vector(get_bbox().p2.x - j*8 - 4, get_bbox().p2.y),
- 0, 90+10*j, Vector(140, -260), Vector(0, 300),
- 1, Color(.6f, .6f, .6f), 4, 1.6f, LAYER_OBJECTS+1));
- Sector::current()->add_object(
- new Particles(Vector(get_bbox().p1.x + j*8 + 4, get_bbox().p2.y),
- 270+10*j, 360, Vector(140, -260), Vector(0, 300),
- 1, Color(.6f, .6f, .6f), 4, 1.6f, LAYER_OBJECTS+1));
+ Sector::current()->add_object(std::make_shared<Particles>(
+ Vector(get_bbox().p2.x - j*8 - 4, get_bbox().p2.y),
+ 0, 90+10*j, Vector(140, -260), Vector(0, 300),
+ 1, Color(.6f, .6f, .6f), 4, 1.6f, LAYER_OBJECTS+1));
+ Sector::current()->add_object(std::make_shared<Particles>(
+ Vector(get_bbox().p1.x + j*8 + 4, get_bbox().p2.y),
+ 270+10*j, 360, Vector(140, -260), Vector(0, 300),
+ 1, Color(.6f, .6f, .6f), 4, 1.6f, LAYER_OBJECTS+1));
}
}
set_state(RECOVERING);
Sector* parent = Sector::current();
if (!parent) return;
for (Sector::GameObjects::iterator i = parent->gameobjects.begin(); i != parent->gameobjects.end(); i++) {
- InfoBlock* block = dynamic_cast<InfoBlock*>(*i);
+ InfoBlock* block = dynamic_cast<InfoBlock*>(i->get());
if (!block) continue;
if (block != this) block->hide_message();
}
if(!dying && !deactivated)
handle_input();
-/*
+ /*
// handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
if (deactivated)
- apply_friction();
-*/
+ apply_friction();
+ */
// extend/shrink tux collision rectangle so that we fall through/walk over 1
// tile holes
Vector ppos = Vector(px, py);
Vector pspeed = Vector(0, 0);
Vector paccel = Vector(0, 0);
- Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite",
- // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
- (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) ?
- // make every other a longer sparkle to make trail a bit fuzzy
- (size_t(game_time*20)%2) ? "small" : "medium"
- :
- "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
+ Sector::current()->add_object(std::make_shared<SpriteParticle>(
+ "images/objects/particles/sparkle.sprite",
+ // draw bright sparkle when there is lots of time left,
+ // dark sparkle when invincibility is about to end
+ (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) ?
+ // make every other a longer sparkle to make trail a bit fuzzy
+ (size_t(game_time*20)%2) ? "small" : "medium"
+ :
+ "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
}
}
SoundManager::current()->play("sounds/skid.wav");
// dust some particles
Sector::current()->add_object(
- new Particles(
+ std::make_shared<Particles>(
Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
Vector paccel = Vector(0, 1000);
std::string action = (dir==LEFT)?"left":"right";
- Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
+ Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
if (climbing) stop_climbing(*climbing);
}
if ((player_status->bonus == ICE_BONUS) && (animate)) {
Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
Vector paccel = Vector(0, 1000);
std::string action = (dir==LEFT)?"left":"right";
- Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
+ Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
if (climbing) stop_climbing(*climbing);
}
player_status->max_fire_bullets = 0;
does_buttjump = false;
physic.set_velocity_y(-300);
on_ground_flag = false;
- Sector::current()->add_object(new Particles(
+ Sector::current()->add_object(std::make_shared<Particles>(
Vector(get_bbox().p2.x, get_bbox().p2.y),
270+20, 270+40,
Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
LAYER_OBJECTS+1));
- Sector::current()->add_object(new Particles(
+ Sector::current()->add_object(std::make_shared<Particles>(
Vector(get_bbox().p1.x, get_bbox().p2.y),
90-40, 90-20,
Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
for (int i = 0; i < 5; i++)
{
// the numbers: starting x, starting y, velocity y
- Sector::current()->add_object(new FallingCoin(get_pos() +
+ Sector::current()->add_object(std::make_shared<FallingCoin>(get_pos() +
Vector(graphicsRandom.rand(5), graphicsRandom.rand(-32,18)),
graphicsRandom.rand(-100,100)));
}
PneumaticPlatform::update(float elapsed_time)
{
if (!slave) {
- Sector::current()->add_object(new PneumaticPlatform(this));
+ Sector::current()->add_object(std::make_shared<PneumaticPlatform>(this));
return;
}
if (!master) {
#include "supertux/direction.hpp"
#include "supertux/moving_object.hpp"
-#include "util/refcounter.hpp"
/**
* An object that inherits from this object is considered "portable" and can
Vector ppos = Vector(px, py);
Vector pspeed = Vector(0, 0);
Vector paccel = Vector(0, 0);
- Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite",
- // draw bright sparkles when very close to Tux, dark sparkles when slightly further
- (disp_x*disp_x + disp_y*disp_y <= 128*128) ?
- // make every other a longer sparkle to make trail a bit fuzzy
- (size_t(game_time*20)%2) ? "small" : "medium" : "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
+ Sector::current()->add_object(std::make_shared<SpriteParticle>(
+ "images/objects/particles/sparkle.sprite",
+ // draw bright sparkles when very close to Tux, dark sparkles when slightly further
+ (disp_x*disp_x + disp_y*disp_y <= 128*128) ?
+ // make every other a longer sparkle to make trail a bit fuzzy
+ (size_t(game_time*20)%2) ? "small" : "medium" : "dark",
+ ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
}
}
}
if (!vertical) { //check if collision happened from above
splash_x = int(particle->pos.x);
splash_y = int(particle->pos.y) - (int(particle->pos.y) % 32) + 32;
- Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
+ Sector::current()->add_object(std::make_shared<RainSplash>(Vector(splash_x, splash_y),vertical));
}
// Uncomment the following to display vertical splashes, too
/* else {
#include "object/specialriser.hpp"
#include "supertux/sector.hpp"
-SpecialRiser::SpecialRiser(Vector pos, MovingObject* _child) :
+SpecialRiser::SpecialRiser(Vector pos, std::shared_ptr<MovingObject> _child) :
offset(),
child(_child)
{
class SpecialRiser : public GameObject
{
public:
- SpecialRiser(Vector pos, MovingObject* child);
+ SpecialRiser(Vector pos, std::shared_ptr<MovingObject> child);
~SpecialRiser();
virtual void update(float elapsed_time);
private:
float offset;
- MovingObject* child;
+ std::shared_ptr<MovingObject> child;
private:
SpecialRiser(const SpecialRiser&);
Vector ppos = Vector(px, py);
Vector pspeed = Vector(0, 0);
Vector paccel = Vector(0, 0);
- Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite",
- // draw bright sparkles when very close to Tux, dark sparkles when slightly further
- (disp_x*disp_x + disp_y*disp_y <= 128*128) ?
- // make every other a longer sparkle to make trail a bit fuzzy
- (size_t(game_time*20)%2) ? "small" : "medium" : "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
+ Sector::current()->add_object(std::make_shared<SpriteParticle>(
+ "images/objects/particles/sparkle.sprite",
+ // draw bright sparkles when very close to Tux, dark sparkles when slightly further
+ (disp_x*disp_x + disp_y*disp_y <= 128*128) ?
+ // make every other a longer sparkle to make trail a bit fuzzy
+ (size_t(game_time*20)%2) ? "small" : "medium" : "dark",
+ ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
}
}
}
void
Thunderstorm::electrify()
{
- Sector::current()->add_object(new Electrifier(200, 1421, ELECTRIFY_TIME));
- Sector::current()->add_object(new Electrifier(201, 1422, ELECTRIFY_TIME));
+ Sector::current()->add_object(std::make_shared<Electrifier>(200, 1421, ELECTRIFY_TIME));
+ Sector::current()->add_object(std::make_shared<Electrifier>(201, 1422, ELECTRIFY_TIME));
}
/* EOF */
return;
}
for(Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) {
- WeakBlock* wb = dynamic_cast<WeakBlock*>(*i);
+ WeakBlock* wb = dynamic_cast<WeakBlock*>(i->get());
if (!wb) continue;
if (wb == this) continue;
if (wb->state != STATE_NORMAL) continue;
// emit a particle
Vector ppos = Vector(graphicsRandom.randf(bbox.p1.x+8, bbox.p2.x-8), graphicsRandom.randf(bbox.p1.y+8, bbox.p2.y-8));
Vector pspeed = Vector(speed.x, speed.y);
- Sector::current()->add_object(new Particles(ppos, 44, 46, pspeed, Vector(0,0), 1, Color(.4f, .4f, .4f), 3, .1f,
+ Sector::current()->add_object(std::make_shared<Particles>(ppos, 44, 46, pspeed, Vector(0,0), 1, Color(.4f, .4f, .4f), 3, .1f,
LAYER_BACKGROUNDTILES+1));
}
}
{
using namespace worldmap;
- floating_image = new _FloatingImage(spritefile);
+ floating_image = std::make_shared<_FloatingImage>(spritefile);
if(Sector::current() != NULL) {
- Sector::current()->add_object(floating_image.get());
+ Sector::current()->add_object(floating_image);
} else if(WorldMap::current() != NULL) {
- WorldMap::current()->add_object(floating_image.get());
+ WorldMap::current()->add_object(floating_image);
} else {
throw new std::runtime_error("Neither sector nor worldmap active");
}
#define HEADER_SUPERTUX_SCRIPTING_FLOATING_IMAGE_HPP
#ifndef SCRIPTING_API
-#define HEADER_SUPERTUX_SCRIPTING_FLOATING_IMAGE_HPP
+#include <memory>
-#include "util/ref.hpp"
+#define HEADER_SUPERTUX_SCRIPTING_FLOATING_IMAGE_HPP
class FloatingImage;
typedef FloatingImage _FloatingImage;
#ifndef SCRIPTING_API
private:
- Ref<_FloatingImage> floating_image;
+ std::shared_ptr<_FloatingImage> floating_image;
#endif
};
global_vm = NULL;
}
-
+
void
Scripting::update_debugger()
{
public:
Scripting(bool enable_debugger);
~Scripting();
-
+
void update_debugger();
private:
<< _( "Environment variables:") << "\n"
<< _( " SUPERTUX2_USER_DIR Directory for user data (savegames, etc.)" ) << "\n"
<< _( " SUPERTUX2_DATA_DIR Directory for the games datafiles" ) << "\n"<< "\n"
-
-
-
+
+
+
<< std::flush;
}
for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
i != sector->gameobjects.end(); ++i) {
- GameObject* object = *i;
+ GameObjectPtr object = *i;
- TileMap* tilemap = dynamic_cast<TileMap*> (object);
+ TileMap* tilemap = dynamic_cast<TileMap*>(object.get());
if(tilemap) {
- transform_tilemap(height, tilemap);
+ transform_tilemap(height, *tilemap);
}
- Player* player = dynamic_cast<Player*> (object);
+ Player* player = dynamic_cast<Player*>(object.get());
if(player) {
Vector pos = player->get_pos();
pos.y = height - pos.y - player->get_bbox().get_height();
player->move(pos);
continue;
}
- BadGuy* badguy = dynamic_cast<BadGuy*> (object);
+ BadGuy* badguy = dynamic_cast<BadGuy*>(object.get());
if(badguy) {
- transform_badguy(height, badguy);
+ transform_badguy(height, *badguy);
}
- Flower* flower = dynamic_cast<Flower*> (object);
+ Flower* flower = dynamic_cast<Flower*>(object.get());
if(flower) {
- transform_flower(flower);
+ transform_flower(*flower);
}
- Platform* platform = dynamic_cast<Platform*> (object);
+ Platform* platform = dynamic_cast<Platform*>(object.get());
if(platform) {
transform_platform(height, *platform);
}
- Block* block = dynamic_cast<Block*> (object);
+ Block* block = dynamic_cast<Block*>(object.get());
if(block) {
transform_block(height, *block);
}
- MovingObject* mobject = dynamic_cast<MovingObject*> (object);
+ MovingObject* mobject = dynamic_cast<MovingObject*>(object.get());
if(mobject) {
- transform_moving_object(height, mobject);
+ transform_moving_object(height, *mobject);
}
}
- for(Sector::SpawnPoints::iterator i = sector->spawnpoints.begin();
- i != sector->spawnpoints.end(); ++i) {
- transform_spawnpoint(height, *i);
+ for(auto i = sector->spawnpoints.begin(); i != sector->spawnpoints.end(); ++i) {
+ transform_spawnpoint(height, **i);
}
if(sector->camera != 0 && sector->player != 0)
}
void
-FlipLevelTransformer::transform_tilemap(float height, TileMap* tilemap)
+FlipLevelTransformer::transform_tilemap(float height, TileMap& tilemap)
{
- for(size_t x = 0; x < tilemap->get_width(); ++x) {
- for(size_t y = 0; y < tilemap->get_height()/2; ++y) {
+ for(size_t x = 0; x < tilemap.get_width(); ++x) {
+ for(size_t y = 0; y < tilemap.get_height()/2; ++y) {
// swap tiles
- int y2 = tilemap->get_height()-1-y;
- uint32_t t1 = tilemap->get_tile_id(x, y);
- uint32_t t2 = tilemap->get_tile_id(x, y2);
- tilemap->change(x, y, t2);
- tilemap->change(x, y2, t1);
+ int y2 = tilemap.get_height()-1-y;
+ uint32_t t1 = tilemap.get_tile_id(x, y);
+ uint32_t t2 = tilemap.get_tile_id(x, y2);
+ tilemap.change(x, y, t2);
+ tilemap.change(x, y2, t1);
}
}
- tilemap->set_drawing_effect(transform_drawing_effect(tilemap->get_drawing_effect()));
- Vector offset = tilemap->get_offset();
- offset.y = height - offset.y - tilemap->get_bbox().get_height();
- tilemap->set_offset(offset);
- Path* path = tilemap->get_path().get();
+ tilemap.set_drawing_effect(transform_drawing_effect(tilemap.get_drawing_effect()));
+ Vector offset = tilemap.get_offset();
+ offset.y = height - offset.y - tilemap.get_bbox().get_height();
+ tilemap.set_offset(offset);
+ Path* path = tilemap.get_path().get();
if (path)
- transform_path(height, tilemap->get_bbox().get_height(), *path);
+ transform_path(height, tilemap.get_bbox().get_height(), *path);
}
void
-FlipLevelTransformer::transform_badguy(float height, BadGuy* badguy)
+FlipLevelTransformer::transform_badguy(float height, BadGuy& badguy)
{
- Vector pos = badguy->get_start_position();
+ Vector pos = badguy.get_start_position();
pos.y = height - pos.y;
- badguy->set_start_position(pos);
+ badguy.set_start_position(pos);
}
void
-FlipLevelTransformer::transform_spawnpoint(float height, SpawnPoint* spawn)
+FlipLevelTransformer::transform_spawnpoint(float height, SpawnPoint& spawn)
{
- Vector pos = spawn->pos;
+ Vector pos = spawn.pos;
pos.y = height - pos.y;
- spawn->pos = pos;
+ spawn.pos = pos;
}
void
-FlipLevelTransformer::transform_moving_object(float height, MovingObject*object)
+FlipLevelTransformer::transform_moving_object(float height, MovingObject& object)
{
- Vector pos = object->get_pos();
- pos.y = height - pos.y - object->get_bbox().get_height();
- object->set_pos(pos);
+ Vector pos = object.get_pos();
+ pos.y = height - pos.y - object.get_bbox().get_height();
+ object.set_pos(pos);
}
void
-FlipLevelTransformer::transform_flower(Flower* flower)
+FlipLevelTransformer::transform_flower(Flower& flower)
{
- flower->drawing_effect = transform_drawing_effect(flower->drawing_effect);
+ flower.drawing_effect = transform_drawing_effect(flower.drawing_effect);
}
void
private:
DrawingEffect transform_drawing_effect(DrawingEffect effect);
void transform_path(float height, float obj_height, Path& path);
- void transform_tilemap(float height, TileMap* tilemap);
- void transform_moving_object(float height, MovingObject* object);
- void transform_badguy(float height, BadGuy* badguy);
- void transform_spawnpoint(float height, SpawnPoint* spawnpoint);
- void transform_flower(Flower *flower);
+ void transform_tilemap(float height, TileMap& tilemap);
+ void transform_moving_object(float height, MovingObject& object);
+ void transform_badguy(float height, BadGuy& badguy);
+ void transform_spawnpoint(float height, SpawnPoint& spawnpoint);
+ void transform_flower(Flower& flower);
void transform_platform(float height, Platform& platform);
void transform_block(float height, Block& block);
};
}
GameObject::GameObject(const GameObject& rhs) :
- RefCounter(),
wants_to_die(rhs.wants_to_die),
remove_listeners(NULL),
name(rhs.name)
#ifndef HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_HPP
#define HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_HPP
+#include <memory>
#include <string>
-#include "util/refcounter.hpp"
-
class DrawingContext;
+class GameObject;
class ObjectRemoveListener;
/**
* - Providing a safe way to remove the object by calling the remove_me
* functions.
*/
-class GameObject : public RefCounter
+class GameObject
{
public:
GameObject();
--- /dev/null
+// SuperTux
+// Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_PTR_HPP
+#define HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_PTR_HPP
+
+#include <memory>
+
+class GameObject;
+
+using GameObjectPtr = std::shared_ptr<GameObject>;
+
+#endif
+
+/* EOF */
if (sequencename == "endsequence") {
if (currentsector->get_players()[0]->get_physic().get_velocity_x() < 0) {
- end_sequence = new EndSequenceWalkLeft();
+ end_sequence = std::make_shared<EndSequenceWalkLeft>();
} else {
- end_sequence = new EndSequenceWalkRight();
+ end_sequence = std::make_shared<EndSequenceWalkRight>();
}
} else if (sequencename == "fireworks") {
- end_sequence = new EndSequenceFireworks();
+ end_sequence = std::make_shared<EndSequenceFireworks>();
} else {
log_warning << "Unknown sequence '" << sequencename << "'. Ignoring." << std::endl;
return;
currentsector->player->set_winning();
// Stop all clocks.
- for(std::vector<GameObject*>::iterator i = currentsector->gameobjects.begin();
+ for(auto i = currentsector->gameobjects.begin();
i != currentsector->gameobjects.end(); ++i)
{
- GameObject* obj = *i;
+ GameObjectPtr obj = *i;
- LevelTime* lt = dynamic_cast<LevelTime*> (obj);
+ auto lt = std::dynamic_pointer_cast<LevelTime>(obj);
if(lt)
lt->stop();
}
int levelnb;
int pause_menu_frame;
- EndSequence* end_sequence;
+ std::shared_ptr<EndSequence> end_sequence;
bool game_pause;
float speed_before_pause;
int total_coins = 0;
for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
Sector* sector = *i;
- for(Sector::GameObjects::iterator o = sector->gameobjects.begin();
- o != sector->gameobjects.end(); ++o) {
- Coin* coin = dynamic_cast<Coin*> (*o);
+ for(auto o = sector->gameobjects.begin(); o != sector->gameobjects.end(); ++o) {
+ Coin* coin = dynamic_cast<Coin*>(o->get());
if(coin)
{
total_coins++;
continue;
}
- BonusBlock *block = dynamic_cast<BonusBlock*> (*o);
+ BonusBlock *block = dynamic_cast<BonusBlock*>(o->get());
if(block)
{
if (block->contents == BonusBlock::CONTENT_COIN)
continue;
}
}
- GoldBomb *goldbomb = dynamic_cast<GoldBomb*> (*o);
+ GoldBomb *goldbomb = dynamic_cast<GoldBomb*>(o->get());
if(goldbomb)
total_coins += 10;
}
Level::get_total_secrets()
{
int total_secrets = 0;
- for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
+ for(auto i = sectors.begin(); i != sectors.end(); ++i)
total_secrets += (*i)->get_total_count<SecretAreaTrigger>();
return total_secrets;
}
add_factory<Switch>("switch");
}
-GameObject*
+GameObjectPtr
ObjectFactory::create(const std::string& name, const Reader& reader)
{
Factories::iterator i = factories.find(name);
}
}
-GameObject*
+GameObjectPtr
ObjectFactory::create(const std::string& name, const Vector& pos, const Direction dir)
{
std::stringstream lisptext;
lisp::Parser parser;
const lisp::Lisp* lisp = parser.parse(lisptext, "create_object");
- GameObject* object = create(name, *(lisp->get_car()));
- return object;
+ return create(name, *(lisp->get_car()));
}
/* EOF */
#include <memory>
#include "supertux/direction.hpp"
+#include "supertux/game_object_ptr.hpp"
#include "util/reader_fwd.hpp"
class Vector;
/** Creates a new gameobject from a lisp node.
* Remember to delete the objects later
*/
- virtual GameObject* create(const Reader& reader) = 0;
+ virtual GameObjectPtr create(const Reader& reader) = 0;
};
template<class C>
ConcreteObjectFactory() {}
~ConcreteObjectFactory() {}
- GameObject* create(const Reader& reader)
+ GameObjectPtr create(const Reader& reader)
{
- return new C(reader);
+ return std::make_shared<C>(reader);
}
};
ObjectFactory();
~ObjectFactory();
- GameObject* create(const std::string& name, const Reader& reader);
- GameObject* create(const std::string& name, const Vector& pos, const Direction dir = AUTO);
+ GameObjectPtr create(const std::string& name, const Reader& reader);
+ GameObjectPtr create(const std::string& name, const Vector& pos, const Direction dir = AUTO);
private:
template<class C>
camera(0),
effect(0)
{
- add_object(new Player(GameSession::current()->get_savegame().get_player_status(), "Tux"));
- add_object(new DisplayEffect("Effect"));
- add_object(new TextObject("Text"));
+ add_object(std::make_shared<Player>(GameSession::current()->get_savegame().get_player_status(), "Tux"));
+ add_object(std::make_shared<DisplayEffect>("Effect"));
+ add_object(std::make_shared<TextObject>("Text"));
SoundManager::current()->preload("sounds/shoot.wav");
for(GameObjects::iterator i = gameobjects.begin();
i != gameobjects.end(); ++i) {
- GameObject* object = *i;
+ GameObjectPtr object = *i;
before_object_remove(object);
- object->unref();
}
-
- for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
- ++i)
- delete *i;
}
Level*
return level;
}
-GameObject*
+GameObjectPtr
Sector::parse_object(const std::string& name_, const Reader& reader)
{
if(name_ == "camera") {
- Camera* camera_ = new Camera(this, "Camera");
+ auto camera_ = std::make_shared<Camera>(this, "Camera");
camera_->parse(reader);
return camera_;
} else if(name_ == "particles-snow") {
- SnowParticleSystem* partsys = new SnowParticleSystem();
+ auto partsys = std::make_shared<SnowParticleSystem>();
partsys->parse(reader);
return partsys;
} else if(name_ == "particles-rain") {
- RainParticleSystem* partsys = new RainParticleSystem();
+ auto partsys = std::make_shared<RainParticleSystem>();
partsys->parse(reader);
return partsys;
} else if(name_ == "particles-comets") {
- CometParticleSystem* partsys = new CometParticleSystem();
+ auto partsys = std::make_shared<CometParticleSystem>();
partsys->parse(reader);
return partsys;
} else if(name_ == "particles-ghosts") {
- GhostParticleSystem* partsys = new GhostParticleSystem();
+ auto partsys = std::make_shared<GhostParticleSystem>();
partsys->parse(reader);
return partsys;
} else if(name_ == "particles-clouds") {
- CloudParticleSystem* partsys = new CloudParticleSystem();
+ auto partsys = std::make_shared<CloudParticleSystem>();
partsys->parse(reader);
return partsys;
} else if(name_ == "money") { // for compatibility with old maps
- return new Jumpy(reader);
+ return std::make_shared<Jumpy>(reader);
} else {
try {
return ObjectFactory::instance().create(name_, reader);
} catch(std::exception& e) {
log_warning << e.what() << "" << std::endl;
- return 0;
+ return {};
}
}
}
} else if(token == "music") {
iter.value()->get(music);
} else if(token == "spawnpoint") {
- SpawnPoint* sp = new SpawnPoint(*iter.lisp());
+ auto sp = std::make_shared<SpawnPoint>(*iter.lisp());
spawnpoints.push_back(sp);
} else if(token == "init-script") {
iter.value()->get(init_script);
ambient_light = Color( vColor );
}
} else {
- GameObject* object = parse_object(token, *(iter.lisp()));
+ GameObjectPtr object = parse_object(token, *(iter.lisp()));
if(object) {
- if(dynamic_cast<Background *>(object)) {
+ if(std::dynamic_pointer_cast<Background>(object)) {
has_background = true;
- } else if(dynamic_cast<Gradient *>(object)) {
+ } else if(std::dynamic_pointer_cast<Gradient>(object)) {
has_background = true;
}
add_object(object);
}
if(!has_background) {
- Gradient* gradient = new Gradient();
+ auto gradient = std::make_shared<Gradient>();
gradient->set_gradient(Color(0.3, 0.4, 0.75), Color(1, 1, 1));
add_object(gradient);
}
if(!camera) {
log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
update_game_objects();
- add_object(new Camera(this, "Camera"));
+ add_object(std::make_shared<Camera>(this, "Camera"));
}
update_game_objects();
bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
if(backgroundimage != "") {
- Background* background = new Background();
+ auto background = std::make_shared<Background>();
background->set_image(backgroundimage, bgspeed);
add_object(background);
} else {
- Gradient* gradient = new Gradient();
+ auto gradient = std::make_shared<Gradient>();
gradient->set_gradient(bkgd_top, bkgd_bottom);
add_object(gradient);
}
std::string particlesystem;
reader.get("particle_system", particlesystem);
if(particlesystem == "clouds")
- add_object(new CloudParticleSystem());
+ add_object(std::make_shared<CloudParticleSystem>());
else if(particlesystem == "snow")
- add_object(new SnowParticleSystem());
+ add_object(std::make_shared<SnowParticleSystem>());
else if(particlesystem == "rain")
- add_object(new RainParticleSystem());
+ add_object(std::make_shared<RainParticleSystem>());
Vector startpos(100, 170);
reader.get("start_pos_x", startpos.x);
reader.get("start_pos_y", startpos.y);
- SpawnPoint* spawn = new SpawnPoint;
+ auto spawn = std::make_shared<SpawnPoint>();
spawn->pos = startpos;
spawn->name = "main";
spawnpoints.push_back(spawn);
std::vector<unsigned int> tiles;
if(reader.get("interactive-tm", tiles)
|| reader.get("tilemap", tiles)) {
- TileMap* tilemap = new TileMap(level->get_tileset());
+ auto tilemap = std::make_shared<TileMap>(level->get_tileset());
tilemap->set(width, height, tiles, LAYER_TILES, true);
// replace tile id 112 (old invisible tile) with 1311 (new invisible tile)
}
if(reader.get("background-tm", tiles)) {
- TileMap* tilemap = new TileMap(level->get_tileset());
+ auto tilemap = std::make_shared<TileMap>(level->get_tileset());
tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
if (height < 19) tilemap->resize(width, 19);
add_object(tilemap);
}
if(reader.get("foreground-tm", tiles)) {
- TileMap* tilemap = new TileMap(level->get_tileset());
+ auto tilemap = std::make_shared<TileMap>(level->get_tileset());
tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
// fill additional space in foreground with tiles of ID 2035 (lightmap/black)
Vector sp_pos;
if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
{
- SpawnPoint* sp = new SpawnPoint;
+ auto sp = std::make_shared<SpawnPoint>();
sp->name = "main";
sp->pos = sp_pos;
spawnpoints.push_back(sp);
if(objects) {
lisp::ListIterator iter(objects);
while(iter.next()) {
- GameObject* object = parse_object(iter.item(), *(iter.lisp()));
+ auto object = parse_object(iter.item(), *(iter.lisp()));
if(object) {
add_object(object);
} else {
}
// add a camera
- Camera* camera_ = new Camera(this, "Camera");
+ auto camera_ = std::make_shared<Camera>(this, "Camera");
add_object(camera_);
update_game_objects();
Vector pos = solids->get_tile_position(x, y);
if(id == 112) {
- add_object(new InvisibleBlock(pos));
+ add_object(std::make_shared<InvisibleBlock>(pos));
solids->change(x, y, 0);
} else if(tile->getAttributes() & Tile::COIN) {
- add_object(new Coin(pos, solids));
+ add_object(std::make_shared<Coin>(pos, solids));
solids->change(x, y, 0);
} else if(tile->getAttributes() & Tile::FULLBOX) {
- add_object(new BonusBlock(pos, tile->getData()));
+ add_object(std::make_shared<BonusBlock>(pos, tile->getData()));
solids->change(x, y, 0);
} else if(tile->getAttributes() & Tile::BRICK) {
if( ( id == 78 ) || ( id == 105 ) ){
- add_object( new Brick(pos, tile->getData(), "images/objects/bonus_block/brickIce.sprite") );
+ add_object( std::make_shared<Brick>(pos, tile->getData(), "images/objects/bonus_block/brickIce.sprite") );
} else if( ( id == 77 ) || ( id == 104 ) ){
- add_object( new Brick(pos, tile->getData(), "images/objects/bonus_block/brick.sprite") );
+ add_object( std::make_shared<Brick>(pos, tile->getData(), "images/objects/bonus_block/brick.sprite") );
} else {
log_warning << "attribute 'brick #t' is not supported for tile-id " << id << std::endl;
- add_object( new Brick(pos, tile->getData(), "images/objects/bonus_block/brick.sprite") );
+ add_object( std::make_shared<Brick>(pos, tile->getData(), "images/objects/bonus_block/brick.sprite") );
}
solids->change(x, y, 0);
} else if(tile->getAttributes() & Tile::GOAL) {
std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
- add_object(new SequenceTrigger(pos, sequence));
+ add_object(std::make_shared<SequenceTrigger>(pos, sequence));
solids->change(x, y, 0);
}
}
// add lights for special tiles
for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); i++) {
- TileMap* tm = dynamic_cast<TileMap*>(*i);
+ TileMap* tm = dynamic_cast<TileMap*>(i->get());
if (!tm) continue;
for(size_t x=0; x < tm->get_width(); ++x) {
for(size_t y=0; y < tm->get_height(); ++y) {
// torch
if (id == 1517) {
float pseudo_rnd = (float)((int)pos.x % 10) / 10;
- add_object(new PulsingLight(center, 1.0f + pseudo_rnd, 0.9f, 1.0f, Color(1.0f, 1.0f, 0.6f, 1.0f)));
+ add_object(std::make_shared<PulsingLight>(center, 1.0f + pseudo_rnd, 0.9f, 1.0f, Color(1.0f, 1.0f, 0.6f, 1.0f)));
}
// lava or lavaflow
if ((id == 173) || (id == 1700) || (id == 1705) || (id == 1706)) {
&& (tm->get_tile_id(x, y-1) != tm->get_tile_id(x,y)))
|| ((x % 3 == 0) && (y % 3 == 0))) {
float pseudo_rnd = (float)((int)pos.x % 10) / 10;
- add_object(new PulsingLight(center, 1.0f + pseudo_rnd, 0.8f, 1.0f, Color(1.0f, 0.3f, 0.0f, 1.0f)));
+ add_object(std::make_shared<PulsingLight>(center, 1.0f + pseudo_rnd, 0.8f, 1.0f, Color(1.0f, 0.3f, 0.0f, 1.0f)));
}
}
}
void
-Sector::add_object(GameObject* object)
+Sector::add_object(GameObjectPtr object)
{
// make sure the object isn't already in the list
#ifndef NDEBUG
}
#endif
- object->ref();
gameobjects_new.push_back(object);
}
void
Sector::activate(const std::string& spawnpoint)
{
- SpawnPoint* sp = 0;
+ std::shared_ptr<SpawnPoint> sp;
for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
++i) {
if((*i)->name == spawnpoint) {
throw scripting::SquirrelError(vm, "Couldn't set sector in roottable");
sq_pop(vm, 1);
- for(GameObjects::iterator i = gameobjects.begin();
- i != gameobjects.end(); ++i) {
- GameObject* object = *i;
+ for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) {
+ GameObjectPtr object = *i;
try_expose(object);
}
// two-player hack: move other players to main player's position
// Maybe specify 2 spawnpoints in the level?
- for(GameObjects::iterator i = gameobjects.begin();
- i != gameobjects.end(); ++i) {
- Player* p = dynamic_cast<Player*>(*i);
+ for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) {
+ Player* p = dynamic_cast<Player*>(i->get());
if (!p) continue;
// spawn smalltux below spawnpoint
throw scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
sq_pop(vm, 1);
- for(GameObjects::iterator i = gameobjects.begin();
- i != gameobjects.end(); ++i) {
- GameObject* object = *i;
+ for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) {
+ GameObjectPtr object = *i;
try_unexpose(object);
}
player->check_bounds();
/* update objects */
- for(GameObjects::iterator i = gameobjects.begin();
- i != gameobjects.end(); ++i) {
- GameObject* object = *i;
+ for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) {
+ GameObjectPtr& object = *i;
if(!object->is_valid())
continue;
Sector::update_game_objects()
{
/** cleanup marked objects */
- for(std::vector<GameObject*>::iterator i = gameobjects.begin();
+ for(auto i = gameobjects.begin();
i != gameobjects.end(); /* nothing */) {
- GameObject* object = *i;
+ GameObjectPtr& object = *i;
if(object->is_valid()) {
++i;
before_object_remove(object);
- object->unref();
i = gameobjects.erase(i);
}
/* add newly created objects */
- for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
+ for(auto i = gameobjects_new.begin();
i != gameobjects_new.end(); ++i)
{
- GameObject* object = *i;
+ GameObjectPtr object = *i;
before_object_add(object);
/* update solid_tilemaps list */
//FIXME: this could be more efficient
solid_tilemaps.clear();
- for(std::vector<GameObject*>::iterator i = gameobjects.begin();
- i != gameobjects.end(); ++i)
+ for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i)
{
- TileMap* tm = dynamic_cast<TileMap*>(*i);
+ TileMap* tm = dynamic_cast<TileMap*>(i->get());
if (!tm) continue;
if (tm->is_solid()) solid_tilemaps.push_back(tm);
}
}
bool
-Sector::before_object_add(GameObject* object)
+Sector::before_object_add(GameObjectPtr object)
{
- Bullet* bullet = dynamic_cast<Bullet*> (object);
- if(bullet != NULL) {
+ auto bullet = dynamic_cast<Bullet*>(object.get());
+ if (bullet)
+ {
bullets.push_back(bullet);
}
- MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
- if(movingobject != NULL) {
+ auto movingobject = dynamic_cast<MovingObject*>(object.get());
+ if (movingobject)
+ {
moving_objects.push_back(movingobject);
}
- Portable* portable = dynamic_cast<Portable*> (object);
- if(portable != NULL) {
+ auto portable = dynamic_cast<Portable*>(object.get());
+ if(portable)
+ {
portables.push_back(portable);
}
- TileMap* tilemap = dynamic_cast<TileMap*> (object);
- if(tilemap != NULL && tilemap->is_solid()) {
+ auto tilemap = dynamic_cast<TileMap*>(object.get());
+ if(tilemap && tilemap->is_solid()) {
solid_tilemaps.push_back(tilemap);
}
- Camera* camera_ = dynamic_cast<Camera*> (object);
- if(camera_ != NULL) {
+ auto camera_ = dynamic_cast<Camera*>(object.get());
+ if(camera_) {
if(this->camera != 0) {
log_warning << "Multiple cameras added. Ignoring" << std::endl;
return false;
this->camera = camera_;
}
- Player* player_ = dynamic_cast<Player*> (object);
- if(player_ != NULL) {
+ auto player_ = dynamic_cast<Player*>(object.get());
+ if(player_) {
if(this->player != 0) {
log_warning << "Multiple players added. Ignoring" << std::endl;
return false;
this->player = player_;
}
- DisplayEffect* effect_ = dynamic_cast<DisplayEffect*> (object);
- if(effect_ != NULL) {
+ auto effect_ = dynamic_cast<DisplayEffect*>(object.get());
+ if(effect_) {
if(this->effect != 0) {
log_warning << "Multiple DisplayEffects added. Ignoring" << std::endl;
return false;
}
void
-Sector::try_expose(GameObject* object)
+Sector::try_expose(GameObjectPtr object)
{
- ScriptInterface* object_ = dynamic_cast<ScriptInterface*> (object);
+ ScriptInterface* object_ = dynamic_cast<ScriptInterface*>(object.get());
if(object_ != NULL) {
HSQUIRRELVM vm = scripting::global_vm;
sq_pushobject(vm, sector_table);
{
HSQUIRRELVM vm = scripting::global_vm;
sq_pushobject(vm, sector_table);
- scripting::SSector* this_ = static_cast<scripting::SSector*> (this);
+ scripting::SSector* this_ = static_cast<scripting::SSector*>(this);
expose_object(vm, -1, this_, "settings", false);
sq_pop(vm, 1);
}
void
-Sector::before_object_remove(GameObject* object)
+Sector::before_object_remove(GameObjectPtr object)
{
- Portable* portable = dynamic_cast<Portable*> (object);
- if(portable != NULL) {
+ Portable* portable = dynamic_cast<Portable*>(object.get());
+ if (portable) {
portables.erase(std::find(portables.begin(), portables.end(), portable));
}
- Bullet* bullet = dynamic_cast<Bullet*> (object);
- if(bullet != NULL) {
+ Bullet* bullet = dynamic_cast<Bullet*>(object.get());
+ if (bullet) {
bullets.erase(std::find(bullets.begin(), bullets.end(), bullet));
}
- MovingObject* moving_object = dynamic_cast<MovingObject*> (object);
- if(moving_object != NULL) {
+ MovingObject* moving_object = dynamic_cast<MovingObject*>(object.get());
+ if (moving_object) {
moving_objects.erase(
std::find(moving_objects.begin(), moving_objects.end(), moving_object));
}
}
void
-Sector::try_unexpose(GameObject* object)
+Sector::try_unexpose(GameObjectPtr object)
{
- ScriptInterface* object_ = dynamic_cast<ScriptInterface*> (object);
+ ScriptInterface* object_ = dynamic_cast<ScriptInterface*>(object.get());
if(object_ != NULL) {
HSQUIRRELVM vm = scripting::global_vm;
SQInteger oldtop = sq_gettop(vm);
context.push_transform();
context.set_translation(camera->get_translation());
- for(GameObjects::iterator i = gameobjects.begin();
- i != gameobjects.end(); ++i) {
- GameObject* object = *i;
+ for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) {
+ GameObjectPtr& object = *i;
if(!object->is_valid())
continue;
if (draw_solids_only)
{
- TileMap* tm = dynamic_cast<TileMap*>(object);
+ TileMap* tm = dynamic_cast<TileMap*>(object.get());
if (tm && !tm->is_solid())
continue;
}
Sector::add_bullet(const Vector& pos, const PlayerStatus* player_status, float xm, Direction dir)
{
// TODO remove this function and move these checks elsewhere...
-
- Bullet* new_bullet = 0;
if((player_status->bonus == FIRE_BONUS &&
(int)bullets.size() >= player_status->max_fire_bullets) ||
(player_status->bonus == ICE_BONUS &&
(int)bullets.size() >= player_status->max_ice_bullets))
return false;
- new_bullet = new Bullet(pos, xm, dir, player_status->bonus);
+ auto new_bullet = std::make_shared<Bullet>(pos, xm, dir, player_status->bonus);
add_object(new_bullet);
SoundManager::current()->play("sounds/shoot.wav");
bool
Sector::add_smoke_cloud(const Vector& pos)
{
- add_object(new SmokeCloud(pos));
+ add_object(std::make_shared<SmokeCloud>(pos));
return true;
}
Sector::get_total_badguys()
{
int total_badguys = 0;
- for(GameObjects::iterator i = gameobjects.begin();
- i != gameobjects.end(); ++i) {
- BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
+ for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) {
+ BadGuy* badguy = dynamic_cast<BadGuy*>(i->get());
if (badguy && badguy->countMe)
total_badguys++;
}
#include "scripting/ssector.hpp"
#include "supertux/direction.hpp"
+#include "supertux/game_object_ptr.hpp"
#include "util/reader_fwd.hpp"
#include "util/writer_fwd.hpp"
#include "util/currenton.hpp"
HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
/// adds a gameobject
- void add_object(GameObject* object);
+ void add_object(GameObjectPtr object);
void set_name(const std::string& name_)
{ this->name = name_; }
{
int total = 0;
for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) {
- if (dynamic_cast<T*>(*i)) total++;
+ if (dynamic_cast<T*>(i->get())) total++;
}
return total;
}
*/
void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
- typedef std::vector<GameObject*> GameObjects;
+ typedef std::vector<GameObjectPtr> GameObjects;
typedef std::vector<MovingObject*> MovingObjects;
- typedef std::vector<SpawnPoint*> SpawnPoints;
+ typedef std::vector<std::shared_ptr<SpawnPoint> > SpawnPoints;
typedef std::vector<Portable*> Portables;
// --- scripting ---
private:
uint32_t collision_tile_attributes(const Rectf& dest) const;
- void before_object_remove(GameObject* object);
- bool before_object_add(GameObject* object);
+ void before_object_remove(GameObjectPtr object);
+ bool before_object_add(GameObjectPtr object);
- void try_expose(GameObject* object);
- void try_unexpose(GameObject* object);
+ void try_expose(GameObjectPtr object);
+ void try_unexpose(GameObjectPtr object);
void try_expose_me();
void try_unexpose_me();
void collision_static_constrains(MovingObject& object);
- GameObject* parse_object(const std::string& name, const Reader& lisp);
+ GameObjectPtr parse_object(const std::string& name, const Reader& lisp);
void fix_old_tiles();
// fade away tilemaps
Sector& sector = *Sector::current();
for(Sector::GameObjects::iterator i = sector.gameobjects.begin(); i != sector.gameobjects.end(); ++i) {
- TileMap* tm = dynamic_cast<TileMap*>(*i);
+ TileMap* tm = dynamic_cast<TileMap*>(i->get());
if (!tm) continue;
if (tm->get_name() != fade_tilemap) continue;
tm->fade(0.0, 1.0);
+++ /dev/null
-// SuperTux
-// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
-//
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-#ifndef HEADER_SUPERTUX_UTIL_REF_HPP
-#define HEADER_SUPERTUX_UTIL_REF_HPP
-
-/** This class behaves like a pointer to a refcounted object, but increments the
- * reference count when new objects are assigned and decrements the refcounter
- * when its lifetime has expired. (similar to std::unique_ptr)
- */
-template<typename T>
-class Ref
-{
-public:
- Ref(T* object_ = 0)
- : object(object_)
- {
- if(object)
- object->ref();
- }
- Ref(const Ref<T>& other)
- : object(other.object)
- {
- if(object)
- object->ref();
- }
- ~Ref()
- {
- if(object)
- object->unref();
- }
-
- Ref<T>& operator= (const Ref<T>& other)
- {
- *this = other.get();
- return *this;
- }
-
- Ref<T>& operator= (T* object_)
- {
- if(object_)
- object_->ref();
- if(this->object)
- this->object->unref();
- this->object = object_;
-
- return *this;
- }
-
- T* operator ->() const
- {
- return object;
- }
-
- T& operator* () const
- {
- return *object;
- }
-
- operator const T* () const
- {
- return object;
- }
-
- T* get() const
- {
- return object;
- }
-
-private:
- T* object;
-};
-
-#endif
-
-/* EOF */
+++ /dev/null
-// Windstille - A Jump'n Shoot Game
-// Copyright (C) 2005 Matthias Braun <matze@braunis.de>
-//
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-#ifndef HEADER_SUPERTUX_UTIL_REFCOUNTER_HPP
-#define HEADER_SUPERTUX_UTIL_REFCOUNTER_HPP
-
-#include <assert.h>
-
-/**
- * A base class that provides reference counting facilities
- */
-class RefCounter
-{
-public:
- RefCounter()
- : refcount(0)
- { }
-
- /** increases reference count */
- void ref()
- {
- refcount++;
- }
- /** decreases reference count. Destroys the object if the reference count
- * reaches 0
- */
- void unref()
- {
- refcount--;
- if(refcount <= 0) {
- delete this;
- }
- }
-
-protected:
- virtual ~RefCounter()
- {
- assert(refcount == 0);
- }
-
-private:
- int refcount;
-};
-
-#endif
-
-/* EOF */
last_position(),
last_target_time()
{
- tux = new Tux(this);
+ tux = std::make_shared<Tux>(this);
add_object(tux);
name = "<no title>";
for(GameObjects::iterator i = game_objects.begin();
i != game_objects.end(); ++i) {
- GameObject* object = *i;
+ GameObjectPtr& object = *i;
try_unexpose(object);
- object->unref();
}
for(SpawnPoints::iterator i = spawn_points.begin();
}
void
-WorldMap::add_object(GameObject* object)
+WorldMap::add_object(GameObjectPtr object)
{
- TileMap* tilemap = dynamic_cast<TileMap*> (object);
+ TileMap* tilemap = dynamic_cast<TileMap*>(object.get());
if(tilemap != 0 && tilemap->is_solid()) {
solid_tilemaps.push_back(tilemap);
}
- object->ref();
try_expose(object);
game_objects.push_back(object);
}
void
-WorldMap::try_expose(GameObject* object)
+WorldMap::try_expose(const GameObjectPtr& object)
{
- ScriptInterface* object_ = dynamic_cast<ScriptInterface*> (object);
+ ScriptInterface* object_ = dynamic_cast<ScriptInterface*>(object.get());
if(object_ != NULL) {
HSQUIRRELVM vm = scripting::global_vm;
sq_pushobject(vm, worldmap_table);
}
void
-WorldMap::try_unexpose(GameObject* object)
+WorldMap::try_unexpose(const GameObjectPtr& object)
{
- ScriptInterface* object_ = dynamic_cast<ScriptInterface*> (object);
+ ScriptInterface* object_ = dynamic_cast<ScriptInterface*>(object.get());
if(object_ != NULL) {
HSQUIRRELVM vm = scripting::global_vm;
SQInteger oldtop = sq_gettop(vm);
lisp::ListIterator iter(sector);
while(iter.next()) {
if(iter.item() == "tilemap") {
- add_object(new TileMap(*(iter.lisp())));
+ add_object(std::make_shared<TileMap>(*(iter.lisp())));
} else if(iter.item() == "background") {
- add_object(new Background(*(iter.lisp())));
+ add_object(std::make_shared<Background>(*(iter.lisp())));
} else if(iter.item() == "music") {
iter.value()->get(music);
} else if(iter.item() == "init-script") {
SpawnPoint* sp = new SpawnPoint(*iter.lisp());
spawn_points.push_back(sp);
} else if(iter.item() == "level") {
- LevelTile* level = new LevelTile(levels_path, *iter.lisp());
- levels.push_back(level);
+ auto level = std::make_shared<LevelTile>(levels_path, *iter.lisp());
+ levels.push_back(level.get());
add_object(level);
} else if(iter.item() == "special-tile") {
- SpecialTile* special_tile = new SpecialTile(*iter.lisp());
- special_tiles.push_back(special_tile);
+ auto special_tile = std::make_shared<SpecialTile>(*iter.lisp());
+ special_tiles.push_back(special_tile.get());
add_object(special_tile);
} else if(iter.item() == "sprite-change") {
- SpriteChange* sprite_change = new SpriteChange(*iter.lisp());
- sprite_changes.push_back(sprite_change);
+ auto sprite_change = std::make_shared<SpriteChange>(*iter.lisp());
+ sprite_changes.push_back(sprite_change.get());
add_object(sprite_change);
} else if(iter.item() == "teleporter") {
- Teleporter* teleporter = new Teleporter(*iter.lisp());
- teleporters.push_back(teleporter);
+ auto teleporter = std::make_shared<Teleporter>(*iter.lisp());
+ teleporters.push_back(teleporter.get());
add_object(teleporter);
} else if(iter.item() == "decal") {
- Decal* decal = new Decal(*iter.lisp());
+ auto decal = std::make_shared<Decal>(*iter.lisp());
add_object(decal);
} else if(iter.item() == "ambient-light") {
std::vector<float> vColor;
{
// update GameObjects
for(size_t i = 0; i < game_objects.size(); ++i) {
- GameObject* object = game_objects[i];
+ GameObjectPtr& object = game_objects[i];
if(!panning || object != tux) {
object->update(delta);
}
// remove old GameObjects
for(GameObjects::iterator i = game_objects.begin();
i != game_objects.end(); ) {
- GameObject* object = *i;
+ GameObjectPtr& object = *i;
if(!object->is_valid()) {
try_unexpose(object);
- object->unref();
i = game_objects.erase(i);
} else {
++i;
/* update solid_tilemaps list */
//FIXME: this could be more efficient
solid_tilemaps.clear();
- for(std::vector<GameObject*>::iterator i = game_objects.begin();
- i != game_objects.end(); ++i)
+ for(auto i = game_objects.begin(); i != game_objects.end(); ++i)
{
- TileMap* tm = dynamic_cast<TileMap*>(*i);
+ TileMap* tm = dynamic_cast<TileMap*>(i->get());
if (!tm) continue;
if (tm->is_solid()) solid_tilemaps.push_back(tm);
}
context.push_transform();
context.set_translation(camera_offset);
- for(GameObjects::iterator i = game_objects.begin();
- i != game_objects.end(); ++i) {
- GameObject* object = *i;
+ for(auto i = game_objects.begin(); i != game_objects.end(); ++i)
+ {
+ GameObjectPtr& object = *i;
if(!panning || object != tux) {
object->draw(context);
}
#include <vector>
#include "control/controller.hpp"
-#include "util/reader_fwd.hpp"
#include "math/vector.hpp"
#include "supertux/console.hpp"
#include "supertux/game_object.hpp"
+#include "supertux/game_object_ptr.hpp"
#include "supertux/level.hpp"
#include "supertux/screen.hpp"
#include "supertux/statistics.hpp"
#include "supertux/tile_manager.hpp"
#include "supertux/timer.hpp"
+#include "util/reader_fwd.hpp"
#include "worldmap/direction.hpp"
#include "worldmap/spawn_point.hpp"
#include "worldmap/special_tile.hpp"
typedef std::vector<SpriteChange*> SpriteChanges;
typedef std::vector<SpawnPoint*> SpawnPoints;
typedef std::vector<LevelTile*> LevelTiles;
- typedef std::vector<GameObject*> GameObjects;
+ typedef std::vector<GameObjectPtr> GameObjects;
typedef std::vector<HSQOBJECT> ScriptList;
- Tux* tux;
+ std::shared_ptr<Tux> tux;
Savegame& m_savegame;
WorldMap(const std::string& filename, Savegame& savegame, const std::string& force_spawnpoint = "");
~WorldMap();
- void add_object(GameObject* object);
+ void add_object(GameObjectPtr object);
- void try_expose(GameObject* object);
- void try_unexpose(GameObject* object);
+ void try_expose(const GameObjectPtr& object);
+ void try_unexpose(const GameObjectPtr& object);
static WorldMap* current()
{ return current_; }
void finished_level(Level* level);
/** returns current Tux incarnation */
- Tux* get_tux() { return tux; }
+ Tux* get_tux() { return tux.get(); }
Savegame& get_savegame() { return m_savegame; }