//setup done void contactListener_FruitCollision(object sender, PostContactEventArgs e) { switch (e.spriteB.SpriteType) { case Sprite.Type.FruitShot: case Sprite.Type.ExplosiveShot: { contactListener.DamageToFruit(e.spriteA, e.damage, e.spriteB.SpriteType); contactListener.DamageToShot(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.VeggieShot: { contactListener.DamageToFruit(e.spriteA, int.MaxValue, e.spriteB.SpriteType); contactListener.DamageToShot(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.CannonballShot: { contactListener.DamageToFruit(e.spriteA, e.damage, e.spriteB.SpriteType); break; } case Sprite.Type.FireballShot: { e.spriteB.IsHit = true; break; } case Sprite.Type.Veggie: { contactListener.DamageToFruit(e.spriteA, int.MaxValue, e.spriteB.SpriteType); contactListener.DamageToVeggie(e.spriteB, int.MaxValue, e.spriteA.SpriteType); break; } case Sprite.Type.Terrain: case Sprite.Type.StaticBlock: case Sprite.Type.CannonBall: case Sprite.Type.Gremlin: case Sprite.Type.Bird: case Sprite.Type.Bat: case Sprite.Type.Fish: case Sprite.Type.Spider: case Sprite.Type.Snowman: case Sprite.Type.Fan: { contactListener.DamageToFruit(e.spriteA, e.damage, e.spriteB.SpriteType); break; } case Sprite.Type.Fruit: { contactListener.DamageToFruit(e.spriteA, e.damage, e.spriteB.SpriteType); contactListener.DamageToFruit(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Block: { contactListener.DamageToFruit(e.spriteA, e.damage, e.spriteB.SpriteType); contactListener.DamageToBlock(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Explosive: { contactListener.DamageToFruit(e.spriteA, e.damage, e.spriteB.SpriteType); contactListener.DamageToExplosive(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Snowball: { contactListener.DamageToFruit(e.spriteA, e.damage, e.spriteB.SpriteType); contactListener.DamageToSnowball(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Switch: { contactListener.DamageToFruit(e.spriteA, e.damage, e.spriteB.SpriteType); contactListener.DamageToSwitch(e.spriteB, 1, e.spriteA.SpriteType); break; } case Sprite.Type.FireballBlast: { contactListener.DamageToFruit(e.spriteA, 10, e.spriteB.SpriteType); break; } default: break; } return; }
//setup done void contactListener_ExplosiveShotCollision(object sender, PostContactEventArgs e) { switch (e.spriteB.SpriteType) { case Sprite.Type.Fruit: { contactListener.DamageToShot(e.spriteA, e.damage, e.spriteB.SpriteType); contactListener.DamageToFruit(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Veggie: { contactListener.DamageToShot(e.spriteA, e.damage, e.spriteB.SpriteType); contactListener.DamageToVeggie(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Terrain: { if (e.spriteA.spriteBody.LinearVelocity.Length() > GameSettings.Vdust) { Vector2 poofOffset = 32f * Vector2.Normalize(e.spriteB.SpriteCenterInWorld - e.spriteA.SpriteCenterInWorld); contactListener.DoSmallPoof((e.spriteA.SpriteCenterInWorld + poofOffset), e.spriteA); } contactListener.DamageToShot(e.spriteA, e.damage, e.spriteB.SpriteType); break; } case Sprite.Type.StaticBlock: case Sprite.Type.CannonBall: case Sprite.Type.Gremlin: case Sprite.Type.Bird: case Sprite.Type.Bat: case Sprite.Type.Fish: case Sprite.Type.Spider: case Sprite.Type.Snowman: case Sprite.Type.Fan: { contactListener.DamageToShot(e.spriteA, e.damage, e.spriteB.SpriteType); break; } case Sprite.Type.Block: { contactListener.DamageToShot(e.spriteA, e.damage, e.spriteB.SpriteType); contactListener.DamageToBlock(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Explosive: { contactListener.DamageToShot(e.spriteA, e.damage, e.spriteB.SpriteType); contactListener.DamageToExplosive(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Snowball: { contactListener.DamageToShot(e.spriteA, e.damage, e.spriteB.SpriteType); contactListener.DamageToSnowball(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Switch: { contactListener.DamageToShot(e.spriteA, e.damage, e.spriteB.SpriteType); contactListener.DamageToSwitch(e.spriteB, 1, e.spriteA.SpriteType); break; } case Sprite.Type.FireballBlast: case Sprite.Type.Tower: case Sprite.Type.Boss: { e.spriteA.IsHit = true; break; } default: break; } return; }
//setup done void contactListener_FishCollision(object sender, PostContactEventArgs e) { switch (e.spriteB.SpriteType) { case Sprite.Type.FruitShot: case Sprite.Type.VeggieShot: case Sprite.Type.ExplosiveShot: { contactListener.DamageToShot(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.CannonballShot: { contactListener.DamageToCreature(e.spriteA, int.MaxValue, e.spriteB.SpriteType); break; } case Sprite.Type.FireballShot: { e.spriteB.IsHit = true; break; } case Sprite.Type.Fruit: { contactListener.DamageToFruit(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Veggie: { contactListener.DamageToVeggie(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Block: { contactListener.DamageToBlock(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Explosive: { contactListener.DamageToExplosive(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Snowball: { contactListener.DamageToSnowball(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.FireballBlast: { contactListener.DamageToCreature(e.spriteA, int.MaxValue, e.spriteB.SpriteType); break; } default: break; } return; }
//setup done void contactListener_CannonBallShotCollision(object sender, PostContactEventArgs e) { // spriteA cannonball shot, spriteB object switch (e.spriteB.SpriteType) { case Sprite.Type.Block: { contactListener.DamageToBlock(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Fruit: { contactListener.DamageToFruit(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Veggie: { contactListener.DamageToVeggie(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Explosive: { contactListener.DamageToExplosive(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Terrain: { if (e.spriteA.spriteBody.LinearVelocity.Length() > GameSettings.Vdust) { Vector2 poofOffset = 32f * Vector2.Normalize(e.spriteB.SpriteCenterInWorld - e.spriteA.SpriteCenterInWorld); contactListener.DoSmallPoof((e.spriteA.SpriteCenterInWorld + poofOffset), e.spriteA); } break; } case Sprite.Type.Beehive: case Sprite.Type.Gremlin: case Sprite.Type.Bird: case Sprite.Type.Bat: case Sprite.Type.Fish: case Sprite.Type.Spider: case Sprite.Type.Snowman: { contactListener.DamageToCreature(e.spriteB, int.MaxValue, e.spriteA.SpriteType); break; } case Sprite.Type.Snowball: { contactListener.DamageToSnowball(e.spriteB, int.MaxValue, e.spriteA.SpriteType); break; } case Sprite.Type.Switch: { contactListener.DamageToSwitch(e.spriteB, 1, e.spriteA.SpriteType); break; } default: break; } return; }
void contactListener_BossCollision(object sender, PostContactEventArgs e) { // spriteA boss, spriteB object switch (e.spriteB.SpriteType) { case Sprite.Type.FruitShot: { contactListener.DamageToShot(e.spriteB, e.damage, e.spriteA.SpriteType); break; } default: break; } }
//setup done void contactListener_BlockCollision(object sender, PostContactEventArgs e) { switch (e.spriteB.SpriteType) { case Sprite.Type.FruitShot: case Sprite.Type.VeggieShot: case Sprite.Type.ExplosiveShot: { contactListener.DamageToShot(e.spriteB, e.damage, e.spriteA.SpriteType); contactListener.DamageToBlock(e.spriteA, e.damage, e.spriteB.SpriteType); break; } case Sprite.Type.FireballShot: { //go through ice and kill it, other blocks explode the shot if (e.spriteA.TextureIndex == 3) contactListener.DamageToBlock(e.spriteA, int.MaxValue, e.spriteB.SpriteType); else e.spriteB.IsHit = true; break; } case Sprite.Type.CannonballShot: case Sprite.Type.CannonBall: case Sprite.Type.StaticBlock: case Sprite.Type.Switch: case Sprite.Type.Terrain: case Sprite.Type.Fan: { contactListener.DamageToBlock(e.spriteA, e.damage, e.spriteB.SpriteType); break; } case Sprite.Type.Snowball: { contactListener.DamageToSnowball(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Fruit: { contactListener.DamageToBlock(e.spriteA, e.damage, e.spriteB.SpriteType); contactListener.DamageToFruit(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Veggie: { contactListener.DamageToBlock(e.spriteA, e.damage, e.spriteB.SpriteType); contactListener.DamageToVeggie(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Block: { contactListener.DamageToBlock(e.spriteA, e.damage, e.spriteB.SpriteType); contactListener.DamageToBlock(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Explosive: { contactListener.DamageToBlock(e.spriteA, e.damage, e.spriteB.SpriteType); contactListener.DamageToExplosive(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.FireballBlast: { if (e.spriteA.TextureIndex != 2) //stone is immume { contactListener.DamageToBlock(e.spriteA, int.MaxValue, e.spriteB.SpriteType); } break; } default: break; } return; }
// for xxxCollision Events, xxx is sprite A and sprite B is the object it collided with void contactListener_TerrainCollision(object sender, PostContactEventArgs e) { switch (e.spriteB.SpriteType) { case Sprite.Type.FruitShot: case Sprite.Type.VeggieShot: case Sprite.Type.ExplosiveShot: { if (e.spriteB.spriteBody.LinearVelocity.Length() > GameSettings.Vdust) { Vector2 poofOffset = 32f * Vector2.Normalize(e.spriteA.SpriteCenterInWorld - e.spriteB.SpriteCenterInWorld); contactListener.DoSmallPoof((e.spriteB.SpriteCenterInWorld + poofOffset), e.spriteB); } contactListener.DamageToShot(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.FireballShot: case Sprite.Type.LightningShot: case Sprite.Type.IceShot: case Sprite.Type.SawShot: { e.spriteB.IsHit = true; break; } case Sprite.Type.CannonballShot: { if (e.spriteB.spriteBody.LinearVelocity.Length() > GameSettings.Vdust) { Vector2 poofOffset = 32f * Vector2.Normalize(e.spriteA.SpriteCenterInWorld - e.spriteB.SpriteCenterInWorld); contactListener.DoSmallPoof((e.spriteB.SpriteCenterInWorld + poofOffset), e.spriteB); } break; } case Sprite.Type.Fruit: { contactListener.DamageToFruit(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Veggie: { contactListener.DamageToVeggie(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Block: { contactListener.DamageToBlock(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Explosive: { contactListener.DamageToExplosive(e.spriteB, e.damage, e.spriteA.SpriteType); break; } case Sprite.Type.Snowball: { contactListener.DamageToSnowball(e.spriteB, e.damage, e.spriteA.SpriteType); break; } default: break; } return; }
private void PostSolve(Contact contact, ContactConstraint impulse) { spriteA = (Sprite)contact.FixtureA.UserData; spriteB = (Sprite)contact.FixtureB.UserData; //code optimization if (spriteA.SpriteType == Sprite.Type.Terrain && spriteB.SpriteType == Sprite.Type.Terrain) return; //relativeVelocity = new Vector2(Math.Abs(contact.FixtureA.Body.LinearVelocity.X - contact.FixtureB.Body.LinearVelocity.X), Math.Abs(contact.FixtureA.Body.LinearVelocity.Y - contact.FixtureB.Body.LinearVelocity.Y)); relativeVelocity = contact.FixtureA.Body.LinearVelocity - contact.FixtureB.Body.LinearVelocity; if (relativeVelocity.Length() < 1) damage = 0; else damage = GetDamage(impulse, spriteA, spriteB); switch (spriteA.SpriteType) { case Sprite.Type.Terrain: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); TerrainCollision(this, postContactArgs); return; } case Sprite.Type.Block: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); BlockCollision(this, postContactArgs); return; } case Sprite.Type.StaticBlock: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); StaticBlockCollision(this, postContactArgs); return; } case Sprite.Type.CannonBall: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); CannonBallCollision(this, postContactArgs); return; } case Sprite.Type.Fruit: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); FruitCollision(this, postContactArgs); return; } case Sprite.Type.Veggie: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); VeggieCollision(this, postContactArgs); return; } case Sprite.Type.Explosive: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); TNTCollision(this, postContactArgs); return; } case Sprite.Type.Switch: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); SwitchCollision(this, postContactArgs); return; } case Sprite.Type.Fan: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); FanCollision(this, postContactArgs); return; } case Sprite.Type.Gremlin: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); GremlinCollision(this, postContactArgs); return; } case Sprite.Type.Bird: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); BirdCollision(this, postContactArgs); return; } case Sprite.Type.Bat: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); BatCollision(this, postContactArgs); return; } case Sprite.Type.Fish: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); FishCollision(this, postContactArgs); return; } case Sprite.Type.Spider: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); SpiderCollision(this, postContactArgs); return; } case Sprite.Type.Snowman: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); SnowmanCollision(this, postContactArgs); return; } case Sprite.Type.Snowball: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); SnowballCollision(this, postContactArgs); return; } case Sprite.Type.FruitShot: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); FruitShotCollision(this, postContactArgs); return; } case Sprite.Type.VeggieShot: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); VeggieShotCollision(this, postContactArgs); return; } case Sprite.Type.ExplosiveShot: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); ExplosiveShotCollision(this, postContactArgs); return; } case Sprite.Type.CannonballShot: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage); CannonBallShotCollision(this, postContactArgs); return; } case Sprite.Type.Boss: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteA, spriteB, damage, contact.FixtureA.IsSensor); BossCollision(this, postContactArgs); return; } default: break; } switch (spriteB.SpriteType) { case Sprite.Type.Terrain: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); TerrainCollision(this, postContactArgs); return; } case Sprite.Type.Block: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); BlockCollision(this, postContactArgs); return; } case Sprite.Type.StaticBlock: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); StaticBlockCollision(this, postContactArgs); return; } case Sprite.Type.CannonBall: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); CannonBallCollision(this, postContactArgs); return; } case Sprite.Type.Fruit: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); FruitCollision(this, postContactArgs); return; } case Sprite.Type.Veggie: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); VeggieCollision(this, postContactArgs); return; } case Sprite.Type.Explosive: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); TNTCollision(this, postContactArgs); return; } case Sprite.Type.Switch: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); SwitchCollision(this, postContactArgs); return; } case Sprite.Type.Fan: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); FanCollision(this, postContactArgs); return; } case Sprite.Type.Gremlin: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); GremlinCollision(this, postContactArgs); return; } case Sprite.Type.Bird: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); BirdCollision(this, postContactArgs); return; } case Sprite.Type.Bat: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); BatCollision(this, postContactArgs); return; } case Sprite.Type.Fish: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); FishCollision(this, postContactArgs); return; } case Sprite.Type.Spider: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); SpiderCollision(this, postContactArgs); return; } case Sprite.Type.Snowman: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); SnowmanCollision(this, postContactArgs); return; } case Sprite.Type.Snowball: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); SnowballCollision(this, postContactArgs); return; } case Sprite.Type.FruitShot: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); FruitShotCollision(this, postContactArgs); return; } case Sprite.Type.VeggieShot: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); VeggieShotCollision(this, postContactArgs); return; } case Sprite.Type.ExplosiveShot: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); ExplosiveShotCollision(this, postContactArgs); return; } case Sprite.Type.CannonballShot: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage); CannonBallShotCollision(this, postContactArgs); return; } case Sprite.Type.Boss: { PostContactEventArgs postContactArgs = new PostContactEventArgs(spriteB, spriteA, damage,contact.FixtureB.IsSensor); BossCollision(this, postContactArgs); return; } default: break; } return; }