| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 #ifndef PHYSICS_LAYER_H_ | |
| 5 #define PHYSICS_LAYER_H_ | |
| 6 | |
| 7 #include "cocos2d.h" | |
| 8 #include "CCLuaStack.h" | |
| 9 #include "Box2D/Box2D.h" | |
| 10 | |
| 11 #ifdef COCOS2D_DEBUG | |
| 12 #include "GLES-Render.h" | |
| 13 #endif | |
| 14 | |
| 15 USING_NS_CC; | |
| 16 | |
| 17 typedef std::vector<cocos2d::CCPoint> PointList; | |
| 18 | |
| 19 /** | |
| 20 * Physics layer that the user interacts with. | |
| 21 */ | |
| 22 class PhysicsLayer : public CCLayerColor, public b2ContactListener { | |
| 23 public: | |
| 24 PhysicsLayer(int level_number); | |
| 25 ~PhysicsLayer(); | |
| 26 | |
| 27 static PhysicsLayer* create(int level_number); | |
| 28 | |
| 29 virtual bool init(); | |
| 30 virtual void draw(); | |
| 31 virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event); | |
| 32 virtual void ccTouchMoved(CCTouch* touch, CCEvent* event); | |
| 33 virtual void ccTouchEnded(CCTouch* touch, CCEvent* event); | |
| 34 virtual void registerWithTouchDispatcher(); | |
| 35 | |
| 36 b2World* GetWorld() { return box2d_world_; } | |
| 37 | |
| 38 #ifdef COCOS2D_DEBUG | |
| 39 void ToggleDebug(); | |
| 40 #endif | |
| 41 | |
| 42 // Called by box2d when contacts start | |
| 43 void BeginContact(b2Contact* contact); | |
| 44 | |
| 45 // Called by box2d when contacts finish | |
| 46 void EndContact(b2Contact* contact); | |
| 47 | |
| 48 // Methods that are exposed to / called by lua the lua | |
| 49 // script. | |
| 50 void LevelComplete(); | |
| 51 | |
| 52 private: | |
| 53 // Called by ccTouchesMoved to draw between two points | |
| 54 void DrawLine(CCPoint& start, CCPoint& end); | |
| 55 // Called by ccTouchesBegan to draw a single point. | |
| 56 void DrawPoint(CCPoint& location); | |
| 57 // Clamp brush location to within the visible area | |
| 58 void ClampBrushLocation(CCPoint& point); | |
| 59 | |
| 60 void LuaNotifyContact(b2Contact* contact, const char* function_name); | |
| 61 | |
| 62 void AddLineToBody(b2Body *body, CCPoint start, CCPoint end); | |
| 63 void AddSphereToBody(b2Body *body, CCPoint* location); | |
| 64 void AddShapeToBody(b2Body *body, b2Shape* shape); | |
| 65 | |
| 66 // Create a new render target for drawing into | |
| 67 void CreateRenderTarget(); | |
| 68 | |
| 69 // Create a physics object based on the points that were drawn. | |
| 70 b2Body* CreatePhysicsBody(); | |
| 71 CCSprite* CreatePhysicsSprite(b2Body* body); | |
| 72 void UpdateWorld(float dt); | |
| 73 bool InitPhysics(); | |
| 74 bool LoadLua(); | |
| 75 void LevelCompleteDone(CCNode* sender); | |
| 76 | |
| 77 private: | |
| 78 int level_number_; | |
| 79 // Brush texture used for drawing shapes | |
| 80 CCSprite* brush_; | |
| 81 float brush_radius_; | |
| 82 bool stars_collected_[3]; | |
| 83 bool goal_reached_; | |
| 84 | |
| 85 // The touch ID that is drawing the current shape. Events from | |
| 86 // other touches are ignored while this has a valid value. | |
| 87 int current_touch_id_; | |
| 88 | |
| 89 // Render target that shapes are drawn into | |
| 90 CCRenderTexture* render_target_; | |
| 91 | |
| 92 // Box2D physics world | |
| 93 b2World* box2d_world_; | |
| 94 | |
| 95 #ifdef COCOS2D_DEBUG | |
| 96 // Debug drawing support for Box2D. | |
| 97 GLESDebugDraw* box2d_debug_draw_; | |
| 98 // Flag to enable drawing of Box2D debug data. | |
| 99 bool debug_enabled_; | |
| 100 #endif | |
| 101 | |
| 102 // Density given to new physics objects | |
| 103 float box2d_density_; | |
| 104 // Restitution given to new physics objects | |
| 105 float box2d_restitution_; | |
| 106 // Friction given to new physics objects | |
| 107 float box2d_friction_; | |
| 108 | |
| 109 // The list of points currently being drawn by the user | |
| 110 PointList points_being_drawn_; | |
| 111 | |
| 112 CCLuaStack* lua_stack_; | |
| 113 }; | |
| 114 | |
| 115 #endif // PHYSICS_LAYER_H_ | |
| OLD | NEW |