-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovement.js
137 lines (122 loc) · 4.36 KB
/
movement.js
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
var shieldIsRestoring = false; // weird
function detectMovement()
{
// TURNING
if (left && !right)
{
if (ship.rotationSpeed < ship.maxRotationSpeed) {ship.rotationSpeed += ship.rotationAcceleration}
else {ship.rotationSpeed = ship.maxRotationSpeed;}
ship.rotation += ship.rotationSpeed;
}
if (right && !left)
{
if (ship.rotationSpeed > -ship.maxRotationSpeed) {ship.rotationSpeed -= ship.rotationAcceleration}
else {ship.rotationSpeed = -ship.maxRotationSpeed;}
ship.rotation += ship.rotationSpeed;
}
if (left && right)
{
if (ship.rotationSpeed > 0.1) {ship.rotationSpeed -= ship.rotationAcceleration}
else if (ship.rotationSpeed < -0.1) {ship.rotationSpeed += ship.rotationAcceleration}
else {ship.rotationSpeed = 0;}
ship.rotation += ship.rotationSpeed;
}
if (!left && !right)
{
if (ship.rotationSpeed > 0.1) {ship.rotationSpeed -= ship.rotationAcceleration}
else if (ship.rotationSpeed < -0.1) {ship.rotationSpeed += ship.rotationAcceleration}
else {ship.rotationSpeed = 0;}
ship.rotation += ship.rotationSpeed;
}
// FORWARDS
if (forward && ship.energyStore > 0)
{
if (boosters && ship.boostTank > 0)
{
ship.boostTank -= ship.boostConsumption;
if (ship.speed < ship.maxSpeed*2) {ship.speed += ship.acceleration;}
else {ship.speed = ship.maxSpeed*2;}
}
else
{
if (ship.speed > ship.maxSpeed+0.1) {ship.speed -= ship.acceleration;}
else if (ship.speed < ship.maxSpeed) {ship.speed += ship.acceleration;}
else {ship.speed = ship.maxSpeed;}
}
ship.xPos += ship.speed * Math.sin(ship.rotation * Math.PI / 180);
ship.yPos += ship.speed * Math.cos(ship.rotation * Math.PI / 180);
//sounds.ship.play();
}
if (!forward || ship.energyStore <= 0)
{
if (ship.speed > ship.maxSpeed+0.1) {ship.speed -= ship.acceleration;}
else if (ship.speed > 0.1) {ship.speed -= ship.speed*ship.deceleration;} // Deceleration value
//else if (ship.speed < -ship.maxSpeed/2-0.1) {ship.speed += ship.acceleration;}
//else if (ship.speed < -0.1) {ship.speed -= ship.speed*ship.deceleration;}
else {ship.speed = 0;}
ship.xPos += ship.speed * Math.sin(ship.rotation * Math.PI / 180);
ship.yPos += ship.speed * Math.cos(ship.rotation * Math.PI / 180);
/*
if (!forward && ship.energyStore < ship.energyCapacity)
{
ship.energyStore += ship.energyRestoration;
}
*/
//sounds.ship.pause();
//sounds.ship.currentTime = 0;
}
if (ship.speed > ship.maxSpeed/2 && ship.energyStore > 0)
{
ship.energyStore -= ship.energyConsumption;
}
else if (ship.speed > 0 && ship.energyStore > 0)
{
ship.energyStore -= ship.energyConsumption/2;
}
if (ship.energyStore < ship.energyCapacity)
{
ship.energyStore += ship.energyRestoration;
}
// where? check line 1 as well
// shield refill
if (ship.shield < ship.maxShield && ship.energyStore > 2)
{
ship.shield += ship.energyConsumption;
ship.energyStore -= ship.energyConsumption;
shieldIsRestoring = true;
}
else
{
shieldIsRestoring = false;
}
// BACKWARDS
/*if (backward) // SHOULD TURN THE OTHER WAY
{
//ship.speed = -5;
if (ship.speed > -ship.maxSpeed/2) {ship.speed -= ship.acceleration;}
else {ship.speed = -ship.maxSpeed/2;}
backgroundX += ship.speed * Math.sin(ship.rotation * Math.PI / 180);
backgroundY += ship.speed * Math.cos(ship.rotation * Math.PI / 180);
}
if (forward && backward)
{
if (ship.speed > 0.1) {ship.speed -= ship.acceleration}
else if (ship.speed < -0.1) {ship.speed += ship.acceleration}
else {ship.speed = 0;}
backgroundX += ship.speed * Math.sin(ship.rotation * Math.PI / 180);
backgroundY += ship.speed * Math.cos(ship.rotation * Math.PI / 180);
}*/
}
function storeLastPosition(xPos, yPos, rotation, array)
{
// push an item
array.push({
x: xPos,
y: yPos,
rotation: rotation
});
//get rid of first item
if (array.length > motionTrailLength) {
array.shift();
}
}