-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCharacter.cs
103 lines (81 loc) · 3.01 KB
/
Character.cs
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
using Godot;
using CharacterState;
public class Character : KinematicBody {
private const float maximumClimbableSlope = Mathf.PI / 4;
private Vector3 up = new Vector3(0,1,0);
private Camera camera;
private Spatial cameraSpatial;
private CapsuleShape moveShape;
private Tween heightTween;
private Tween leanTween;
private Tween bobbingTween;
private CharacterStateManager stateManager;
public override void _Ready()
{
camera = (Camera)GetNode("CameraSpatial/Camera");
cameraSpatial = (Spatial)GetNode("CameraSpatial");
moveShape = (CapsuleShape)((CollisionShape)GetNode("moveShape")).GetShape();
heightTween = (Tween) GetNode("CameraSpatial/HeightTween");
leanTween = (Tween)GetNode("CameraSpatial/LeanTween");
bobbingTween = (Tween)GetNode("CameraSpatial/Camera/BobbingTween");
Input.SetMouseMode(Input.MOUSE_MODE_CAPTURED);
stateManager = new CharacterStateManager(this);
}
public override void _Input(InputEvent ev) {
stateManager._Input(ev);
}
public override void _PhysicsProcess(float dt) {
stateManager._PhysicsProcess(dt);
}
public void ChangeHeight(float height)
{
//Make sure that there is a difference between current height and requested height
if (height - moveShape.GetHeight() != 0)
{
heightTween.InterpolateProperty(moveShape, "height", moveShape.GetHeight(), height, 0.1f, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
Vector3 cameraHeightTranslation = camera.GetTranslation();
cameraHeightTranslation.y = height / 2;
heightTween.InterpolateProperty(camera, "translation", camera.GetTranslation(), cameraHeightTranslation, 0.1f, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
heightTween.Start();
}
}
/**
* Where the player wants to consciously go.
* If I get pushed forward by someone, that isn't a conscious desire to go in that direction.
* If I decide to walk over and pick something up, that is conscious movement.
**/
public Vector3 ConsciousMovement(Vector3 movement)
{
return MoveAndSlide(movement, up, 1f, 4, maximumClimbableSlope);
}
/**
* Stuff like gravity, things out of direct player control
**/
public KinematicCollision UnconsciousMovement(Vector3 movement)
{
return MoveAndCollide(movement);
}
public void RotateCamera(Vector3 rotation)
{
Vector3 nextCameraRotation = new Vector3
{
x = Mathf.max(Mathf.min(rotation.x + camera.GetRotation().x, Mathf.PI / 2), -Mathf.PI / 2) //Clamp vertical camera movement
};
camera.SetRotation(nextCameraRotation);
Vector3 nextCameraSpatialRotation = rotation + cameraSpatial.GetRotation();
nextCameraSpatialRotation.x = 0;
cameraSpatial.SetRotation(nextCameraSpatialRotation);
}
public Vector3 GetCharacterRotation()
{
return cameraSpatial.GetRotation();
}
public void LeanAtDegrees(float angle)
{
Vector3 tempRotationDeg = cameraSpatial.GetRotationDeg();
Vector3 futureRotation = tempRotationDeg;
futureRotation.z = angle;
leanTween.InterpolateProperty(cameraSpatial, "rotation_deg", tempRotationDeg, futureRotation, 0.1f, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT);
leanTween.Start();
}
}