Advertisement
Guest User

Enemy.cs

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