4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "dispenser.hpp"
24 #include "object/bullet.hpp"
25 #include "random_generator.hpp"
26 #include "lisp/writer.hpp"
27 #include "object_factory.hpp"
28 #include "audio/sound_manager.hpp"
30 #include "object/player.hpp"
35 Dispenser::Dispenser(const lisp::Lisp& reader)
36 : BadGuy(reader, "images/creatures/dispenser/dispenser.sprite")
38 set_colgroup_active(COLGROUP_MOVING_STATIC);
39 sound_manager->preload("sounds/squish.wav");
40 reader.get("cycle", cycle);
41 reader.get("badguy", badguys);
42 random = false; // default
43 reader.get("random", random);
44 type = "dropper"; //default
45 reader.get("type", type);
51 if (badguys.size() <= 0)
52 throw std::runtime_error("No badguys in dispenser.");
54 if (type == "rocketlauncher") {
55 sprite->set_action(dir == LEFT ? "working-left" : "working-right");
56 set_colgroup_active(COLGROUP_MOVING); //if this were COLGROUP_MOVING_STATIC MrRocket would explode on launch.
58 if (start_dir == AUTO) {
61 } else if (type == "cannon") {
62 sprite->set_action("working");
64 sprite->set_action("dropper");
67 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
72 Dispenser::write(lisp::Writer& writer)
74 writer.start_list("dispenser");
76 writer.write("x", start_position.x);
77 writer.write("y", start_position.y);
78 writer.write("cycle", cycle);
79 writer.write("random", random);
80 writer.write("type", type);
81 writer.write("badguy", badguys);
83 writer.end_list("dispenser");
92 if( autotarget && !swivel ){ // auto cannon sprite might be wrong
93 Player* player = this->get_nearest_player();
95 dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
96 sprite->set_action(dir == LEFT ? "working-left" : "working-right");
99 dispense_timer.start(cycle, true);
104 Dispenser::deactivate()
106 dispense_timer.stop();
109 //TODO: Add launching velocity to certain badguys
111 Dispenser::collision_squished(GameObject& object)
113 //Cannon launching MrRocket can be broken by jumping on it
114 //other dispencers are not that fragile.
115 if (broken || type != "rocketlauncher") {
119 sprite->set_action(dir == LEFT ? "broken-left" : "broken-right");
120 dispense_timer.start(0);
121 set_colgroup_active(COLGROUP_MOVING_STATIC); // Tux can stand on broken cannon.
122 Player* player = dynamic_cast<Player*>(&object);
124 player->bounce(*this);
126 sound_manager->play("sounds/squish.wav", get_pos());
132 Dispenser::collision(GameObject& other, const CollisionHit& hit)
134 Player* player = dynamic_cast<Player*> (&other);
137 if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
138 collision_squished(*player);
147 Bullet* bullet = dynamic_cast<Bullet*> (&other);
149 return collision_bullet(*bullet, hit);
157 Dispenser::active_update(float )
159 if (dispense_timer.check()) {
160 // auto always shoots in Tux's direction
162 if( sprite->animation_done()) {
163 sprite->set_action(dir == LEFT ? "working-left" : "working-right");
167 Player* player = this->get_nearest_player();
168 if( player && !swivel ){
169 Direction targetdir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
170 if( dir != targetdir ){ // no target: swivel cannon
173 sprite->set_action(dir == LEFT ? "swivel-left" : "swivel-right", 1);
174 } else { // tux in sight: shoot
185 Dispenser::launch_badguy()
187 //FIXME: Does is_offscreen() work right here?
188 if (!is_offscreen()) {
189 Direction launchdir = dir;
190 if( !autotarget && start_dir == AUTO ){
191 Player* player = this->get_nearest_player();
193 launchdir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
197 if (badguys.size() > 1) {
199 next_badguy = systemRandom.rand(badguys.size());
204 if (next_badguy >= badguys.size())
209 std::string badguy = badguys[next_badguy];
211 if(badguy == "random") {
212 log_warning << "random is outdated; use a list of badguys to select from." << std::endl;
216 GameObject* badguy_object = NULL;
219 if (type == "dropper")
220 badguy_object = create_badguy_object(badguy, Vector(get_pos().x, get_pos().y+32), launchdir);
221 else if (type == "cannon")
222 badguy_object = create_badguy_object(badguy, Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y), launchdir);
223 else if (type == "rocketlauncher")
224 badguy_object = create_badguy_object(badguy, Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y), launchdir);
227 Sector::current()->add_object(badguy_object);
228 } catch(std::exception& e) {
229 log_warning << "Error dispensing badguy: " << e.what() << std::endl;
239 dispense_timer.stop();
243 Dispenser::unfreeze()
250 Dispenser::is_freezable() const
254 IMPLEMENT_FACTORY(Dispenser, "dispenser")