
var canvas=document.getElementById("mcanvas");

var ctx=canvas.getContext("2d");

var Text=function(x,y,texto,cor,font)
{
	this.x=x;
	this.y=y;
	this.cor=cor;
	this.texto=texto;
	
	if (font==null)
		this.font="8pt arial";
	else this.font=font;
	
	this.Draw=function(ctx)
	{
		ctx.font = this.font;
        ctx.fillStyle = this.cor;
        ctx.fillText(this.texto, this.x, this.y);
        
	}
};

var DPixel=function(x,y,cor)
{
	this.x=x;
	this.y=y;
	this.cor=cor;
	
	this.Draw=function(ctx)
	{
		ctx.beginPath();
        ctx.moveTo(this.x, this.y);
        ctx.lineTo(this.x, this.y);
        ctx.lineWidth = 1;

        ctx.strokeStyle = this.cor;
        ctx.stroke();
	}
};

var DLine=function(x,y,xf,yf,cor,g)
{
	this.x=x;
	this.y=y;
	this.yf=yf;
	this.xf=xf;
	this.cor=cor;
	
	
	this.Draw=function(ctx)
	{
		ctx.beginPath();
        ctx.moveTo(this.x, this.y);
        ctx.lineTo(this.xf, this.yf);
        ctx.lineWidth = g;

        ctx.strokeStyle = this.cor;
        ctx.stroke();
        
	}
	
	
};

var grelha=function(x,y,w,h,nl,nc,cor)
{
	this.x=x;
	this.y=y;
	this.w=w;
	this.h=h;
	this.nl=nl;
	this.nc=nc;
	this.cor=cor;
	
	if (w<x)
		w=x;
	if (h<y)
		h=y;
	
	this.xf=w-x;
	this.yf=h-y;
	
	
	
	if (nc>w)
		this.nc=w;
	if (nl>h)
		this.nl=h;
	
	this.stepx=w/nc;
	this.stepy=h/nl;
	
	
	this.update=function(x,y,w,h,nl,nc,cor)
	{
		this.x=x;
		this.y=y;
		this.w=w;
		this.h=h;
		this.nl=nl;
		this.nc=nc;
		this.cor=cor;
	
		if (w<x)
			w=x;
		if (h<y)
			h=y;
	
		this.xf=w-x;
		this.yf=h-y;
	
	
	
		if (nc>w)
			this.nc=w;
		if (nl>h)
			this.nl=h;
	
		this.stepx=w/nc;
		this.stepy=h/nl;
	};
	
	
	this.DrawSingle=function(ctx,x,y,xf,yf)
	{
		ctx.beginPath();
        ctx.moveTo(x,y);
        ctx.lineTo(xf, yf);
        ctx.lineWidth = 1;
		ctx.strokeStyle = this.cor;
        ctx.stroke();
        
	};
	
	this.Draw=function(ctx)
	{
		this.DrawSingle(ctx,this.x,this.y,this.xf,this.y);
		this.DrawSingle(ctx,this.xf,this.y,this.xf,this.yf);
		this.DrawSingle(ctx,this.x,this.yf,this.xf,this.yf);
		this.DrawSingle(ctx,this.x,this.y,this.x,this.yf);
		for (i=0; i<this.nl; i++)
		{
			this.DrawSingle(ctx,this.x,this.y+(i*this.stepy),this.xf,this.y+(i*this.stepy));
		}
		for (j=0; j<this.nc; j++)
		{
			this.DrawSingle(ctx,this.x+(j*this.stepx),this.y,this.x+(j*this.stepx),this.yf);
		}
	}
};

var DCircle=function(x,y,raio,cor,fillcolor)
{
	this.x=x;
	this.y=y;
	this.raio=raio;
	this.cor=cor;
	
	
	this.Draw=function(ctx)
	{
		ctx.beginPath();
        ctx.arc(this.x, this.y, this.raio, 0, 2 * Math.PI, false);
        
        
        if (fillcolor!=null)
        {
        	ctx.fillStyle = fillcolor;
        	ctx.fill();
        }
        
        ctx.lineWidth = 1;
        ctx.strokeStyle = this.cor;
        ctx.stroke();
        
	}
};

var DRect=function(x,y,w,h,cor,fillcolor)
{
	this.x=x;
	this.y=y;
	this.w=w;
	this.h=h;
	this.cor=cor;
	this.fillcolor=fillcolor;
	
	this.Draw=function(ctx)
	{
		
		ctx.beginPath();
		ctx.rect(this.x, this.y,this.w,this.h);
		ctx.fillStyle = this.fillcolor;
		ctx.fill();
		ctx.lineWidth = 1;
		ctx.strokeStyle = this.cor;
		
	}
};

Array.prototype.Apagar=function(elem,todos)
{
	for (i=0; i<this.length; i++)
	{
		if (this[i]==elem)
		{
			this.splice(i,1);
			
			if (todos==null || !todos)
				break;
			else i--;
		}
	}
};

Array.prototype.ApagarPos=function(pos)
{
	this.splice(pos,1);
};

Array.prototype.ApagarTodos=function()
{
	this.length=0;
};

Array.prototype.InserirPos=function(pos,ele)
{
	var arr1=this.slice(0,pos);
	var arr2=this.slice(pos);
	this.ApagarTodos();
	for (i=0; i<arr1.length; i++)
	{
		this.push(arr1[i]);
	}
	this.push(ele);
	
	for (j=0; j<arr2.length; j++)
	{
		this.push(arr2[j]);
	}
	
}

Array.prototype.Pertence=function(ele)
{
	
	for (i=0; i<this.length; i++)
	{
		if (this[i]==ele)
			return true;
	}
	return false;
}

Array.prototype.Ocorrencia=function(ele)
{
	var n=0;
	for (i=0; i<this.length; i++)
	{
		if (this[i]==ele)
			n++;
	}
	return n;
}


var Color=function(r,g,b,a)
{
	this.r=r;
	this.g=g;
	this.b=b;
	this.a=a;
	
	this.Set=function()
	{
		return "rgba("+this.r+","+this.g+","+this.b+","+this.a+")";
	};
};

var Rectangulo=function(x,y,w,h)
{
	
	this.x=x;
	this.y=y;
	this.w=w;
	this.h=h;
	this.cor=new Color(0,0,0,1);
	
	this.intercepta=function(obj)
	{
		var offset=0;
		
		
		if (this.pertence(obj.x-offset,obj.y-offset) || 
			this.pertence(obj.x+obj.w-offset,obj.y-offset) ||
			this.pertence(obj.x-offset,obj.y+obj.h-offset) ||
			this.pertence(obj.x+obj.w-offset,obj.y+obj.h-offset))
		{
			return true;
		}
		else if (obj.pertence(this.x-offset,this.y-offset) || 
			obj.pertence(this.x+this.w-offset,this.y-offset) ||
			obj.pertence(this.x-offset,this.y+this.h-offset) ||
			obj.pertence(this.x+this.w-offset,this.y+this.h-offset))
		{
			return true;
		}
		else
			return false;
	};
	
	this.pertence=function(x,y)
	{
		if (x>=this.x && x<=this.x+this.w && y>=this.y && y<=this.y+this.h)
		{
			return true;
		}
		else
			return false;
	};
	
	this.Draw=function(ctx)
	{
		
		
		
		ctx.fillStyle = this.cor.Set();
		ctx.fillRect(this.x, this.y,this.w,this.h);
		
		
		
	};
	
	
};

var Vector2=function(x,y)
{
	this.xant=0;
	this.yant=0;
	this.x=x;
	this.y=y;
	
	this.Set=function(x,y)
	{
		this.xant=this.x;
		this.yant=this.y;
		this.x=x;
		this.y=y;
		
	};
	
	this.normaliza=function(x,y)
	{
		var v=new Vector2(x,y);
		var norma=Math.sqrt((x*x)+(y*y));
		v.x=x/norma;
		v.y=y/norma;
		return v;
	};
	
	this.move=function(vector)
	{
		this.x+=vector.x;
		this.y+=vector.y;
	};
	
	this.distancia=function(vector)
	{
		var dist=0;
		if (vector!=null)
		{
			var dx=vector.x-this.x;
			var dy=vector.y-this.y;
		
			var dist=Math.sqrt((dx*dx)+(dy*dy));
		}
		else
		{
			var dx=this.x-this.xant;
			var dy=this.y-this.yant;
		
			var dist=Math.sqrt((dx*dx)+(dy*dy));
		}
		
		return dist;
	};
	
	this.mudou=function()
	{
		if (this.x!=this.xant || this.y!=this.yant)
			return true;
		else
			return false;
	};
	
	this.diferenca=function(vector,inverte)
	{
		var inv=1;
		
		if (inverte)
			inv=-1;
			
		if (vector==null)
			return new Vector2((this.x-this.xant)*inv,(this.y-this.yant)*inv);
		else
			return new Vector2((this.x-vector.xant)*inv,(this.y-vector.yant)*inv);
	};
	
};

var Animacao=function(w,h,row,column,limite,imgsrc,fps,columns,rows)
{
	if (fps==null || fps>=33)
		this.fps=1;
	else
		this.fps=33/fps;
	
	this.fpsCounter=0;
	this.frame=0;
	this.w=w;
	this.h=h;
	this.rowstart=row;
	this.columnstart=column;
	this.row=row;
	this.column=column;
	this.rows=rows;
	this.columns=columns;
	
	if (limite==null || limite==0)
		this.limite=9999999999;
	else
		this.limite=limite-1;
	this.limiteCount=0;
	this.image=new Image();
	this.image.src=imgsrc;
	this.position=new Vector2(0,0);
	this.cropPosition=new Vector2(0,0);
	
	
	
	this.setLimit=function(limite)
	{
		this.limite=limite-1;
	};
	
	this.setRow=function(num)
	{
		this.row=num;
		this.rowstart=num;
		this.cropPosition.x=this.column*this.w;
		this.cropPosition.y=this.row*this.h;
	};
	
	this.setColumn=function(num)
	{
		this.column=num;
		this.columnstart=num;
		this.cropPosition.x=this.column*this.w;
		this.cropPosition.y=this.row*this.h;
	};
	
	this.update=function()
	{
		
		
		
		this.cropPosition.x=this.column*this.w;
		this.cropPosition.y=this.row*this.h;
		
		if (this.columns==null || this.columns==0)
		{
			this.columns=this.image.width/this.w;
			
		}
		if (this.rows==null || this.rows==0)
		{
			
			this.rows=this.image.height/this.h;
		}
	};
	
	this.Draw=function(ctx)
	{
		if (this.fpsCounter==0)
		{
			if (this.limiteCount<this.limite)
			{
				this.limiteCount++;
				this.column++;
				if (this.column>=this.columns)
				{
					this.row++;
					this.column=0;
					if (this.row>=this.rows)
					{
						this.row=this.rowstart;
						this.columnstart=this.columnstart;
						this.limiteCount=0;
					}
				}
			}
			else
			{
				this.row=this.rowstart;
				this.columnstart=this.columnstart;
				this.limiteCount=0;
			}
		}
		
		ctx.drawImage(this.image,this.cropPosition.x,this.cropPosition.y,this.w,this.h,this.position.x,this.position.y,this.w,this.h);
		
		this.fpsCounter++;
		if (this.fpsCounter>this.fps)
			this.fpsCounter=0;
	};
};

var Inputs=function()
{
	this.a=false;
	this.b=false;
	this.c=false;
	this.d=false;
	this.e=false;
	this.f=false;
	this.g=false;
	this.h=false;
	this.i=false;
	this.j=false;
	this.k=false;
	this.l=false;
	this.m=false;
	this.n=false;
	this.o=false;
	this.p=false;
	this.k=false;
	this.q=false;
	this.r=false;
	this.s=false;
	this.t=false;
	this.u=false;
	this.v=false;
	this.w=false;
	this.x=false;
	this.y=false;
	this.z=false;
	this.left=false;
	this.right=false;
	this.down=false;
	this.enter=false;
	this.space=false;
	this.mouseIsdown=false;
	this.mouseIsdownAndMoving=false;
	this.mousePos=new Vector2(0,0);
	this.offset=new Vector2(0,0);
	this.clamp=new Vector2(0,0);
	
	
};

var input=new Inputs();

function getMousePos(canvas,e)
{
    var rect=canvas.getBoundingClientRect();
    return { x:e.clientX-rect.left, y:e.clientY-rect.top };
}

canvas.onmousemove=function(e)
{
    input.mousePos.x=getMousePos(canvas,e).x;
    input.mousePos.y=getMousePos(canvas,e).y;
    input.mouseIsMoving=true;
	
	
};

canvas.onmousedown=function(e)
{
	
	
    input.mouseIsdown=true;
    input.mousePos.x=getMousePos(canvas,e).x;
    input.mousePos.y=getMousePos(canvas,e).y;
};

document.documentElement.onmouseup=function(e)
{
	
    input.mouseIsdown=false;
	
    input.mouseIsMoving=false;
};

document.documentElement.onkeydown=function(e)
{
    var codigo;
    if (window.event)
        codigo=window.event.keyCode;
    else if (e)
        codigo=e.which;
		
    switch (codigo)
    {
        case 13 :
            input.enter=true;
            break;
        case 32 :
            input.space=true;
            break;
        case 37 :
            input.left=true;
            break;
        case 38 :
            input.up=true;
            break;
        case 39 :
            input.right=true;
            break;
        case 40 :
            input.down=true;
            break;
        case 65 :
            input.a=true;
            break;
        case 66 :
            input.b=true;
            break;
        case 67 :
            input.c=true;
            break;
        case 68 :
            input.d=true;
            break;
        case 69 :
            input.e=true;
            break;
        case 70 :
            input.f=true;
            break;
        case 71 :
            input.g=true;
            break;
        case 72 :
            input.h=true;
            break;
        case 73 :
            input.i=true;
            break;
        case 74:
            input.j=true;
            break;
        case 75:
            input.k=true;
            break;
        case 76:
            input.l=true;
            break;
        case 77:
            input.m=true;
            break;
        case 78:
            input.n=true;
            break;
        case 79:
            input.o=true;
            break;
        case 80:
            input.p=true;
            break;
        case 81:
            input.q=true;
            break;
        case 82:
            input.r=true;
            break;
        case 83:
            input.s=true;
            break;
        case 84:
            input.t=true;
            break;
        case 85:
            input.u=true;
            break;
        case 86:
            input.v=true;
            break;
        case 87:
            input.w=true;
            break;
        case 88:
            input.x=true;
            break;
        case 89:
            input.y=true;
            break;
        case 90:
            input.z=true;
            break;
    }
};

document.documentElement.onkeyup=function(e)
{
    var codigo;
    if (window.event)
        codigo=window.event.keyCode;
    else if (e)
        codigo=e.which;
		
    switch (codigo)
    {
        case 13 :
            input.enter=false;
            break;
        case 32 :
            input.space=false;
            break;
        case 37 :
            input.left=false;
            break;
        case 38 :
            input.up=false;
            break;
        case 39 :
            input.right=false;
            break;
        case 40 :
            input.down=false;
            break;
        case 65 :
            input.a=false;
            break;
        case 66 :
            input.b=false;
            break;
        case 67 :
            input.c=false;
            break;
        case 68 :
            input.d=false;
            break;
        case 69 :
            input.e=false;
            break;
        case 70 :
            input.f=false;
            break;
        case 71 :
            input.g=false;
            break;
        case 72 :
            input.h=false;
            break;
        case 73 :
            input.i=false;
            break;
        case 74:
            input.j=false;
            break;
        case 75:
            input.k=false;
            break;
        case 76:
            input.l=false;
            break;
        case 77:
            input.m=false;
            break;
        case 78:
            input.n=false;
            break;
        case 79:
            input.o=false;
            break;
        case 80:
            input.p=false;
            break;
        case 81:
            input.q=false;
            break;
        case 82:
            input.r=false;
            break;
        case 83:
            input.s=false;
            break;
        case 84:
            input.t=false;
            break;
        case 85:
            input.u=false;
            break;
        case 86:
            input.v=false;
            break;
        case 87:
            input.w=false;
            break;
        case 88:
            input.x=false;
            break;
        case 89:
            input.y=false;
            break;
        case 90:
            input.z=false;
            break;
    }
};





