2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "badguy/dispenser.hpp"
19 #include "audio/sound_manager.hpp"
20 #include "math/random_generator.hpp"
21 #include "object/bullet.hpp"
22 #include "object/player.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "supertux/sector.hpp"
25 #include "util/reader.hpp"
27 Dispenser::Dispenser(const Reader& reader) :
28 BadGuy(reader, "images/creatures/dispenser/dispenser.sprite"),
39 set_colgroup_active(COLGROUP_MOVING_STATIC);
40 sound_manager->preload("sounds/squish.wav");
41 reader.get("cycle", cycle);
42 reader.get("badguy", badguys);
43 random = false; // default
44 reader.get("random", random);
45 type = "dropper"; //default
46 reader.get("type", type);
52 if (badguys.size() <= 0)
53 throw std::runtime_error("No badguys in dispenser.");
55 if (type == "rocketlauncher") {
56 sprite->set_action(dir == LEFT ? "working-left" : "working-right");
57 set_colgroup_active(COLGROUP_MOVING); //if this were COLGROUP_MOVING_STATIC MrRocket would explode on launch.
59 if (start_dir == AUTO) {
62 } else if (type == "cannon") {
63 sprite->set_action("working");
65 sprite->set_action("dropper");
68 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
78 if( autotarget && !swivel ){ // auto cannon sprite might be wrong
79 Player* player = this->get_nearest_player();
81 dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
82 sprite->set_action(dir == LEFT ? "working-left" : "working-right");
85 dispense_timer.start(cycle, true);
90 Dispenser::deactivate()
92 dispense_timer.stop();
95 //TODO: Add launching velocity to certain badguys
97 Dispenser::collision_squished(GameObject& object)
99 //Cannon launching MrRocket can be broken by jumping on it
100 //other dispensers are not that fragile.
101 if (broken || type != "rocketlauncher") {
105 sprite->set_action(dir == LEFT ? "broken-left" : "broken-right");
106 dispense_timer.start(0);
107 set_colgroup_active(COLGROUP_MOVING_STATIC); // Tux can stand on broken cannon.
108 Player* player = dynamic_cast<Player*>(&object);
110 player->bounce(*this);
112 sound_manager->play("sounds/squish.wav", get_pos());
118 Dispenser::collision(GameObject& other, const CollisionHit& hit)
120 Player* player = dynamic_cast<Player*> (&other);
123 if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
124 collision_squished(*player);
133 Bullet* bullet = dynamic_cast<Bullet*> (&other);
135 return collision_bullet(*bullet, hit);
142 Dispenser::active_update(float )
144 if (dispense_timer.check()) {
145 // auto always shoots in Tux's direction
147 if( sprite->animation_done()) {
148 sprite->set_action(dir == LEFT ? "working-left" : "working-right");
152 Player* player = this->get_nearest_player();
153 if( player && !swivel ){
154 Direction targetdir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
155 if( dir != targetdir ){ // no target: swivel cannon
158 sprite->set_action(dir == LEFT ? "swivel-left" : "swivel-right", 1);
159 } else { // tux in sight: shoot
170 Dispenser::launch_badguy()
172 //FIXME: Does is_offscreen() work right here?
173 if (!is_offscreen()) {
174 Direction launchdir = dir;
175 if( !autotarget && start_dir == AUTO ){
176 Player* player = this->get_nearest_player();
178 launchdir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
182 if (badguys.size() > 1) {
184 next_badguy = systemRandom.rand(badguys.size());
189 if (next_badguy >= badguys.size())
194 std::string badguy = badguys[next_badguy];
196 if(badguy == "random") {
197 log_warning << "random is outdated; use a list of badguys to select from." << std::endl;
201 GameObject* badguy_object = NULL;
206 if (type == "dropper")
207 spawnpoint = Vector(get_pos().x, get_pos().y+32);
208 else if (type == "cannon")
209 spawnpoint = Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y);
210 else if (type == "rocketlauncher")
211 spawnpoint = Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y);
213 badguy_object = create_object(badguy, spawnpoint, launchdir);
216 Sector::current()->add_object(badguy_object);
217 } catch(std::exception& e) {
218 log_warning << "Error dispensing badguy: " << e.what() << std::endl;
228 dispense_timer.stop();
232 Dispenser::unfreeze()
239 Dispenser::is_freezable() const
243 IMPLEMENT_FACTORY(Dispenser, "dispenser");