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