00001
00002
00003
00004
00005
00006 namespace NewGamePhysics.GraphicalElements
00007 {
00008 using System;
00009 using System.Collections.Generic;
00010 using System.Linq;
00011 using System.Text;
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.Utilities;
00019 using NewGamePhysics.PhysicalElements;
00020 using NewGamePhysics.Mathematics;
00021 using NewGamePhysics.Physics;
00022
00027 public class ShootingGallery : GraphicalElementBase
00028 {
00032 private int numTargets = 0;
00033
00037 private int numActiveTargets = 0;
00038
00042 Vector2 simulationOrigin;
00043
00047 private double width;
00048
00052 private double height;
00053
00057 Vector2 screenOrigin;
00058
00062 double screenScale;
00063
00067 private ShootingGalleryTarget[] shootingGalleryTargets = null;
00068
00073 private double targetConveyerSpeed = 0.001;
00074
00079 public double TargetConveyerSpeed
00080 {
00081 get { return this.targetConveyerSpeed; }
00082 set { this.targetConveyerSpeed = value; }
00083 }
00084
00088 public int NumTargets
00089 {
00090 get { return this.numTargets; }
00091 }
00092
00096 public int NumActiveTargets
00097 {
00098 get { return this.numActiveTargets; }
00099 }
00100
00110 public ShootingGallery(
00111 ScreenManager screenManager,
00112 Vector2 simulationOrigin,
00113 double width,
00114 double height,
00115 Vector2 screenOrigin,
00116 double screenScale)
00117 : base(screenManager)
00118 {
00119 this.simulationOrigin = simulationOrigin;
00120 this.width = width;
00121 this.height = height;
00122 this.screenOrigin = screenOrigin;
00123 this.screenScale = screenScale;
00124 }
00125
00132 public void Reset(string[] targetTypes, double gravity, double atmosphereDensity)
00133 {
00134 if (targetTypes == null || targetTypes.Length == 0)
00135 {
00136 throw new ArgumentException(
00137 "Can reset only to one or more targets",
00138 "targetTypes");
00139 }
00140
00141 this.numTargets = targetTypes.Length;
00142
00143 if (gravity == 0.0)
00144 {
00145 throw new ArgumentException(
00146 "Gravity must be non-zero.",
00147 "gravity");
00148 }
00149
00150
00151 double stepX = this.width / this.numTargets;
00152 if (stepX <= this.height)
00153 {
00154 throw new ArgumentException(
00155 "Cannot fit that many targets into the width of the gallery without overlap.",
00156 "numTargets");
00157 }
00158
00159 this.numActiveTargets = this.numTargets;
00160 this.shootingGalleryTargets = new ShootingGalleryTarget[this.numTargets];
00161 for (int i = 0; i < this.numTargets; i++)
00162 {
00163 this.shootingGalleryTargets[i] = new ShootingGalleryTarget(
00164 this.ScreenManager,
00165 (ShootingGalleryTargetType)Enum.Parse(
00166 typeof(ShootingGalleryTargetType),
00167 targetTypes[i]),
00168 gravity,
00169 atmosphereDensity,
00170 this.height,
00171 this.height,
00172 this.screenOrigin,
00173 this.screenScale);
00174 this.shootingGalleryTargets[i].Position =
00175 new Vector2(
00176 this.simulationOrigin.X + (float)(i * stepX),
00177 this.simulationOrigin.Y);
00178 }
00179 }
00180
00187 public int ShootAtTargets(Vector2 shotCenter, double bulletSize)
00188 {
00189 int numberHits = 0;
00190
00191 foreach (ShootingGalleryTarget shootingGalleryTarget in this.shootingGalleryTargets)
00192 {
00193 if (shootingGalleryTarget.Visible && !shootingGalleryTarget.Animated)
00194 {
00195 if (shootingGalleryTarget.ShootAtTarget(shotCenter, bulletSize))
00196 {
00197 numberHits++;
00198 }
00199 }
00200 }
00201
00202 return numberHits;
00203 }
00204
00210 public void Update(GameTime gameTime)
00211 {
00212 foreach (ShootingGalleryTarget shootingGalleryTarget in this.shootingGalleryTargets)
00213 {
00214 if (shootingGalleryTarget.Visible)
00215 {
00216 if (shootingGalleryTarget.Animated)
00217 {
00218 shootingGalleryTarget.Update();
00219 if (!shootingGalleryTarget.Visible)
00220 {
00221 this.numActiveTargets--;
00222 }
00223 }
00224 else
00225 {
00226 shootingGalleryTarget.Move(this.targetConveyerSpeed);
00227
00228
00229 Vector2 position = shootingGalleryTarget.Position;
00230 if ((position.X - this.simulationOrigin.X) < 0.0)
00231 {
00232 position.X = this.simulationOrigin.X + (float)this.width;
00233 shootingGalleryTarget.Position = position;
00234 }
00235 else if ((position.X - this.simulationOrigin.X) > (float)this.width)
00236 {
00237 position.X = this.simulationOrigin.X;
00238 shootingGalleryTarget.Position = position;
00239 }
00240 }
00241 }
00242 }
00243 }
00244
00249 public void Draw(GameTime gameTime)
00250 {
00251 foreach (ShootingGalleryTarget shootingGalleryTarget in this.shootingGalleryTargets)
00252 {
00253 shootingGalleryTarget.Draw(gameTime);
00254 }
00255 }
00256 }
00257 }