<head>
<!-- <script type="text/javascript" src="jquery.js"></script> -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
// скрипты игры
/**
* класс инородных объектов
*/
function CSpiceObject(name, xLim, yLim){
{ // properties
this.name = name?name:"I";
this.x = 0.; // положение по х
this.y = 0.; // положение по у
this.speed = 8.; // скорость в простарнстве
this.a = 0.; // направление (угол в градусах)
this.turnAD = 30.; // возможный разброс изменеия угла поворота
this.r = 35.; // радиус объекта
this.damag = 1.; // наносимый урон противоположной строне
this.live = 1.; // уровень собственной жизни
this.from = 0; // за кого воюют
this.xLim = xLim?xLim:100;
this.yLim = yLim?yLim:100;
}
this.checkEdge = function(){
if( this.x > (this.xLim-this.r)
|| (this.x-this.r) < 0
|| this.y > (this.yLim - this.r)
|| this.y < 0
)
this.setPos(this.xLim - Math.random() * this.xLim, 10, 90);
}
this.rewiev = function(){
/*
$("#"+this.name).css("top",alianOobject[k].y-this.r);
$("#"+this.name).css("left",alianOobject[k].x-this.r);
*/
$("#"+this.name).css("top",this.y-this.r);
$("#"+this.name).css("left",this.x-this.r);
$("#"+this.name).css("width",this.r*2);
$("#"+this.name).css("height",this.r*2);
}
this.move = function(){
var a
= (Math
.PI/180
) * this
.a
;
this.turn(Math.random()*this.turnAD-this.turnAD/2.)
this
.x
+=Math
.cos(a
) * this
.speed
; this
.y
+=Math
.sin(a
) * this
.speed
;
//$("#test-view").val($("#test-view").val()+" \n:"+this.x+" \n-"+this.y);
this.checkEdge();
this.rewiev();
//return this.x+"-"+this.y+"-"+,this.name;
}
this.turn = function(turnD){
this.a+=turnD;
}
this.setPos = function(x, y, a, turnD, speed){
this.x=x;
this.y=y;
this.a=a?a:this.a;
this.turnAD = turnD?turnD:this.turnAD;
this.speed = speed?speed:this.speed;
}
this.create = function(className){
var text = '<div class="'+className+'" id="'+this.name+'" style="width:'+(this.r*2)+'px; height:'+(this.r*2)+'px;"></div>';
$(document.body).append(text);
//alert(this.name+" - "+text);
}
this.drop = function(){
$("#"+this.name).remove();
}
this.go = function(keyPressX,keyPressY){
if (keyPressY>0) this.y+=this.speed;
if (keyPressY<0) this.y-=this.speed;
if (keyPressX<0) this.x-=this.speed;
if (keyPressX>0) this.x+=this.speed;
//$("#test-view").val(key);
this.rewiev();
/*
$("#"+this.name).css("top",this.y-this.r);
$("#"+this.name).css("left",this.x-this.r);
*/
}
this.collision = function(collisionObj){
var x2
= Math
.abs(collisionObj
.x
-this
.x
); var y2
= Math
.abs(collisionObj
.y
-this
.y
); x2 *= x2;
y2 *= y2;
var r2 = collisionObj.r+this.r;
r2 = r2 * r2;
// $("#test-view").val(x2 + y2 - r2);
if( (x2+y2-r2) < 0 )
return true;
return false;
}
}
var keyPressX = 0;
var keyPressY = 0;
// массив инородных обектов
var alianOobject = [];
var myObj;
// задание исходного положения и добавление чужих в вывод
$(document).ready(function(){
myObj = new CSpiceObject("mo",$(window).width()-50,$(window).height()-50);
// $(window).width(),$(window).height()
myObj.setPos(300,300,0,0,10);
myObj.r=60;
var dateTime
= new Date(); // заполнение массива инородных объектов
for(i=0;i<2;i++){
alianOobject[i] = new CSpiceObject("o"+dateTime.getTime()+i,$(window).width()-50,$(window).height()-50);
}
for(k=0;k<alianOobject.length;k++){
alianOobject[k].setPos(800-Math.random()*800,0,90,Math.random()*30,2+Math.random()*2);
alianOobject[k].create("obj");
}
});
// прорисовка кадров и выполнеие движекния ботов
setInterval(function() {
var text ="";
//myObj.collision( alianOobject[0] );
for(k=0; k<alianOobject.length; k++){
alianOobject[k].move();
if( myObj.collision(alianOobject[k]) ){
//alianOobject[k].setPos( Math.random() * 1000, 10, 90);
alianOobject[k].drop();
alianOobject.splice(k,1);
if(myObj.r>5)
myObj.r--;
myObj.name
$("#"+myObj.name).html(myObj.r);
}
}
myObj.go(keyPressX,keyPressY);
myObj.checkEdge()
},40);
// удаление по 1 объекту каждую секунду
setInterval(function(){
if(alianOobject.length>1){
//$("#test-view").val(alianOobject.length);
//alianOobject[alianOobject.length-1].drop();
//alianOobject.splice(alianOobject.length-1,1);
}
},1000);
// добавление новых объектов в игру через 6 сек
setInterval(function(){
var dateTime
= new Date(); var countEl = alianOobject.length+Math.random()*10;
for(i=alianOobject.length;i<countEl;i++){
alianOobject[i] = new CSpiceObject("o"+dateTime.getTime()+i,$(window).width()-50,$(window).height()-50);
alianOobject[i].create("obj");
//$("#test-view").val(" "+alianOobject[i].name);
}
myObj.r++;
$("#"+myObj.name).html(myObj.r);
},6000);
// обработка кликов клавиатуры
$(document).ready(function(){
$(document).keydown(function(eventObject){
if (eventObject.which==40) keyPressY=1;
if (eventObject.which==38) keyPressY=-1;
if (eventObject.which==39) keyPressX=1;
if (eventObject.which==37) keyPressX=-1;
});
});
$(document).ready(function(){
$(document).keyup(function(eventObject){
if (eventObject.which==40) keyPressY=0;
if (eventObject.which==38) keyPressY=0;
if (eventObject.which==39) keyPressX=0;
if (eventObject.which==37) keyPressX=0;
});
});
</script>
<style>
div.obj{
position:absolute;
#width:30px;
#height:30px;
border:2px solid #F00;
#float:none;
#clear:left;
#clear:top;
padding:0px;
margin:0px;
top:0px;
background:#AAA;
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
font-size:20pt;
text-align:center;
margin:10px;
}
</style>
</head>
</head>
<body>