supertux
sticky_object.hpp
1 // SuperTux
2 // Copyright (C) 2024 Daniel Ward <weluvgoatz@gmail.com>
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_STICKY_OBJECT_HPP
18 #define HEADER_SUPERTUX_OBJECT_STICKY_OBJECT_HPP
19 
20 #include "badguy/badguy.hpp"
21 #include "object/moving_sprite.hpp"
22 
23 #include "supertux/sector.hpp"
24 
27 class StickyObject : public MovingSprite
28 {
29 public:
30  StickyObject(const Vector& pos, const std::string& sprite_name,
31  int layer = LAYER_OBJECTS, CollisionGroup collision_group = COLGROUP_MOVING);
32  StickyObject(const ReaderMapping& reader, const std::string& sprite_name,
33  int layer = LAYER_OBJECTS, CollisionGroup collision_group = COLGROUP_MOVING);
34  virtual GameObjectClasses get_class_types() const override { return MovingSprite::get_class_types().add(typeid(StickyObject)); }
35 
36  virtual void update(float dt_sec) override;
37 
38  virtual ObjectSettings get_settings() override;
39 
40  virtual void move_for_owner(MovingObject& object);
41 
42  bool is_sticky() const { return m_sticky; }
43 
44 private:
45  template<class T>
46  void sticky_update()
47  {
48  for (auto& obj : Sector::get().get_objects_by_type<T>())
49  {
50  if (m_col.m_bbox.grown(8.f).overlaps(obj.get_bbox()))
51  {
52  m_col.set_movement(obj.get_movement());
53  if (!m_sticking)
54  {
55  m_displacement_from_owner = get_pos() - obj.get_pos();
56  m_sticking = true;
57  }
58  move_for_owner(obj);
59  return;
60  }
61  }
62  }
63 
64 protected:
65  bool m_sticky; // determines if the object CAN stick, period.
66  bool m_sticking; // determines if the object has found something to stick to.
67  Vector m_displacement_from_owner;
68 
69 private:
70  StickyObject(const StickyObject&) = delete;
71  StickyObject& operator=(const StickyObject&) = delete;
72 };
73 
74 
77 class StickyBadguy : public BadGuy
78 {
79 public:
80  StickyBadguy(const ReaderMapping& reader, const std::string& sprite_name, Direction default_direction,
81  int layer = LAYER_OBJECTS, CollisionGroup collision_group = COLGROUP_MOVING);
82  StickyBadguy(const ReaderMapping& reader, const std::string& sprite_name,
83  int layer = LAYER_OBJECTS, CollisionGroup collision_group = COLGROUP_MOVING);
84  virtual GameObjectClasses get_class_types() const override { return BadGuy::get_class_types().add(typeid(StickyBadguy)); }
85 
86  virtual void sticky_update(float dt_sec);
87 
88  virtual ObjectSettings get_settings() override;
89 
90  virtual void move_for_owner(MovingObject& object);
91 
92  bool is_sticky() const { return m_sticky; }
93 
94 private:
95  template<class T>
96  void sticky_update()
97  {
98  for (auto& obj : Sector::get().get_objects_by_type<T>())
99  {
100  if (m_col.m_bbox.grown(8.f).overlaps(obj.get_bbox()))
101  {
102  m_col.set_movement(obj.get_movement());
103  if (!m_sticking)
104  {
105  m_displacement_from_owner = get_pos() - obj.get_pos();
106  m_sticking = true;
107  }
108  move_for_owner(obj);
109  return;
110  }
111  }
112  }
113 
114 protected:
115  bool m_sticky; // determines if the object CAN stick, period.
116  bool m_sticking; // determines if the object has found something to stick to.
117  Vector m_displacement_from_owner;
118 
119 private:
120  StickyBadguy(const StickyBadguy&) = delete;
121  StickyBadguy& operator=(const StickyBadguy&) = delete;
122 };
123 
124 #endif
125 
126 /* EOF */
Base class for moving sprites that can hurt the Player.
Definition: badguy.hpp:38
virtual GameObjectClasses get_class_types() const override
List notable classes in inheritance hierarchy of class.
Definition: badguy.hpp:71
virtual GameObjectClasses get_class_types() const override
List notable classes in inheritance hierarchy of class.
Definition: sticky_object.hpp:84
Definition: object_settings.hpp:39
Rectf m_bbox
The bounding box of the object (as used for collision detection, this isn&#39;t necessarily the bounding ...
Definition: collision_object.hpp:150
virtual GameObjectClasses get_class_types() const override
List notable classes in inheritance hierarchy of class.
Definition: moving_sprite.hpp:60
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: sticky_object.cpp:44
Base class for all dynamic/moving game objects.
Definition: moving_object.hpp:35
virtual GameObjectClasses get_class_types() const override
List notable classes in inheritance hierarchy of class.
Definition: sticky_object.hpp:34
This is a class for BadGuys that can stick to the sides, top and bottom of MovingObjects, such as platforms, fallblock, tilemap, etc.
Definition: sticky_object.hpp:77
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
Definition: reader_mapping.hpp:32
This is a class for MovingSprites that can stick to the sides, top and bottom of MovingObjects, such as platforms, fallblock, tilemap, etc.
Definition: sticky_object.hpp:27
Abstract base class for ""MovingObject""s, that are represented by a sprite.
Definition: moving_sprite.hpp:33
static Sector & get()
get currently activated sector.
Definition: sector.hpp:74