Turned a lot of other global objects into Currentons
[supertux.git] / src / badguy / owl.cpp
1 //  SuperTux
2 //  Copyright (C) 2008 Wolfgang Becker <uafr@gmx.de>
3 //  Copyright (C) 2010 Florian Forster <supertux at octo.it>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "badguy/owl.hpp"
19
20 #include "audio/sound_manager.hpp"
21 #include "object/anchor_point.hpp"
22 #include "object/player.hpp"
23 #include "object/rock.hpp"
24 #include "sprite/sprite.hpp"
25 #include "supertux/object_factory.hpp"
26 #include "supertux/sector.hpp"
27 #include "util/reader.hpp"
28 #include "util/log.hpp"
29
30 #define FLYING_SPEED 120.0
31 #define ACTIVATION_DISTANCE 128.0
32
33 Owl::Owl(const Reader& reader) :
34   BadGuy(reader, "images/creatures/owl/owl.sprite", LAYER_OBJECTS + 1),
35   carried_obj_name("skydive"),
36   carried_object(NULL)
37 {
38   reader.get("carry", carried_obj_name);
39   set_action (dir == LEFT ? "left" : "right", /* loops = */ -1);
40 }
41
42 Owl::Owl(const Vector& pos, Direction d) :
43   BadGuy(pos, d, "images/creatures/owl/owl.sprite", LAYER_OBJECTS + 1),
44   carried_obj_name("skydive"),
45   carried_object(NULL)
46 {
47   set_action (dir == LEFT ? "left" : "right", /* loops = */ -1);
48 }
49
50 void
51 Owl::initialize()
52 {
53   GameObject *game_object;
54
55   physic.set_velocity_x(dir == LEFT ? -FLYING_SPEED : FLYING_SPEED);
56   physic.enable_gravity(false);
57   sprite->set_action(dir == LEFT ? "left" : "right");
58
59   game_object = ObjectFactory::instance().create(carried_obj_name, get_pos(), dir);
60   if (game_object == NULL) {
61     log_fatal << "Creating \"" << carried_obj_name << "\" object failed." << std::endl;
62     return;
63   }
64
65   carried_object = dynamic_cast<Portable *> (game_object);
66   if (carried_object == NULL) {
67     log_warning << "Object is not portable: " << carried_obj_name << std::endl;
68     delete game_object;
69     return;
70   }
71
72   Sector::current ()->add_object (game_object);
73 } /* void initialize */
74
75 bool
76 Owl::is_above_player (void)
77 {
78   Player* player = Sector::current()->get_nearest_player (this->get_bbox ());
79   if (!player)
80     return false;
81
82   /* Let go of carried objects a short while *before* Tux is below us. This
83    * makes it more likely that we'll hit him. */
84   float x_offset = (dir == LEFT) ? ACTIVATION_DISTANCE : -ACTIVATION_DISTANCE;
85
86   const Rectf& player_bbox = player->get_bbox();
87   const Rectf& owl_bbox = get_bbox();
88
89   if ((player_bbox.p1.y >= owl_bbox.p2.y) /* player is below us */
90       && ((player_bbox.p2.x + x_offset) > owl_bbox.p1.x)
91       && ((player_bbox.p1.x + x_offset) < owl_bbox.p2.x))
92     return true;
93   else
94     return false;
95 }
96
97 void
98 Owl::active_update (float elapsed_time)
99 {
100   BadGuy::active_update (elapsed_time);
101
102   if(frozen)
103     return;
104
105   if (carried_object != NULL) {
106     if (!is_above_player ()) {
107       Vector obj_pos = get_anchor_pos (bbox, ANCHOR_BOTTOM);
108       obj_pos.x -= 16.0; /* FIXME: Actually do use the half width of the carried object here. */
109       obj_pos.y += 3.0; /* Move a little away from the hitbox (the body). Looks nicer. */
110
111       //To drop enemie before leave the screen
112       if (obj_pos.x<=16 || obj_pos.x+16>=Sector::current()->get_width()){
113         carried_object->ungrab (*this, dir);
114         carried_object = NULL;
115       }
116
117      else
118         carried_object->grab (*this, obj_pos, dir);
119     }
120     else { /* if (is_above_player) */
121       carried_object->ungrab (*this, dir);
122       carried_object = NULL;
123     }
124   }
125 }
126
127 bool
128 Owl::collision_squished(GameObject&)
129 {
130   Player* player = Sector::current()->get_nearest_player (this->get_bbox ());
131   if (player)
132     player->bounce (*this);
133
134   if (carried_object != NULL) {
135     carried_object->ungrab (*this, dir);
136     carried_object = NULL;
137   }
138
139   kill_fall ();
140   return true;
141 }
142
143 void
144 Owl::kill_fall()
145 {
146   SoundManager::current()->play("sounds/fall.wav", get_pos());
147   physic.set_velocity_y(0);
148   physic.set_acceleration_y(0);
149   physic.enable_gravity(true);
150   set_state(STATE_FALLING);
151
152   if (carried_object != NULL) {
153     carried_object->ungrab (*this, dir);
154     carried_object = NULL;
155   }
156
157   // start dead-script
158   run_dead_script();
159 }
160
161 void
162 Owl::freeze()
163 {
164   if (carried_object != NULL) {
165     carried_object->ungrab (*this, dir);
166     carried_object = NULL;
167   }
168   physic.enable_gravity(true);
169   BadGuy::freeze();
170 }
171
172 void
173 Owl::unfreeze()
174 {
175   BadGuy::unfreeze();
176   physic.set_velocity_x(dir == LEFT ? -FLYING_SPEED : FLYING_SPEED);
177   physic.enable_gravity(false);
178   sprite->set_action(dir == LEFT ? "left" : "right");
179 }
180
181 bool
182 Owl::is_freezable() const
183 {
184   return true;
185 }
186
187 void
188 Owl::collision_solid(const CollisionHit& hit)
189 {
190   if(frozen)
191   {
192     BadGuy::collision_solid(hit);
193     return;
194   }
195   if(hit.top || hit.bottom) {
196     physic.set_velocity_y(0);
197   } else if(hit.left || hit.right) {
198     if (dir == LEFT) {
199       set_action ("right", /* loops = */ -1);
200       dir = RIGHT;
201       physic.set_velocity_x (FLYING_SPEED);
202     }
203     else {
204       set_action ("left", /* loops = */ -1);
205       dir = LEFT;
206       physic.set_velocity_x (-FLYING_SPEED);
207     }
208   }
209 } /* void Owl::collision_solid */
210
211 /* vim: set sw=2 sts=2 et fdm=marker : */
212 /* EOF */