1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2020, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
20 /**
21  * \file object/object.h
22  * \brief CObject - base class for all game objects
23  */
24 
25 #pragma once
26 
27 #include "object/crash_sphere.h"
28 #include "object/object_create_params.h"
29 #include "object/object_interface_type.h"
30 #include "object/old_object_interface.h"
31 
32 #include <vector>
33 
34 namespace Gfx
35 {
36 struct ModelCrashSphere;
37 } // namespace Gfx
38 
39 class CLevelParserLine;
40 
41 namespace CBot
42 {
43 class CBotVar;
44 }
45 
46 /**
47  * \class CObject
48  * \brief Base class for all 3D in-game objects
49  *
50  * CObject serves as a base class for all in-game objects, including:
51  *  - buildings,
52  *  - robots,
53  *  - astronaut,
54  *  - plants,
55  *  - aliens.
56  *
57  * As every object has its specific behavior, there are or will be
58  * separate subclasses for each of the specific objects. For the time being,
59  * old object interface is still present, but its functions will be moved to
60  * appropriate subclasses with time. The new CObject interface implemented
61  * here will feature only functions common to all objects.
62  */
63 class CObject : public COldObjectInterface
64 {
65 protected:
66     //! Constructor only accessible to subclasses
67     CObject(int id, ObjectType type);
68 
69 public:
70     CObject(const CObject&) = delete;
71     CObject& operator=(const CObject&) = delete;
72 
73     virtual ~CObject();
74 
75     //! Returns object type
GetType()76     inline ObjectType  GetType() const
77     {
78         return m_type;
79     }
80     //! Returns object's unique id
GetID()81     inline int GetID() const
82     {
83         return m_id;
84     }
85 
86     //! Writes object properties to line in level file
87     virtual void Write(CLevelParserLine* line) = 0;
88     //! Reads object properties from line in level file
89     virtual void Read(CLevelParserLine* line) = 0;
90     //! Reads params required for object creation
91     static ObjectCreateParams ReadCreateParams(CLevelParserLine* line);
92 
93     //! Updates all interface controls
UpdateInterface()94     virtual void UpdateInterface() {};
95 
96     //! Check if object implements the given type of interface
Implements(ObjectInterfaceType type)97     inline bool Implements(ObjectInterfaceType type) const
98     {
99         return m_implementedInterfaces[static_cast<int>(type)];
100     }
101 
102     //! Returns object's position
103     virtual Math::Vector GetPosition() const;
104     //! Sets object's position
105     virtual void SetPosition(const Math::Vector& pos);
106 
107     //! Returns object's rotation (Euler angles)
108     /** Angles are given in radians */
109     virtual Math::Vector GetRotation() const;
110     //! Sets object's rotation (Euler angles)
111     /** Angles are given in radians */
112     virtual void SetRotation(const Math::Vector& rotation);
113 
114     //!@{
115     //! Shortcuts for rotation components
116     void SetRotationX(float angle);
117     void SetRotationY(float angle);
118     void SetRotationZ(float angle);
119     float GetRotationX();
120     float GetRotationY();
121     float GetRotationZ();
122     //!@}
123 
124     //! Returns object's scale
125     virtual Math::Vector GetScale() const;
126     //! Sets objects's scale
127     virtual void SetScale(const Math::Vector& scale);
128     //! Sets objects's scale (uniform value)
129     void SetScale(float scale);
130 
131     //!@{
132     //! Shortcuts for scale components
133     void SetScaleX(float angle);
134     void SetScaleY(float angle);
135     void SetScaleZ(float angle);
136     float GetScaleX();
137     float GetScaleY();
138     float GetScaleZ();
139     //!@}
140 
141     //! Sets crash spheres for object
142     void SetCrashSpheres(const std::vector<Gfx::ModelCrashSphere>& crashSpheres);
143     //! Adds a new crash sphere
144     /** Crash sphere position is given in object coordinates */
145     void AddCrashSphere(const CrashSphere& crashSphere);
146     //! Returns total number of crash spheres
147     int GetCrashSphereCount();
148     //! Returns the first crash sphere (assumes it exists)
149     /** Crash sphere position is returned in world coordinates */
150     CrashSphere GetFirstCrashSphere();
151     //! Returns all crash spheres
152     /** Crash sphere position is returned in world coordinates */
153     std::vector<CrashSphere> GetAllCrashSpheres();
154     //! Removes all crash spheres
155     void DeleteAllCrashSpheres();
156     //! Returns true if this object can collide with the other one
157     bool CanCollideWith(CObject* other);
158 
159     //! Returns sphere used to test for camera collisions
160     Math::Sphere GetCameraCollisionSphere();
161     //! Sets sphere used to test for camera collisions
162     // TODO: remove from here once no longer necessary
163     void SetCameraCollisionSphere(const Math::Sphere& sphere);
164 
165     //! Sets the transparency of object
166     virtual void SetTransparency(float value) = 0;
167 
168     //! Sets flag controlling animation effect on level reset
169     void SetAnimateOnReset(bool animateOnReset);
170     //! Returns flag controlling animation effect on level reset
171     bool GetAnimateOnReset();
172 
173     //! Turns object collisions on/off
174     void SetCollisions(bool collisions);
175     //! Returns true if collisions are enabled
176     bool GetCollisions();
177 
178     //! Sets object team (shouldn't be called after creation because the model won't update!)
179     void SetTeam(int team);
180     //! Returns object team
181     int GetTeam();
182 
183     //! Enable object activation only after you come close
184     void SetProxyActivate(bool activate);
185     //! Returns close activation mode
186     bool GetProxyActivate();
187 
188     //! Sets distance for close activation
189     void SetProxyDistance(float distance);
190     //! Returns distance for close activation
191     float GetProxyDistance();
192 
193     //! Returns CBot "object" variable associated with this object
194     CBot::CBotVar* GetBotVar();
195 
196     //! Returns tooltip text for an object
197     std::string GetTooltipText();
198 
199     //! Set "lock" mode of an object (for example, a robot while it's being factored, or a building while it's built)
200     void SetLock(bool lock);
201     //! Return "lock" mode of an object
202     bool GetLock();
203 
204     //! Is this object active (not dead)?
GetActive()205     virtual bool GetActive() { return true; }
206     //! Is this object detectable (not dead and not underground)?
GetDetectable()207     virtual bool GetDetectable() { return true; }
208 
209     //! Returns true if this object can collide with bullets even though it's not damageable itself
210     //! This is useful to make Barriers protect from bullets
211     //! \todo It will work like this for now but later I'd like to refactor this to something more manageable ~krzys_h
IsBulletWall()212     virtual bool IsBulletWall() { return false; }
213 
214 protected:
215     //! Transform crash sphere by object's world matrix
216     virtual void TransformCrashSphere(Math::Sphere& crashSphere) = 0;
217     //! Transform crash sphere by object's world matrix
218     virtual void TransformCameraCollisionSphere(Math::Sphere& collisionSphere) = 0;
219 
220 protected:
221     const int m_id; //!< unique identifier
222     ObjectType m_type; //!< object type
223     ObjectInterfaceTypes m_implementedInterfaces; //!< interfaces that the object implements
224     Math::Vector m_position;
225     Math::Vector m_rotation;
226     Math::Vector m_scale;
227     std::vector<CrashSphere> m_crashSpheres; //!< crash spheres
228     Math::Sphere m_cameraCollisionSphere;
229     bool m_animateOnReset;
230     bool m_collisions;
231     int m_team;
232     bool m_proxyActivate;
233     float m_proxyDistance;
234     CBot::CBotVar* m_botVar;
235     bool m_lock;
236 };
237