Advertisement
Rodrigo_Medeiros

ControladorDePersonagem

Jul 28th, 2015
2,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.48 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // END USER LICENSE / CREATIVE COMMONS by-sa v4.0 -> https://creativecommons.org/licenses/by/4.0/
  3. ////////////////////////////////////////////////////////////////////////////////////////////////////
  4. // With this license you are free to:
  5. // * Share — copy and redistribute the material in any medium or format
  6. // * Adapt — remix, transform, and build upon the material
  7. // for any purpose, even commercially.
  8. // The licensor cannot revoke these freedoms as long as you follow the license terms.
  9. //
  10. // Under the following terms:
  11. // * Attribution — You must give appropriate credit to Rodrigo M.Lehnemann and provide a link to the license. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  12. // No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.  
  13. //
  14. // Notices:
  15. // You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation.
  16. // No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material.
  17. ////////////////////////////////////////////////////////////////////////////////////////////////////
  18. // Version 1.3
  19.  
  20. using System;
  21. using System.Threading.Tasks;
  22. using SiliconStudio.Core.Mathematics;
  23. using SiliconStudio.Xenko.Engine;
  24. using SiliconStudio.Xenko.Input;
  25. using SiliconStudio.Xenko.Physics;
  26.  
  27. namespace //Put the namespace of your Project here.
  28. {
  29.     public class ControladorDePersonagem : AsyncScript
  30.     {
  31.         //Input de Animações
  32.  
  33.         public string animacaoParado = "Parado";
  34.         public string animacaoAndando = "Andando";
  35.         public string animacaoCorrendo = "Correndo";
  36.         public string animacaoTras = "Tras";
  37.         public string animacaoEsquerda = "Esquerda";
  38.         public string animacaoDireita = "Direita";
  39.        
  40.         //Variávveis do Script
  41.        
  42.         private enum estado
  43.         {
  44.             parado,
  45.             andando,
  46.             correndo,
  47.             tras,
  48.             esquerda,
  49.             direita
  50.         };
  51.  
  52.         private int estadoAtual;
  53.         private int estadoAnterior ;
  54.         private Vector3 movimento;
  55.         private CharacterComponent characterController;
  56.  
  57.         public void Inicio()
  58.         {
  59.             Input.LockMousePosition(true);
  60.             characterController = Entity.Get<CharacterComponent>();
  61.    
  62.             movimento.X = 0;
  63.             movimento.Y = 0;
  64.             movimento.Z = 0;
  65.  
  66.             estadoAtual = (int)estado.parado;
  67.             estadoAnterior = (int)estado.parado;
  68.             AnimaPersonagem();
  69.         }
  70.  
  71.         private void ControleDoPersonagem()
  72.         {
  73.             //Frente Sem Arma ------------------------
  74.             if (Input.IsKeyPressed(Keys.W)) // <<--- Para inserir uma tecla, é necessário chamar o objeto Keys. mais referência da tecla.
  75.             {
  76.                 estadoAnterior = estadoAtual;
  77.                 estadoAtual = (int)estado.andando;
  78.                 AnimaPersonagem();
  79.             }
  80.             if (Input.IsKeyReleased(Keys.W))
  81.             {
  82.                 if (estadoAtual == (int)estado.andando || estadoAtual == (int)estado.correndo)
  83.                 {
  84.                     estadoAnterior = estadoAtual;
  85.                     estadoAtual = (int)estado.parado;
  86.                     AnimaPersonagem();
  87.                 }    
  88.             }
  89.  
  90.             //Correr Sem Arma ------------------------
  91.             if (Input.IsKeyPressed(Keys.LeftShift) && estadoAtual == (int)estado.andando)
  92.             {
  93.                 estadoAnterior = estadoAtual;
  94.                 estadoAtual = (int)estado.correndo;
  95.                 AnimaPersonagem();
  96.             }
  97.             if (Input.IsKeyReleased(Keys.LeftShift))
  98.             {
  99.                 if (estadoAtual == (int)estado.correndo)
  100.                 {
  101.                     estadoAnterior = estadoAtual;
  102.                     estadoAtual = (int)estado.andando;
  103.                     AnimaPersonagem();
  104.                 }
  105.             }
  106.  
  107.             if (Input.IsKeyPressed(Keys.S))
  108.             {
  109.                 estadoAnterior = estadoAtual;
  110.                 estadoAtual = (int)estado.tras;
  111.                 AnimaPersonagem();
  112.             }
  113.             if (Input.IsKeyReleased(Keys.S))
  114.             {
  115.                 if (estadoAtual == (int)estado.tras)
  116.                 {
  117.                     estadoAnterior = estadoAtual;
  118.                     estadoAtual = (int)estado.parado;
  119.                     AnimaPersonagem();
  120.                 }  
  121.             }
  122.  
  123.             //Esquerda Sem Arma ------------------------
  124.             if (Input.IsKeyPressed(Keys.A))
  125.             {
  126.                 estadoAnterior = estadoAtual;
  127.                 estadoAtual = (int)estado.esquerda;
  128.                 AnimaPersonagem();
  129.             }
  130.             if (Input.IsKeyReleased(Keys.A))
  131.             {
  132.                 if (estadoAtual == (int)estado.esquerda)
  133.                 {
  134.                     estadoAnterior = estadoAtual;
  135.                     estadoAtual = (int)estado.parado;
  136.                     AnimaPersonagem();
  137.                 }
  138.             }
  139.  
  140.             //Direita Sem Arma ------------------------
  141.             if (Input.IsKeyPressed(Keys.D))
  142.             {
  143.                 estadoAnterior = estadoAtual;
  144.                 estadoAtual = (int)estado.direita;
  145.                 AnimaPersonagem();
  146.             }
  147.             if (Input.IsKeyReleased(Keys.D))
  148.             {
  149.                 if (estadoAtual == (int)estado.direita)
  150.                 {
  151.                     estadoAnterior = estadoAtual;
  152.                     estadoAtual = (int)estado.parado;
  153.                     AnimaPersonagem();
  154.                 }
  155.             }
  156.         }
  157.  
  158.         private void MovePersonagem()
  159.         {
  160.             var tempoTranscorrido = (float)Game.UpdateTime.Elapsed.TotalSeconds;
  161.             Entity.Transform.UpdateLocalMatrix();
  162.  
  163.             if (estadoAtual == 0)
  164.             {
  165.                 movimento.X = 0;
  166.                 movimento.Z = 0;
  167.             }
  168.             else
  169.             {
  170.  
  171.                 if (estadoAtual == 1) { movimento = Entity.Transform.LocalMatrix.Forward * -1.4F * tempoTranscorrido; }
  172.                 if (estadoAtual == 2) { movimento = Entity.Transform.LocalMatrix.Forward * -5.0f * tempoTranscorrido; }
  173.                 if (estadoAtual == 3) { movimento = Entity.Transform.LocalMatrix.Backward * -1.4F * tempoTranscorrido; }
  174.                 if (estadoAtual == 4) { movimento = Entity.Transform.LocalMatrix.Left * -1.4F * tempoTranscorrido; }
  175.                 if (estadoAtual == 5) { movimento = Entity.Transform.LocalMatrix.Right * -1.0F * tempoTranscorrido; }
  176.             }
  177.             characterController.Move(movimento);
  178.         }
  179.  
  180.         private void AnimaPersonagem()
  181.         {
  182.             if (estadoAtual == (int)estado.parado)
  183.             {
  184.                 Entity.Get<AnimationComponent>().Crossfade(animacaoParado, TimeSpan.FromMilliseconds(300));
  185.             }
  186.             if (estadoAtual == (int)estado.andando)
  187.             {
  188.                 Entity.Get<AnimationComponent>().Crossfade(animacaoAndando, TimeSpan.FromMilliseconds(300));
  189.             }
  190.             if (estadoAtual == (int)estado.correndo)
  191.             {
  192.                 Entity.Get<AnimationComponent>().Crossfade(animacaoCorrendo, TimeSpan.FromMilliseconds(300));
  193.             }
  194.             if (estadoAtual == (int)estado.direita)
  195.             {
  196.                 Entity.Get<AnimationComponent>().Crossfade(animacaoDireita, TimeSpan.FromMilliseconds(300));
  197.             }
  198.             if (estadoAtual == (int)estado.esquerda)
  199.             {
  200.                 Entity.Get<AnimationComponent>().Crossfade(animacaoEsquerda, TimeSpan.FromMilliseconds(300));
  201.             }
  202.             if (estadoAtual == (int)estado.tras)
  203.             {
  204.                 Entity.Get<AnimationComponent>().Crossfade(animacaoTras, TimeSpan.FromMilliseconds(300));
  205.             }
  206.         }
  207.  
  208.         private Quaternion GiraPersonagem(float movimentoMouse, Quaternion rotacao)
  209.         {
  210.             var entityRotation = Quaternion.RotationY((1f * -movimentoMouse) * (float)Math.PI);
  211.             rotacao *= entityRotation;
  212.             return rotacao;
  213.         }
  214.  
  215.         public async override Task Execute()
  216.         {
  217.             Inicio();
  218.             float movimentoMouse = 0f;
  219.  
  220.             Quaternion atualRotacao = Entity.Transform.Rotation;
  221.             Quaternion novaRotacao = Entity.Transform.Rotation;
  222.  
  223.             //Loop do Script
  224.             while (Game.IsRunning)
  225.             {
  226.                 await Script.NextFrame();
  227.                 //Input do Mouse
  228.                 movimentoMouse = 0.95f * movimentoMouse;
  229.                 movimentoMouse = Input.MouseDelta.X;
  230.                
  231.                 //Gira o Personagem Conforme Movimento do Mouse
  232.                 novaRotacao = GiraPersonagem(movimentoMouse, atualRotacao);
  233.                 Entity.Transform.Rotation = novaRotacao;
  234.                 Vector3 moveY = novaRotacao.Axis;
  235.                 atualRotacao = Entity.Transform.Rotation;
  236.  
  237.                 //Aplica Movimentos, Transformações e Animações
  238.                 ControleDoPersonagem();
  239.                 MovePersonagem();
  240.             }
  241.         }
  242.     }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement