<title>模拟服务端攻击算法。。。汗。。 solq</title>
<body>
<style>
#target1,#target2,.s{
color:red;
position:absolute;
}
#target1{
top:330px;
left:500px;
}
</style>
<div id="target1">*1</div>
<div id="target2">*2</div>
</body>
<script>
/*
当两人物的距离在范围内 并且
被攻击的目标要在 攻击的技能 角度 范围 内,就当碰撞成功。。。。
blog:cnblogs.com/solq
*/
var angle=350; //当前攻击玩家角度 如果=60 攻击目标 30(60-angleRange/2)~90(60+angleRange/2) 角度内
var angleRange=60; //角度范围
var radius=150; //半径 c = 三角形斜边 其实是两物体的距离
window.onload=function()
{
document.onmousedown=function(e){
//console.log($$("target2"))
$$("target2").style.left=e.clientX;
$$("target2").style.top=e.clientY;
//alert(" clientx:"+e.clientX+" clienty:"+e.clientY + " layerX:"+e.layerX+" layery:"+e.layerY)
check();
}
}
////////////////////////////////////////////////////////////////////
function check()
{
var x1,x2,y1,y2,a,b,c;
x1=getOffset($$("target1")).left;
x2=getOffset($$("target2")).left;
y1=getOffset($$("target1")).top;
y2=getOffset($$("target2")).top;
a=x2-x1;
b=y2-y1;
c=getC(a,b);
/*
cosC = (a^2 + b^2 - c^2) / (2·a·b)
cosB = (a^2 + c^2 -b^2) / (2·a·c)
cosA = (c^2 + b^2 - a^2) / (2·b·c)
*/
var t=getAngle(a,c);
//alert( a + " " + b);
if( (a<0 && b<0 ) || (a>0 && b<0))
{
}else //if(a>0 && b>0) //修正在攻击玩家背后。。的角度
{
t=360-(t+180);
}
//alert(" 角 : " +( t+90) + " a边:" + a + " b边:" + b )
if(!checkRadius(c)) //checkRadius(c) &&
{
alert("距离太远");
}
if(!checkAngle(t+90))
{
alert(t+90 + "不在范围内")
}
}
function checkRadius(c) //检测距离
{
if(c>radius)
{
return false;
}
return true;
}
function reviseAngle(angle) //修正角度
{
if(angle<0)
angle+=360;
else if(angle > 360)
angle-=360;
return angle;
}
function checkAngle(a)
{
var angleA=angle-angleRange/2; //范围 a
var angleB=angle+angleRange/2; //范围 b
if(angleA<0 || angleB>360) //特殊的角度处理
{
angleA=reviseAngle(angleA);
angleB=reviseAngle(angleB);
if(a>=180 && a>=angleA)
return true;
else if(a<=180 && a<=angleB)
return true;
else
return false;
}
//alert("angleA:" + angleA + " angleb : " + angleB + "a: " + a );
if(a>=angleA && a<=angleB)
{
return true;
}
return false;
}
function getAngle(a,c)
{
var pai=2*Math.asin(1);
return 180*Math.asin(a/c)/pai;
}
function getC(a,b)
{
return Math.sqrt(a*a+b*b);
}
function getY(x)
{
var y= Math.sqrt(radius*radius-x*x);
return y;
}
//js 处理函数
/*
function init()
{
var angleA=angle-angleRange/2; //范围 a
var angleB=angle+angleRange/2; //范围 b
var d1=createDom("a点");
var d2=createDom("b点");
var d3=createDom("中间点");
var x,y,px,py;
px=getOffset($$("target1")).left;
py=getOffset($$("target1")).top;
x=radius* Math.sin(60 * Math.PI / 180);
y=radius* Math.cos(60 * Math.PI / 180);
//alert(x + " == " + y);
setXY(d3,px+x,px+y);
var ax,ay;
ax=radius*Math.cos(30);
ay=radius*Math.sin(30);
//alert(ax + "" + ay)
//a - op =np
//setXY(d1,px+ax,px+ay);
//alert(ax + " == " + ay);
var bx,by;
bx=radius*Math.cos(90);
by=radius*Math.sin(90);
//setXY(d2,px-bx,py-by);
//alert(by + " xx " + (radius*Math.cos(90)) )
}*/
function setXY(dom,x,y){
dom.style.left=x;
dom.style.top=y;
}
function createDom(arg) {
var objE = document.createElement("div");
objE.setAttribute("class","s");
objE.innerHTML = arg;
document.body.appendChild(objE);
return objE;
}
function getOffset( el ) {
var _x = 0;
var _y = 0;
while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
_x += el.offsetLeft - el.scrollLeft;
_y += el.offsetTop - el.scrollTop;
el = el.parentNode;
}
return { top: _y, left: _x };
}
function $$(id){return document.getElementById(id);}
</script>
<script type="text/javascript">
var pai=2*Math.asin(1);
//document.write(180*Math.asin(0.5)/pai+"<br>");//正弦值0.5的反三角度数
//document.write(Math.sin(30*pai/180));//30度角的正弦
</script>