CUGL
Cornell University Game Library
CUBoxObstacle.h
1 //
2 // CUBoxObstacle.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This class implements a rectangular physics object, and is the primary type
6 // of physics object to use. Hence the name, Box2D.
7 //
8 // This class uses our standard shared-pointer architecture.
9 //
10 // 1. The constructor does not perform any initialization; it just sets all
11 // attributes to their defaults.
12 //
13 // 2. All initialization takes place via init methods, which can fail if an
14 // object is initialized more than once.
15 //
16 // 3. All allocation takes place via static constructors which return a shared
17 // pointer.
18 //
19 // CUGL zlib License:
20 // This software is provided 'as-is', without any express or implied
21 // warranty. In no event will the authors be held liable for any damages
22 // arising from the use of this software.
23 //
24 // Permission is granted to anyone to use this software for any purpose,
25 // including commercial applications, and to alter it and redistribute it
26 // freely, subject to the following restrictions:
27 //
28 // 1. The origin of this software must not be misrepresented; you must not
29 // claim that you wrote the original software. If you use this software
30 // in a product, an acknowledgment in the product documentation would be
31 // appreciated but is not required.
32 //
33 // 2. Altered source versions must be plainly marked as such, and must not
34 // be misrepresented as being the original software.
35 //
36 // 3. This notice may not be removed or altered from any source distribution.
37 //
38 // This file is based on the CS 3152 PhysicsDemo Lab by Don Holden, 2007
39 //
40 // Author: Walker White
41 // Version: 11/6/16
42 //
43 #ifndef __CU_BOX_OBSTACLE_H__
44 #define __CU_BOX_OBSTACLE_H__
45 
46 #include <Box2D/Collision/Shapes/b2PolygonShape.h>
47 #include "CUSimpleObstacle.h"
48 
49 namespace cugl {
50 
51 #pragma mark -
52 #pragma mark Box Obstacle
53 
63 class BoxObstacle : public SimpleObstacle {
64 protected:
66  b2PolygonShape _shape;
68  b2Fixture* _geometry;
71 
72 
73 #pragma mark -
74 #pragma mark Scene Graph Methods
75 
83  void resize(const Size& size);
84 
92  virtual void resetDebug() override;
93 
94 
95 #pragma mark -
96 #pragma mark Constructors
97 public:
104  BoxObstacle(void) : SimpleObstacle(), _geometry(nullptr) { }
105 
115  virtual ~BoxObstacle() {
116  CUAssertLog(_geometry == nullptr, "You must deactive physics before deleting an object");
117  }
118 
124  virtual bool init() override { return init(Vec2::ZERO,Size::ZERO); }
125 
138  virtual bool init(const Vec2& pos) override { return init(pos,Size::ZERO); }
139 
153  virtual bool init(const Vec2& pos, const Size& size);
154 
155 
156 #pragma mark -
157 #pragma mark Static Constructors
158 
163  static std::shared_ptr<BoxObstacle> alloc() {
164  std::shared_ptr<BoxObstacle> result = std::make_shared<BoxObstacle>();
165  return (result->init() ? result : nullptr);
166  }
167 
180  static std::shared_ptr<BoxObstacle> alloc(const Vec2& pos) {
181  std::shared_ptr<BoxObstacle> result = std::make_shared<BoxObstacle>();
182  return (result->init(pos) ? result : nullptr);
183  }
184 
198  static std::shared_ptr<BoxObstacle> alloc(const Vec2& pos, const Size& size) {
199  std::shared_ptr<BoxObstacle> result = std::make_shared<BoxObstacle>();
200  return (result->init(pos,size) ? result : nullptr);
201  }
202 
203 
204 #pragma mark -
205 #pragma mark Dimensions
206 
211  const Size& getDimension() const { return _dimension; }
212 
218  void setDimension(const Size& value) { resize(value); markDirty(true); }
219 
226  void setDimension(float width, float height) { setDimension(Size(width,height)); }
227 
233  float getWidth() const { return _dimension.width; }
234 
240  void setWidth(float value) { setDimension(value,_dimension.height); }
241 
247  float getHeight() const { return _dimension.height; }
248 
254  void setHeight(float value) { setDimension(_dimension.width,value); }
255 
256 
257 #pragma mark -
258 #pragma mark Physics Methods
259 
264  virtual void createFixtures() override;
265 
271  virtual void releaseFixtures() override;
272 
273 };
274 
275 }
276 #endif /* __CU_BOX_OBSTACLE_H__ */
Definition: CUSize.h:57
void resize(const Size &size)
virtual bool init() override
Definition: CUBoxObstacle.h:124
float getHeight() const
Definition: CUBoxObstacle.h:247
Definition: CUVec2.h:61
void markDirty(bool value)
Definition: CUObstacle.h:811
BoxObstacle(void)
Definition: CUBoxObstacle.h:104
static const Size ZERO
Definition: CUSize.h:66
virtual bool init(const Vec2 &pos) override
Definition: CUBoxObstacle.h:138
Definition: CUBoxObstacle.h:63
virtual void releaseFixtures() override
Definition: CUSimpleObstacle.h:63
float width
Definition: CUSize.h:61
void setDimension(float width, float height)
Definition: CUBoxObstacle.h:226
void setDimension(const Size &value)
Definition: CUBoxObstacle.h:218
void setHeight(float value)
Definition: CUBoxObstacle.h:254
void setWidth(float value)
Definition: CUBoxObstacle.h:240
float height
Definition: CUSize.h:63
float getWidth() const
Definition: CUBoxObstacle.h:233
static std::shared_ptr< BoxObstacle > alloc()
Definition: CUBoxObstacle.h:163
static std::shared_ptr< BoxObstacle > alloc(const Vec2 &pos)
Definition: CUBoxObstacle.h:180
static std::shared_ptr< BoxObstacle > alloc(const Vec2 &pos, const Size &size)
Definition: CUBoxObstacle.h:198
virtual void resetDebug() override
virtual void createFixtures() override
virtual ~BoxObstacle()
Definition: CUBoxObstacle.h:115
Definition: CUAnimationNode.h:52
static const Vec2 ZERO
Definition: CUVec2.h:71
const Size & getDimension() const
Definition: CUBoxObstacle.h:211
b2PolygonShape _shape
Definition: CUBoxObstacle.h:66
Size _dimension
Definition: CUBoxObstacle.h:70
b2Fixture * _geometry
Definition: CUBoxObstacle.h:68