com.redthirddivision.astilade.entities.Entity.java Source code

Java tutorial

Introduction

Here is the source code for com.redthirddivision.astilade.entities.Entity.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;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.redthirddivision.astilade.render.Texture2D;
import com.redthirddivision.astilade.utils.Debugger;
import com.redthirddivision.astilade.world.World;

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

    protected Vector2 position;
    protected Texture2D texture;
    protected ShapeRenderer sr;
    protected Rectangle walkBounds;
    protected Rectangle hitBox;
    protected World world;
    protected int xTile, yTile;

    public Entity(World world, Vector2 position, String textureFile) {
        this(world, position, new Texture2D(textureFile));
    }

    public Entity(World world, Vector2 position, Texture2D texture) {
        this.world = world;
        this.position = position;
        this.texture = texture;
        this.sr = new ShapeRenderer();
        xTile = (int) (position.x / 64);
        yTile = (int) (position.y / 64);
        world.addEntity(this);
    }

    public boolean hasCollisionWith(Rectangle other) {
        return walkBounds.overlaps(other);
    }

    public abstract void update();

    public abstract void render(SpriteBatch batch);

    public void renderDebug() {
        if (walkBounds != null)
            Debugger.drawBounds(sr, walkBounds, Color.WHITE, world.getCamera());
        if (hitBox != null)
            Debugger.drawBounds(sr, hitBox, Color.GREEN, world.getCamera());
    }

    public void dispose() {
        if (texture != null)
            texture.dispose();
    }

    public Vector2 getPosition() {
        return position;
    }

    public void setPosition(Vector2 position) {
        this.position = position;
    }

    public int getxTile() {
        return xTile;
    }

    public int getyTile() {
        return yTile;
    }

}