Repository navigation
Expand file tree
/
Copy pathaircraft.js
More file actions
180 lines (144 loc) · 4.62 KB
/
Copy pathaircraft.js
File metadata and controls
180 lines (144 loc) · 4.62 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
var checkpointBuilder = Qt.createComponent("Checkpoint.qml");
var checkpoints = new Array();
var checkpoint = null;
function startControl() {
clearCheckpoints();
}
function clearCheckpoints() {
//reset all checkpoints
var l = checkpoints.length;
for (var i = 0; i < l; i++) {
var cp = checkpoints.pop();
cp.destroy();
}
}
function createCheckpoint(x,y) {
var cp = checkpointBuilder.createObject(checkpointLayer);
checkpoints.push(cp);
cp.x = x;
cp.y = y;
return cp;
}
function updateControl(x,y) {
//if the last checkpoint is a landing point we just return and do nothing at all
if (checkpoints.length > 0 && checkpoints[checkpoints.length - 1].landing) {
return;
}
var obj = game.canLand(aircraft.type,x,y);
//if we have found an suitable airstrip we land
if ( obj != null ) {
var cp = createCheckpoint(obj[0], obj[1]);
cp.landing = true;
var rad = (obj[2] * (Math.PI / 180));
var vector_x = Math.sin(rad) * obj[3];
var vector_y = -Math.cos(rad) * obj[3];
aircraft.landingParams = [obj[2], Math.round(obj[0] + vector_x), Math.round(obj[1] + vector_y), obj[4]];
} else if (checkpoints.length > 0) { //we append if len is appropriet
var last_cp = checkpoints[checkpoints.length - 1];
if (Util.distance(x,y,last_cp.x,last_cp.y) > 25) {
createCheckpoint(x,y);
}
} else { //first checkpoint we head for it
if (Util.distance(x,y,aircraft.getCenterX(),aircraft.getCenterY()) > 50) {
var cp = createCheckpoint(x,y);
}
}
}
function canLanding(x,y) {
return airport.canLand(x,y);
}
function updateFlightPath(deg)
{
checkpoint = null;
var rad = (deg * (Math.PI / 180));
var vector_x = Math.sin(rad);
var vector_y = -Math.cos(rad);
var tox = aircraft.getCenterX() + vector_x * 10;
var toy = aircraft.getCenterY() + vector_y * 10;
var fly_distance = Util.distance(aircraft.getCenterX(), aircraft.getCenterY(),tox,toy);
flyto(tox - aircraft.width / 2,toy - aircraft.height / 2,fly_distance * 35);
}
function flyto(tox,toy,duration) {
xanim.duration = Math.round(duration / window.gamespeed);
yanim.duration = Math.round(duration / window.gamespeed);
xanim.to = tox;
yanim.to = toy;
if (!moveanim.running) {
moveanim.start();
}
}
function clampX(changedX)
{
//did we move out of the rectangle
if (changedX < -aircraft.width) {
moveanim.stop();
aircraft.x = window.width;
aircraft.updateFlightPath(aircraft.rotation);
} else if (changedX > window.width) {
moveanim.stop();
aircraft.x = -aircraft.width;
aircraft.updateFlightPath(aircraft.rotation);
}
}
function clampY(changedY)
{
//did we move out of the rectangle
if (changedY < -aircraft.height) {
moveanim.stop();
aircraft.y = window.height;
aircraft.updateFlightPath(aircraft.rotation);
} else if (changedY > window.height) {
moveanim.stop();
aircraft.y = -aircraft.height;
aircraft.updateFlightPath(aircraft.rotation);
}
}
function headForNextCheckpoint() {
if (checkpoints.length > 0) {
var curr_cp = checkpoints[0];
if (curr_cp.landing) {
game.removePlane(aircraft);
aircraft.landing = true;
curr_cp.destroy();
return;
}
if (curr_cp == checkpoint) {
checkpoints.shift();
curr_cp.destroy();
}
}
clampX(aircraft.x);
clampY(aircraft.y);
if (checkpoints.length > 0) {
checkpoint = checkpoints[0];
headfor(checkpoint);
} else {
updateFlightPath(aircraft.rotation);
}
}
/**
* Calculate distance and rotation to the next checkpoint. Then rotate and start animation
*/
function headfor(checkpoint)
{
var v_1_x = 0;
var v_1_y = 1;
var v_2_x = aircraft.getCenterX() - checkpoint.getCenterX();
var v_2_y = aircraft.getCenterY() - checkpoint.getCenterY();
var rad = Util.calculateAngle(v_1_x,v_1_y,v_2_x,v_2_y);
if (0 < v_2_x) {
aircraft.rotate(360 - (rad * (180 / Math.PI)));
} else {
aircraft.rotate(rad * (180 / Math.PI));
}
var fly_distance = Util.distance(aircraft.getCenterX(), aircraft.getCenterY(),checkpoint.getCenterX(),checkpoint.getCenterY());
flyto(checkpoint.getCenterX() - aircraft.width / 2, checkpoint.getCenterY() - aircraft.height / 2,fly_distance * 35);
}
function speedChanged() {
moveanim.stop();
if (checkpoint != null) {
headfor(checkpoint);
} else {
updateFlightPath();
}
}