Advertisement
Spectrewiz

Player Template

Aug 27th, 2012
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace Horror
  13. {
  14.     public class Player : Obj
  15.     {
  16.         public static Player player;
  17.         public int health = 100, ammo = 32,
  18.             maxHealth = 100, maxAmmo = 32;
  19.  
  20.         public bool inMenu;
  21.  
  22.         public Player(Vector2 position)
  23.             : base(position)
  24.         {
  25.             this.position = position;
  26.             this.name = "Player";
  27.             player = this;
  28.             velocity = new Vector2(5, 5);
  29.             this.health = maxHealth;
  30.             this.alive = true;
  31.         }
  32.  
  33.         public override void Update()
  34.         {
  35.             player = this;
  36.             if (!alive) return;
  37.  
  38.             mouse = Mouse.GetState();
  39.             key = Keyboard.GetState();
  40.  
  41.             if (key.IsKeyDown(Keys.Up) || key.IsKeyDown(Keys.W))
  42.                 position.Y -= velocity.Y;
  43.             if (key.IsKeyDown(Keys.Down) || key.IsKeyDown(Keys.S))
  44.                 position.Y += velocity.Y;
  45.             if (key.IsKeyDown(Keys.Right) || key.IsKeyDown(Keys.D))
  46.                 position.X += velocity.X;
  47.             if (key.IsKeyDown(Keys.Left) || key.IsKeyDown(Keys.A))
  48.                 position.X -= velocity.X;
  49.             if (key.IsKeyDown(Keys.Escape))
  50.                 Menu();
  51.  
  52.             if (position.Y < 0)
  53.                 position.Y = 0;
  54.             else if (position.Y > Game.stage.Height)
  55.                 position.Y = Game.stage.Height;
  56.  
  57.             if (position.X < 0)
  58.                 position.X = 0;
  59.             else if (position.X > Game.stage.Width)
  60.                 position.X = Game.stage.Width;
  61.            
  62.             base.Update();
  63.         }
  64.  
  65.         public void Menu()
  66.         {
  67.             if (!inMenu)
  68.             {
  69.                 inMenu = true;
  70.                 Game.switchGameState(State.game_menu);
  71.             }
  72.             else
  73.             {
  74.                 inMenu = false;
  75.                 Game.switchGameState(State.game);
  76.             }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement