Advertisement
Guest User

player class

a guest
Jun 8th, 2013
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1.     class Player
  2.     {
  3.         Model playerModel;
  4.         public Matrix playerWorld;
  5.         Matrix[] playerTransforms;
  6.  
  7.         float speed = 200f;
  8.         float rotationRate = 0.3f;
  9.  
  10.         public Player()
  11.         {
  12.             playerWorld = Matrix.CreateScale(0.1f);
  13.         }
  14.  
  15.         public void LoadContent(ContentManager content)
  16.         {
  17.             playerModel = content.Load<Model>("playerModel");
  18.             playerTransforms = new Matrix[playerModel.Bones.Count];
  19.             playerModel.CopyAbsoluteBoneTransformsTo(playerTransforms);
  20.  
  21.         }
  22.  
  23.  
  24.         public void Update(float dt)
  25.         {
  26.             KeyboardState kbs = Keyboard.GetState();
  27.  
  28.             if (kbs.IsKeyDown(Keys.W))
  29.                 playerWorld *= Matrix.CreateTranslation(playerWorld.Forward * speed * dt);
  30.  
  31.             if(kbs.IsKeyDown(Keys.S))
  32.                 playerWorld *= Matrix.CreateTranslation(playerWorld.Backward * speed * dt);
  33.  
  34.             if (kbs.IsKeyDown(Keys.A))
  35.             {
  36.                 Vector3 temp = playerWorld.Translation;
  37.                 playerWorld.Translation = Vector3.Zero;
  38.                 playerWorld *= Matrix.CreateRotationY(rotationRate * dt);
  39.                 playerWorld.Translation = temp;
  40.             }
  41.  
  42.             if (kbs.IsKeyDown(Keys.D))
  43.             {
  44.                 Vector3 temp = playerWorld.Translation;
  45.                 playerWorld.Translation = Vector3.Zero;
  46.                 playerWorld *= Matrix.CreateRotationY(-rotationRate * dt);
  47.                 playerWorld.Translation = temp;
  48.             }
  49.         }
  50.  
  51.         public void Draw(Camera3rdPerson cam)
  52.         {
  53.             foreach (ModelMesh mm in playerModel.Meshes)
  54.             {
  55.                 foreach (BasicEffect bfx in mm.Effects)
  56.                 {
  57.                     bfx.View = cam.view;
  58.                     bfx.Projection = cam.proj;
  59.                     bfx.World = playerTransforms[mm.ParentBone.Index] * playerWorld;
  60.                 }
  61.                 mm.Draw();
  62.             }
  63.         }
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement