private PlayerSprite LoadPlayer(IPlayerInput playerInput, int playerNumber) { //TODO: this should be done via different sprites Color playerColor = Color.White; switch (playerNumber) { case 1: playerColor = Color.White; break; case 2: playerColor = Color.Black; break; case 3: playerColor = Color.Green; break; case 4: playerColor = Color.Yellow; break; } PlayerSprite player = new PlayerSprite(playerInput, playerColor, playerNumber, 1, 0, "crosshair", 1.0f); return(player); }
private void UpdatePlayer(PlayerSprite player) { player.UpdatePlayerIsActive(); if (player.IsActive) { player.UpdatePauseState(); if (player.IsPaused) { this.Exit(); } player.UpdateFiringState(); if (player.IsFiring) { Vector2 shotLocation = player.GetShotLocation(); foreach (Target target in levels[0].Waves[levels[0].CurrentWaveIndex].Targets) { if (target.IsActive) { //TODO: factor this out so pixel based hit detection can be used elsewhere //TODO: pixel based hit detection does not work when target is rotated... //only do pixel detection if mouse in area if (target.BoundingBox.Contains(new Rectangle((int)shotLocation.X, (int)shotLocation.Y, 1, 1))) { //do we hit alpha pixel or 'body' pixel? Vector2 relativeShotLocation = new Vector2(shotLocation.X - target.BoundingBox.X, shotLocation.Y - target.BoundingBox.Y); relativeShotLocation = Vector2.Divide(relativeShotLocation, target.Scale); Rectangle targetTexturePixels = Textures.Instance.SpriteSheet.SourceRectangle(target.GetSpriteSheetIndex()); Vector2 hitPointOnSpriteSheet = new Vector2(targetTexturePixels.X + relativeShotLocation.X, targetTexturePixels.Y + relativeShotLocation.Y); int hitPointOnSpriteSheetIndex = (int)hitPointOnSpriteSheet.Y * Textures.Instance.SpriteSheet.Texture.Width + (int)hitPointOnSpriteSheet.X; Color pixelColor = Textures.Instance.SpriteSheetColors[hitPointOnSpriteSheetIndex]; //hit! if (pixelColor.A != 0) { target.IsActive = false; player.Score += target.PointValue; //Explosion particle explosionEmitterParticleSystem.AddParticles(new Vector2(target.BoundingBox.Center.X, target.BoundingBox.Center.Y)); } } } } } player.UpdatePlayerPosition(); player.MoveTo(new Vector2(player.Position.X, player.Position.Y)); } }