supertux
crusher.hpp
1 // Crusher - A block to stand on, which can drop down to crush the player
2 // Copyright (C) 2008 Christoph Sommer <christoph.sommer@2008.expires.deltadevelopment.de>
3 //
4 // This program 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 // This program 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
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HEADER_SUPERTUX_OBJECT_CRUSHER_HPP
18 #define HEADER_SUPERTUX_OBJECT_CRUSHER_HPP
19 
20 #include "object/moving_sprite.hpp"
21 #include "supertux/physic.hpp"
22 
23 class Player;
24 
26 class Crusher final : public MovingSprite
27 {
28 public:
29  enum CrusherState
30  {
31  IDLE,
32  CRUSHING,
33  RECOVERING
34  };
35 
36  enum class Direction
37  {
38  DOWN,
39  UP,
40  LEFT,
41  RIGHT
42  };
43 
44 private:
45  enum CrusherSize
46  {
47  NORMAL,
48  LARGE
49  };
50 
51  enum Type
52  {
53  ICE,
54  ROCK,
55  CORRUPTED
56  };
57 
58 public:
59  Crusher(const ReaderMapping& reader);
60 
61  virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override;
62  virtual void collision_solid(const CollisionHit& hit) override;
63  virtual void update(float dt_sec) override;
64  virtual void draw(DrawingContext& context) override;
65 
66  virtual void after_editor_set() override;
67  virtual bool is_sideways() const { return m_sideways; }
68 
69  static std::string class_name() { return "crusher"; }
70  virtual std::string get_class_name() const override { return class_name(); }
71  static std::string display_name() { return _("Crusher"); }
72  virtual std::string get_display_name() const override { return display_name(); }
73  virtual GameObjectClasses get_class_types() const override { return MovingSprite::get_class_types().add(typeid(Crusher)); }
74 
75  virtual ObjectSettings get_settings() override;
76  GameObjectTypes get_types() const override;
77  std::string get_default_sprite_name() const override;
78 
79  virtual void on_flip(float height) override;
80 
81  Physic& get_physic() { return m_physic; }
82  bool is_big() const { return m_ic_size == LARGE; }
83  CrusherState get_state() const { return m_state; }
84 
85 private:
86  void spawn_roots(Direction direction);
87 
88  bool found_victim() const;
89  bool not_ice() const;
90  void set_state(CrusherState state, bool force = false);
91  void after_sprite_set();
92  Vector eye_position(bool right) const;
93 
94  void on_type_change(int old_type) override;
95 
96 private:
97  CrusherState m_state;
98  CrusherSize m_ic_size;
99  Type m_ic_type;
100  Vector m_start_position;
101  Physic m_physic;
102  float m_cooldown_timer;
103  bool m_sideways;
104  Direction m_side_dir;
105 
106  SpritePtr m_lefteye;
107  SpritePtr m_righteye;
108  SpritePtr m_whites;
109 
110 private:
111  Crusher(const Crusher&) = delete;
112  Crusher& operator=(const Crusher&) = delete;
113 };
114 
115 class CrusherRoot : public MovingSprite
116 {
117 public:
118  CrusherRoot(Vector position, Crusher::Direction direction, float delay, int layer);
119  virtual GameObjectClasses get_class_types() const override { return MovingSprite::get_class_types().add(typeid(CrusherRoot)); }
120 
121  virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override;
122  virtual void update(float dt_sec) override;
123 
124 private:
125  void start_animation();
126  bool delay_gone() const { return m_delay_remaining <= 0.f; }
127 
128 private:
129  Vector m_original_pos;
130  Crusher::Direction m_direction;
131  float m_delay_remaining;
132 
133 private:
134  CrusherRoot(const CrusherRoot&) = delete;
135  CrusherRoot& operator=(const CrusherRoot&) = delete;
136 };
137 
138 #endif
139 
140 /* EOF */
GameObjectTypes get_types() const override
Get all types of the object, if available.
Definition: crusher.cpp:72
Physics engine.
Definition: physic.hpp:27
virtual void on_flip(float height) override
When level is flipped vertically.
Definition: crusher.cpp:660
virtual HitResponse collision(GameObject &other, const CollisionHit &hit) override
this function is called when the object collided with any other object
Definition: crusher.cpp:123
virtual void draw(DrawingContext &context) override
The GameObject should draw itself onto the provided DrawingContext if this function is called...
Definition: crusher.cpp:469
virtual void update(float dt_sec) override
This function is called once per frame and allows the object to update it&#39;s state.
Definition: crusher.cpp:245
Definition: crusher.hpp:115
Definition: object_settings.hpp:39
This class is the base class for crushers that tux can stand on.
Definition: crusher.hpp:26
virtual GameObjectClasses get_class_types() const override
List notable classes in inheritance hierarchy of class.
Definition: crusher.hpp:119
virtual std::string get_display_name() const override
Returns the display name of the object, translated to the user&#39;s locale.
Definition: crusher.hpp:72
virtual void collision_solid(const CollisionHit &hit) override
this function is called when the object collided with something solid
Definition: crusher.cpp:166
virtual GameObjectClasses get_class_types() const override
List notable classes in inheritance hierarchy of class.
Definition: moving_sprite.hpp:60
This class is responsible for: Updating and drawing the object.
Definition: game_object.hpp:83
A helper structure to list all the type_indexes of the classes in the type hierarchy of a given class...
Definition: game_object.hpp:57
virtual GameObjectClasses get_class_types() const override
List notable classes in inheritance hierarchy of class.
Definition: crusher.hpp:73
Definition: reader_mapping.hpp:32
Abstract base class for ""MovingObject""s, that are represented by a sprite.
Definition: moving_sprite.hpp:33
This module contains methods controlling the player.
Definition: player.hpp:47
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42
This class collects data about a collision.
Definition: collision_hit.hpp:44