PageRenderTime 56ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/Poing2/GamePowerUp.cs

http://github.com/BCProgramming/BASeBlock
C# | 305 lines | 175 code | 90 blank | 40 comment | 16 complexity | 86a92d260889032eefefb7565d8de783 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Windows.Forms;
  6. using BASeCamp.BASeBlock.Particles;
  7. namespace BASeCamp.BASeBlock
  8. {
  9. /// <summary>
  10. /// used to assign certain default behaviours to a GamePowerup, specifically
  11. /// some visual distinctions between "good" and "bad" powerups.
  12. /// </summary>
  13. public abstract class PowerupAttribute : Attribute
  14. {
  15. public abstract void PerformFrame(GamePowerUp powerup, BCBlockGameState gamestate);
  16. public abstract void Draw(GamePowerUp powerup,Graphics g);
  17. }
  18. public class ColouredGamePowerupAttribute : PowerupAttribute
  19. {
  20. private Color _Color;
  21. private float _Radius;
  22. private Image drawimage = null;
  23. public ColouredGamePowerupAttribute(Color usecolor, float Radius)
  24. {
  25. _Color = usecolor;
  26. _Radius = Radius;
  27. drawimage = LightOrb.DrawLightOrb(new Size((int)(Radius * 2), (int)(Radius * 2)), usecolor);
  28. }
  29. public override void Draw(GamePowerUp powerup,Graphics g)
  30. {
  31. g.DrawImage(drawimage, powerup.CenterPoint().X - _Radius, powerup.CenterPoint().Y - _Radius);
  32. }
  33. public override void PerformFrame(GamePowerUp powerup, BCBlockGameState gamestate)
  34. {
  35. //nothing needed.
  36. }
  37. }
  38. public class PositivePowerupAttribute : ColouredGamePowerupAttribute
  39. {
  40. public PositivePowerupAttribute()
  41. : base(Color.Green, 32)
  42. {
  43. }
  44. }
  45. public class PowerupCollectionEditor : BaseMultiEditor
  46. {
  47. public PowerupCollectionEditor(Type ptype)
  48. : base(ptype)
  49. {
  50. }
  51. protected override Type[] CreateNewItemTypes()
  52. {
  53. return GamePowerUp.GetPowerUpTypes();
  54. }
  55. }
  56. public class GamePowerUp : AnimatedSprite
  57. {
  58. //public delegate
  59. //public delegate bool PerformFrameFunction(BCBlockGameState bb, ref List<GameObject> AddObjects,ref List<GameObject> removeobjects);
  60. //public PointF Velocity= new PointF(0,2);
  61. public static readonly SizeF defaultSize = new SizeF(24, 12);
  62. public delegate bool CollectPowerupfunction(BCBlockGameState bb);
  63. protected CollectPowerupfunction usefunction;
  64. //tweaked for extensibility...
  65. public static Type[] GetPowerUpTypes()
  66. {
  67. //return BCBlockGameState.MTypeManager[typeof(GamePowerUp)].ManagedTypes.ToArray();
  68. return BCBlockGameState.MTypeManager[typeof(GamePowerUp)].ManagedTypes.ToArray();
  69. // return new Type[] {typeof(NullPowerUp),typeof(FrustratorPowerup), typeof (PaddlePlusPowerup), typeof (PaddleMinusPowerup),typeof(AddBallPowerup),typeof(StickyPaddlePowerUp),typeof(TerminatorPowerUp),typeof(LifeUpPowerUp),typeof(EnemySpawnerPowerUp),
  70. // typeof(MagnetPowerup),typeof(IcePowerUp),typeof(BallSplitterPowerup),typeof(ExtraLifePowerup),typeof(ShieldPowerup)
  71. // };
  72. }
  73. public static float[] GetPowerUpChance()
  74. {
  75. Type[] inspecttypes = GetPowerUpTypes();
  76. float[] returnfloat = new float[inspecttypes.Length];
  77. for (int i = 0; i < inspecttypes.Length; i++)
  78. {
  79. float usechance = 1f;
  80. //check for static "PowerupChance()" routine.
  81. MethodInfo getchanceproc = inspecttypes[i].GetMethod("PowerupChance", BindingFlags.Static);
  82. if (getchanceproc != null)
  83. {
  84. try
  85. {
  86. usechance = (float)getchanceproc.Invoke(null, new object[0]);
  87. }
  88. catch
  89. {
  90. usechance = 1f;
  91. }
  92. }
  93. returnfloat[i] = usechance;
  94. }
  95. return returnfloat;
  96. /* return new float[]
  97. { 10f, //null
  98. 2f, //Frustrator
  99. 2f, //PaddlePlus
  100. 3f, //PaddleMinus
  101. 5f, //AddBallPowerup
  102. 2f, //StickyPaddlePowerUp
  103. 1f, //TerminatorPowerUp
  104. 1f, //LifeUpPowerUp
  105. 2f, //EnemySpawnerPowerup
  106. 3f, //MagnetPowerup
  107. 0f, //IcePowerup
  108. 2f, //BallSplitterPowerup
  109. 0.4f, //Extra life
  110. 1f //shield
  111. };*/
  112. }
  113. protected void AddScore(BCBlockGameState ParentGame, int scoreadd)
  114. {
  115. AddScore(ParentGame, scoreadd, "");
  116. }
  117. public override void Draw(Graphics g)
  118. {
  119. Object[] result = GetType().GetCustomAttributes(typeof(PowerupAttribute), true);
  120. foreach (Object iterate in result)
  121. {
  122. if (iterate is PowerupAttribute)
  123. {
  124. ((PowerupAttribute)iterate).Draw(this, g);
  125. }
  126. }
  127. base.Draw(g);
  128. }
  129. protected void AddScore(BCBlockGameState ParentGame, int scoreadd, String prefixString)
  130. {
  131. //PointF MidPoint = new PointF(mBlockrect.Left + (mBlockrect.Width / 2), mBlockrect.Top + (mBlockrect.Height / 2));
  132. String usestring;
  133. PointF MidPoint = new PointF(Location.X + Size.Width / 2, Location.Y + Size.Height / 2);
  134. int addedscore = scoreadd + (Math.Sign(scoreadd) * ParentGame.GameObjects.Count * 10);
  135. if (String.IsNullOrEmpty(prefixString))
  136. usestring = addedscore.ToString();
  137. else
  138. usestring = prefixString + "(" + addedscore.ToString() + ")";
  139. ParentGame.GameScore += addedscore;
  140. //ParentGame.GameObjects.AddLast(new BasicFadingText(usestring, MidPoint, new PointF(((float)BCBlockGameState.rgen.NextDouble() * 0.2f) * -0.1f, ((float)BCBlockGameState.rgen.NextDouble()) * -0.7f), new Font(BCBlockGameState.GetMonospaceFont(), 16), null, null));
  141. ParentGame.Defer(() => ParentGame.GameScore += addedscore);
  142. ParentGame.GameObjects.AddLast(new BasicFadingText(usestring, MidPoint, new PointF(((float)BCBlockGameState.rgen.NextDouble() * 0.2f) * -0.1f, ((float)BCBlockGameState.rgen.NextDouble()) * -0.7f), new Font(BCBlockGameState.GetMonospaceFont(), 16), null, null));
  143. //ParentGame.EnqueueMessage(usestring);
  144. }
  145. public GamePowerUp(PointF Location, SizeF ObjectSize, Image ImageUse, CollectPowerupfunction powerupfunc)
  146. : base(Location, ObjectSize, new Image[] { ImageUse }, 0, 0, 3)
  147. {
  148. NextAttributesFunc = null;
  149. usefunction = powerupfunc;
  150. }
  151. public GamePowerUp(PointF Location, SizeF ObjectSize, Image[] Imagesuse, int framedelay, CollectPowerupfunction powerupfunc)
  152. : base(Location, ObjectSize, Imagesuse, 0, 0, framedelay)
  153. {
  154. //VelocityChange = new VelocityChangerLeafy();
  155. NextAttributesFunc = null;
  156. usefunction = powerupfunc;
  157. }
  158. public GamePowerUp(PointF Location, SizeF ObjectSize, String[] ImageFrameskeys, int framedelay, CollectPowerupfunction powerupfunc)
  159. : base(Location, ObjectSize, ImageFrameskeys, 0, 0, framedelay)
  160. {
  161. usefunction = powerupfunc;
  162. }
  163. private bool TouchesPaddle(Paddle paddlecheck)
  164. {
  165. if (paddlecheck == null) return false;
  166. return new RectangleF(Location, Size).IntersectsWith(paddlecheck.Getrect());
  167. }
  168. public override bool PerformFrame(BCBlockGameState gamestate)
  169. {
  170. //return base.PerformFrame(gamestate, ref AddObjects, ref removeobjects);
  171. base.PerformFrame(gamestate);
  172. if (TouchesPaddle(gamestate.PlayerPaddle))
  173. {
  174. //call into each behaviour. If any return true, we want to reject.
  175. if (!gamestate.PlayerPaddle.Behaviours.Any((b) => b.getPowerup(gamestate, gamestate.PlayerPaddle, this)))
  176. {
  177. bool retval = usefunction(gamestate);
  178. // removeobjects.Add(this);
  179. return retval;
  180. }
  181. else
  182. {
  183. return true;
  184. }
  185. }
  186. if (Location.Y > gamestate.GameArea.Height)
  187. return true;
  188. else if (Location.Y < gamestate.GameArea.Top)
  189. {
  190. Location = new PointF(Location.X, 1);
  191. Velocity = new PointF(Velocity.X, Math.Abs(Velocity.Y));
  192. }
  193. if (Location.X > gamestate.GameArea.Width)
  194. {
  195. //bounce off right wall.
  196. Location = new PointF(getRectangle().Right - getRectangle().Width + 1, Location.Y);
  197. Velocity = new PointF(Math.Abs(Velocity.X), Velocity.Y);
  198. }
  199. else if (Location.X < gamestate.GameArea.Left)
  200. {
  201. //bounce off left wall.
  202. }
  203. Object[] result = GetType().GetCustomAttributes(typeof(PowerupAttribute), true);
  204. foreach (Object iterate in result)
  205. {
  206. if (iterate is PowerupAttribute)
  207. {
  208. ((PowerupAttribute)iterate).PerformFrame(this, gamestate);
  209. }
  210. }
  211. return false;
  212. // return usefunction(gamestate,ref AddObjects, ref removeobjects);
  213. }
  214. }
  215. }