-
Notifications
You must be signed in to change notification settings - Fork 0
/
angry4.html
174 lines (146 loc) · 3.62 KB
/
angry4.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
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
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="engine/math.js"></script>
<script type="text/javascript" src="engine/body.js"></script>
<script type="text/javascript" src="engine/world.js"></script>
<script type="text/javascript" src="engine/collision.js"></script>
<script type="text/javascript" src="engine/contact.js"></script>
<script type="text/javascript" src="engine/constraint.js"></script>
<script type="text/javascript">
var img_bird = new Image();
var img_bird2 = new Image();
var img_pig = new Image();
var img_canion = new Image();
var img_canion_base = new Image();
img_bird.src = "bird.png";
img_bird2.src = "bird2.png";
img_pig.src = "pig.png";
img_canion.src = "canion2.png";
img_canion_base.src = "canion1.png";
function loadscene()
{
// piso
var p = _world.AddBox(OX+DX/2,OY+DY,DX,2);
p.SetStatic();
// cañon
canion = _world.AddBox(OX+5,OY+DY-5,7,2);
canion.SetOrient(-0.5);
canion.SetStatic();
canion.img = img_canion;
base_canion = _world.AddBox(OX+6,OY+DY-4,12,5);
base_canion.SetStatic();
base_canion.img = img_canion_base;
// pilares
p = _world.AddBox(OX+50,OY+DY-5,3,10);
p = _world.AddBox(OX+60,OY+DY-5,3,10);
p = _world.AddBox(OX+55,OY+DY-12,14,2);
// chanchito
p = _world.AddCircle(OX+55,OY+DY-14,2);
p.img = img_pig;
init = true;
}
function doKeyDown(e)
{
e = e || window.event;
if(e.keyCode==40)
{
// "abajo"
canion.SetOrient( canion.orient + 0.05);
}
else
if(e.keyCode==38)
{
// "arriba"
canion.SetOrient( canion.orient - 0.05);
}
else
if(e.keyCode==39)
{
// "derecha"
potencia+=10;
}
else
if(e.keyCode==37)
{
// "izquierda"
potencia-=10;
}else {
if(e.keyCode==32) //barra espaciadora
{
// espacio: dispara un pajaro
// el pajaro sale desde la punta del cañon
var x = canion.position.x + Math.cos(canion.orient)*10;
var y = canion.position.y + Math.sin(canion.orient)*10;
var p = _world.AddCircle(x,y,2);
p.img = img_bird;
//bird2
//var p = _world.AddTri(x, y, x+3, y-6, x+6, y);
//p.img = img_bird2;
//p.SetOrient( -canion.orient + 0.15);
// y su velocidad depende de la potencia
var k = potencia/100 * 40;
p.velocity.x = Math.cos(canion.orient)* k;
p.velocity.y = Math.sin(canion.orient)* k;
}
}
}
var elapsed_time = 1/60;
var canvas;
var ctx;
var ex = 10;
var ey = 10;
var ox = 20;
var oy = 20;
// pos y tama�o de pantalla
var OX = 1;
var OY = 1;
var DX = 90;
var DY = 50;
// posicion del mouse
var mouse_x = 0;
var mouse_y = 0;
var gravity = new Vector2( 0, 40 );
var fixed_dt = 1.0/60.0; // avance del tiempo constante
var _world = new World();
var init = false;
var canion = null;
var potencia = 100;
function Update()
{
_world.Update(elapsed_time);
}
function Render()
{
if (canvas.getContext)
{
// borro la pantalla
ctx.fillStyle = 'rgba(0,0,0,255)';
ctx.fillRect(0,0,2000,2000);
// dibujo la escena esquematica con el motor
_world.Render(ctx);
ctx.font = "16px Arial";
ctx.fillText("potencia = "+ potencia.toFixed(0) , 10, 20);
}
}
function RenderLoop()
{
if (!init)
return;
Update();
Render();
}
function main()
{
document.addEventListener( "keydown", doKeyDown, true);
canvas = document.getElementById('mycanvas');
ctx = canvas.getContext('2d');
loadscene();
setInterval(RenderLoop, elapsed_time * 1000);
}
</script>
</head>
<body onload="main();">
<canvas id="mycanvas" width="900" height="600"></canvas>
</body>
</html>