Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- // END USER LICENSE / CREATIVE COMMONS by-sa v4.0 -> https://creativecommons.org/licenses/by/4.0/
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- // With this license you are free to:
- // * Share — copy and redistribute the material in any medium or format
- // * Adapt — remix, transform, and build upon the material
- // for any purpose, even commercially.
- // The licensor cannot revoke these freedoms as long as you follow the license terms.
- //
- // Under the following terms:
- // * 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.
- // No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.
- //
- // Notices:
- // 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.
- // 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.
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- // Version 1.3
- using System;
- using System.Threading.Tasks;
- using SiliconStudio.Core.Mathematics;
- using SiliconStudio.Xenko.Engine;
- using SiliconStudio.Xenko.Input;
- using SiliconStudio.Xenko.Physics;
- namespace //Put the namespace of your Project here.
- {
- public class ControladorDePersonagem : AsyncScript
- {
- //Input de Animações
- public string animacaoParado = "Parado";
- public string animacaoAndando = "Andando";
- public string animacaoCorrendo = "Correndo";
- public string animacaoTras = "Tras";
- public string animacaoEsquerda = "Esquerda";
- public string animacaoDireita = "Direita";
- //Variávveis do Script
- private enum estado
- {
- parado,
- andando,
- correndo,
- tras,
- esquerda,
- direita
- };
- private int estadoAtual;
- private int estadoAnterior ;
- private Vector3 movimento;
- private CharacterComponent characterController;
- public void Inicio()
- {
- Input.LockMousePosition(true);
- characterController = Entity.Get<CharacterComponent>();
- movimento.X = 0;
- movimento.Y = 0;
- movimento.Z = 0;
- estadoAtual = (int)estado.parado;
- estadoAnterior = (int)estado.parado;
- AnimaPersonagem();
- }
- private void ControleDoPersonagem()
- {
- //Frente Sem Arma ------------------------
- if (Input.IsKeyPressed(Keys.W)) // <<--- Para inserir uma tecla, é necessário chamar o objeto Keys. mais referência da tecla.
- {
- estadoAnterior = estadoAtual;
- estadoAtual = (int)estado.andando;
- AnimaPersonagem();
- }
- if (Input.IsKeyReleased(Keys.W))
- {
- if (estadoAtual == (int)estado.andando || estadoAtual == (int)estado.correndo)
- {
- estadoAnterior = estadoAtual;
- estadoAtual = (int)estado.parado;
- AnimaPersonagem();
- }
- }
- //Correr Sem Arma ------------------------
- if (Input.IsKeyPressed(Keys.LeftShift) && estadoAtual == (int)estado.andando)
- {
- estadoAnterior = estadoAtual;
- estadoAtual = (int)estado.correndo;
- AnimaPersonagem();
- }
- if (Input.IsKeyReleased(Keys.LeftShift))
- {
- if (estadoAtual == (int)estado.correndo)
- {
- estadoAnterior = estadoAtual;
- estadoAtual = (int)estado.andando;
- AnimaPersonagem();
- }
- }
- if (Input.IsKeyPressed(Keys.S))
- {
- estadoAnterior = estadoAtual;
- estadoAtual = (int)estado.tras;
- AnimaPersonagem();
- }
- if (Input.IsKeyReleased(Keys.S))
- {
- if (estadoAtual == (int)estado.tras)
- {
- estadoAnterior = estadoAtual;
- estadoAtual = (int)estado.parado;
- AnimaPersonagem();
- }
- }
- //Esquerda Sem Arma ------------------------
- if (Input.IsKeyPressed(Keys.A))
- {
- estadoAnterior = estadoAtual;
- estadoAtual = (int)estado.esquerda;
- AnimaPersonagem();
- }
- if (Input.IsKeyReleased(Keys.A))
- {
- if (estadoAtual == (int)estado.esquerda)
- {
- estadoAnterior = estadoAtual;
- estadoAtual = (int)estado.parado;
- AnimaPersonagem();
- }
- }
- //Direita Sem Arma ------------------------
- if (Input.IsKeyPressed(Keys.D))
- {
- estadoAnterior = estadoAtual;
- estadoAtual = (int)estado.direita;
- AnimaPersonagem();
- }
- if (Input.IsKeyReleased(Keys.D))
- {
- if (estadoAtual == (int)estado.direita)
- {
- estadoAnterior = estadoAtual;
- estadoAtual = (int)estado.parado;
- AnimaPersonagem();
- }
- }
- }
- private void MovePersonagem()
- {
- var tempoTranscorrido = (float)Game.UpdateTime.Elapsed.TotalSeconds;
- Entity.Transform.UpdateLocalMatrix();
- if (estadoAtual == 0)
- {
- movimento.X = 0;
- movimento.Z = 0;
- }
- else
- {
- if (estadoAtual == 1) { movimento = Entity.Transform.LocalMatrix.Forward * -1.4F * tempoTranscorrido; }
- if (estadoAtual == 2) { movimento = Entity.Transform.LocalMatrix.Forward * -5.0f * tempoTranscorrido; }
- if (estadoAtual == 3) { movimento = Entity.Transform.LocalMatrix.Backward * -1.4F * tempoTranscorrido; }
- if (estadoAtual == 4) { movimento = Entity.Transform.LocalMatrix.Left * -1.4F * tempoTranscorrido; }
- if (estadoAtual == 5) { movimento = Entity.Transform.LocalMatrix.Right * -1.0F * tempoTranscorrido; }
- }
- characterController.Move(movimento);
- }
- private void AnimaPersonagem()
- {
- if (estadoAtual == (int)estado.parado)
- {
- Entity.Get<AnimationComponent>().Crossfade(animacaoParado, TimeSpan.FromMilliseconds(300));
- }
- if (estadoAtual == (int)estado.andando)
- {
- Entity.Get<AnimationComponent>().Crossfade(animacaoAndando, TimeSpan.FromMilliseconds(300));
- }
- if (estadoAtual == (int)estado.correndo)
- {
- Entity.Get<AnimationComponent>().Crossfade(animacaoCorrendo, TimeSpan.FromMilliseconds(300));
- }
- if (estadoAtual == (int)estado.direita)
- {
- Entity.Get<AnimationComponent>().Crossfade(animacaoDireita, TimeSpan.FromMilliseconds(300));
- }
- if (estadoAtual == (int)estado.esquerda)
- {
- Entity.Get<AnimationComponent>().Crossfade(animacaoEsquerda, TimeSpan.FromMilliseconds(300));
- }
- if (estadoAtual == (int)estado.tras)
- {
- Entity.Get<AnimationComponent>().Crossfade(animacaoTras, TimeSpan.FromMilliseconds(300));
- }
- }
- private Quaternion GiraPersonagem(float movimentoMouse, Quaternion rotacao)
- {
- var entityRotation = Quaternion.RotationY((1f * -movimentoMouse) * (float)Math.PI);
- rotacao *= entityRotation;
- return rotacao;
- }
- public async override Task Execute()
- {
- Inicio();
- float movimentoMouse = 0f;
- Quaternion atualRotacao = Entity.Transform.Rotation;
- Quaternion novaRotacao = Entity.Transform.Rotation;
- //Loop do Script
- while (Game.IsRunning)
- {
- await Script.NextFrame();
- //Input do Mouse
- movimentoMouse = 0.95f * movimentoMouse;
- movimentoMouse = Input.MouseDelta.X;
- //Gira o Personagem Conforme Movimento do Mouse
- novaRotacao = GiraPersonagem(movimentoMouse, atualRotacao);
- Entity.Transform.Rotation = novaRotacao;
- Vector3 moveY = novaRotacao.Axis;
- atualRotacao = Entity.Transform.Rotation;
- //Aplica Movimentos, Transformações e Animações
- ControleDoPersonagem();
- MovePersonagem();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement