1 ///////////////////////////////////////////////////////////////////////////////
2 //            Copyright (C) 2004-2011 by The Allacrost Project
3 //            Copyright (C) 2012-2016 by Bertram (Valyria Tear)
4 //                         All Rights Reserved
5 //
6 // This code is licensed under the GNU GPL version 2. It is free software
7 // and you may modify it and/or redistribute it under the terms of this license.
8 // See https://www.gnu.org/copyleft/gpl.html for details.
9 ///////////////////////////////////////////////////////////////////////////////
10 
11 #ifndef __PHYSICAL_OBJECT_HEADER__
12 #define __PHYSICAL_OBJECT_HEADER__
13 
14 #include "modes/map/map_objects/map_object.h"
15 
16 namespace vt_map
17 {
18 
19 namespace private_map
20 {
21 
22 /** ****************************************************************************
23 *** \brief Represents visible objects on the map that have no motion.
24 ***
25 *** This class represents both still image and animated objects. These objects
26 *** are usually fixed in place and do not change their position.
27 ***
28 *** \note If the object does not have any animated images, set the 'updatable'
29 *** member of the base class to false. Forgetting to do this will do no harm, but
30 *** it will make it heavier.
31 *** ***************************************************************************/
32 class PhysicalObject : public MapObject
33 {
34 public:
35     explicit PhysicalObject(MapObjectDrawLayer layer);
36     virtual ~PhysicalObject() override;
37 
38     //! \brief A C++ wrapper made to create a new object from scripting,
39     //! without letting Lua handling the object life-cycle.
40     //! \note We don't permit luabind to use constructors here as it can't currently
41     //! give the object ownership at construction time.
42     static PhysicalObject* Create(MapObjectDrawLayer layer);
43 
44     //! \brief Updates the object's current animation.
45     virtual void Update() override;
46 
47     //! \brief Draws the object to the screen, if it is visible.
48     virtual void Draw() override;
49 
50     /** \brief Sets a new animation using the animation filename provided
51     *** \param animation_filename The name of the animation file to use for the animation
52     *** \return The animation id that can later be used with SetCurrentAnimation() or -1 if invalid
53     **/
54     int32_t AddAnimation(const std::string& animation_filename);
55 
56     /** \brief Sets a new still animation using the image filename provided
57     *** \param image_filename The name of the image file to use for the animation
58     *** \return The animation id that can later be used with SetCurrentAnimation() or -1 if invalid
59     **/
60     int32_t AddStillFrame(const std::string& image_filename);
61 
AddAnimation(const vt_video::AnimatedImage & new_img)62     void AddAnimation(const vt_video::AnimatedImage& new_img) {
63         _animations.push_back(new_img);
64     }
65 
66     void SetCurrentAnimation(uint32_t animation_id);
67 
SetAnimationProgress(uint32_t progress)68     void SetAnimationProgress(uint32_t progress) {
69         _animations[_current_animation_id].SetTimeProgress(progress);
70     }
71 
GetCurrentAnimationId()72     uint32_t GetCurrentAnimationId() const {
73         return _current_animation_id;
74     }
75 
RandomizeCurrentAnimationFrame()76     void RandomizeCurrentAnimationFrame() {
77         _animations[_current_animation_id].RandomizeAnimationFrame();
78     }
79 
80     /** \brief Adds an event triggered when talking to a physical object
81     *** \param event_id The event string id
82     **/
SetEventWhenTalking(const std::string & event_id)83     void SetEventWhenTalking(const std::string& event_id) {
84         _event_when_talking = event_id;
85     }
86 
87     /** \brief Removes the event linked to a physical object
88     **/
ClearEventWhenTalking()89     void ClearEventWhenTalking() {
90         _event_when_talking.clear();
91     }
92 
93     //! \brief Returns the event id triggered when talking to the physical object.
GetEventIdWhenTalking()94     const std::string& GetEventIdWhenTalking() const {
95         return _event_when_talking;
96     }
97 
98     virtual void SetGrayscale(bool grayscale = true) override {
99         _grayscale = grayscale;
100         for (vt_video::AnimatedImage& animation : _animations) {
101             animation.SetGrayscale(grayscale);
102         }
103     }
104     //@}
105 
106 protected:
107     /** \brief A vector containing all the object's animations.
108     *** These need not be actual animations. If you just want a still image, add only a single
109     *** frame to the animation. Usually only need a single still image or animation will be
110     *** needed, but a vector is used here in case others are needed.
111     **/
112     std::vector<vt_video::AnimatedImage> _animations;
113 
114 private:
115     /** \brief The index to the animations vector that contains the current image to display
116     *** When modifying this member, take care not to exceed the bounds of the animations vector
117     **/
118     uint32_t _current_animation_id;
119 
120     //! \brief The event id triggered when talking to the sprite.
121     std::string _event_when_talking;
122 };
123 
124 } // namespace private_map
125 
126 } // namespace vt_map
127 
128 #endif // __PHYSICAL_OBJECT_HEADER__
129