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 __MAP_TREASURE_HEADER__
12 #define __MAP_TREASURE_HEADER__
13 
14 #include "modes/map/map_objects/map_physical_object.h"
15 
16 #include "modes/map/map_treasure_content.h"
17 
18 namespace vt_map
19 {
20 
21 namespace private_map
22 {
23 
24 /** ****************************************************************************
25 *** \brief Represents an obtainable treasure on the map which the player may access
26 ***
27 *** A treasure is a specific type of physical object, usually in the form of a
28 *** treasure chest. When the player accesses these treasures, the chest animates as
29 *** it is being opened and the treasure supervisor is initialized once the opening
30 *** animation is complete. Each treasure object on a map has a global event associated
31 *** with it to determine whether the treasure contents have already been retrieved by
32 *** the player.
33 ***
34 *** Image files for treasures are single row multi images where the frame ordering
35 *** goes from closed, to opening, to open. This means each map treasure has exactly
36 *** three animations. The closed and open animations are usually single frame images.
37 ***
38 *** To add contents to the treasure for this object, you will need to retreive the
39 *** pointer to the MapTreasure object via the GetTreasure() method, then add drunes
40 *** and/or objects (items/equipment/etc) to the MapTreasure.
41 ***
42 *** \todo Add support for more treasure features, such as locked chests, chests which
43 *** trigger a battle, etc.
44 *** ***************************************************************************/
45 class TreasureObject : public PhysicalObject
46 {
47     //! \brief Constants representing the three types of animations for the treasure
48     enum {
49         TREASURE_CLOSED_ANIM   = 0,
50         TREASURE_OPENING_ANIM  = 1,
51         TREASURE_OPEN_ANIM     = 2
52     };
53 
54 public:
55     /** \param treasure_name The name of the treasure. Used to store and load the treasure state.
56     *** \param treasure The treasure object holding its content.
57     *** \param closed_animation_file The animation file used to display the treasure when it is closed.
58     *** \param opening_animation_file The animation file used to display the treasure when it is opening.
59     *** \param open_animation_file The animation file used to display the treasure when it is open.
60     **/
61     TreasureObject(const std::string &treasure_name,
62                    MapObjectDrawLayer layer,
63                    const std::string &closed_animation_file,
64                    const std::string &opening_animation_file,
65                    const std::string &open_animation_file);
66 
~TreasureObject()67     ~TreasureObject() {
68         delete _treasure;
69     }
70 
71     //! \brief A C++ wrapper made to create a new object from scripting,
72     //! without letting Lua handling the object life-cycle.
73     //! \note We don't permit luabind to use constructors here as it can't currently
74     //! give the object ownership at construction time.
75     static TreasureObject* Create(const std::string &treasure_name,
76                                   MapObjectDrawLayer layer,
77                                   const std::string &closed_animation_file,
78                                   const std::string &opening_animation_file,
79                                   const std::string &open_animation_file);
80 
GetTreasureName()81     std::string GetTreasureName() const {
82         return _treasure_name;
83     }
84 
85     //! \brief Opens the treasure, which changes the active animation and initializes the treasure supervisor when the opening animation finishes.
86     void Open();
87 
88     //! \brief Changes the current animation if it has finished looping
89     void Update();
90 
91     //! \brief Retrieves a pointer to the MapTreasure object holding the treasure.
GetTreasure()92     MapTreasureContent* GetTreasure() {
93         return _treasure;
94     }
95 
96     //! \brief Sets the number of drunes present in the chest's contents.
SetDrunes(uint32_t amount)97     void SetDrunes(uint32_t amount) {
98         _treasure->SetDrunes(amount);
99     }
100 
101     /** \brief Adds an item to the contents of the TreasureObject
102     *** \param id The id of the GlobalObject to add
103     *** \param quantity The number of the object to add (default == 1)
104     *** \return True if the object was added successfully
105     **/
106     bool AddItem(uint32_t id, uint32_t quantity = 1);
107 
108     /** \brief Adds an event triggered at start of the treasure event.
109     *** \param event_id The id of the event to add
110     **/
111     void AddEvent(const std::string& event_id);
112 
113 private:
114     //! \brief Stores the contents of the treasure which will be processed by the treasure supervisor
115     MapTreasureContent* _treasure;
116 
117     //! \brief The treasure object name
118     std::string _treasure_name;
119 
120     //! \brief Events triggered at the start of the treasure event.
121     std::vector<std::string> _events;
122 
123     //! \brief Tells whether the events have been started. So we can keep track of
124     //! whether they've finished before opening the treasure supervisor.
125     bool _events_triggered;
126 
127     //! \brief Tells whether the treasure is being opened.
128     bool _is_opening;
129 
130     //! \brief Loads the state of the chest from the global event corresponding to the current map
131     void _LoadState();
132 };
133 
134 } // namespace private_map
135 
136 } // namespace vt_map
137 
138 #endif // __MAP_TREASURE_HEADER__
139