void StartScheduling() { Schedule(t => { visibleBananas.Add(AddBanana()); elapsedTime += t; if (ShouldEndGame()) { EndGame(); } AddBall(); }, 1.0f); Schedule(t => CheckCollision()); Schedule(t => { world.Step(t, 8, 1); foreach (CCPhysicsSprite sprite in ballsBatch.Children) { if (sprite.Visible && sprite.PhysicsBody.Position.x < 0f || sprite.PhysicsBody.Position.x * PTM_RATIO > ContentSize.Width) { //or should it be Layer.VisibleBoundsWorldspace.Size.Width world.DestroyBody(sprite.PhysicsBody); sprite.Visible = false; sprite.RemoveFromParent(); } else { sprite.UpdateBallTransform(); } } }); }
public override void Update(float dt) { _world.Step(dt, 8, 1); foreach (CCNode node in Children) { var sprite = node as CCPhysicsSprite; if (sprite == null) { continue; } if (sprite.Visible && sprite.PhysicsBody.Position.y < 0f) { _world.DestroyBody(sprite.PhysicsBody); sprite.Visible = false; } else { sprite.UpdateSprite(); } } //#if WINDOWS || WINDOWSGL || LINUX || MACOS // // This needs replacing with EventDispatcher // CCInputState.Instance.Update(dt); // PlayerIndex p; // if (CCInputState.Instance.IsKeyPress(Microsoft.Xna.Framework.Input.Keys.D, PlayerIndex.One, out p)) // { // _world.Dump(); //#if PROFILING // b2Profile profile = _world.Profile; // CCLog.Log("]-----------[{0:F4}]-----------------------[", profile.step); // CCLog.Log("Solve Time = {0:F4}", profile.solve); // CCLog.Log("# bodies = {0}", profile.bodyCount); // CCLog.Log("# contacts = {0}", profile.contactCount); // CCLog.Log("# joints = {0}", profile.jointCount); // CCLog.Log("# toi iters = {0}", profile.toiSolverIterations); // if (profile.step > 0f) // { // CCLog.Log("Solve TOI Time = {0:F4} {1:F2}%", profile.solveTOI, profile.solveTOI / profile.step * 100f); // CCLog.Log("Solve TOI Advance Time = {0:F4} {1:F2}%", profile.solveTOIAdvance, profile.solveTOIAdvance / profile.step * 100f); // } // // CCLog.Log("BroadPhase Time = {0:F4}", profile.broadphase); // CCLog.Log("Collision Time = {0:F4}", profile.collide); // CCLog.Log("Solve Velocity Time = {0:F4}", profile.solveVelocity); // CCLog.Log("Solve Position Time = {0:F4}", profile.solvePosition); // CCLog.Log("Step Time = {0:F4}", profile.step); //#endif // } //#endif }
public GameLayer() { var touchListener = new CCEventListenerTouchAllAtOnce(); touchListener.OnTouchesEnded = OnTouchesEnded; AddEventListener(touchListener, this); Color = new CCColor3B(CCColor4B.White); Opacity = 255; visibleBananas = new List <CCSprite> (); hitBananas = new List <CCSprite> (); // batch node for physics balls ballsBatch = new CCSpriteBatchNode("balls", 100); ballTexture = ballsBatch.Texture; AddChild(ballsBatch, 1, 1); AddGrass(); AddSun(); AddMonkey(); Schedule(t => { visibleBananas.Add(AddBanana()); elapsedTime += t; if (ShouldEndGame()) { EndGame(); } AddBall(); }, 1.0f); Schedule(t => CheckCollision()); Schedule(t => { world.Step(t, 8, 1); foreach (CCPhysicsSprite sprite in ballsBatch.Children) { if (sprite.Visible && sprite.PhysicsBody.Position.x < 0f || sprite.PhysicsBody.Position.x * PTM_RATIO > ContentSize.Width) //or should it be Layer.VisibleBoundsWorldspace.Size.Width { world.DestroyBody(sprite.PhysicsBody); sprite.Visible = false; sprite.RemoveFromParent(); } else { sprite.UpdateTransformedSpriteTextureQuads(); } } }); }
void CheckCollision() { visibleBananas.ForEach(banana => { bool hit = banana.BoundingBoxTransformedToParent.IntersectsRect(monkey.BoundingBoxTransformedToParent); if (hit) { hitBananas.Add(banana); CCSimpleAudioEngine.SharedEngine.PlayEffect("Sounds/tap"); Explode(banana.Position); banana.RemoveFromParent(); } }); hitBananas.ForEach(banana => visibleBananas.Remove(banana)); int ballHitCount = ballsBatch.Children.Count(ball => ball.BoundingBoxTransformedToParent.IntersectsRect(monkey.BoundingBoxTransformedToParent)); if (ballHitCount > 0) { EndGame(); } Schedule(t => { visibleBananas.Add(AddBanana()); elapsedTime += t; if (ShouldEndGame()) { EndGame(); } AddBall(); }, 1.0f); Schedule(t => { world.Step(t, 8, 1); foreach (CCPhysicsSprite sprite in ballsBatch.Children) { if (sprite.Visible && sprite.PhysicsBody.Position.x < 0f || sprite.PhysicsBody.Position.x * PTM_RATIO > ContentSize.Width) { world.DestroyBody(sprite.PhysicsBody); sprite.Visible = false; sprite.RemoveFromParent(); } else { sprite.UpdateBallTransform(); } } }); }
public override void Update(float dt) { _world.Step(dt, 8, 1); //_world.Step(dt, 10, 3); foreach (CCPhysicsSprite sprite in _batch.Children) { if (sprite.Visible && sprite.PhysicsBody.Position.y < 0f) { _world.DestroyBody(sprite.PhysicsBody); sprite.Visible = false; } } #if WINDOWS || WINDOWSGL || LINUX || MACOS CCInputState.Instance.Update(dt); PlayerIndex p; if (CCInputState.Instance.IsKeyPress(Microsoft.Xna.Framework.Input.Keys.D, PlayerIndex.One, out p)) { _world.Dump(); #if PROFILING b2Profile profile = _world.Profile; CCLog.Log("]-----------[{0:F4}]-----------------------[", profile.step); CCLog.Log("Solve Time = {0:F4}", profile.solve); CCLog.Log("# bodies = {0}", profile.bodyCount); CCLog.Log("# contacts = {0}", profile.contactCount); CCLog.Log("# joints = {0}", profile.jointCount); CCLog.Log("# toi iters = {0}", profile.toiSolverIterations); if (profile.step > 0f) { CCLog.Log("Solve TOI Time = {0:F4} {1:F2}%", profile.solveTOI, profile.solveTOI / profile.step * 100f); CCLog.Log("Solve TOI Advance Time = {0:F4} {1:F2}%", profile.solveTOIAdvance, profile.solveTOIAdvance / profile.step * 100f); } CCLog.Log("BroadPhase Time = {0:F4}", profile.broadphase); CCLog.Log("Collision Time = {0:F4}", profile.collide); CCLog.Log("Solve Velocity Time = {0:F4}", profile.solveVelocity); CCLog.Log("Solve Position Time = {0:F4}", profile.solvePosition); CCLog.Log("Step Time = {0:F4}", profile.step); #endif } #endif }
public void LaunchBomb(b2Vec2 position, b2Vec2 velocity) { if (m_bomb != null) { m_world.DestroyBody(m_bomb); m_bomb = null; } b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position = position; bd.bullet = true; m_bomb = m_world.CreateBody(bd); m_bomb.LinearVelocity = velocity; b2CircleShape circle = new b2CircleShape(); circle.Radius = 0.3f; b2FixtureDef fd = new b2FixtureDef(); fd.shape = circle; fd.density = 20.0f; fd.restitution = 0.0f; b2Vec2 minV = position - new b2Vec2(0.3f, 0.3f); b2Vec2 maxV = position + new b2Vec2(0.3f, 0.3f); b2AABB aabb = new b2AABB(); aabb.LowerBound = minV; aabb.UpperBound = maxV; m_bomb.CreateFixture(fd); }