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"
23 #include "object/bullet.hpp"
24 #include "random_generator.hpp"
26 Dispenser::Dispenser(const lisp::Lisp& reader)
27 : BadGuy(reader, "images/creatures/dispenser/dispenser.sprite")
29 set_colgroup_active(COLGROUP_MOVING_STATIC);
30 sound_manager->preload("sounds/squish.wav");
31 reader.get("cycle", cycle);
32 reader.get_vector("badguy", badguys);
33 random = false; // default
34 reader.get("random", random);
35 type = "dropper"; //default
36 reader.get("type", type);
42 if (badguys.size() <= 0)
43 throw std::runtime_error("No badguys in dispenser.");
45 if (type == "rocketlauncher") {
46 sprite->set_action(dir == LEFT ? "working-left" : "working-right");
47 set_colgroup_active(COLGROUP_MOVING); //if this were COLGROUP_MOVING_STATIC MrRocket would explode on launch.
49 if (start_dir == AUTO) {
52 } else if (type == "cannon") {
53 sprite->set_action("working");
55 sprite->set_action("dropper");
58 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
63 Dispenser::write(lisp::Writer& writer)
65 writer.start_list("dispenser");
67 writer.write_float("x", start_position.x);
68 writer.write_float("y", start_position.y);
69 writer.write_float("cycle", cycle);
70 writer.write_bool("random", random);
71 writer.write_string("type", type);
72 writer.write_string_vector("badguy", badguys);
74 writer.end_list("dispenser");
83 if( autotarget && !swivel ){ // auto cannon sprite might be wrong
84 Player* player = this->get_nearest_player();
86 dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
87 sprite->set_action(dir == LEFT ? "working-left" : "working-right");
90 dispense_timer.start(cycle, true);
95 Dispenser::deactivate()
97 dispense_timer.stop();
100 //TODO: Add launching velocity to certain badguys
102 Dispenser::collision_squished(GameObject& object)
104 //Cannon launching MrRocket can be broken by jumping on it
105 //other dispencers are not that fragile.
106 if (broken || type != "rocketlauncher") {
110 sprite->set_action(dir == LEFT ? "broken-left" : "broken-right");
111 dispense_timer.start(0);
112 set_colgroup_active(COLGROUP_MOVING_STATIC); // Tux can stand on broken cannon.
113 Player* player = dynamic_cast<Player*>(&object);
115 player->bounce(*this);
117 sound_manager->play("sounds/squish.wav", get_pos());
123 Dispenser::collision(GameObject& other, const CollisionHit& hit)
125 Player* player = dynamic_cast<Player*> (&other);
128 if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
129 collision_squished(*player);
138 Bullet* bullet = dynamic_cast<Bullet*> (&other);
140 return collision_bullet(*bullet, hit);
148 Dispenser::active_update(float )
150 if (dispense_timer.check()) {
151 // auto always shoots in Tux's direction
153 if( sprite->animation_done()) {
154 sprite->set_action(dir == LEFT ? "working-left" : "working-right");
158 Player* player = this->get_nearest_player();
159 if( player && !swivel ){
160 Direction targetdir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
161 if( dir != targetdir ){ // no target: swivel cannon
164 sprite->set_action(dir == LEFT ? "swivel-left" : "swivel-right", 1);
165 } else { // tux in sight: shoot
176 Dispenser::launch_badguy()
178 //FIXME: Does is_offscreen() work right here?
179 if (!is_offscreen()) {
180 Direction launchdir = dir;
181 if( !autotarget && start_dir == AUTO ){
182 Player* player = this->get_nearest_player();
184 launchdir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
188 if (badguys.size() > 1) {
190 next_badguy = systemRandom.rand(badguys.size());
195 if (next_badguy >= badguys.size())
200 std::string badguy = badguys[next_badguy];
201 GameObject* badguy_object = NULL;
203 if (type == "dropper")
204 badguy_object = create_badguy_object(badguy, Vector(get_pos().x, get_pos().y+32), launchdir);
205 else if (type == "cannon")
206 badguy_object = create_badguy_object(badguy, Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y), launchdir);
207 else if (type == "rocketlauncher")
208 badguy_object = create_badguy_object(badguy, Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y), launchdir);
211 Sector::current()->add_object(badguy_object);
219 dispense_timer.stop();
223 Dispenser::unfreeze()
230 Dispenser::is_freezable() const
234 IMPLEMENT_FACTORY(Dispenser, "dispenser")