123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- using System.Linq;
- using Assets.HeroEditor4D.Common.Scripts.CharacterScripts;
- using Assets.HeroEditor4D.Common.Scripts.Enums;
- using UnityEngine;
- namespace Assets.HeroEditor4D.Common.Scripts.ExampleScripts
- {
- /// <summary>
- /// An example of how to handle user input, play animations and move a character.
- /// </summary>
- public class ControlsExample : MonoBehaviour
- {
- public Character4D Character;
- public FirearmFxExample FirearmFx;
- public bool InitDirection;
- public int MovementSpeed;
- public playerNetwork playerNetwork;
- [SerializeField]private bool _moving;
- public void Start()
- {
- Character.AnimationManager.SetState(CharacterState.Idle);
- if (InitDirection)
- {
- Character.SetDirection(Vector2.down);
- }
- }
- public void Update()
- {
- if(!playerNetwork.isLocalPlayer){return;}
- if (playerNetwork.isDead) {
- return;
- }
- SetDirection();
- Move();
- ChangeState();
- Actions();
- }
- private void SetDirection()
- {
- Vector2 direction;
- if(joystick.Input == Vector2.zero){
- if (Input.GetKeyDown(KeyCode.LeftArrow))
- {
- direction = Vector2.left;
- }
- else if (Input.GetKeyDown(KeyCode.RightArrow))
- {
- direction = Vector2.right;
- }
- else if (Input.GetKeyDown(KeyCode.UpArrow))
- {
- direction = Vector2.up;
- }
- else if (Input.GetKeyDown(KeyCode.DownArrow))
- {
- direction = Vector2.down;
- }
- else return;
- }else{
- direction = JoyToDir();
- // direction= joystick.Input;
- }
- // Debug.Log(direction);
- Character.SetDirection(direction);
- }
- Vector2 JoyToDir(){
- if(joystick.Input == Vector2.zero){return Vector2.zero;}
-
- Vector2 direction;
- if(Mathf.Abs(joystick.Input.x) > Mathf.Abs(joystick.Input.y)){
- if(joystick.Input.x > 0){
- direction = Vector2.right;
- }else{
- direction = Vector2.left;
- }
- }else{
- if(joystick.Input.y > 0){
- direction = Vector2.up;
- }else{
- direction = Vector2.down;
- }
- }
- return direction;
- }
- private void Move()
- {
-
- if (MovementSpeed == 0) return;
- var direction = Vector2.zero;
- if (Input.GetKey(KeyCode.LeftArrow))
- {
- direction += Vector2.left;
- }
- if (Input.GetKey(KeyCode.RightArrow))
- {
- direction += Vector2.right;
- }
- if (Input.GetKey(KeyCode.UpArrow))
- {
- direction += Vector2.up;
- }
- if (Input.GetKey(KeyCode.DownArrow))
- {
- direction += Vector2.down;
- }
- if(joystick.Input != Vector2.zero){
- direction = joystick.Input;
- }
- if (direction == Vector2.zero)
- {
- if (_moving)
- {
- Character.AnimationManager.SetState(CharacterState.Idle);
- _moving = false;
- }
- }
- else
- {
- Character.AnimationManager.SetState(CharacterState.Run);
- Character.transform.position += (Vector3) direction.normalized * MovementSpeed * Time.deltaTime;
- _moving = true;
- }
- }
- private void Actions()
- {
- if (Input.GetKeyDown(KeyCode.S))
- {
- switch (Character.WeaponType)
- {
- case WeaponType.Melee1H:
- Character.AnimationManager.Slash(twoHanded: false);
- break;
- case WeaponType.Melee2H:
- case WeaponType.Paired:
- Character.AnimationManager.Slash(twoHanded: true);
- break;
- case WeaponType.Bow:
- Character.AnimationManager.ShotBow();
- break;
- case WeaponType.Crossbow:
- Character.AnimationManager.CrossbowShot();
- break;
- }
- }
- else if (Input.GetKeyDown(KeyCode.J))
- {
- Character.AnimationManager.Jab();
- }
- else if (Input.GetKey(KeyCode.F))
- {
- if (Character.AnimationManager.IsAction) return;
- switch (Character.WeaponType)
- {
- case WeaponType.Crossbow:
- Character.AnimationManager.CrossbowShot();
- break;
- case WeaponType.Firearm1H:
- case WeaponType.Firearm2H:
- {
- Character.AnimationManager.Fire();
- if (Character.Parts[0].PrimaryWeapon != null)
- {
- var firearm = Character.SpriteCollection.Firearm1H.SingleOrDefault(i => i.Sprites.Contains(Character.Parts[0].PrimaryWeapon))
- ?? Character.SpriteCollection.Firearm2H.SingleOrDefault(i => i.Sprites.Contains(Character.Parts[0].PrimaryWeapon));
- if (firearm != null)
- {
- FirearmFx.CreateFireMuzzle(firearm.Name, firearm.Collection);
- }
- }
- break;
- }
- case WeaponType.Paired:
- Character.AnimationManager.SecondaryShot();
- break;
- }
- }
- }
- private void ChangeState()
- {
- if (Input.GetKeyDown(KeyCode.I))
- {
- Character.AnimationManager.SetState(CharacterState.Idle);
- }
- if (Input.GetKeyDown(KeyCode.E))
- {
- Character.AnimationManager.SetState(CharacterState.Ready);
- }
- if (Input.GetKeyDown(KeyCode.W))
- {
- Character.AnimationManager.SetState(CharacterState.Walk);
- }
- if (Input.GetKeyDown(KeyCode.R))
- {
- Character.AnimationManager.SetState(CharacterState.Run);
- }
- if (Input.GetKeyDown(KeyCode.U))
- {
- Character.AnimationManager.SetState(CharacterState.Jump);
- }
- if (Input.GetKeyDown(KeyCode.C))
- {
- Character.AnimationManager.SetState(CharacterState.Climb);
- }
- if (Input.GetKeyDown(KeyCode.D))
- {
- Character.AnimationManager.Die();
- }
- if (Input.GetKeyDown(KeyCode.H))
- {
- Character.AnimationManager.Hit();
- }
- if (Input.GetKeyDown(KeyCode.B))
- {
- Character.AnimationManager.SetState(CharacterState.ShieldBlock);
- }
- else if (Input.GetKeyUp(KeyCode.B))
- {
- Character.AnimationManager.SetState(CharacterState.Idle);
- }
- }
- public void TurnLeft()
- {
- Character.SetDirection(Vector2.left);
- }
- public void TurnRight()
- {
- Character.SetDirection(Vector2.right);
- }
- public void TurnUp()
- {
- Character.SetDirection(Vector2.up);
- }
- public void TurnDown()
- {
- Character.SetDirection(Vector2.down);
- }
- public void Show4Directions()
- {
- Character.SetDirection(Vector2.zero);
- }
- public void OpenLink(string url)
- {
- Application.OpenURL(url);
- }
- }
- }
|