| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "physics_layer.h" | 5 #include "physics_layer.h" |
| 6 #include "gameplay_scene.h" | 6 #include "gameplay_scene.h" |
| 7 | 7 |
| 8 #include "physics_nodes/CCPhysicsSprite.h" | 8 #include "physics_nodes/CCPhysicsSprite.h" |
| 9 #include "CCLuaEngine.h" | 9 #include "CCLuaEngine.h" |
| 10 #include "LuaBox2D.h" | 10 #include "LuaBox2D.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 current_instance_ = this; | 48 current_instance_ = this; |
| 49 | 49 |
| 50 setTouchEnabled(true); | 50 setTouchEnabled(true); |
| 51 | 51 |
| 52 InitPhysics(); | 52 InitPhysics(); |
| 53 | 53 |
| 54 // load level from lua file. | 54 // load level from lua file. |
| 55 LoadLua(); | 55 LoadLua(); |
| 56 | 56 |
| 57 // calculate brush size | 57 // calculate brush size |
| 58 brush_ = (CCSprite*)getChildByTag(TAG_BRUSH); | 58 CCSpriteBatchNode* brush_batch = (CCSpriteBatchNode*)getChildByTag(TAG_BRUSH); |
| 59 assert(brush_); | 59 assert(brush_batch); |
| 60 brush_ = CCSprite::createWithTexture(brush_batch->getTexture()); |
| 61 brush_->retain(); |
| 60 CCSize brush_size = brush_->getContentSize(); | 62 CCSize brush_size = brush_->getContentSize(); |
| 61 brush_radius_ = MAX(brush_size.height/2, brush_size.width/2); | 63 brush_radius_ = MAX(brush_size.height/2, brush_size.width/2); |
| 62 | 64 |
| 63 // script physics updates each frame | 65 // script physics updates each frame |
| 64 schedule(schedule_selector(PhysicsLayer::UpdateWorld)); | 66 schedule(schedule_selector(PhysicsLayer::UpdateWorld)); |
| 65 return true; | 67 return true; |
| 66 } | 68 } |
| 67 | 69 |
| 68 PhysicsLayer::PhysicsLayer() : | 70 PhysicsLayer::PhysicsLayer() : |
| 69 goal_reached_(false), | 71 goal_reached_(false), |
| 70 current_touch_id_(-1), | 72 current_touch_id_(-1), |
| 71 render_target_(NULL), | 73 render_target_(NULL), |
| 72 box2d_density_(DEFAULT_DENSITY), | 74 box2d_density_(DEFAULT_DENSITY), |
| 73 box2d_restitution_(DEFAULT_RESTITUTION), | 75 box2d_restitution_(DEFAULT_RESTITUTION), |
| 74 box2d_friction_(DEFAULT_FRICTION), | 76 box2d_friction_(DEFAULT_FRICTION), |
| 75 #ifdef COCOS2D_DEBUG | 77 #ifdef COCOS2D_DEBUG |
| 76 debug_enabled_(false) | 78 debug_enabled_(false) |
| 77 #endif | 79 #endif |
| 78 { | 80 { |
| 79 memset(stars_collected_, 0, sizeof(stars_collected_)); | 81 memset(stars_collected_, 0, sizeof(stars_collected_)); |
| 80 } | 82 } |
| 81 | 83 |
| 82 PhysicsLayer::~PhysicsLayer() { | 84 PhysicsLayer::~PhysicsLayer() { |
| 85 brush_->release(); |
| 83 if (current_instance_ == this) | 86 if (current_instance_ == this) |
| 84 current_instance_ = NULL; | 87 current_instance_ = NULL; |
| 85 delete box2d_world_; | 88 delete box2d_world_; |
| 86 #ifdef COCOS2D_DEBUG | 89 #ifdef COCOS2D_DEBUG |
| 87 delete box2d_debug_draw_; | 90 delete box2d_debug_draw_; |
| 88 #endif | 91 #endif |
| 89 } | 92 } |
| 90 | 93 |
| 91 void PhysicsLayer::registerWithTouchDispatcher() | 94 void PhysicsLayer::registerWithTouchDispatcher() |
| 92 { | 95 { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 b2Body* ground_body = box2d_world_->CreateBody(&ground_def); | 160 b2Body* ground_body = box2d_world_->CreateBody(&ground_def); |
| 158 | 161 |
| 159 CCLog("origin: %.fx%.f", origin.x, origin.y); | 162 CCLog("origin: %.fx%.f", origin.x, origin.y); |
| 160 CCLog("size: %.fx%f", visible_size.width, visible_size.height); | 163 CCLog("size: %.fx%f", visible_size.width, visible_size.height); |
| 161 | 164 |
| 162 // Define the ground box shape. | 165 // Define the ground box shape. |
| 163 b2EdgeShape ground_box; | 166 b2EdgeShape ground_box; |
| 164 ground_box.Set(b2Vec2(0, 0), b2Vec2(world_width, 0)); | 167 ground_box.Set(b2Vec2(0, 0), b2Vec2(world_width, 0)); |
| 165 ground_body->CreateFixture(&ground_box, 0); | 168 ground_body->CreateFixture(&ground_box, 0); |
| 166 | 169 |
| 170 box2d_world_->SetContactListener(this); |
| 171 |
| 167 #ifdef COCOS2D_DEBUG | 172 #ifdef COCOS2D_DEBUG |
| 168 box2d_debug_draw_ = new GLESDebugDraw(PTM_RATIO); | 173 box2d_debug_draw_ = new GLESDebugDraw(PTM_RATIO); |
| 169 box2d_world_->SetDebugDraw(box2d_debug_draw_); | 174 box2d_world_->SetDebugDraw(box2d_debug_draw_); |
| 170 | 175 |
| 171 uint32 flags = 0; | 176 uint32 flags = 0; |
| 172 flags += b2Draw::e_shapeBit; | 177 flags += b2Draw::e_shapeBit; |
| 173 flags += b2Draw::e_jointBit; | 178 flags += b2Draw::e_jointBit; |
| 174 flags += b2Draw::e_centerOfMassBit; | 179 flags += b2Draw::e_centerOfMassBit; |
| 175 //flags += b2Draw::e_aabbBit; | 180 //flags += b2Draw::e_aabbBit; |
| 176 //flags += b2Draw::e_pairBit; | 181 //flags += b2Draw::e_pairBit; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 198 CCRect CalcBoundingBox(CCSprite* sprite) { | 203 CCRect CalcBoundingBox(CCSprite* sprite) { |
| 199 CCSize size = sprite->getContentSize(); | 204 CCSize size = sprite->getContentSize(); |
| 200 CCPoint pos = sprite->getPosition(); | 205 CCPoint pos = sprite->getPosition(); |
| 201 return CCRectMake(pos.x - size.width, pos.y - size.height, | 206 return CCRectMake(pos.x - size.width, pos.y - size.height, |
| 202 size.width, size.height/2); | 207 size.width, size.height/2); |
| 203 } | 208 } |
| 204 | 209 |
| 205 void PhysicsLayer::UpdateWorld(float dt) { | 210 void PhysicsLayer::UpdateWorld(float dt) { |
| 206 // update physics | 211 // update physics |
| 207 box2d_world_->Step(dt, VELOCITY_ITERATIONS, POS_ITERATIONS); | 212 box2d_world_->Step(dt, VELOCITY_ITERATIONS, POS_ITERATIONS); |
| 213 } |
| 208 | 214 |
| 209 CCSprite* ball = (CCSprite*)getChildByTag(TAG_BALL); | 215 void PhysicsLayer::BeginContact(b2Contact* contact) { |
| 210 assert(ball); | 216 CCPhysicsSprite* ball = (CCPhysicsSprite*)getChildByTag(TAG_BALL); |
| 211 CCRect ball_bounds = CalcBoundingBox(ball); | 217 b2Body* ball_body = ball->getB2Body(); |
| 218 b2Body* body1 = contact->GetFixtureA()->GetBody(); |
| 219 b2Body* body2 = contact->GetFixtureB()->GetBody(); |
| 220 b2Body* body_other = NULL; |
| 212 | 221 |
| 213 // TODO(sbc): Investigate using box2d to do collision for us | 222 if (body1 == ball_body) |
| 214 // http://www.iforce2d.net/b2dtut/sensors | 223 body_other = body2; |
| 224 else if (body2 == ball_body) |
| 225 body_other = body1; |
| 226 else // we are only interested in collisions involving the ball |
| 227 return; |
| 215 | 228 |
| 216 // check for stars being reached by ball | |
| 217 int star_tags[] = { TAG_STAR1, TAG_STAR2, TAG_STAR3 }; | 229 int star_tags[] = { TAG_STAR1, TAG_STAR2, TAG_STAR3 }; |
| 218 for (uint i = 0; i < sizeof(star_tags)/sizeof(int); i++) { | 230 for (uint i = 0; i < sizeof(star_tags)/sizeof(int); i++) { |
| 219 if (stars_collected_[i]) | 231 if (stars_collected_[i]) |
| 220 continue; | 232 continue; |
| 221 CCSprite* star = (CCSprite*)getChildByTag(star_tags[i]); | 233 CCPhysicsSprite* star = (CCPhysicsSprite*)getChildByTag(star_tags[i]); |
| 222 assert(star); | 234 if (body_other == star->getB2Body()) { |
| 223 CCRect star_bounds = CalcBoundingBox(star); | |
| 224 | |
| 225 if (ball_bounds.intersectsRect(star_bounds)) { | |
| 226 CCLog("star %d reached", i); | 235 CCLog("star %d reached", i); |
| 227 stars_collected_[i] = true; | 236 stars_collected_[i] = true; |
| 228 CCAction* action = CCFadeOut::create(0.5f); | 237 CCAction* action = CCFadeOut::create(0.5f); |
| 229 star->runAction(action); | 238 star->runAction(action); |
| 230 } | 239 } |
| 231 } | 240 } |
| 232 | 241 |
| 233 // check for goal being reached by ball | 242 if (!goal_reached_) |
| 234 if (!goal_reached_) { | 243 { |
| 235 CCSprite* goal = (CCSprite*)getChildByTag(TAG_GOAL); | 244 CCPhysicsSprite* goal = (CCPhysicsSprite*)getChildByTag(TAG_GOAL); |
| 236 assert(goal); | 245 if (body_other == goal->getB2Body()) { |
| 237 CCRect goal_bounds = CalcBoundingBox(goal); | |
| 238 if (ball_bounds.intersectsRect(goal_bounds)) { | |
| 239 CCLog("goal reached"); | 246 CCLog("goal reached"); |
| 240 goal_reached_ = true; | 247 goal_reached_ = true; |
| 241 | 248 |
| 242 // fade out the goal and trigger gameover callback when its | 249 // fade out the goal and trigger gameover callback when its |
| 243 // done | 250 // done |
| 244 CCActionInterval* fadeout = CCFadeOut::create(0.5f); | 251 CCActionInterval* fadeout = CCFadeOut::create(0.5f); |
| 245 CCFiniteTimeAction* fadeout_done = CCCallFuncN::create(this, | 252 CCFiniteTimeAction* fadeout_done = CCCallFuncN::create(this, |
| 246 callfuncN_selector(PhysicsLayer::LevelComplete)); | 253 callfuncN_selector(PhysicsLayer::LevelComplete)); |
| 247 CCSequence* seq = CCSequence::create(fadeout, fadeout_done, NULL); | 254 CCSequence* seq = CCSequence::create(fadeout, fadeout_done, NULL); |
| 248 goal->runAction(seq); | 255 goal->runAction(seq); |
| 249 } | 256 } |
| 250 } | 257 } |
| 251 } | 258 } |
| 252 | 259 |
| 253 void PhysicsLayer::LevelComplete(CCNode* sender) { | 260 void PhysicsLayer::LevelComplete(CCNode* sender) { |
| 254 unschedule(schedule_selector(PhysicsLayer::UpdateWorld)); | 261 unschedule(schedule_selector(PhysicsLayer::UpdateWorld)); |
| 255 setTouchEnabled(false); | 262 setTouchEnabled(false); |
| 256 GameplayScene* scene = static_cast<GameplayScene*>(getParent()); | 263 GameplayScene* scene = static_cast<GameplayScene*>(getParent()); |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 float posx = SCREEN_TO_WORLD((sx+ex)/2) - body->GetPosition().x; | 540 float posx = SCREEN_TO_WORLD((sx+ex)/2) - body->GetPosition().x; |
| 534 float posy = SCREEN_TO_WORLD((sy+ey)/2) - body->GetPosition().y; | 541 float posy = SCREEN_TO_WORLD((sy+ey)/2) - body->GetPosition().y; |
| 535 | 542 |
| 536 float width = SCREEN_TO_WORLD(abs(distance)); | 543 float width = SCREEN_TO_WORLD(abs(distance)); |
| 537 float height = SCREEN_TO_WORLD(brush_->boundingBox().size.height); | 544 float height = SCREEN_TO_WORLD(brush_->boundingBox().size.height); |
| 538 | 545 |
| 539 b2PolygonShape shape; | 546 b2PolygonShape shape; |
| 540 shape.SetAsBox(width / 2, height / 2, b2Vec2(posx, posy), angle); | 547 shape.SetAsBox(width / 2, height / 2, b2Vec2(posx, posy), angle); |
| 541 AddShapeToBody(body, &shape); | 548 AddShapeToBody(body, &shape); |
| 542 } | 549 } |
| OLD | NEW |