Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
dyingIsMainstream
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
2
Issues
2
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gamejam-gpn15
dyingIsMainstream
Commits
6fcab720
Commit
6fcab720
authored
Jun 05, 2015
by
Philipp Adolf
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Store key codes in array indexed by enum
parent
1733117d
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
25 deletions
+23
-25
PlayerControl.cs
Assets/Scripts/PlayerControl.cs
+23
-25
No files found.
Assets/Scripts/PlayerControl.cs
View file @
6fcab720
...
...
@@ -5,6 +5,8 @@ using System.Collections;
public
class
PlayerControl
:
MonoBehaviour
{
private
enum
Keys
:
int
{
Left
=
0
,
Right
,
Jump
,
Dash
,
Shoot
};
// movement config
public
float
pushForce
=
20f
;
public
float
gravity
=
-
15f
;
...
...
@@ -38,7 +40,7 @@ public class PlayerControl : MonoBehaviour
public
Vector3
velocity
;
public
int
playerNumber
=
1
;
//gibt an, ob es sich um player one, player two, etc. handelt. sollte nicht 0 sein;
KeyCode
goRightKeyCode
,
goLeftKeyCode
,
jumpKeyCode
,
dashKeyCode
,
shootKeyCode
;
KeyCode
[]
keyCodes
=
new
KeyCode
[
Keys
.
GetNames
(
typeof
(
Keys
)).
Length
]
;
void
Start
()
{
...
...
@@ -48,31 +50,27 @@ public class PlayerControl : MonoBehaviour
switch
(
playerNumber
)
{
case
1
:
setKeyCodes
(
KeyCode
.
D
,
KeyCode
.
A
,
KeyCode
.
W
,
KeyCode
.
Space
,
KeyCode
.
LeftShift
);
keyCodes
[(
int
)
Keys
.
Right
]
=
KeyCode
.
D
;
keyCodes
[(
int
)
Keys
.
Left
]
=
KeyCode
.
A
;
keyCodes
[(
int
)
Keys
.
Jump
]
=
KeyCode
.
W
;
keyCodes
[(
int
)
Keys
.
Dash
]
=
KeyCode
.
Space
;
keyCodes
[(
int
)
Keys
.
Shoot
]
=
KeyCode
.
LeftShift
;
break
;
case
2
:
setKeyCodes
(
KeyCode
.
RightArrow
,
KeyCode
.
LeftArrow
,
KeyCode
.
UpArrow
,
KeyCode
.
RightControl
,
KeyCode
.
RightShift
);
case
2
:
keyCodes
[(
int
)
Keys
.
Right
]
=
KeyCode
.
RightArrow
;
keyCodes
[(
int
)
Keys
.
Left
]
=
KeyCode
.
LeftArrow
;
keyCodes
[(
int
)
Keys
.
Jump
]
=
KeyCode
.
UpArrow
;
keyCodes
[(
int
)
Keys
.
Dash
]
=
KeyCode
.
RightControl
;
keyCodes
[(
int
)
Keys
.
Shoot
]
=
KeyCode
.
RightShift
;
break
;
//TODO player 3 and 4
default
:
Debug
.
LogError
(
"No keys defined for player number "
+
playerNumber
);
break
;
}
}
private
void
setKeyCodes
(
KeyCode
right
,
KeyCode
left
,
KeyCode
up
,
KeyCode
dash
,
KeyCode
shoot
)
{
goRightKeyCode
=
right
;
goLeftKeyCode
=
left
;
jumpKeyCode
=
up
;
dashKeyCode
=
dash
;
shootKeyCode
=
shoot
;
}
void
onControllerCollider
(
RaycastHit2D
hit
)
{
Rigidbody2D
body
=
hit
.
collider
.
gameObject
.
GetComponent
<
Rigidbody2D
>();
...
...
@@ -95,7 +93,7 @@ public class PlayerControl : MonoBehaviour
updateDashDirection
();
if
(
canDash
()
&&
Input
.
GetKey
(
dashKeyCode
))
if
(
canDash
()
&&
Input
.
GetKey
(
keyCodes
[(
int
)
Keys
.
Dash
]
))
{
lastDashStart
=
Time
.
time
;
currentDashDirection
=
dashDirection
;
...
...
@@ -108,7 +106,7 @@ public class PlayerControl : MonoBehaviour
}
else
{
if
(
Input
.
GetKey
(
goRightKeyCode
))
if
(
Input
.
GetKey
(
keyCodes
[(
int
)
Keys
.
Right
]
))
{
normalizedHorizontalSpeed
=
1
;
if
(
transform
.
localScale
.
x
<
0f
)
...
...
@@ -116,7 +114,7 @@ public class PlayerControl : MonoBehaviour
//if (_controller.isGrounded)
}
else
if
(
Input
.
GetKey
(
goLeftKeyCode
))
else
if
(
Input
.
GetKey
(
keyCodes
[(
int
)
Keys
.
Left
]
))
{
normalizedHorizontalSpeed
=
-
1
;
if
(
transform
.
localScale
.
x
>
0f
)
...
...
@@ -131,7 +129,7 @@ public class PlayerControl : MonoBehaviour
//if (_controller.isGrounded)
}
if
(
Input
.
GetKey
(
jumpKeyCode
))
if
(
Input
.
GetKey
(
keyCodes
[(
int
)
Keys
.
Jump
]
))
{
//to avoid DOUBLE JUMP
if
(
_controller
.
isGrounded
)
...
...
@@ -149,7 +147,7 @@ public class PlayerControl : MonoBehaviour
velocity
.
y
+=
gravity
*
Time
.
deltaTime
;
/*==========Power Ups // Bullet management ===*/
if
(
Input
.
GetKeyDown
(
shootKeyCode
))
if
(
Input
.
GetKeyDown
(
keyCodes
[(
int
)
Keys
.
Shoot
]
))
{
Debug
.
Log
(
"Shoot pressed. Can shoot: "
+
canShoot
());
if
(
canShoot
())
...
...
@@ -166,11 +164,11 @@ public class PlayerControl : MonoBehaviour
private
void
updateDashDirection
()
{
float
horizontalDashDirection
=
0.0f
;
if
(
Input
.
GetKey
(
goRightKeyCode
))
if
(
Input
.
GetKey
(
keyCodes
[(
int
)
Keys
.
Right
]
))
{
horizontalDashDirection
=
1.0f
;
}
else
if
(
Input
.
GetKey
(
goLeftKeyCode
))
else
if
(
Input
.
GetKey
(
keyCodes
[(
int
)
Keys
.
Left
]
))
{
horizontalDashDirection
=
-
1.0f
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment