AUTO-canon always shoots towards tux.
[supertux.git] / src / badguy / dispenser.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
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"
32
33 Dispenser::Dispenser(const lisp::Lisp& reader)
34         : BadGuy(reader, "images/creatures/dispenser/dispenser.sprite")
35 {
36   reader.get("cycle", cycle);
37   reader.get("badguy", badguy);
38   autotarget = false;
39   swivel = false;
40   if (badguy == "mrrocket") {
41      sprite->set_action(dir == LEFT ? "working-left" : "working-right");
42      if( start_dir == AUTO ){
43       autotarget = true;
44      }
45   }
46   else {sprite->set_action("dropper");}
47   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
48   countMe = false;
49 }
50
51 void
52 Dispenser::write(lisp::Writer& writer)
53 {
54   writer.start_list("dispenser");
55
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);
60
61   writer.end_list("dispenser");
62 }
63
64 void
65 Dispenser::activate()
66 {
67    if(frozen)
68      return;
69    if (badguy == "mrrocket") {
70       sprite->set_action(dir == LEFT ? "working-left" : "working-right");
71    }
72    dispense_timer.start(cycle, true);
73    launch_badguy();
74 }
75
76 void
77 Dispenser::deactivate()
78 {
79    dispense_timer.stop();
80 }
81
82 //TODO: Add launching velocity to certain badguys
83 bool
84 Dispenser::collision_squished(GameObject& object)
85 {
86   //TODO: Should it act like a normal tile when killed?
87   sprite->set_action(dir == LEFT ? "broken-left" : "broken-right");
88   dispense_timer.start(0);
89   Player* player = dynamic_cast<Player*>(&object);
90   if (player) player->bounce(*this);
91   kill_squished(object);
92   return true;
93 }
94
95 void
96 Dispenser::active_update(float )
97 {
98   if (dispense_timer.check()) {
99     // auto always shoots in Tux's direction
100     if( autotarget ){ 
101       if( sprite->animation_done()) {
102         sprite->set_action(dir == LEFT ? "working-left" : "working-right");
103         swivel = false;
104       }
105
106       Player* player = this->get_nearest_player();
107       if( player && !swivel ){
108         Direction targetdir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
109         if( dir != targetdir ){ // no target: swivel cannon 
110           swivel = true;
111           dir = targetdir;
112           sprite->set_action(dir == LEFT ? "swivel-left" : "swivel-right", 1);
113         } else { // tux in sight: shoot
114           launch_badguy();
115         }
116       }
117     } else {
118       launch_badguy();
119     }
120   }
121 }
122
123 //      Add themed randomizer
124 void
125 Dispenser::launch_badguy()
126 {
127   //FIXME: Does is_offscreen() work right here?
128   if (!is_offscreen()) {
129     if (badguy == "snowball")
130       Sector::current()->add_object(new SnowBall(Vector(get_pos().x, get_pos().y+32), dir));
131     else if (badguy == "bouncingsnowball")
132       Sector::current()->add_object(new BouncingSnowball(Vector(get_pos().x, get_pos().y+32), dir));
133     else if (badguy == "mrbomb")
134       Sector::current()->add_object(new MrBomb(Vector(get_pos().x, get_pos().y+32), dir));
135     else if (badguy == "mriceblock")
136       Sector::current()->add_object(new MrIceBlock(Vector(get_pos().x, get_pos().y+32), dir));
137     else if (badguy == "snail")
138       Sector::current()->add_object(new Snail(Vector(get_pos().x, get_pos().y+32), dir));
139     else if (badguy == "mrrocket") {
140       Sector::current()->add_object(new MrRocket(Vector(get_pos().x+(dir == LEFT ? -32 : 32), get_pos().y), dir));}
141     else if (badguy == "poisonivy")
142       Sector::current()->add_object(new PoisonIvy(Vector(get_pos().x, get_pos().y+32), dir));
143     else if (badguy == "skullyhop")
144       Sector::current()->add_object(new SkullyHop(Vector(get_pos().x, get_pos().y+44), dir));
145     else if (badguy == "random")
146     {
147       switch (systemRandom.rand(7))
148       {
149         case 0: Sector::current()->add_object(new SnowBall(Vector(get_pos().x, get_pos().y+32), dir)); break;
150         case 1: Sector::current()->add_object(new BouncingSnowball(Vector(get_pos().x, get_pos().y+32), dir)); break;
151         case 2: Sector::current()->add_object(new MrBomb(Vector(get_pos().x, get_pos().y+32), dir)); break;
152         case 3: Sector::current()->add_object(new MrIceBlock(Vector(get_pos().x, get_pos().y+32), dir)); break;
153         case 4: Sector::current()->add_object(new PoisonIvy(Vector(get_pos().x, get_pos().y+32), dir)); break;
154         case 5: Sector::current()->add_object(new Snail(Vector(get_pos().x, get_pos().y+32), dir)); break;
155         case 6: Sector::current()->add_object(new SkullyHop(Vector(get_pos().x, get_pos().y+44), dir)); break;
156       }
157     }
158   }
159 }
160
161 void
162 Dispenser::freeze()
163 {
164   BadGuy::freeze();
165   dispense_timer.stop();
166 }
167
168 void
169 Dispenser::unfreeze()
170 {
171   BadGuy::unfreeze();
172   activate();
173 }
174
175 bool
176 Dispenser::is_freezable() const
177 {
178   return true;
179 }
180 IMPLEMENT_FACTORY(Dispenser, "dispenser")