CUGL
Cornell University Game Library
CUPolygonObstacle.h
1 //
2 // CUPolygonObstacle.h
3 // Cornell Extensions to Cocos2D
4 //
5 // This class implements a polygonal Physics object. This is different from
6 // PolygonNode, which is used for drawing. This class is substantially more
7 // complex than the other physics objects, but it will allow you to draw
8 // arbitrary shapes. Be careful modifying this file as there are a lot of
9 // subtleties here.
10 //
11 // This class uses our standard shared-pointer architecture.
12 //
13 // 1. The constructor does not perform any initialization; it just sets all
14 // attributes to their defaults.
15 //
16 // 2. All initialization takes place via init methods, which can fail if an
17 // object is initialized more than once.
18 //
19 // 3. All allocation takes place via static constructors which return a shared
20 // pointer.
21 //
22 // CUGL zlib License:
23 // This software is provided 'as-is', without any express or implied
24 // warranty. In no event will the authors be held liable for any damages
25 // arising from the use of this software.
26 //
27 // Permission is granted to anyone to use this software for any purpose,
28 // including commercial applications, and to alter it and redistribute it
29 // freely, subject to the following restrictions:
30 //
31 // 1. The origin of this software must not be misrepresented; you must not
32 // claim that you wrote the original software. If you use this software
33 // in a product, an acknowledgment in the product documentation would be
34 // appreciated but is not required.
35 //
36 // 2. Altered source versions must be plainly marked as such, and must not
37 // be misrepresented as being the original software.
38 //
39 // 3. This notice may not be removed or altered from any source distribution.
40 //
41 // This file is based on the CS 3152 PhysicsDemo Lab by Don Holden, 2007
42 //
43 // Author: Walker White
44 // Version: 11/6/16
45 //
46 #ifndef __CU_POLYGON_OBSTACLE_H__
47 #define __CU_POLYGON_OBSTACLE_H__
48 
49 #include "CUSimpleObstacle.h"
50 #include <cugl/math/CUPoly2.h>
51 
52 namespace cugl {
53 
54 #pragma mark -
55 #pragma mark Polygon Obstacle
56 
64 protected:
68  b2PolygonShape* _shapes;
70  b2Fixture** _geoms;
74  int _fixCount;
75 
76 
77 #pragma mark -
78 #pragma mark Scene Graph Methods
79 
86  void resize(const Size& size);
87 
95  virtual void resetDebug() override;
96 
102  void resetShapes();
103 
104 
105 #pragma mark -
106 #pragma mark Constructors
107 public:
114  PolygonObstacle(void) : SimpleObstacle(), _shapes(nullptr), _geoms(nullptr) { }
115 
122  virtual ~PolygonObstacle();
123 
124  // Turn off init warnings
125  using SimpleObstacle::init;
126 
137  virtual bool init(const Poly2& poly) { return init(poly,Vec2(0.5,0.5)); }
138 
153  virtual bool init(const Poly2& poly, const Vec2& anchor);
154 
155 
156 #pragma mark -
157 #pragma mark Static Constructors
158 
168  static std::shared_ptr<PolygonObstacle> alloc(const Poly2& poly) {
169  std::shared_ptr<PolygonObstacle> result = std::make_shared<PolygonObstacle>();
170  return (result->init(poly) ? result : nullptr);
171  }
172 
186  static std::shared_ptr<PolygonObstacle> alloc(const Poly2& poly, const Vec2& anchor) {
187  std::shared_ptr<PolygonObstacle> result = std::make_shared<PolygonObstacle>();
188  return (result->init(poly,anchor) ? result : nullptr);
189  }
190 
191 
192 #pragma mark -
193 #pragma mark Dimensions
194 
199  const Size& getSize() const { return _polygon.getBounds().size; }
200 
210  void setSize(const Size& value) { resize(value); markDirty(true); }
211 
218  void setSize(float width, float height) { setSize(Size(width, height)); }
219 
225  float getWidth() const { return _polygon.getBounds().size.width; }
226 
236  void setWidth(float value) { setSize(value,getHeight()); }
237 
243  float getHeight() const { return _polygon.getBounds().size.height; }
244 
254  void setHeight(float value) { setSize(getWidth(),value); }
255 
266  const Vec2& getAnchor() const { return _anchor; }
267 
278  void setAnchor(const Vec2& value) { setAnchor(value.x, value.y); }
279 
291  void setAnchor(float x, float y);
292 
298  const Poly2& getPolygon() const { return _polygon; }
299 
308  void setPolygon(const Poly2& value);
309 
310 
311 #pragma mark -
312 #pragma mark Physics Methods
313 
319  virtual void createFixtures() override;
320 
326  virtual void releaseFixtures() override;
327 
328 
329 };
330 
331 }
332 #endif /* __CU_POLYGON_OBSTACLE_H__ */
Definition: CUSize.h:57
float x
Definition: CUVec2.h:66
float y
Definition: CUVec2.h:68
void setPolygon(const Poly2 &value)
PolygonObstacle(void)
Definition: CUPolygonObstacle.h:114
void setHeight(float value)
Definition: CUPolygonObstacle.h:254
static std::shared_ptr< PolygonObstacle > alloc(const Poly2 &poly)
Definition: CUPolygonObstacle.h:168
Definition: CUPoly2.h:115
Definition: CUVec2.h:61
void markDirty(bool value)
Definition: CUObstacle.h:811
static std::shared_ptr< PolygonObstacle > alloc(const Poly2 &poly, const Vec2 &anchor)
Definition: CUPolygonObstacle.h:186
const Rect & getBounds() const
Definition: CUPoly2.h:834
int _fixCount
Definition: CUPolygonObstacle.h:74
void setAnchor(const Vec2 &value)
Definition: CUPolygonObstacle.h:278
const Poly2 & getPolygon() const
Definition: CUPolygonObstacle.h:298
Definition: CUPolygonObstacle.h:63
b2Fixture ** _geoms
Definition: CUPolygonObstacle.h:70
b2PolygonShape * _shapes
Definition: CUPolygonObstacle.h:68
virtual bool init(const Poly2 &poly)
Definition: CUPolygonObstacle.h:137
const Vec2 & getAnchor() const
Definition: CUPolygonObstacle.h:266
Definition: CUSimpleObstacle.h:63
float width
Definition: CUSize.h:61
virtual void releaseFixtures() override
void setSize(float width, float height)
Definition: CUPolygonObstacle.h:218
void setWidth(float value)
Definition: CUPolygonObstacle.h:236
virtual void resetDebug() override
float height
Definition: CUSize.h:63
Poly2 _polygon
Definition: CUPolygonObstacle.h:66
virtual ~PolygonObstacle()
const Size & getSize() const
Definition: CUPolygonObstacle.h:199
float getWidth() const
Definition: CUPolygonObstacle.h:225
Size size
Definition: CURect.h:51
Vec2 _anchor
Definition: CUPolygonObstacle.h:72
virtual void createFixtures() override
Definition: CUAnimationNode.h:52
float getHeight() const
Definition: CUPolygonObstacle.h:243
virtual bool init()
Definition: CUObstacle.h:156
void setSize(const Size &value)
Definition: CUPolygonObstacle.h:210
void resize(const Size &size)