Compiler warning fix
[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 "sprite/sprite.hpp"
21 #include "supertux/object_factory.hpp"
22 #include "supertux/sector.hpp"
23 #include "object/player.hpp"
24 #include "object/rock.hpp"
25 #include "util/reader.hpp"
26 #include "util/log.hpp"
27
28 #define FLYING_SPEED 120.0
29 #define ACTIVATION_DISTANCE 128.0
30
31 Owl::Owl(const Reader& reader) :
32   BadGuy(reader, "images/creatures/owl/owl.sprite"),
33   carried_obj_name("rock"),
34   carried_object(NULL)
35 {
36   reader.get("carry", carried_obj_name);
37   set_action (dir == LEFT ? "left" : "right", /* loops = */ -1);
38 }
39
40 Owl::Owl(const Vector& pos, Direction d) :
41   BadGuy(pos, d, "images/creatures/owl/owl.sprite"),
42   carried_obj_name("rock"),
43   carried_object(NULL)
44 {
45   set_action (dir == LEFT ? "left" : "right", /* loops = */ -1);
46 }
47
48 void
49 Owl::initialize()
50 {
51   GameObject *game_object;
52
53   physic.set_velocity_x(dir == LEFT ? -FLYING_SPEED : FLYING_SPEED);
54   physic.enable_gravity(false);
55   sprite->set_action(dir == LEFT ? "left" : "right");
56
57   game_object = ObjectFactory::instance().create(carried_obj_name, get_pos(), dir);
58   if (game_object == NULL) {
59     log_fatal << "Creating \"" << carried_obj_name << "\" object failed." << std::endl;
60     return;
61   }
62
63   carried_object = dynamic_cast<Portable *> (game_object);
64   if (carried_object == NULL) {
65     log_warning << "Object is not portable: " << carried_obj_name << std::endl;
66     delete game_object;
67     return;
68   }
69
70   Sector::current ()->add_object (game_object);
71 } /* void initialize */
72
73 bool
74 Owl::is_above_player (void)
75 {
76   Player* player = Sector::current()->get_nearest_player (this->get_bbox ());
77   if (!player)
78     return false;
79
80   /* Let go of carried objects a short while *before* Tux is below us. This
81    * makes it more likely that we'll hit him. */
82   float x_offset = (dir == LEFT) ? ACTIVATION_DISTANCE : -ACTIVATION_DISTANCE;
83
84   const Rectf& player_bbox = player->get_bbox();
85   const Rectf& owl_bbox = get_bbox();
86
87   if ((player_bbox.p1.y >= owl_bbox.p2.y) /* player is below us */
88       && ((player_bbox.p2.x + x_offset) > owl_bbox.p1.x)
89       && ((player_bbox.p1.x + x_offset) < owl_bbox.p2.x))
90     return true;
91   else
92     return false;
93 }
94
95 void
96 Owl::active_update (float elapsed_time)
97 {
98   BadGuy::active_update (elapsed_time);
99
100   if (carried_object != NULL) {
101     if (!is_above_player ()) {
102       Vector obj_pos = get_pos ();
103
104       obj_pos.y += bbox.get_height ();
105       carried_object->grab (*this, obj_pos, dir);
106     }
107     else { /* if (is_above_player) */
108       carried_object->ungrab (*this, dir);
109       carried_object = NULL;
110     }
111   }
112 }
113
114 bool
115 Owl::collision_squished(GameObject&)
116 {
117   Player* player = Sector::current()->get_nearest_player (this->get_bbox ());
118   if (player)
119     player->bounce (*this);
120
121   if (carried_object != NULL) {
122     carried_object->ungrab (*this, dir);
123     carried_object = NULL;
124   }
125
126   kill_fall ();
127   return true;
128 }
129
130 void
131 Owl::collision_solid(const CollisionHit& hit)
132 {
133   if(hit.top || hit.bottom) {
134     physic.set_velocity_y(0);
135   } else if(hit.left || hit.right) {
136     if (dir == LEFT) {
137       set_action ("right", /* loops = */ -1);
138       physic.set_velocity_x (FLYING_SPEED);
139     }
140     else {
141       set_action ("left", /* loops = */ -1);
142       physic.set_velocity_x (-FLYING_SPEED);
143     }
144   }
145 } /* void Owl::collision_solid */
146
147 /* vim: set sw=2 sts=2 et fdm=marker : */
148 /* EOF */