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.
23 #include "dispenser.hpp"
24 #include "object/bullet.hpp"
25 #include "random_generator.hpp"
27 Dispenser::Dispenser(const lisp::Lisp& reader)
28 : BadGuy(reader, "images/creatures/dispenser/dispenser.sprite")
30 set_colgroup_active(COLGROUP_MOVING_STATIC);
31 sound_manager->preload("sounds/squish.wav");
32 reader.get("cycle", cycle);
33 reader.get("badguy", badguys);
34 random = false; // default
35 reader.get("random", random);
36 type = "dropper"; //default
37 reader.get("type", type);
43 if (badguys.size() <= 0)
44 throw std::runtime_error("No badguys in dispenser.");
46 if (type == "rocketlauncher") {
47 sprite->set_action(dir == LEFT ? "working-left" : "working-right");
48 set_colgroup_active(COLGROUP_MOVING); //if this were COLGROUP_MOVING_STATIC MrRocket would explode on launch.
50 if (start_dir == AUTO) {
53 } else if (type == "cannon") {
54 sprite->set_action("working");
56 sprite->set_action("dropper");
59 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
64 Dispenser::write(lisp::Writer& writer)
66 writer.start_list("dispenser");
68 writer.write("x", start_position.x);
69 writer.write("y", start_position.y);
70 writer.write("cycle", cycle);
71 writer.write("random", random);
72 writer.write("type", type);
73 writer.write("badguy", badguys);
75 writer.end_list("dispenser");
84 if( autotarget && !swivel ){ // auto cannon sprite might be wrong
85 Player* player = this->get_nearest_player();
87 dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
88 sprite->set_action(dir == LEFT ? "working-left" : "working-right");
91 dispense_timer.start(cycle, true);
96 Dispenser::deactivate()
98 dispense_timer.stop();
101 //TODO: Add launching velocity to certain badguys
103 Dispenser::collision_squished(GameObject& object)
105 //Cannon launching MrRocket can be broken by jumping on it
106 //other dispencers are not that fragile.
107 if (broken || type != "rocketlauncher") {
111 sprite->set_action(dir == LEFT ? "broken-left" : "broken-right");
112 dispense_timer.start(0);
113 set_colgroup_active(COLGROUP_MOVING_STATIC); // Tux can stand on broken cannon.
114 Player* player = dynamic_cast<Player*>(&object);
116 player->bounce(*this);
118 sound_manager->play("sounds/squish.wav", get_pos());
124 Dispenser::collision(GameObject& other, const CollisionHit& hit)
126 Player* player = dynamic_cast<Player*> (&other);
129 if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
130 collision_squished(*player);
139 Bullet* bullet = dynamic_cast<Bullet*> (&other);
141 return collision_bullet(*bullet, hit);
149 Dispenser::active_update(float )
151 if (dispense_timer.check()) {
152 // auto always shoots in Tux's direction
154 if( sprite->animation_done()) {
155 sprite->set_action(dir == LEFT ? "working-left" : "working-right");
159 Player* player = this->get_nearest_player();
160 if( player && !swivel ){
161 Direction targetdir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
162 if( dir != targetdir ){ // no target: swivel cannon
165 sprite->set_action(dir == LEFT ? "swivel-left" : "swivel-right", 1);
166 } else { // tux in sight: shoot
177 Dispenser::launch_badguy()
179 //FIXME: Does is_offscreen() work right here?
180 if (!is_offscreen()) {
181 Direction launchdir = dir;
182 if( !autotarget && start_dir == AUTO ){
183 Player* player = this->get_nearest_player();
185 launchdir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
189 if (badguys.size() > 1) {
191 next_badguy = systemRandom.rand(badguys.size());
196 if (next_badguy >= badguys.size())
201 std::string badguy = badguys[next_badguy];
202 GameObject* badguy_object = NULL;
204 if (type == "dropper")
205 badguy_object = create_badguy_object(badguy, Vector(get_pos().x, get_pos().y+32), launchdir);
206 else if (type == "cannon")
207 badguy_object = create_badguy_object(badguy, Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y), launchdir);
208 else if (type == "rocketlauncher")
209 badguy_object = create_badguy_object(badguy, Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y), launchdir);
212 Sector::current()->add_object(badguy_object);
220 dispense_timer.stop();
224 Dispenser::unfreeze()
231 Dispenser::is_freezable() const
235 IMPLEMENT_FACTORY(Dispenser, "dispenser")