-
Notifications
You must be signed in to change notification settings - Fork 0
/
funciones.js
94 lines (78 loc) · 3.92 KB
/
funciones.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
// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
//stats
function Stats(){var l=Date.now(),m=l,g=0,n=Infinity,o=0,h=0,p=Infinity,q=0,r=0,s=0,f=document.createElement("div");f.id="stats";f.addEventListener("mousedown",function(b){b.preventDefault();t(++s%2)},!1);f.style.cssText="width:80px;opacity:0.9;cursor:pointer";var a=document.createElement("div");a.id="fps";a.style.cssText="padding:0 0 3px 3px;text-align:left;background-color:#002";f.appendChild(a);var i=document.createElement("div");i.id="fpsText";i.style.cssText="color:#0ff;font-family:Helvetica,Arial,sans-serif;font-size:9px;font-weight:bold;line-height:15px";
i.innerHTML="FPS";a.appendChild(i);var c=document.createElement("div");c.id="fpsGraph";c.style.cssText="position:relative;width:74px;height:30px;background-color:#0ff";for(a.appendChild(c);74>c.children.length;){var j=document.createElement("span");j.style.cssText="width:1px;height:30px;float:left;background-color:#113";c.appendChild(j)}var d=document.createElement("div");d.id="ms";d.style.cssText="padding:0 0 3px 3px;text-align:left;background-color:#020;display:none";f.appendChild(d);var k=document.createElement("div");
k.id="msText";k.style.cssText="color:#0f0;font-family:Helvetica,Arial,sans-serif;font-size:9px;font-weight:bold;line-height:15px";k.innerHTML="MS";d.appendChild(k);var e=document.createElement("div");e.id="msGraph";e.style.cssText="position:relative;width:74px;height:30px;background-color:#0f0";for(d.appendChild(e);74>e.children.length;)j=document.createElement("span"),j.style.cssText="width:1px;height:30px;float:left;background-color:#131",e.appendChild(j);var t=function(b){s=b;switch(s){case 0:a.style.display=
"block";d.style.display="none";break;case 1:a.style.display="none",d.style.display="block"}};return{REVISION:11,domElement:f,setMode:t,begin:function(){l=Date.now()},end:function(){var b=Date.now();g=b-l;n=Math.min(n,g);o=Math.max(o,g);k.textContent=g+" MS ("+n+"-"+o+")";var a=Math.min(30,30-30*(g/200));e.appendChild(e.firstChild).style.height=a+"px";r++;b>m+1E3&&(h=Math.round(1E3*r/(b-m)),p=Math.min(p,h),q=Math.max(q,h),i.textContent=h+" FPS ("+p+"-"+q+")",a=Math.min(30,30-30*(h/100)),c.appendChild(c.firstChild).style.height=
a+"px",m=b,r=0);return b},update:function(){l=this.end()}}};
//posició
function Pos(x, y) {
this.x = x;
this.y = y;
}
Pos.prototype.getIndex = function() {
return "(" + this.x + " , "+this.y+")";
}
Pos.prototype.toString = function() {
return this.getIndex(x, y);
}
Pos.prototype.clone = function() {
return new Pos(this.x, this.y);
}
//cámara
function Camara(x, y) {
this.pos = new Pos(x||0, y||0);
this.actualPos = this.pos.clone();
}
Camara.prototype = {
logic : function(dt) {
this.pos.x += (this.actualPos.x - this.pos.x)/(dt);
this.pos.y += (this.actualPos.y - this.pos.y)/(dt);
},
focus : function(ctx) {
ctx.translate(-(this.pos.x-canvas.width/2),
-(this.pos.y-canvas.height/2));
}
}
//Cell
var CELL_EDGE = 50;
function Cell () {
this.color = this.cellColors[Math.floor
(Math.random()*this.cellColors.length)];
this.genBuffer();
}
Cell.prototype = {
playerId : undefined,
cellColors : ["#524323", "#F04323", "#1DC200"],
color : undefined
}
Cell.prototype.genBuffer = function (ctx) {
var buffer = document.createElement('canvas');
buffer.width = CELL_EDGE;
buffer.height = CELL_EDGE;
var bufferCtx = buffer.getContext('2d');
bufferCtx.fillStyle = this.color;
bufferCtx.fillRect(0,0,CELL_EDGE,CELL_EDGE);
this.buff = buffer;
}
// mapa
function Map() {
this.cells = {};
}
Map.prototype.getCellAtPos = function(pos) {
var index = pos.getIndex();
var cell = this.cells[index];
if(cell === undefined) {
cell = new Cell();
this.cells[index] = cell;
}
return cell;
}