-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerInputHandler.cs
More file actions
149 lines (122 loc) · 5.63 KB
/
Copy pathPlayerInputHandler.cs
File metadata and controls
149 lines (122 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using UnityEngine;
using KinematicCharacterController;
/*
* Changelog
* 3/10 - Added joystick support, added support for 2 player (one on mouse+keyboard, other on controller)
* 2/23 - Added shield functionality. Now can't attack with shield up.
*/
public class PlayerInputHandler : MonoBehaviour{
public PlayerCharacterController Character;
public bool isUsingController;
public PlayerMeleeAttack meleeAttack;
public DirectionalEnergyShield directionalEnergyShield;
public PlayerRangedAttack rangedAttack;
public PlayerAimedShot aimedShot;
public PlayerFireballAttack fireballAttack;
//TODO move to its own script
public GameObject animatorContainer;
private Animator animator;
public RuntimeAnimatorController idle;
public RuntimeAnimatorController walking;
//True when the shield or aim button is pressed, false when released;
private bool shieldOrAimButtonDown = false;
private void Start(){
//TODO lock cursor when building, annoying for testing
//Cursor.lockState = CursorLockMode.None;
//Cursor.visible = false;
animator = animatorContainer.GetComponent<Animator>();
}
private void Update(){
HandleCharacterInput();
}
private void LateUpdate(){
}
private void HandleCharacterInput(){
PlayerCharacterInputs characterInputs = new PlayerCharacterInputs();
float vertical;
float horizontal;
float verticalLook;
float horizontalLook;
if (isUsingController) {
vertical = Input.GetAxisRaw("VerticalP2");
horizontal = Input.GetAxisRaw("HorizontalP2");
verticalLook = Input.GetAxisRaw("VerticalRightStickP2");
horizontalLook = Input.GetAxisRaw("HorizontalRightStickP2");
//characterInputs.JumpDown = Input.GetButtonDown("JumpP2");
characterInputs.Dash = Input.GetButtonDown("JumpP2");
} else {
vertical = Input.GetAxisRaw("Vertical");
horizontal = Input.GetAxisRaw("Horizontal");
verticalLook = Input.mousePosition.y;
horizontalLook = Input.mousePosition.x;
//characterInputs.JumpDown = Input.GetButtonDown("Jump");
characterInputs.Dash = Input.GetButtonDown("Jump");
}
if (aimedShot == null || (aimedShot != null && !aimedShot.aimedShotActive)) {
characterInputs.MoveAxisForward = vertical;
characterInputs.MoveAxisRight = horizontal;
} else {
characterInputs.MoveAxisForward = 0;
characterInputs.MoveAxisRight = 0;
}
characterInputs.LookAxisForward = verticalLook;
characterInputs.LookAxisRight = horizontalLook;
changeAnimatorState(vertical, horizontal);
// characterInputs.JumpDown = Input.GetKeyDown(KeyCode.Space);
// characterInputs.Dash = Input.GetKeyDown(KeyCode.LeftControl);
// characterInputs.CrouchDown = Input.GetKeyDown(KeyCode.C);
// characterInputs.CrouchUp = Input.GetKeyUp(KeyCode.C);
// Apply inputs to character
Character.SetInputs(ref characterInputs);
//Process other inputs not handled by the character controller
if ((!isUsingController && Input.GetButton("Fire1")) || (isUsingController && Input.GetButton("Fire1P2")) ) { // GetButton fires as long as the button is held
// Debug.Log("is controller: " + isUsingController + " " + Input.GetButton("Fire1P2"));
//shield player can't attack if shield is raised
if (directionalEnergyShield != null && !directionalEnergyShield.shieldActive) {
if (meleeAttack != null) {
meleeAttack.Attack();
}
}
if (aimedShot == null || (aimedShot != null && !aimedShot.aimedShotActive)) {
if(rangedAttack != null) {
rangedAttack.shoot();
}
}
}
if (!shieldOrAimButtonDown && ((!isUsingController && Input.GetButtonDown("Fire2")) || (isUsingController && Input.GetAxis("Fire2AxisP2")> .1f))) {
if (directionalEnergyShield != null) {
shieldOrAimButtonDown = true;
directionalEnergyShield.activateShield();
}
if (aimedShot != null) {
shieldOrAimButtonDown = true;
aimedShot.initiateShot();
}
}
if ((!isUsingController && Input.GetButtonUp("Fire2")) || (isUsingController && Input.GetAxis("Fire2AxisP2") < .1f)) {
if (directionalEnergyShield != null) {
shieldOrAimButtonDown = false;
directionalEnergyShield.deactivateShield();
}
if (aimedShot != null) {
shieldOrAimButtonDown = false;
aimedShot.disengageShot();
}
}
// TODO test with controller
if(!isUsingController && Input.GetButton("Fireback") || (isUsingController && Input.GetButton("FirebackP2"))){
fireballAttack.fire();
}
}
//TODO move to its own script
private void changeAnimatorState(float VerticalInput, float HorizontalInput) {
const float MIN_MOVEMENT = .02f;
float VertMag = Mathf.Abs(VerticalInput);
float HorzMag = Mathf.Abs(HorizontalInput);
if (VertMag > MIN_MOVEMENT || HorzMag > MIN_MOVEMENT) {
animator.runtimeAnimatorController = walking;
} else {
animator.runtimeAnimatorController = idle;
}
}
}