268 lines
7.5 KiB
C#
Executable File
268 lines
7.5 KiB
C#
Executable File
using System.Linq;
|
|
using Assets.HeroEditor4D.Common.Scripts.CharacterScripts;
|
|
using Assets.HeroEditor4D.Common.Scripts.Enums;
|
|
using UnityEngine;
|
|
using Mirror;
|
|
|
|
//namespace Assets.HeroEditor4D.Common.Scripts.ExampleScripts
|
|
|
|
public class ControlsExample : NetworkBehaviour
|
|
{
|
|
|
|
public Character4D Character;
|
|
public Assets.HeroEditor4D.Common.Scripts.ExampleScripts.FirearmFxExample FirearmFx;
|
|
public bool InitDirection;
|
|
public float MovementSpeed = 1.0f;
|
|
|
|
private bool _moving;
|
|
public playerNetwork playerNet;
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
{
|
|
if(!isLocalPlayer){
|
|
Destroy(this);
|
|
}else{
|
|
cameraRPG.instance.SetTarget(transform) ;
|
|
}
|
|
Character.AnimationManager.SetState(CharacterState.Idle);
|
|
|
|
|
|
|
|
if (InitDirection)
|
|
{
|
|
Character.SetDirection(Vector2.down);
|
|
}
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if(playerNet.health <= 0 ){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.Space))
|
|
{
|
|
playerNet.OnAttack();
|
|
}
|
|
else if (Input.GetKeyDown(KeyCode.L))
|
|
{
|
|
Character.AnimationManager.Jab();
|
|
}
|
|
else if (Input.GetKey(KeyCode.L))
|
|
{
|
|
if (Character.AnimationManager.IsAction) return;
|
|
|
|
switch (Character.WeaponType)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
break;
|
|
case WeaponType.Paired:
|
|
Character.AnimationManager.SecondaryShot();
|
|
break;
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ChangeState()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.L))
|
|
{
|
|
Character.AnimationManager.SetState(CharacterState.Idle);
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.L))
|
|
{
|
|
Character.AnimationManager.SetState(CharacterState.Ready);
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.L))
|
|
{
|
|
Character.AnimationManager.SetState(CharacterState.Walk);
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.L))
|
|
{
|
|
Character.AnimationManager.SetState(CharacterState.Run);
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.F))
|
|
{
|
|
Character.AnimationManager.SetState(CharacterState.Jump);
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.L))
|
|
{
|
|
Character.AnimationManager.SetState(CharacterState.Climb);
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.L))
|
|
{
|
|
Character.AnimationManager.Die();
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.L))
|
|
{
|
|
Character.AnimationManager.Hit();
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.L))
|
|
{
|
|
Character.AnimationManager.SetState(CharacterState.ShieldBlock);
|
|
}
|
|
else if (Input.GetKeyUp(KeyCode.L))
|
|
{
|
|
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);
|
|
}
|
|
|
|
|
|
|
|
} |