-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathShip.cs
More file actions
202 lines (168 loc) · 5.01 KB
/
Ship.cs
File metadata and controls
202 lines (168 loc) · 5.01 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ship : GravityObject {
public InputSettings inputSettings;
public Transform hatch;
public float hatchAngle;
public Transform camViewPoint;
public Transform pilotSeatPoint;
public LayerMask groundedMask;
public GameObject window;
[Header ("Handling")]
public float thrustStrength = 20;
public float rotSpeed = 5;
public float rollSpeed = 30;
public float rotSmoothSpeed = 10;
[Header ("Interact")]
public Interactable flightControls;
Rigidbody rb;
Quaternion targetRot;
Quaternion smoothedRot;
Vector3 thrusterInput;
PlayerController pilot;
bool shipIsPiloted;
int numCollisionTouches;
bool hatchOpen;
KeyCode ascendKey = KeyCode.Space;
KeyCode descendKey = KeyCode.LeftShift;
KeyCode rollCounterKey = KeyCode.Q;
KeyCode rollClockwiseKey = KeyCode.E;
KeyCode forwardKey = KeyCode.W;
KeyCode backwardKey = KeyCode.S;
KeyCode leftKey = KeyCode.A;
KeyCode rightKey = KeyCode.D;
void Awake () {
InitRigidbody ();
targetRot = transform.rotation;
smoothedRot = transform.rotation;
inputSettings.Begin ();
}
void Update () {
if (shipIsPiloted) {
HandleMovement ();
}
// Animate hatch
float hatchTargetAngle = (hatchOpen) ? hatchAngle : 0;
hatch.localEulerAngles = Vector3.right * Mathf.LerpAngle (hatch.localEulerAngles.x, hatchTargetAngle, Time.deltaTime);
HandleCheats ();
}
void HandleMovement () {
Debug.Log(Input.GetAxis("Select"));
// Thruster input
float thrustInputX = Input.GetAxis("Horizontal");
float thrustInputY = Input.GetAxis("Throttle");
float thrustInputZ = Input.GetAxis("Vertical");
thrusterInput = new Vector3 (thrustInputX, thrustInputY, thrustInputZ);
// Rotation input
float yawInput = Input.GetAxisRaw ("Look X") * rotSpeed * inputSettings.mouseSensitivity / 100f;
float pitchInput = Input.GetAxisRaw ("Look Y") * rotSpeed * inputSettings.mouseSensitivity / 100f;
float rollInput = Input.GetAxis("Roll") * rollSpeed * Time.deltaTime;
// Calculate rotation
if (numCollisionTouches == 0) {
var yaw = Quaternion.AngleAxis (yawInput, transform.up);
var pitch = Quaternion.AngleAxis (-pitchInput, transform.right);
var roll = Quaternion.AngleAxis (-rollInput, transform.forward);
targetRot = yaw * pitch * roll * targetRot;
smoothedRot = Quaternion.Slerp (transform.rotation, targetRot, Time.deltaTime * rotSmoothSpeed);
} else {
targetRot = transform.rotation;
smoothedRot = transform.rotation;
}
}
void FixedUpdate () {
// Gravity
Vector3 gravity = NBodySimulation.CalculateAcceleration (rb.position);
rb.AddForce (gravity, ForceMode.Acceleration);
// Thrusters
Vector3 thrustDir = transform.TransformVector (thrusterInput);
rb.AddForce (thrustDir * thrustStrength, ForceMode.Acceleration);
if (numCollisionTouches == 0) {
rb.MoveRotation (smoothedRot);
}
}
void TeleportToBody (CelestialBody body) {
rb.velocity = body.velocity;
rb.MovePosition (body.transform.position + (transform.position - body.transform.position).normalized * body.radius * 2);
}
void HandleCheats () {
if (Universe.cheatsEnabled) {
if (Input.GetKeyDown (KeyCode.Return) && IsPiloted && Time.timeScale != 0) {
var shipHud = FindObjectOfType<ShipHUD> ();
if (shipHud.LockedBody) {
TeleportToBody (shipHud.LockedBody);
}
}
}
}
void InitRigidbody () {
rb = GetComponent<Rigidbody> ();
rb.interpolation = RigidbodyInterpolation.Interpolate;
rb.useGravity = false;
rb.isKinematic = false;
rb.centerOfMass = Vector3.zero;
rb.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
}
public void ToggleHatch () {
hatchOpen = !hatchOpen;
}
public void TogglePiloting () {
if (shipIsPiloted) {
StopPilotingShip ();
} else {
PilotShip ();
}
}
public void PilotShip () {
pilot = FindObjectOfType<PlayerController> ();
shipIsPiloted = true;
pilot.Camera.transform.parent = camViewPoint;
pilot.Camera.transform.localPosition = Vector3.zero;
pilot.Camera.transform.localRotation = Quaternion.identity;
pilot.gameObject.SetActive (false);
hatchOpen = false;
window.SetActive (false);
}
void StopPilotingShip () {
shipIsPiloted = false;
pilot.transform.position = pilotSeatPoint.position;
pilot.transform.rotation = pilotSeatPoint.rotation;
pilot.Rigidbody.velocity = rb.velocity;
pilot.gameObject.SetActive (true);
window.SetActive (true);
pilot.ExitFromSpaceship ();
}
void OnCollisionEnter (Collision other) {
if (groundedMask == (groundedMask | (1 << other.gameObject.layer))) {
numCollisionTouches++;
}
}
void OnCollisionExit (Collision other) {
if (groundedMask == (groundedMask | (1 << other.gameObject.layer))) {
numCollisionTouches--;
}
}
public void SetVelocity (Vector3 velocity) {
rb.velocity = velocity;
}
public bool ShowHUD {
get {
return shipIsPiloted;
}
}
public bool HatchOpen {
get {
return hatchOpen;
}
}
public bool IsPiloted {
get {
return shipIsPiloted;
}
}
public Rigidbody Rigidbody {
get {
return rb;
}
}
}