Skip to content

Commit 6c21007

Browse files
committed
refractoring and better documentation
1 parent 59b1554 commit 6c21007

File tree

8 files changed

+161
-27
lines changed

8 files changed

+161
-27
lines changed

main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ def index():
1010

1111

1212
if __name__ == "__main__":
13-
app.run(host="0.0.0.0", port=5000, debug=True)
14-
13+
app.run(host="0.0.0.0", port=5000, debug=True)

static/js/game.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
import Scene from './scene/Scene.js'
22
import CustomPlane from './plane/Plane.js';
3-
import { mixColor } from './scene/Color.js';
4-
3+
import {mixColor} from './scene/Color.js';
54

5+
// global variables
66
var scene;
7-
87
var instruments;
98

10-
9+
// start when loaded
1110
$(document).ready(() => {
1211
scene = new Scene();
1312
setup()
1413
})
1514

16-
1715
let last_time = 0;
1816

17+
/**
18+
* Set up game
19+
*/
1920
function setup() {
2021
// Flight instruments setup
2122
var options = {
2223
width: "18%",
2324
height: 200,
24-
size : 200, // Sets the size in pixels of the indicator (square)
25-
roll : 0, // Roll angle in degrees for an attitude indicator
26-
pitch : 400, // Pitch angle in degrees for an attitude indicator
27-
heading: 0, // Heading angle in degrees for an heading indicator
28-
vario: 0, // Variometer in 1000 feets/min for the variometer indicator
29-
airspeed: 0, // Air speed in knots for an air speed indicator
30-
altitude: 0, // Altitude in feets for an altimeter indicator
31-
pressure: 1000, // Pressure in hPa for an altimeter indicator
32-
showBox : false, // Sets if the outer squared box is visible or not (true or false)
33-
img_directory : '../static/assets/instruments/' // The directory where the images are saved to
25+
size : 200,
26+
roll : 0,
27+
pitch : 400,
28+
heading: 0,
29+
vario: 0,
30+
airspeed: 0,
31+
altitude: 0,
32+
pressure: 1000,
33+
showBox : false,
34+
img_directory : '../static/assets/instruments/'
3435
}
3536

36-
// dict of instruments
37+
// "dict" of instruments
3738
instruments = {
3839
attitude: $.flightIndicator('#attitude', 'attitude', options),
3940
heading: $.flightIndicator('#heading', 'heading', options),
@@ -249,10 +250,12 @@ function setup() {
249250
last_time = performance.now()
250251
render()
251252
}
252-
let counterthing = 1
253253

254+
let counterthing = 1
254255

255-
// render loop
256+
/**
257+
* Render loop
258+
*/
256259
function render() {
257260
// calculate time difference
258261
let now = performance.now()

static/js/physics/atmosphere.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
1+
// useful constants
22
const STANDARD_GRAVITY = 9.80665
33
const RSpecificAir = 287.058;
44
const heatCapacityRatio = 1.4;
55

6+
// Layer data
67
// https://en.wikipedia.org/wiki/International_Standard_Atmosphere
78
const layers = [
89
{
@@ -87,10 +88,19 @@ const layers = [
8788
},
8889
];
8990

91+
/**
92+
* Convert Celsius to Kelvin
93+
* @param {Number} temperature Temperature in celsius
94+
*/
9095
function celsiusToKelvin(temperature) {
9196
return temperature + 273.15;
9297
}
9398

99+
/**
100+
* Calculate temperature given atmospheric layer and altitude
101+
* @param {Object} layer Atmospheric layer data
102+
* @param {Number} height Altitude
103+
*/
94104
function getTemperature(layer, height) {
95105
const bT = celsiusToKelvin(layer.baseTemperature);
96106
const a = layer.lapse / 1000;
@@ -100,6 +110,11 @@ function getTemperature(layer, height) {
100110
return temperature;
101111
}
102112

113+
/**
114+
* Calculate pressure of layer with no lapse rate
115+
* @param {Object} layer Atmospheric layer data
116+
* @param {Number} height Altitude
117+
*/
103118
function getPressureNoLapse(layer, height) {
104119
// https://en.wikipedia.org/wiki/Barometric_formula
105120
const l = layer;
@@ -109,6 +124,11 @@ function getPressureNoLapse(layer, height) {
109124
return pressure;
110125
}
111126

127+
/**
128+
* Calculate pressure
129+
* @param {Object} layer Atmospheric layer data
130+
* @param {Number} temperature temperature in celsius
131+
*/
112132
function getPressure(layer, temperature) {
113133
const l = layer;
114134
const lapse = l.lapse / 1000;
@@ -122,6 +142,10 @@ function getPressure(layer, temperature) {
122142
return pressure;
123143
}
124144

145+
/**
146+
* Get atmospheric layer data
147+
* @param {Number} height Altitude (m)
148+
*/
125149
function getLayer(height) {
126150
// find atmospheric layer
127151
for (let i = 1; i < layers.length; i++) {
@@ -132,10 +156,18 @@ function getLayer(height) {
132156
return null;
133157
}
134158

159+
/**
160+
* Calculate speed of sound in air
161+
* @param {Number} temperature Temperature in Kelvin
162+
*/
135163
function speedOfSound(temperature) {
136164
return Math.sqrt(heatCapacityRatio * RSpecificAir * temperature);
137165
}
138166

167+
/**
168+
* Get the atmospheric data for a given altitude using ISA
169+
* @param {Number} altitude altitude (m)
170+
*/
139171
export function ISA(altitude) {
140172
if (altitude > 86000 || altitude < -611) {
141173
return {

static/js/plane/Plane.js

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ import { Vec2, Vector } from "./Vec2.js";
22
import GraphicsVector from "../scene/GraphicsVector.js";
33
import { ISA } from "../physics/atmosphere.js";
44

5-
5+
/**
6+
* A physical object with position, mass, and rotation.
7+
*/
68
class physicalObject {
9+
/**
10+
*
11+
* @param {Number} x The x value (m)
12+
* @param {Number} y The y value (m)
13+
* @param {Number} mass The mass (kg)
14+
*/
715
constructor(x, y, mass) {
816
this.pos = new Vec2(x, y);
917
this.mass = mass;
@@ -19,20 +27,33 @@ class physicalObject {
1927
this.momentOfInertia = 100;
2028
}
2129

30+
/**
31+
* Add a force for next update
32+
* @param {Number} force The force to add in Newton
33+
*/
2234
addForce(force) {
2335
this.force.addInPlace(force);
2436
}
2537

38+
/**
39+
* Add a torque for next update
40+
* @param {Number} torque The torque in Nm
41+
*/
2642
addTorque(torque) {
2743
this.torque += torque
2844
}
2945

46+
/**
47+
* Update next timestep
48+
* @param {Number} dt Timestep
49+
*/
3050
update(dt) {
3151
// velocity verlet
3252
const newAcc = this.force.divide(this.mass);
53+
this.pos.addInPlace( Vector.add(this.vel.multiply(dt), this.acc.multiply(0.5 * Math.pow(dt,2))) )
54+
this.vel.addInPlace(Vector.add(this.acc, newAcc).multiply(0.5 * dt));
55+
3356
this.acc = newAcc;
34-
this.pos.addInPlace( Vector.add(this.vel.multiply(dt), newAcc.multiply(0.5 * Math.pow(dt,2))) )
35-
this.vel.addInPlace(newAcc.multiply(dt));
3657

3758
// reset force
3859
this.force.x = 0;
@@ -50,7 +71,17 @@ class physicalObject {
5071

5172
}
5273

74+
/**
75+
* An interactive plane with custom parameters
76+
*/
5377
export default class CustomPlane {
78+
/**
79+
*
80+
* @param {Number} x The x value (m)
81+
* @param {Number} y The y value (m)
82+
* @param {Object} keys An updating object with the currently pressed keys keys. (key=keycode, val=bool)
83+
* @param {Object} instruments An object containing the flightIndicator instruments.
84+
*/
5485
constructor(x, y, keys, instruments) {
5586
// var texture = PIXI.Texture.fromImage('static/assets/sprites/A220.png');
5687
var texture = PIXI.Texture.fromImage('../static/assets/sprites/A220.png')
@@ -113,10 +144,18 @@ export default class CustomPlane {
113144
}
114145
}
115146

147+
/**
148+
* Get the thrust fraction (thrust/max thrust)
149+
* @returns {Number}
150+
*/
116151
getThrustFraction() {
117152
return this.thrust / this.maxThrust
118153
}
119154

155+
/**
156+
* Update physical parameters after timestep
157+
* @param {Number} dt The timestep
158+
*/
120159
update(dt) {
121160
if (this.keys[38]) {
122161
this.thrust += this.maxThrust / 100;
@@ -271,6 +310,10 @@ export default class CustomPlane {
271310

272311
}
273312

313+
/**
314+
* Show or hide the force/velocity vectors
315+
* @param {Boolean} setting Show the vector
316+
*/
274317
showVector(setting) {
275318
for (var i = 0; i < this.vectorList.length; i++) {
276319
this.vectorList[i].setVisibility(setting)

static/js/scene/Camera.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Class representing a camera with pan and zoom
3+
*/
14
export default class Camera extends PIXI.Container {
25
constructor(renderer, x = 0, y = 0, zoom = 1) {
36
super();
@@ -8,18 +11,30 @@ export default class Camera extends PIXI.Container {
811
this.update();
912
}
1013

14+
/**
15+
* Set camera position
16+
* @param {Number} x The x position
17+
* @param {*} y The y position
18+
*/
1119
setPos(x, y) {
1220
this.posx = x;
1321
this.posy = y;
1422
this.update();
1523
}
1624

25+
/**
26+
* Set camera zoom
27+
* @param {Number} zoom Zoom amount
28+
*/
1729
setZoom(zoom) {
1830
this.zoom = zoom;
1931
cam.scale.x = cam.scale.y = zoom;
2032
this.update()
2133
}
2234

35+
/**
36+
* Update camera position
37+
*/
2338
update() {
2439
this.x = this.zoom * this.posx + this.renderer.view.width / 2;
2540
this.y = this.zoom * this.posy + this.renderer.view.height / 2;

static/js/scene/Color.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
1+
/**
2+
* Blend together two colors
3+
* @param {Number[]} A First color represented by an array with three values (0-255)
4+
* @param {Number[]} B Second color represented by an array with three values (0-255)
5+
* @param {Number} amount Blend ratio 0-1
6+
* @returns {Number[]} New blended color represented by array with three values (0-1)
7+
*/
28
export function mixColor(A, B, amount) {
39
var newColor = []
410
for (var i = 0; i < 3; i++) {

static/js/scene/GraphicsVector.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* A class representing a graphical vector arrow
3+
*/
14
export default class GraphicsVector extends PIXI.Graphics {
25
constructor(vec, scaling = 1, color=0xff0000) {
36
super();
@@ -11,18 +14,32 @@ export default class GraphicsVector extends PIXI.Graphics {
1114
this.lineTo(vec.x * this.scaling, vec.y * this.scaling)
1215
}
1316

17+
/**
18+
* Show the vector
19+
*/
1420
show() {
1521
this.show = true;
1622
}
1723

24+
/**
25+
* Hide the vector
26+
*/
1827
hide() {
1928
this.show = false;
2029
}
2130

31+
/**
32+
* Show/hide the vector
33+
* @param {Boolean} setting Show vector
34+
*/
2235
setVisibility(setting) {
2336
this.show = setting
2437
}
2538

39+
/**
40+
* Update vector values
41+
* @param {Vec2} vec 2d Vector
42+
*/
2643
update(vec) {
2744
this.clear()
2845

0 commit comments

Comments
 (0)