Based off of mathnerd314's patch to fix issue #327
[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 "object/bullet.hpp"
24 #include "random_generator.hpp"
25
26 Dispenser::Dispenser(const lisp::Lisp& reader)
27         : BadGuy(reader, "images/creatures/dispenser/dispenser.sprite")
28 {
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);
37   next_badguy = 0;
38   autotarget = false;
39   swivel = false;
40   broken = false;
41
42   if (badguys.size() <= 0)
43     throw std::runtime_error("No badguys in dispenser.");
44
45   if (type == "rocket-launcher") {
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.
48
49     if (start_dir == AUTO) {
50       autotarget = true;
51     }
52   } else if (type == "cannon") {
53     sprite->set_action("working");
54   } else {
55     sprite->set_action("dropper");
56   }
57
58   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
59   countMe = false;
60 }
61
62 void
63 Dispenser::write(lisp::Writer& writer)
64 {
65   writer.start_list("dispenser");
66
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);
73
74   writer.end_list("dispenser");
75 }
76
77 void
78 Dispenser::activate()
79 {
80    if( broken ){
81      return;
82    }
83    if( autotarget && !swivel ){ // auto cannon sprite might be wrong
84       Player* player = this->get_nearest_player();
85       if( player ){
86         dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
87         sprite->set_action(dir == LEFT ? "working-left" : "working-right");
88       }
89    }
90    dispense_timer.start(cycle, true);
91    launch_badguy();
92 }
93
94 void
95 Dispenser::deactivate()
96 {
97    dispense_timer.stop();
98 }
99
100 //TODO: Add launching velocity to certain badguys
101 bool
102 Dispenser::collision_squished(GameObject& object)
103 {
104   //Cannon launching MrRocket can be broken by jumping on it
105   //other dispencers are not that fragile.
106   if (broken || type != "rocket-launcher") {
107     return false;
108   }
109
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);
114   if (player){
115     player->bounce(*this);
116   }
117   sound_manager->play("sounds/squish.wav", get_pos());
118   broken = true;
119   return true;
120 }
121
122 HitResponse
123 Dispenser::collision(GameObject& other, const CollisionHit& hit)
124 {
125   Player* player = dynamic_cast<Player*> (&other);
126   if(player) {
127     // hit from above?
128     if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
129       collision_squished(*player);
130       return FORCE_MOVE;
131     }
132     if(frozen){
133       unfreeze();
134     }
135     return FORCE_MOVE;
136   }
137
138   Bullet* bullet = dynamic_cast<Bullet*> (&other);
139   if(bullet){
140     return collision_bullet(*bullet, hit);
141   }
142
143   return FORCE_MOVE;
144 }
145
146
147 void
148 Dispenser::active_update(float )
149 {
150   if (dispense_timer.check()) {
151     // auto always shoots in Tux's direction
152     if( autotarget ){ 
153       if( sprite->animation_done()) {
154         sprite->set_action(dir == LEFT ? "working-left" : "working-right");
155         swivel = false;
156       }
157
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 
162           swivel = true;
163           dir = targetdir;
164           sprite->set_action(dir == LEFT ? "swivel-left" : "swivel-right", 1);
165         } else { // tux in sight: shoot
166           launch_badguy();
167         }
168       }
169     } else {
170       launch_badguy();
171     }
172   }
173 }
174
175 void
176 Dispenser::launch_badguy()
177 {
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();
183       if( player ){
184         launchdir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
185       } 
186     } 
187
188     if (badguys.size() > 1) {
189       if (random) {
190         next_badguy = systemRandom.rand(badguys.size());
191       }
192       else {
193         next_badguy++;
194
195         if (next_badguy >= badguys.size())
196           next_badguy = 0;
197       }
198     }
199
200     std::string badguy = badguys[next_badguy];
201     GameObject* badguy_object = NULL;
202
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 == "rocket-launcher")
208       badguy_object = create_badguy_object(badguy, Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y), launchdir);
209
210     if (badguy_object)
211       Sector::current()->add_object(badguy_object);
212   }
213 }
214
215 void
216 Dispenser::freeze()
217 {
218   BadGuy::freeze();
219   dispense_timer.stop();
220 }
221
222 void
223 Dispenser::unfreeze()
224 {
225   BadGuy::unfreeze();
226   activate();
227 }
228
229 bool
230 Dispenser::is_freezable() const
231 {
232   return true;
233 }
234 IMPLEMENT_FACTORY(Dispenser, "dispenser")