Advertisement
Guest User

Player.cs

a guest
Oct 31st, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. #region Using Statements
  2. using System;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Input;
  6. using Microsoft.Xna.Framework.Audio;
  7.  
  8.  
  9. #endregion
  10.  
  11. namespace Application
  12. {
  13.     public class Player
  14.     {
  15.         // position variable
  16.         protected Vector2 position = Vector2.Zero;
  17.         //texture variable
  18.         protected Texture2D texture = null;
  19.         //player sound PEW!
  20.         protected SoundEffect shootSfx;
  21.  
  22.         protected float speed = 200.0f;
  23.  
  24.         protected float cooldownTimer = 0;
  25.  
  26.         protected bool isDead = false;
  27.         public bool IsDead {
  28.             get { return isDead; }
  29.             set { isDead = value; }  
  30.         }
  31.  
  32.         public Texture2D Texture {
  33.             get { return texture; }
  34.             set { texture = value; }
  35.         }
  36.  
  37.         public Vector2 Position {
  38.             get { return position; }
  39.             set { position = value; }
  40.         }
  41.  
  42.         public SoundEffect ShootSfx{
  43.             set { shootSfx = value; }
  44.         }
  45.  
  46.         public Rectangle Bounds {
  47.             get { return new Rectangle ((int)position.X, (int)position.Y, texture.Bounds.Width, texture.Bounds.Height); }
  48.         }  
  49.  
  50.         public Player ()
  51.         {
  52.         }
  53.  
  54.         public void Update( float deltaTime )
  55.         {
  56.             if (Keyboard.GetState ().IsKeyDown (Keys.Left))
  57.                 position.X -= speed * deltaTime;
  58.             if (Keyboard.GetState ().IsKeyDown (Keys.Right))
  59.                 position.X += speed * deltaTime;
  60.             if (Keyboard.GetState ().IsKeyDown (Keys.Up))
  61.                 position.Y -= speed * deltaTime;
  62.             if (Keyboard.GetState ().IsKeyDown (Keys.Down))
  63.                 position.Y += speed * deltaTime;
  64.  
  65.             if (cooldownTimer > 0)
  66.                 cooldownTimer -= deltaTime;
  67.  
  68.             if (Keyboard.GetState ().IsKeyDown (Keys.Space)) {
  69.                 if (cooldownTimer <= 0) {
  70.                     foreach (Bullet b in Game1.bullets) {
  71.                         if (b.IsDead == true) {
  72.                             b.IsDead = false;
  73.                             b.Position = Position;
  74.                             b.isFromPlayer = true;
  75.                             shootSfx.Play ();
  76.                             break;
  77.                         }
  78.                     }
  79.                     cooldownTimer = 0.3f;
  80.                 }
  81.             }
  82.  
  83.             if (CheckCollisions () == true) {
  84.                 isDead = true;
  85.             }
  86.         }
  87.  
  88.         public void Draw(SpriteBatch spriteBatch)
  89.         {
  90.  
  91.             if (texture == null)
  92.                 return;
  93.  
  94.             spriteBatch.Draw (texture, position, Color.White);
  95.         }
  96.  
  97.         public bool CheckCollisions()
  98.         {
  99.             Rectangle bounds = Bounds;
  100.  
  101.             //check all bullets
  102.             foreach (Bullet b in Game1.bullets) {
  103.                 if (b.IsDead == false && b.isFromPlayer == false && bounds.Intersects (b.Bounds) == true) {
  104.                     b.IsDead = true;
  105.                     return true;
  106.                 }
  107.             }
  108.  
  109.             return false;
  110.  
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement