-
Notifications
You must be signed in to change notification settings - Fork 1
/
sketch.html
137 lines (113 loc) · 2.87 KB
/
sketch.html
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
<!DOCTYPE html>
<head>
<!-- <meta http-equiv="refresh" content="30">-->
<meta charset="utf-8">
</head>
<body>
<section id="window">
HUD
</section>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.14/p5.js"></script>
<script src="unity.js"></script>
<script src="ship.js"></script>
<script src="keyControls.js"></script>
<script src="prop.js"></script>
<script src="HUD.js"></script>
<script src="Force.js"></script>
<script>
// ---------
function displayGrid(){
fill(100);
stroke(100);
for(var i=0; i<height; i+= 50){
line(0, i, width, i);
}
for(var j=0; j<width; j+=50){
line(j, 0, j, height);
}
}
var SCALE = 50;
var obj;
var forcaAntiHorario, forcaHorario,
forcaParaFrente, forcaParaTras,
forcaLateralEsq, forcaLateralDir;
var forcas = [];
var parcial;
// center the camera and the ship in the middle of the screen
var CENTER_CAMERA = true;
function setup(){
// size(800, 600);
createCanvas(800, 600);
// obj 'ship' e a posição inicial
obj = new Ship(width/2, height/2);
// adiciona as diversas unidades ao obj
// linha de comprimento 5
for(var i=0; i<5; i++){
obj.addUnity( new Unity(i * 20, 0, 20) );
}
// coluna de comprimento 4
for(var i=1; i<5; i++){
obj.addUnity( new Unity(2 * 20, i * 20, 20) );
}
// propulsores e localização
var soft_propellers = [
// pos.x, pos.y, rotate
[obj.cmX+20, 20*4, -PI/2],
[obj.cmX-20, 20*4, PI/2],
[obj.cmX, -20, PI]
];
soft_propellers.forEach( function(pair){
let prop_control = new Prop_Control( pair[0], pair[1]);
prop_control.force.rotate(pair[2]);
obj.addUnity( prop_control );
});
var strong_propellers = [
[obj.cmX, 20*5, 0]
]
strong_propellers.forEach( function(pair){
let engine = new Main_Engine( pair[0], pair[1]);
engine.force.rotate(pair[2]);
obj.addUnity( engine );
});
// force = constructor(x, y, fx fy){
forcas.push( forcaAntiHorario = new Force(4 * 20, 20, 0, -5));
forcas.push( forcaHorario = new Force(0 * 20, 20, 0, -5));
var empuxo = 10;
forcas.push( forcaParaFrente = new Force( obj.cmX, 5*20, 0, -empuxo*2) )
forcas.push( forcaParaTras = new Force( obj.cmX, -20, 0, empuxo) );
forcas.push( forcaLateralEsq = new Force( obj.cmX, obj.cmY, empuxo, 0) );
forcas.push( forcaLateralDir = new Force( obj.cmX, obj.cmY, -empuxo, 0) );
frameRate(30);
}
var STOP = false;
function draw(){
if(!STOP){
background(200);
if (CENTER_CAMERA){
push();
translate(width/2 - obj.pos.x, height/2-obj.pos.y);
displayGrid();
}
action(obj);
atualizaForca();
obj.update();
obj.display();
if (CENTER_CAMERA){
pop();
}
}
}
function pause(){
STOP = !STOP;
}
// arrumar depois.
function atualizaForca(){
forcas.forEach( function(f){
if(f.thrust){
obj.applyForce(f);
}
});
}
</script>
</body>
</html>