Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Using Statements
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Audio;
- #endregion
- namespace Application
- {
- public class Player
- {
- // position variable
- protected Vector2 position = Vector2.Zero;
- //texture variable
- protected Texture2D texture = null;
- //player sound PEW!
- protected SoundEffect shootSfx;
- protected float speed = 200.0f;
- protected float cooldownTimer = 0;
- protected bool isDead = false;
- public bool IsDead {
- get { return isDead; }
- set { isDead = value; }
- }
- public Texture2D Texture {
- get { return texture; }
- set { texture = value; }
- }
- public Vector2 Position {
- get { return position; }
- set { position = value; }
- }
- public SoundEffect ShootSfx{
- set { shootSfx = value; }
- }
- public Rectangle Bounds {
- get { return new Rectangle ((int)position.X, (int)position.Y, texture.Bounds.Width, texture.Bounds.Height); }
- }
- public Player ()
- {
- }
- public void Update( float deltaTime )
- {
- if (Keyboard.GetState ().IsKeyDown (Keys.Left))
- position.X -= speed * deltaTime;
- if (Keyboard.GetState ().IsKeyDown (Keys.Right))
- position.X += speed * deltaTime;
- if (Keyboard.GetState ().IsKeyDown (Keys.Up))
- position.Y -= speed * deltaTime;
- if (Keyboard.GetState ().IsKeyDown (Keys.Down))
- position.Y += speed * deltaTime;
- if (cooldownTimer > 0)
- cooldownTimer -= deltaTime;
- if (Keyboard.GetState ().IsKeyDown (Keys.Space)) {
- if (cooldownTimer <= 0) {
- foreach (Bullet b in Game1.bullets) {
- if (b.IsDead == true) {
- b.IsDead = false;
- b.Position = Position;
- b.isFromPlayer = true;
- shootSfx.Play ();
- break;
- }
- }
- cooldownTimer = 0.3f;
- }
- }
- if (CheckCollisions () == true) {
- isDead = true;
- }
- }
- public void Draw(SpriteBatch spriteBatch)
- {
- if (texture == null)
- return;
- spriteBatch.Draw (texture, position, Color.White);
- }
- public bool CheckCollisions()
- {
- Rectangle bounds = Bounds;
- //check all bullets
- foreach (Bullet b in Game1.bullets) {
- if (b.IsDead == false && b.isFromPlayer == false && bounds.Intersects (b.Bounds) == true) {
- b.IsDead = true;
- return true;
- }
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement