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