com.redthirddivision.astilade.entities.mobs.Enemy.java Source code

Java tutorial

Introduction

Here is the source code for com.redthirddivision.astilade.entities.mobs.Enemy.java

Source

/*   Copyright 2014 Matthew Rogers "BossLetsPlays"
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/
package com.redthirddivision.astilade.entities.mobs;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.redthirddivision.astilade.ai.AStarPathFinder;
import com.redthirddivision.astilade.ai.Mover;
import com.redthirddivision.astilade.ai.Path;
import com.redthirddivision.astilade.ai.PathFinder;
import com.redthirddivision.astilade.entities.Entity;
import com.redthirddivision.astilade.world.World;

/**
 * <strong>Project:</strong> Kingdom of Astilade-core <br>
 * <strong>File:</strong> Enemy.java
 *
 * @author <a href = "http://redthirddivision.com/team/BossLetsPlays"> Matthew Rogers</a>
 */
public abstract class Enemy extends Entity implements Mover {

    private static int id = 0;

    protected Player player;
    protected Animation animation;
    protected TextureRegion[] frames;
    protected TextureRegion currentFrame;
    protected float stateTime;
    protected int dir;
    protected float velX, velY;
    protected float scale;
    protected int enemyID;
    protected int speed;
    protected PathFinder finder;
    protected Path path;
    protected Vector2 lastFinds = new Vector2(-1, -1);

    public Enemy(World world, Vector2 position, String textureFile) {
        super(world, position, textureFile);
        this.enemyID = id++;
        this.player = world.getPlayer();
        this.finder = new AStarPathFinder(world, 500, true);
        this.scale = 0.75f;
        this.speed = 1;
        this.dir = 0;
        TextureRegion[][] tmp = TextureRegion.split(texture.getTexture(),
                texture.getTexture().getWidth() / getCols(), texture.getTexture().getHeight() / getRows());
        this.frames = new TextureRegion[getCols() * getRows()];

        int index = 0;
        for (int x = 0; x < getRows(); x++) {
            for (int y = 0; y < getCols(); y++) {
                frames[index++] = tmp[x][y];
            }
        }

        this.animation = new Animation(1, frames);
        this.stateTime = 0f;
        this.currentFrame = animation.getKeyFrame(0);

        //TODO
        this.walkBounds = new Rectangle(position.x, position.y, (currentFrame.getRegionWidth() * scale) / 4,
                (currentFrame.getRegionHeight() * scale) / 4 - 5);
        this.hitBox = new Rectangle(position.x, position.y, currentFrame.getRegionWidth() * scale,
                currentFrame.getRegionHeight() * scale);
    }

    public abstract int getCols();

    public abstract int getRows();

    @Override
    public void update() {
        xTile = (int) (position.x / 64);
        yTile = (int) (position.y / 64);
        updateBounds();
        stateTime += Gdx.graphics.getDeltaTime() * 8;
        if (stateTime > getCols())
            stateTime = 0;

        Vector2 tempPos = position;

        if (player.getxTile() > xTile)
            velX = speed;
        else if (player.getxTile() < xTile)
            velX = -speed;
        else
            velX = 0;
        if (player.getyTile() > yTile)
            velY = speed;
        else if (player.getyTile() < yTile)
            velY = -speed;
        else
            velY = 0;
        tempPos.add(velX, velY);

        //TODO
        if (path != null && path.contains((int) (tempPos.x / 64), (int) (tempPos.y / 64))) {
            position.add(velX, velY);
        }

        findPlayer();
        animate();
    }

    protected void findPlayer() {
        if (lastFinds.x != player.getxTile() || lastFinds.y != player.getyTile()) {
            lastFinds.x = player.getxTile();
            lastFinds.y = player.getyTile();
            path = finder.findPath(this, xTile, yTile, player.getxTile(), player.getyTile());
        }
    }

    protected void updateBounds() { //TODO
        float xx = position.x + 18;
        float yy = position.y - (currentFrame.getRegionHeight() - (currentFrame.getRegionHeight() * scale)) + 23;
        walkBounds.set(xx, yy, (currentFrame.getRegionWidth() * scale) / 4,
                (currentFrame.getRegionHeight() * scale) / 4 - 5);
        hitBox.set(position.x, position.y, currentFrame.getRegionWidth() * scale,
                currentFrame.getRegionHeight() * scale);
    }

    protected void animate() {
        if (velX == 1) {
            currentFrame = animation.getKeyFrame(8 + stateTime);
            dir = 3;
        }

        if (velX == -1) {
            currentFrame = animation.getKeyFrame(4 + stateTime);
            dir = 2;
        }

        if (velY == 1) {
            currentFrame = animation.getKeyFrame(12 + stateTime);
            dir = 1;
        }

        if (velY == -1) {
            currentFrame = animation.getKeyFrame(0 + stateTime);
            dir = 0;
        }
    }

    @Override
    public void render(SpriteBatch batch) {
        texture.renderRegion(batch, currentFrame, position, scale);
    }

    public int getID() {
        return enemyID;
    }

}