123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Graphics;
- namespace Asteroid_Belt_Assault
- {
- class PlayerManager
- {
- public Sprite playerSprite;
- private float playerSpeed = 160.0f;
- private Rectangle playerAreaLimit;
- public long PlayerScore = 0;
- public int LivesRemaining = 3;
- public bool Destroyed = false;
- private Vector2 gunOffset = new Vector2(25, 10);
- private float shotTimer = 0.0f;
- private float minShotTimer = 0.2f;
- private int playerRadius = 15;
- public ShotManager PlayerShotManager;
- public PlayerManager(
- Texture2D texture,
- Rectangle initialFrame,
- int frameCount,
- Rectangle screenBounds)
- {
- playerSprite = new Sprite(
- new Vector2(500, 500),
- texture,
- initialFrame,
- Vector2.Zero);
- PlayerShotManager = new ShotManager(
- texture,
- new Rectangle(0, 300, 5, 5),
- 4,
- 2,
- 250f,
- screenBounds);
- playerAreaLimit =
- new Rectangle(
- 0,
- screenBounds.Height / 2,
- screenBounds.Width,
- screenBounds.Height / 2);
- for (int x = 1; x < frameCount; x++)
- {
- playerSprite.AddFrame(
- new Rectangle(
- initialFrame.X + (initialFrame.Width * x),
- initialFrame.Y,
- initialFrame.Width,
- initialFrame.Height));
- }
- playerSprite.CollisionRadius = playerRadius;
- }
- private void FireShot()
- {
- if (shotTimer >= minShotTimer)
- {
- PlayerShotManager.FireShot(
- playerSprite.Location + gunOffset,
- new Vector2(0, -1),
- true);
- shotTimer = 0.0f;
- }
- }
- private void HandleKeyboardInput(KeyboardState keyState)
- {
- if (keyState.IsKeyDown(Keys.Up))
- {
- playerSprite.Velocity += new Vector2(0, -1);
- }
- if (keyState.IsKeyDown(Keys.Down))
- {
- playerSprite.Velocity += new Vector2(0, 1);
- }
- if (keyState.IsKeyDown(Keys.Left))
- {
- playerSprite.Velocity += new Vector2(-1, 0);
- }
- if (keyState.IsKeyDown(Keys.Right))
- {
- playerSprite.Velocity += new Vector2(1, 0);
- }
- if (keyState.IsKeyDown(Keys.Space))
- {
- FireShot();
- }
- }
- private void HandleGamepadInput(GamePadState gamePadState)
- {
- playerSprite.Velocity +=
- new Vector2(
- gamePadState.ThumbSticks.Left.X,
- -gamePadState.ThumbSticks.Left.Y);
- if (gamePadState.Buttons.A == ButtonState.Pressed)
- {
- FireShot();
- }
- }
- private void imposeMovementLimits()
- {
- Vector2 location = playerSprite.Location;
- if (location.X < playerAreaLimit.X)
- location.X = playerAreaLimit.X;
- if (location.X >
- (playerAreaLimit.Right - playerSprite.Source.Width))
- location.X =
- (playerAreaLimit.Right - playerSprite.Source.Width);
- if (location.Y < playerAreaLimit.Y)
- location.Y = playerAreaLimit.Y;
- if (location.Y >
- (playerAreaLimit.Bottom - playerSprite.Source.Height))
- location.Y =
- (playerAreaLimit.Bottom - playerSprite.Source.Height);
- playerSprite.Location = location;
- }
- public void Update(GameTime gameTime)
- {
- PlayerShotManager.Update(gameTime);
- if (!Destroyed)
- {
- playerSprite.Velocity = Vector2.Zero;
- shotTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
- HandleKeyboardInput(Keyboard.GetState());
- HandleGamepadInput(GamePad.GetState(PlayerIndex.One));
- playerSprite.Velocity.Normalize();
- playerSprite.Velocity *= playerSpeed;
- playerSprite.Update(gameTime);
- imposeMovementLimits();
- }
- }
- public void Draw(SpriteBatch spriteBatch)
- {
- PlayerShotManager.Draw(spriteBatch);
- if (!Destroyed)
- {
- playerSprite.Draw(spriteBatch);
- }
- }
- }
- }
|