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 "badguy/bouncing_snowball.hpp"
24 #include "badguy/snowball.hpp"
25 #include "badguy/mrbomb.hpp"
26 #include "badguy/mriceblock.hpp"
27 #include "badguy/mrrocket.hpp"
28 #include "badguy/poisonivy.hpp"
29 #include "badguy/snail.hpp"
30 #include "badguy/skullyhop.hpp"
31 #include "random_generator.hpp"
33 Dispenser::Dispenser(const lisp::Lisp& reader)
34 : BadGuy(reader, "images/creatures/dispenser/dispenser.sprite")
36 reader.get("cycle", cycle);
37 reader.get("badguy", badguy);
40 if (badguy == "mrrocket") {
41 sprite->set_action(dir == LEFT ? "working-left" : "working-right");
42 if( start_dir == AUTO ){
46 else {sprite->set_action("dropper");}
47 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
52 Dispenser::write(lisp::Writer& writer)
54 writer.start_list("dispenser");
56 writer.write_float("x", start_position.x);
57 writer.write_float("y", start_position.y);
58 writer.write_float("cycle", cycle);
59 writer.write_string("badguy", badguy);
61 writer.end_list("dispenser");
67 dispense_timer.start(cycle, true);
72 Dispenser::deactivate()
74 dispense_timer.stop();
77 //TODO: Add launching velocity to certain badguys
79 Dispenser::collision_squished(GameObject& object)
81 //TODO: Should it act like a normal tile when killed?
82 sprite->set_action(dir == LEFT ? "broken-left" : "broken-right");
83 dispense_timer.start(0);
84 Player* player = dynamic_cast<Player*>(&object);
85 if (player) player->bounce(*this);
86 kill_squished(object);
91 Dispenser::active_update(float )
93 if (dispense_timer.check()) {
94 // auto always shoots in Tux's direction
96 if( sprite->animation_done()) {
97 sprite->set_action(dir == LEFT ? "working-left" : "working-right");
101 Player* player = this->get_nearest_player();
102 if( player && !swivel ){
103 Direction targetdir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
104 if( dir != targetdir ){ // no target: swivel cannon
107 sprite->set_action(dir == LEFT ? "swivel-left" : "swivel-right", 1);
108 } else { // tux in sight: shoot
118 // Add themed randomizer
120 Dispenser::launch_badguy()
122 //FIXME: Does is_offscreen() work right here?
123 if (!is_offscreen()) {
124 if (badguy == "snowball")
125 Sector::current()->add_object(new SnowBall(Vector(get_pos().x, get_pos().y+32), dir));
126 else if (badguy == "bouncingsnowball")
127 Sector::current()->add_object(new BouncingSnowball(Vector(get_pos().x, get_pos().y+32), dir));
128 else if (badguy == "mrbomb")
129 Sector::current()->add_object(new MrBomb(Vector(get_pos().x, get_pos().y+32), dir));
130 else if (badguy == "mriceblock")
131 Sector::current()->add_object(new MrIceBlock(Vector(get_pos().x, get_pos().y+32), dir));
132 else if (badguy == "snail")
133 Sector::current()->add_object(new Snail(Vector(get_pos().x, get_pos().y+32), dir));
134 else if (badguy == "mrrocket") {
135 Sector::current()->add_object(new MrRocket(Vector(get_pos().x+(dir == LEFT ? -32 : 32), get_pos().y), dir));}
136 else if (badguy == "poisonivy")
137 Sector::current()->add_object(new PoisonIvy(Vector(get_pos().x, get_pos().y+32), dir));
138 else if (badguy == "skullyhop")
139 Sector::current()->add_object(new SkullyHop(Vector(get_pos().x, get_pos().y+44), dir));
140 else if (badguy == "random")
142 switch (systemRandom.rand(7))
144 case 0: Sector::current()->add_object(new SnowBall(Vector(get_pos().x, get_pos().y+32), dir)); break;
145 case 1: Sector::current()->add_object(new BouncingSnowball(Vector(get_pos().x, get_pos().y+32), dir)); break;
146 case 2: Sector::current()->add_object(new MrBomb(Vector(get_pos().x, get_pos().y+32), dir)); break;
147 case 3: Sector::current()->add_object(new MrIceBlock(Vector(get_pos().x, get_pos().y+32), dir)); break;
148 case 4: Sector::current()->add_object(new PoisonIvy(Vector(get_pos().x, get_pos().y+32), dir)); break;
149 case 5: Sector::current()->add_object(new Snail(Vector(get_pos().x, get_pos().y+32), dir)); break;
150 case 6: Sector::current()->add_object(new SkullyHop(Vector(get_pos().x, get_pos().y+44), dir)); break;
160 dispense_timer.stop();
164 Dispenser::unfreeze()
171 Dispenser::is_freezable() const
175 IMPLEMENT_FACTORY(Dispenser, "dispenser")