JS Galaxian Shooter敌人射击

时间:2016-12-09 03:11:10

标签: javascript list

嘿,我正在尝试制作超级简单的Galaxian Shooter游戏。我在让敌人名单中的所有敌人分别射击时遇到了问题。现在,只有一个射击子弹。我知道在这段代码中有很多更好的方法可以做到,我还没有把它变得高效而且我正在修复它所以所以请不要说任何事情。第193,86,72和42行都与enemyBulletList有关,如果有帮助的话。

var c = document.getElementById("myCanvas");
    ctx = c.getContext("2d");
    keys = [];
    width = 500;
    height = 500;
    bulletList = {};
    enemyBulletList = {};
    enemyList = {};
    enemyAmount = 10;

var player = {
    x:400,
    y:450,
    speedX:7,
    speedY:7,
    width:32,
    height:32,
    hp:3,
    shootCdTimer:0,
    shootCd:1,
};

tickShootCooldown = function(){
	player.shootCdTimer--;
}
    setInterval(tickShootCooldown,500);
    
onGameStart = function(){
	generateEnemy();
    generateBullet();
    generateEnemyBullet();
    setInterval(changeEnemyDirection,2000);
    setInterval(changeEnemyDirection2,4000);
}
    setTimeout(onGameStart,0);

Bullet = function(id,x,y,speedX,speedY,width,height) {
    var asd = {
        x:x,
        y:y,
        speedX:speedX,
        speedY:speedY,
        name:'B',
        id:id,
        width:width,
        height:height,
        color:'black',
    };
    bulletList[id] = asd;
}
generateBullet = function() {
    var x = player.x;
    var y = player.y;
    var width = 5;
    var height = 15;
    var id = Math.random();
    var speedX = 0;
    var speedY = 15;
    Bullet(id,x,y,speedX,speedY,width,height);
}
EnemyBullet = function(id,x,y,speedX,speedY,width,height) {
    var asd = {
        x:x,
        y:y,
        speedX:speedX,
        speedY:speedY,
        name:'B',
        id:id,
        width:width,
        height:height,
        color:'black',
    };
    enemyBulletList[id] = asd;
}
generateEnemyBullet = function() {
    for(var key3 in enemyList){
        var x = enemyList[key3].x;
        var y = enemyList[key3].y;
    }
    var width = 5;
    var height = 15;
    var id = Math.random();
    var speedX = 0;
    var speedY = -15;
    EnemyBullet(id,x,y,speedX,speedY,width,height);
}
    
Enemy = function(id,x,y,speedX,speedY,width,height) {
    var asd = {
        x:x,
        y:y,
        speedX:speedX,
        speedY:speedY,
        name:'B',
        id:id,
        width:width,
        height:height,
        color:'red',
    };
    enemyList[id] = asd;
}
generateEnemy = function() {
    var x = 50;
    var y = 50;
    var width = 32;
    var height = 32;
    var id = 1;
    var speedX = 2;
    var speedY = 0;
    Enemy(id,x,y,speedX,speedY,width,height);
    Enemy(id*2,x*2,y,speedX,speedY,width,height);
    Enemy(id*3,x*3,y,speedX,speedY,width,height);
    Enemy(id*4,x*4,y,speedX,speedY,width,height);
    Enemy(id*5,x*5,y,speedX,speedY,width,height);
    Enemy(id*6,x,y*2,speedX,speedY,width,height);
    Enemy(id*7,x*2,y*2,speedX,speedY,width,height);
    Enemy(id*8,x*3,y*2,speedX,speedY,width,height);
    Enemy(id*9,x*4,y*2,speedX,speedY,width,height);
    Enemy(id*10,x*5,y*2,speedX,speedY,width,height);
}
changeEnemyDirection = function() {
    for(var key in enemyList){
        if(enemyList[key].speedX > 0){
            enemyList[key].speedX *= -1;
        }
    }
}
changeEnemyDirection2 = function() {
    for(var key in enemyList){
        if(enemyList[key].speedX < 0){
            enemyList[key].speedX *= -1;
        }
    }
}

    
function update() {
    ctx.clearRect(0, 0, width, height);
    ctx.fillRect(player.x,player.y,player.width,player.height);
    
    if(enemyAmount < 1){
        generateEnemy();
        enemyAmount = 10;
    }
    
    if(player.x < player.width/2){
        player.x = player.width/2;
    }
    if(player.x > width - player.width){
        player.x = width - player.width;
    }
    if(player.y < height-150){
        player.y = height-150;
    }
    if(player.y > height-32){
        player.y = height-33;
    }
    
    if (keys[39]) { //right arrow
            player.x+=player.speedX;
    }
    if (keys[37]) { //left arrow
            player.x-=player.speedX;
    }
    if (keys[38]) { //up arrow
            player.y-=player.speedY;
    }
    if (keys[40]) { //down arrow
            player.y+=player.speedY;
    }
    if (keys[32]) { //space bar
            if(player.shootCdTimer<=0){
                generateBullet();
                player.shootCdTimer = player.shootCd;
            }
    }
    
    for(var key in bulletList) {
        updateEntity(bulletList[key]);
    }
    for(var keyy in enemyBulletList) {
        updateEntity(enemyBulletList[keyy]);
    }
    
        for(var key2 in enemyList) {
            var isColliding = testCollisionEntity(bulletList[key],enemyList[key2]);
            if(isColliding) {
                delete enemyList[key2];
                enemyAmount --;
                delete bulletList[key];
                break;
            }
        }
    
    //for(var key3 in enemyList){
        var timer = Math.random();
        if(timer <= 0.01){
            generateEnemyBullet(enemyList[key2]);
        }
   // }
    
    for(var key in enemyList){ //change to collision with enemy bullet
        updateEntity(enemyList[key]);
        var isColliding = testCollisionEntity(player,enemyList[key]);
        if(isColliding){
            player.hp = player.hp - 1;
        }
    }
    
    
    ctx.fillText(player.shootCdTimer,5,10);
}
    setInterval(update,20);
    

testCollisionEntity = function (entity1,entity2){       //return if colliding (true/false)
        var rect1 = {
                x:entity1.x-entity1.width/2,
                y:entity1.y-entity1.height/2,
                width:entity1.width,
                height:entity1.height,
        }
        var rect2 = {
                x:entity2.x-entity2.width/2,
                y:entity2.y-entity2.height/2,
                width:entity2.width,
                height:entity2.height,
        }
        return testCollisionRectRect(rect1,rect2);
       
}
testCollisionRectRect = function(rect1,rect2){
        return rect1.x <= rect2.x+rect2.width
                && rect2.x <= rect1.x+rect1.width
                && rect1.y <= rect2.y + rect2.height
                && rect2.y <= rect1.y + rect1.height;
}

    
    
updateEntity = function(something){
	updateEntityPosition(something);
	drawEntity(something);
}

updateEntityPosition = function(something){
	something.x += something.speedX;
    something.y -= something.speedY;
}

drawEntity = function(something){
	ctx.save();
	ctx.fillStyle = something.color;
	ctx.fillRect(something.x-something.width/2,something.y-something.height/2,something.width,something.height);
	ctx.restore();
}


	
    
    
document.body.addEventListener("keydown", function (e) {
    keys[e.keyCode] = true;
});

document.body.addEventListener("keyup", function (e) {
    keys[e.keyCode] = false;
});
<canvas id="myCanvas" width="500" height="500" style="border:1px solid #d3d3d3;">

0 个答案:

没有答案