- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/NewGamePhysics/GraphicalElements/ShootingGalleryTarget.cs

    00001 ﻿//-----------------------------------------------------------------------------
    00002 // ShootingGalleryTarget.cs
    00003 // (A. Schiffler, 2009)
    00004 //-----------------------------------------------------------------------------
    00005
    00006 namespace NewGamePhysics.GraphicalElements
    00007 {
    00008     using System;
    00009     using System.Collections.Generic;
    00010     using System.Text;
    00011
    00012     using Microsoft.Xna.Framework;
    00013     using Microsoft.Xna.Framework.Content;
    00014     using Microsoft.Xna.Framework.Graphics;
    00015     using Microsoft.Xna.Framework.Input;
    00016
    00017     using NewGamePhysics.StateManager;
    00018     using NewGamePhysics.Mathematics;
    00019     using NewGamePhysics.Physics;
    00020     using NewGamePhysics.GraphicalElements;
    00021     using NewGamePhysics.PhysicalElements;
    00022     using NewGamePhysics.Utilities;
    00023
    00027     public enum ShootingGalleryTargetType
    00028     {
    00032         CopperBall,
    00033
    00037         SilverBall,
    00038
    00042         GoldBall,
    00043     }
    00044
    00049     public class ShootingGalleryTarget : GraphicalElementBase
    00050     {
    00054         private CircularObjectSimulation animatedCircularObject;
    00055
    00059         private double width;
    00060
    00064         private double height;
    00065
    00069         Vector2 screenOrigin;
    00070
    00074         double screenScale;
    00075
    00079         Vector2 screenPosition;
    00080
    00084         private Texture2D targetTexture;
    00085
    00089         private bool visible;
    00090
    00094         private bool animated;
    00095
    00099         private double radius;
    00100
    00104         Vector2 spriteScale;
    00105
    00109         Vector2 spriteOrigin;
    00110
    00123         public ShootingGalleryTarget(
    00124             ScreenManager screenManager,
    00125             ShootingGalleryTargetType type,
    00126             double gravity,
    00127             double atmosphereDensity,
    00128             double simulationWidth,
    00129             double simulationHeight,
    00130             Vector2 screenOrigin,
    00131             double screenScale)
    00132             : base(screenManager)
    00133         {
    00134             this.width = simulationWidth;
    00135             this.height = simulationHeight;
    00136             this.screenOrigin = screenOrigin;
    00137             this.screenScale = screenScale;
    00138             this.radius = Math.Min(width, height) / 2;
    00139             double diameter = 2.0 * this.radius;
    00140             this.screenPosition = new Vector2();
    00141
    00142             this.visible = true;
    00143             this.animated = false;
    00144
    00145             string textureName = string.Empty;
    00146             switch (type)
    00147             {
    00148                 case ShootingGalleryTargetType.CopperBall:
    00150                     this.animatedCircularObject =
    00151                         new CircularObjectSimulation(diameter, 8.94, gravity, atmosphereDensity);
    00152                     textureName = "copperball";
    00153                     break;
    00154                 case ShootingGalleryTargetType.SilverBall:
    00156                     this.animatedCircularObject =
    00157                         new CircularObjectSimulation(diameter, 10.49, gravity, atmosphereDensity);
    00158                     textureName = "silverball";
    00159                     break;
    00160                 case ShootingGalleryTargetType.GoldBall:
    00162                     this.animatedCircularObject =
    00163                         new CircularObjectSimulation(diameter, 19.30, gravity, atmosphereDensity);
    00164                     textureName = "goldball";
    00165                     break;
    00166             }
    00167
    00168             // Prepare texture
    00169             if (screenManager.Textures.ContainsKey(textureName))
    00170             {
    00171                 this.targetTexture = screenManager.Textures[textureName];
    00172             }
    00173             else
    00174             {
    00175                 throw new ArgumentException("Texture with name " + textureName + " not found");
    00176             }
    00177
    00178             float spriteScaleX = (float)(this.width * this.screenScale / this.targetTexture.Width);
    00179             float spriteScaleY = (float)(this.height * this.screenScale / this.targetTexture.Height);
    00180             this.spriteScale = new Vector2(spriteScaleX, spriteScaleY);
    00181             this.spriteOrigin = new Vector2();
    00182         }
    00183
    00187         public bool Animated
    00188         {
    00189             get { return this.animated; }
    00190             set { this.animated = value; }
    00191         }
    00192
    00196         public bool Visible
    00197         {
    00198             get { return this.visible; }
    00199             set { this.visible = value; }
    00200         }
    00201
    00205         public Vector2 Position
    00206         {
    00207             get { return this.animatedCircularObject.GetPosition(); }
    00208             set { this.animatedCircularObject.SetPosition(value); }
    00209         }
    00210
    00215         public void Move(double horizontalDisplacement)
    00216         {
    00217             if (this.visible)
    00218             {
    00219                 Vector2 position =
    00220                     this.animatedCircularObject.GetPosition();
    00221                 position.X += (float)horizontalDisplacement;
    00222                 this.animatedCircularObject.SetPosition(position);
    00223             }
    00224         }
    00225
    00232         public bool ShootAtTarget(Vector2 shotCenter, double bulletSize)
    00233         {
    00234             if (this.visible)
    00235             {
    00236                 Vector2 targetPosition =
    00237                     this.animatedCircularObject.GetPosition();
    00238
    00239                 if (IntersectionTest.CircleInCircle2D(
    00240                     targetPosition,
    00241                     this.radius,
    00242                     shotCenter,
    00243                     bulletSize))
    00244                 {
    00245                     this.animated = true;
    00246                     return true;
    00247                 }
    00248             }
    00249
    00250             return false;
    00251         }
    00252
    00257         public void Update()
    00258         {
    00259             if (this.visible && this.animated)
    00260             {
    00261                 // Let target fall
    00262                 this.animatedCircularObject.Animate();
    00263
    00264                 // Check if we left the screen
    00265                 UpdateScreenPosition();
    00266                 if (this.screenPosition.Y > (this.Viewport.Height + this.radius * this.screenScale))
    00267                 {
    00268                     this.visible = false;
    00269                 }
    00270             }
    00271         }
    00272
    00277         public void Draw(GameTime gameTime)
    00278         {
    00279             if (this.visible)
    00280             {
    00281                 // Get fresh screen coordinates
    00282                 UpdateScreenPosition();
    00283
    00284                 // Draw target
    00285                 this.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend);
    00286                 this.SpriteBatch.Draw(
    00287                     this.targetTexture,
    00288                     this.screenPosition,
    00289                     null,
    00290                     Color.White,
    00291                     0.0f,
    00292                     this.spriteOrigin,
    00293                     this.spriteScale,
    00294                     SpriteEffects.None,
    00295                     0.0f);
    00296                 this.SpriteBatch.End();
    00297             }
    00298         }
    00299
    00303         private void UpdateScreenPosition()
    00304         {
    00305             Vector2 targetPosition =
    00306                 this.animatedCircularObject.GetPosition();
    00307             screenPosition.X = (float)(this.screenOrigin.X + this.screenScale * (targetPosition.X - this.width / 2));
    00308             screenPosition.Y = (float)(this.screenOrigin.Y + this.screenScale * (targetPosition.Y - this.height / 2));
    00309         }
    00310     }
    00311 }