1 /*
2  * Copyright (C) 2006-2019 Christopho, Solarus - http://www.solarus-games.org
3  *
4  * Solarus is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Solarus is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include "solarus/core/Map.h"
18 #include "solarus/core/QuestFiles.h"
19 #include "solarus/entities/Stream.h"
20 #include "solarus/entities/StreamAction.h"
21 #include "solarus/graphics/Sprite.h"
22 #include <memory>
23 
24 namespace Solarus {
25 
26 /**
27  * \brief Creates a new stream.
28  * \param name Name identifying the entity on the map or an empty string.
29  * \param layer Layer of the entity to create.
30  * \param xy Coordinates of the entity to create.
31  * \param direction Direction of the stream (0 to 7).
32  * \param sprite_name Animation set id of a sprite or an empty string.
33  */
Stream(const std::string & name,int layer,const Point & xy,int direction,const std::string & sprite_name)34 Stream::Stream(
35     const std::string& name,
36     int layer,
37     const Point& xy,
38     int direction,
39     const std::string& sprite_name
40 ):
41   Entity(name, 0, layer, xy, Size(16, 16)),
42   speed(64),
43   allow_movement(true),
44   allow_attack(true),
45   allow_item(true) {
46 
47   set_collision_modes(CollisionMode::COLLISION_OVERLAPPING);
48   set_origin(8, 13);
49   set_direction(direction);
50   if (!sprite_name.empty()) {
51     create_sprite(sprite_name);
52     notify_direction_changed();
53   }
54 }
55 
56 /**
57  * \brief Returns the type of entity.
58  * \return the type of entity
59  */
get_type() const60 EntityType Stream::get_type() const {
61   return ThisType;
62 }
63 
64 /**
65  * \brief Returns the speed of this stream.
66  * \return The speed in pixels per second.
67  */
get_speed() const68 int Stream::get_speed() const {
69   return speed;
70 }
71 
72 /**
73  * \brief Sets the speed of this stream.
74  * \param speed The speed in pixels per second.
75  */
set_speed(int speed)76 void Stream::set_speed(int speed) {
77   this->speed = speed;
78 }
79 
80 /**
81  * \brief Returns whether the player can move the hero in this stream.
82  * \return \c true if the hero can move, \c false if this is a blocking stream.
83  */
get_allow_movement() const84 bool Stream::get_allow_movement() const {
85   return allow_movement;
86 }
87 
88 /**
89  * \brief Sets whether the player can move the hero in this stream.
90  * \param allow_movement \c true to allow the hero to move, \c false if
91  * this is a blocking stream.
92  */
set_allow_movement(bool allow_movement)93 void Stream::set_allow_movement(bool allow_movement) {
94   this->allow_movement = allow_movement;
95 }
96 
97 /**
98  * \brief Returns whether the hero can use the sword in this stream.
99  * \return \c true if the hero can use the sword.
100  */
get_allow_attack() const101 bool Stream::get_allow_attack() const {
102   return allow_attack;
103 }
104 
105 /**
106  * \brief Sets whether the hero can use the sword in this stream.
107  * \param allow_attack \c true to allow the hero to use the sword.
108  */
set_allow_attack(bool allow_attack)109 void Stream::set_allow_attack(bool allow_attack) {
110   this->allow_attack = allow_attack;
111 }
112 
113 /**
114  * \brief Returns whether the hero can use equipment items in this stream.
115  * \return \c true if the hero can use equipment items.
116  */
get_allow_item() const117 bool Stream::get_allow_item() const {
118   return allow_item;
119 }
120 
121 /**
122  * \brief Sets whether the hero can use equipment items in this stream.
123  * \param allow_item \c true to allow the hero to use equipment items.
124  */
set_allow_item(bool allow_item)125 void Stream::set_allow_item(bool allow_item) {
126   this->allow_item = allow_item;
127 }
128 
129 /**
130  * \copydoc Entity::notify_direction_changed
131  */
notify_direction_changed()132 void Stream::notify_direction_changed() {
133 
134   Entity::notify_direction_changed();
135 
136   // Give the correct direction to the sprite if any.
137   int direction8 = get_direction();
138   const SpritePtr& sprite = get_sprite();
139   if (sprite != nullptr &&
140       sprite->get_nb_directions() >= 8) {
141     // There is a sprite with all necessary directions.
142     sprite->set_current_direction(direction8);
143   }
144 }
145 
146 /**
147  * \copydoc Entity::is_obstacle_for
148  */
is_obstacle_for(Entity & other)149 bool Stream::is_obstacle_for(Entity& other) {
150 
151   return other.is_stream_obstacle(*this);
152 }
153 
154 /**
155  * \copydoc Entity::notify_collision
156  */
notify_collision(Entity & entity_overlapping,CollisionMode)157 void Stream::notify_collision(Entity& entity_overlapping, CollisionMode /* collision_mode */) {
158 
159   const Point& xy_move = direction_to_xy_move(get_direction());
160   entity_overlapping.notify_collision_with_stream(*this, xy_move.x, xy_move.y);
161 }
162 
163 /**
164  * \brief Applies this stream on an entity.
165  * \param target The entity to move. It should overlap the stream.
166  */
activate(Entity & target)167 void Stream::activate(Entity& target) {
168 
169   target.start_stream_action(std::unique_ptr<StreamAction>(
170       new StreamAction(*this, target)
171   ));
172 }
173 
174 }
175 
176