GameObjects.BossEnemy.java Source code

Java tutorial

Introduction

Here is the source code for GameObjects.BossEnemy.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GameObjects;

import Patterns.Pattern;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import goldDaniel.Constants;

/**
 *
 * @author goldd
 */
public class BossEnemy extends Enemy {

    private float circleFireAngle;
    private float playerAngle;

    Weapon[] circleWeapon;
    //the bullets that rotate in a circle, shooting one at a time
    Weapon aimedCircleWeapon;

    //the weapon that fires directly at the player
    Weapon playerWeapon;

    boolean flip = false;

    boolean movingLeft = true;

    private final int MAX_HEALTH = 400;

    public BossEnemy(SpriteBatch s, Vector2 position, String filename, Pattern pattern) {
        super(s);
        health = MAX_HEALTH;
        isAlive = true;
        //creates circle weapon with 64 bullets

        circleWeapon = new Weapon[32];
        for (int i = 0; i < circleWeapon.length; i++) {
            circleWeapon[i] = new Weapon(s, 300, 0.8f,
                    (float) Math.PI * 2f * ((float) i / (float) circleWeapon.length - 1f));
        }
        aimedCircleWeapon = new Weapon(s, 20, 1.6f, circleFireAngle);

        playerWeapon = new Weapon(s, 120, 1.95f, playerAngle);

        texture = new Texture(filename);
        this.pattern = pattern;
        width = 128;
        height = 128;
        this.position = position;
    }

    public void setPlayerAngle(Player player) {
        playerAngle = (float) Math.atan2((player.position.y + player.height / 2) - (position.y + height / 2),
                (player.position.x + player.width / 2) - (position.x + width / 2));
    }

    public int getMaxHealth() {
        return MAX_HEALTH;
    }

    @Override
    public void update() {
        for (int i = 0; i < circleWeapon.length; i++) {
            circleWeapon[i].update();
        }
        aimedCircleWeapon.update();
        playerWeapon.update();
        ////////////////////////////////////////////////////////////////////

        //moves boss left and right
        if (movingLeft) {
            position.x += 1;
        } else {
            position.x -= 1;
        }

        if (position.x > Constants.SCREEN_WIDTH / 4 * 3) {
            movingLeft = !movingLeft;
        } else if (position.x < Constants.SCREEN_WIDTH / 4) {
            movingLeft = !movingLeft;
        }

        //adjusts circle angle
        if (flip) {
            circleFireAngle += 0.034f;
        } else {
            circleFireAngle -= 0.034f;
        }
        if (circleFireAngle <= -Math.PI || circleFireAngle >= 0) {
            flip = !flip;
        }
        //if the boss is alive, try to fire
        if (isAlive) {
            for (int i = 0; i < circleWeapon.length; i++) {
                circleWeapon[i].fire(position.x + width / 2, position.y + height / 2);
            }
            //fires bullet in direction of circleFireAngle
            aimedCircleWeapon.fire(position.x + width / 2, position.y + height / 2, circleFireAngle);

            //fires bullet in player direction
            playerWeapon.fire(position.x + width / 2, position.y + height / 2, playerAngle);
        }

    }

    @Override
    public void draw() {
        super.draw();
        drawWeapon();
    }

    public boolean areBulletsColliding(GameObject obj) {
        for (int i = 0; i < circleWeapon.length; i++) {
            if (circleWeapon[i].doBulletCollision(obj)) {
                return true;
            }
        }
        if (aimedCircleWeapon.doBulletCollision(obj)) {
            return true;
        }
        return playerWeapon.doBulletCollision(obj);
    }

    public void drawWeapon() {
        s.setColor(Color.GREEN);
        aimedCircleWeapon.draw();
        s.setColor(Color.MAGENTA);
        playerWeapon.draw();
        s.setColor(Color.WHITE);
        for (int i = 0; i < circleWeapon.length; i++) {
            circleWeapon[i].draw();
        }
    }
}