Jump to content

Help! I need to figure out how to get bots to shoot bullets


kian jolley
 Share

Recommended Posts

Below is the code for the game I'm making and for some reason I'm having trouble getting the bots shooting properly. I have managed to get the player you control shooting properly and the bullets tracking your movement, but the bot just wont seem to function as I hoped. I figured the way the bots shoot should be similar to the player shoots, but it's not the case. I figure I'm just missing something small, but I don't know what. Any help would be much appreciated! Thanks in advance!

 

var game = new Phaser.Game(
    800,
    600,
    Phaser.AUTO,
    'asteroids',
    { preload: preload, create: create, update: update, render: render, init: init}
);

    function init() {

        var text = "Welcome to asteroids!";
        var style = { font: "12px Arial", fill: "#0f0", align: "center" };
        var t = game.add.text(this.world.centerX, this.world.centerY, text, style);
        t.anchor.setTo(0.5, 0.5);

    }

    function preload() {

        game.load.image('bullet', '/static/img/shmup-bullet.png');
        game.load.image('ship', '/static/img/thrust_ship.png');
        game.load.image('bot', '/static/img/thrust_ship2.png');
        game.load.image('pineapple', '/static/img/bombs.png');
        game.load.image('starfield', '/static/img/starfield.png');
        game.load.image('enemyBullets', '/static/img/bullet111.png');
        game.load.spritesheet('kaboom', '/static/img/explode.png', 128, 128);

    }

    var scoreString = '';
    var scoreText;
    var lives;
    var score = 0;
    var explosions;
    var cursors;
    var fireButton;
    var setupInvader;
    var explosions;
    var bot1;
    var bot2;
    var bot3;
    var bot4;
    var sprite;
    var bullets;
    var bulletTime = 0;
    var enemyBullets;
    var enemyBulletTime = 0;

    function create(){

        game.physics.startSystem(Phaser.Physics.ARCADE);

        //background
        starfield = game.add.tileSprite(0, 0, 800, 600, 'starfield');

        //your bullets
        bullets = game.add.group();
        bullets.enableBody = true;
        bullets.physicsBodyType = Phaser.Physics.ARCADE;
        bullets.createMultiple(30, 'bullet');
        bullets.setAll('anchor.x', 0.5);
        bullets.setAll('anchor.y', 1);
        bullets.setAll('outOfBoundsKill', true);
        bullets.setAll('checkWorldBounds', true);

        //enemy bullets
        enemyBullets = game.add.group();
        enemyBullets.enableBody = true;
        enemyBullets.physicsBodyType = Phaser.Physics.ARCADE;
        enemyBullets.createMultiple(30, 'enemyBullets');
        enemyBullets.setAll('anchor.x', 0.5);
        enemyBullets.setAll('anchor.y', 1);
        enemyBullets.setAll('outOfBoundsKill', true);
        enemyBullets.setAll('checkWorldBounds', true);

        //you're ship
        sprite = this.add.sprite(400, 300, 'ship');
        sprite.anchor.set(0.5);
        game.physics.enable(sprite, Phaser.Physics.ARCADE);
        sprite.body.drag.set(70);
        sprite.body.maxVelocity.set(200);

        //enemy ship(s)
        bot1 = this.add.sprite(400, 300, 'bot');
        bot1.anchor.set(0.5);
        game.physics.enable(bot1, Phaser.Physics.ARCADE);
        bot1.body.drag.set(70);
        bot1.body.maxVelocity.set(200);
        bot1.pivot.x = 100;

        bot2 = game.add.sprite(400, 300, 'bot');
        game.physics.enable(bot2, Phaser.Physics.ARCADE);
        bot2.body.drag.set(70);
        bot2.body.maxVelocity.set(200);
        bot2.pivot.x = 175;

        bot3 = game.add.sprite(400, 300, 'bot');
        game.physics.enable(bot3, Phaser.Physics.ARCADE);
        bot3.body.drag.set(70);
        bot3.body.maxVelocity.set(200);
        bot3.pivot.x = 250;

        bot4 = game.add.sprite(400, 300, 'bot');
        game.physics.enable(bot4, Phaser.Physics.ARCADE);
        bot4.body.drag.set(70);
        bot4.body.maxVelocity.set(200);
        bot4.pivot.x = 325;

        //bombs
        bombs();

        //scores
        scores();

        cursors = this.input.keyboard.createCursorKeys();
        fireButton = this.input.keyboard.addKey(Phaser.KeyCode.SPACEBAR);

        //explosion
        explosion();
    }

    function scores(){
        //score
        scoreString = 'Score : ';
        scoreText = game.add.text(10, 10, scoreString + score, { font: '34px Arial', fill: '#0f0' });

        //lives
        lives = game.add.group();
        game.add.text(game.world.width - 100, 10, 'Lives : ', { font: '34px Arial', fill: '#0f0' });

        //text
        stateText = game.add.text(game.world.centerX,game.world.centerY,' ', { font: '84px Arial', fill: '#0f0' });
        stateText.anchor.setTo(0.5, 0.5);
        stateText.visible = false;

        for (var i = 0; i < 3; i++) 
        {
            var ship = lives.create(game.world.width - 100 + (30 * i), 60, 'ship');
            ship.anchor.setTo(0.5, 0.5);
            ship.angle = 90;
            ship.alpha = 0.4;
        }
    }

    function explosion(){
        explosions = game.add.group();
        explosions.createMultiple(30, 'kaboom');
        explosions.forEach(setupInvader, this);
    }

    function setupInvader (invader) {

        invader.anchor.x = 0.5;
        invader.anchor.y = 0.5;
        invader.animations.add('kaboom');
    }

    function bombs(){
        pineapples = game.add.group();
        pineapples.enableBody = true;
        pineapples.physicsBodyType = Phaser.Physics.ARCADE;

        for (var i = 0; i < 10; i++)
        {
            var pineapple = pineapples.create(200 + i * 48,50, 'pineapple');

            //This allows your sprite to collide with the world bounds like they were rigid objects
            pineapple.body.collideWorldBounds=true;
            pineapple.body.gravity.x = game.rnd.integerInRange(-50, 50);
            pineapple.body.gravity.y = 100 + Math.random() * 100;
            pineapple.body.bounce.setTo(1.1, 1.1);
        } 
    }

    function update(){

        //start of player movement 
        if (cursors.up.isDown)
        {
            game.physics.arcade.accelerationFromRotation(sprite.rotation, 300, sprite.body.acceleration);
        }
        else
        {
            sprite.body.acceleration.set(0);
        }

        if (cursors.left.isDown)
        {
            sprite.body.angularVelocity = -300;
        }
        else if (cursors.right.isDown)
        {
            sprite.body.angularVelocity = 300;
        }
        else
        {
            sprite.body.angularVelocity = 0;
        }

         if (fireButton.isDown)
        {
            fireBullet();
        }

        if (bot1)
        {
            fireEnemyBullet1();
        }

/*
        if (bot2)
        {
            fireEnemyBullet2();
        }

        if (bot3)
        {
            fireEnemyBullet3();
        }

        if (bot4)
        {
            fireEnemyBullet4();
        }
*/

           game.world.wrap(sprite, 16);
        //end of player movement 

        bot1.rotation += 0.025;
        bot1.pivot.x = 100;
        bot2.rotation += 0.03;
        bot3.rotation += 0.035;
        bot4.rotation += 0.04;

        game.physics.arcade.collide(sprite, pineapples, playerHitByBombs, null, this);
        game.physics.arcade.collide(pineapples, bullets, bombsHitByPlayer, null, this);
        //game.physics.arcade.collide(sprite, enemyBullets, playerHitByBullets, null, this);
        game.physics.arcade.collide(bot1, bullets, bot1HitByPlayer, null, this);
        game.physics.arcade.collide(bot2, bullets, bot1HitByPlayer, null, this);
        game.physics.arcade.collide(bot3, bullets, bot1HitByPlayer, null, this);
        game.physics.arcade.collide(bot4, bullets, bot1HitByPlayer, null, this);


    }

    function playerHitByBombs(player, bullet){
        
        bullet.kill();

        live = lives.getFirstAlive();

        if (live)
        {
            live.kill();
        }

        //  And create an explosion :)
        var explosion = explosions.getFirstExists(false);
        explosion.reset(sprite.body.x, sprite.body.y);
        explosion.play('kaboom', 30, false, true);

        // When the player dies
        if (lives.countLiving() < 1)
        {
            player.kill();
            pineapples.callAll('kill');

            stateText.text=" GAME OVER \n Click to restart";
            stateText.visible = true;

            //the "click to restart" handler
            game.input.onTap.addOnce(restart,this);
        }        
    }


    function playerHitByBullets(player, bullet){
        
        bullet.kill();

        live = lives.getFirstAlive();

        if (live)
        {
            live.kill();
        }

        //  And create an explosion :)
        var explosion = explosions.getFirstExists(false);
        explosion.reset(sprite.body.x, sprite.body.y);
        explosion.play('kaboom', 30, false, true);

        // When the player dies
        if (lives.countLiving() < 1)
        {
            player.kill();
            enemyBullets.callAll('kill');

            stateText.text=" GAME OVER \n Click to restart";
            stateText.visible = true;

            //the "click to restart" handler
            game.input.onTap.addOnce(restart,this);
        }        
    }

    function bot1HitByPlayer(bot, bullet){

        bot.kill();
        bullet.kill();

        //  Increase the score
        score += 20;
        scoreText.text = scoreString + score;

        //  And create an explosion :)
        var explosion = explosions.getFirstExists(false);
        explosion.reset(bot.body.x, bot.body.y);
        explosion.play('kaboom', 30, false, true);

        if (pineapples.countLiving() == 0 && bot1.alive == 0 && bot2.alive == 0 && bot3.alive == 0 && bot4.alive == 0)
        {
            score += 1000;
            scoreText.text = scoreString + score;

            stateText.text = " You Won, \n Click to restart";
            stateText.visible = true;

            //the "click to restart" handler
            game.input.onTap.addOnce(restart,this);
        }

    }


    function bombsHitByPlayer(bombs, bullet){

        bombs.kill();
        bullet.kill();

        //  Increase the score
        score += 20;
        scoreText.text = scoreString + score;

        //  And create an explosion :)
        var explosion = explosions.getFirstExists(false);
        explosion.reset(bombs.body.x, bombs.body.y);
        explosion.play('kaboom', 30, false, true);

        if (pineapples.countLiving() == 0 && bot1.alive == 0 && bot2.alive == 0 && bot3.alive == 0 && bot4.alive == 0)
        {
            score += 1000;
            scoreText.text = scoreString + score;

            stateText.text = " You Won, \n Click to restart";
            stateText.visible = true;

            //the "click to restart" handler
            game.input.onTap.addOnce(restart,this);
        }
    }

    function restart () {
        //  A new level starts
        
        //resets the life count
        lives.callAll('revive');
        //  And brings the aliens back from the dead :)
        pineapples.removeAll();
        bombs();
        //bot1.removeAll();
        //createBot1();

        //revives the player
        sprite.revive();
        sprite.reset(400, 300);
        bot1.revive();
        bot1.reset(400, 300);
        bot2.revive();
        bot2.reset(400, 300);
        bot3.revive();
        bot3.reset(400, 300);
        bot4.revive();
        bot4.reset(400, 300);        
        //hides the text
        stateText.visible = false;
    }

    function fireBullet () {
        var BULLET_SPEED = 400;
        //  To avoid them being allowed to fire too fast we set a time limit
        if (game.time.now > bulletTime)
        {
            //  Grab the first bullet we can from the pool
            bullet = bullets.getFirstExists(false);

            if (bullet)
            {
                //  And fire it
                bullet.reset(sprite.x, sprite.y);
                bullet.body.velocity.y = -400;
                bulletTime = game.time.now + 200;
                //Make bullet come out of tip of ship with right angle
                bullet.reset(sprite.x, sprite.y);
                bullet.angle = sprite.angle;
                game.physics.arcade.velocityFromAngle(bullet.angle, BULLET_SPEED, bullet.body.velocity);
                bullet.body.velocity.x += sprite.body.velocity.x;
            }
        }
    }


    function fireEnemyBullet1 () {

        var BULLET_SPEED = 100;
        //  To avoid them being allowed to fire too fast we set a time limit
        if (game.time.now > enemyBulletTime)
        {
            //  Grab the first bullet we can from the pool
            bullet = enemyBullets.getFirstExists(false);

            if (bullet)
            {
                //  And fire it
                bullet.reset(bot1.x, bot1.y);
                bullet.body.velocity.y = -100;
                enemyBulletTime = game.time.now + 1000;
                //  Make bullet come out of tip of ship with right angle
                bullet.reset(bot1.x, bot1.y);
                bullet.angle = bot1.angle;
                game.physics.arcade.velocityFromAngle(bullet.angle - 90, BULLET_SPEED, bullet.body.velocity);
                bullet.body.velocity.x += bot1.body.velocity.x;
            }
        }
    }

/*
    function fireEnemyBullet2 () {
        var BULLET_SPEED = 100;
        //  To avoid them being allowed to fire too fast we set a time limit
        if (game.time.now > enemyBulletTime)
        {
            //  Grab the first bullet we can from the pool
            bullet = enemyBullets.getFirstExists(false);

            if (bullet)
            {
                //  And fire it
                bullet.reset(bot2.x, bot2.y);
                bullet.body.velocity.y = -100;
                enemyBulletTime = game.time.now + 1000;
                //  Make bullet come out of tip of ship with right angle
                bullet.reset(bot2.x, bot2.y);
                bullet.angle = sprite.angle;
                game.physics.arcade.velocityFromAngle(bullet.angle, BULLET_SPEED, bullet.body.velocity);
                bullet.body.velocity.x += bot2.body.velocity.x;
            }
        }
    }

    function fireEnemyBullet3 () {
        var BULLET_SPEED = 100;
        //  To avoid them being allowed to fire too fast we set a time limit
        if (game.time.now > enemyBulletTime)
        {
            //  Grab the first bullet we can from the pool
            bullet = enemyBullets.getFirstExists(false);

            if (bullet)
            {
                //  And fire it
                bullet.reset(bot3.x, bot3.y);
                bullet.body.velocity.y = -100;
                enemyBulletTime = game.time.now + 1000;
                //  Make bullet come out of tip of ship with right angle
                bullet.reset(bot3.x, bot3.y);
                bullet.angle = sprite.angle;
                game.physics.arcade.velocityFromAngle(bullet.angle, BULLET_SPEED, bullet.body.velocity);
                bullet.body.velocity.x += bot3.body.velocity.x;
            }
        }
    }

    function fireEnemyBullet4 () {
        var BULLET_SPEED = 100;
        //  To avoid them being allowed to fire too fast we set a time limit
        if (game.time.now > enemyBulletTime)
        {
            //  Grab the first bullet we can from the pool
            bullet = enemyBullets.getFirstExists(false);

            if (bullet)
            {
                //  And fire it
                bullet.reset(bot4.x, bot4.y);
                bullet.body.velocity.y = -100;
                enemyBulletTime = game.time.now + 1000;
                //  Make bullet come out of tip of ship with right angle
                bullet.reset(bot4.x, bot4.y);
                bullet.angle = sprite.angle;
                game.physics.arcade.velocityFromAngle(bullet.angle, BULLET_SPEED, bullet.body.velocity);
                bullet.body.velocity.x += bot4.body.velocity.x;
            }
        }
    }
*/

    function render(){

    }
 

main.js

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...