Advertisement
another90sm

Untitled

Jun 6th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.38 KB | None | 0 0
  1. namespace SpaceShipFartrothu.Players
  2. {
  3.     using System.Collections.Generic;
  4.     using System.Linq;
  5.     using GameObjects;
  6.     using Microsoft.Xna.Framework;
  7.     using Microsoft.Xna.Framework.Content;
  8.     using Microsoft.Xna.Framework.Graphics;
  9.     using Microsoft.Xna.Framework.Input;
  10.  
  11.     public class Player2
  12.     {
  13.         public Texture2D Texture, BulletTexture, HealthTexture;
  14.         public Vector2 Position, HealthBarPosition;
  15.         public Rectangle BoundingBox, HealthRectangle;
  16.         public List<Bullet> BulletList;
  17.  
  18.         public int Speed;
  19.         public int Health;
  20.         public float BulletDelay;
  21.         public bool IsColiding;
  22.  
  23.         public Player2()
  24.         {
  25.             this.BulletList = new List<Bullet>();
  26.             this.Texture = null;
  27.             this.Position = new Vector2(700, 600);
  28.             this.BulletDelay = 20;
  29.             this.Speed = 10;
  30.             this.IsColiding = false;
  31.             this.Health = 200;
  32.             this.HealthBarPosition = new Vector2(1110, 50);
  33.         }
  34.         public void LoadContent(ContentManager content)
  35.         {
  36.             this.Texture = content.Load<Texture2D>("ship_p2");
  37.             this.BulletTexture = content.Load<Texture2D>("bullet");
  38.             this.HealthTexture = content.Load<Texture2D>("healthbar");
  39.         }
  40.         public void Draw(SpriteBatch spriteBatch)
  41.         {
  42.             spriteBatch.Draw(this.Texture, this.Position, Color.White);
  43.             spriteBatch.Draw(this.HealthTexture, this.HealthRectangle, Color.White);
  44.  
  45.             foreach (var bullet in this.BulletList)
  46.             {
  47.                 bullet.Draw(spriteBatch);
  48.             }
  49.         }
  50.         public void Update(GameTime gameTime)
  51.         {
  52.             var keyState = Keyboard.GetState();
  53.  
  54.             this.BoundingBox = new Rectangle(
  55.                 (int)this.Position.X,
  56.                 (int)this.Position.Y,
  57.                 this.Texture.Width,
  58.                 this.Texture.Height);
  59.  
  60.             this.HealthRectangle = new Rectangle(
  61.                 (int)this.HealthBarPosition.X,
  62.                 (int)this.HealthBarPosition.Y,
  63.                 this.Health,
  64.                 25);
  65.  
  66.             if (keyState.IsKeyDown(Keys.RightAlt))
  67.             {
  68.                 this.Shoot();
  69.             }
  70.  
  71.             this.UpdateBullets();
  72.  
  73.             //Moving faster
  74.             if (keyState.IsKeyDown(Keys.Space))
  75.             {
  76.                 this.Speed = 10;
  77.             }
  78.             if (keyState.IsKeyUp(Keys.Space))
  79.             {
  80.                 this.Speed = 5;
  81.             }
  82.  
  83.             // Ship controllers
  84.             if (keyState.IsKeyDown(Keys.Up))
  85.             {
  86.                 this.Position.Y = this.Position.Y - this.Speed;
  87.             }
  88.             if (keyState.IsKeyDown(Keys.Left))
  89.             {
  90.                 this.Position.X = this.Position.X - this.Speed;
  91.             }
  92.             if (keyState.IsKeyDown(Keys.Down))
  93.             {
  94.                 this.Position.Y = this.Position.Y + this.Speed;
  95.             }
  96.             if (keyState.IsKeyDown(Keys.Right))
  97.             {
  98.                 this.Position.X = this.Position.X + this.Speed;
  99.             }
  100.             // Moving left and right through screen borders
  101.             if (this.Position.X <= -30 || this.Position.X >= 1366)
  102.             {
  103.                 if (this.Position.X > 1366)
  104.                 {
  105.                     this.Position.X = -30;
  106.                 }
  107.                 else if (this.Position.X < -30)
  108.                 {
  109.                     this.Position.X = 1366;
  110.                 }
  111.             }
  112.             if (this.Position.Y <= 0)
  113.             {
  114.                 this.Position.Y = 0;
  115.             }
  116.             if (this.Position.Y >= 768 - this.Texture.Height)
  117.             {
  118.                 this.Position.Y = 768 - this.Texture.Height;
  119.             }
  120.         }
  121.  
  122.         public void Shoot()
  123.         {
  124.             if (this.BulletDelay >= 0)
  125.             {
  126.                 this.BulletDelay--;
  127.             }
  128.  
  129.             if (this.BulletDelay <= 0)
  130.             {
  131.                 Bullet newBullet = new Bullet(this.BulletTexture);
  132.                 newBullet.Position = new Vector2(
  133.                     this.Position.X + 32 - newBullet.Texture.Width / 2,
  134.                     this.Position.Y + 30);
  135.  
  136.                 newBullet.IsVisible = true;
  137.  
  138.  
  139.                 if (this.BulletList.Count() < 20)
  140.                 {
  141.                     this.BulletList.Add(newBullet);
  142.                 }
  143.             }
  144.             if (this.BulletDelay == 0)
  145.             {
  146.                 this.BulletDelay = 20;
  147.             }
  148.         }
  149.  
  150.         public void UpdateBullets()
  151.         {
  152.             foreach (var bullet in this.BulletList)
  153.             {
  154.                 bullet.BoundingBox = new Rectangle(
  155.                 (int)bullet.Position.X,
  156.                 (int)bullet.Position.Y,
  157.                 bullet.Texture.Width,
  158.                 bullet.Texture.Height);
  159.  
  160.                 bullet.Position.Y = bullet.Position.Y - bullet.Speed;
  161.                 if (bullet.Position.Y <= 0)
  162.                 {
  163.                     bullet.IsVisible = false;
  164.                 }
  165.             }
  166.  
  167.             for (int i = 0; i < this.BulletList.Count; i++)
  168.             {
  169.                 if (!this.BulletList[i].IsVisible)
  170.                 {
  171.                     this.BulletList.RemoveAt(i);
  172.                     i--;
  173.                 }
  174.             }
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement