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

Java tutorial

Introduction

Here is the source code for com.redthirddivision.astilade.entities.mobs.Player.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.Input.Keys;
import com.badlogic.gdx.Preferences;
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.Astilade;
import com.redthirddivision.astilade.entities.Entity;
import com.redthirddivision.astilade.render.Texture2D;
import com.redthirddivision.astilade.utils.LogHelper;
import com.redthirddivision.astilade.world.World;

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

    //TODO: Scale walkBounds and make Mob class

    private static final long serialVersionUID = 4806378327001650322L;

    private static final int col = 3;
    private static final int row = 4;

    private Animation animation;
    private TextureRegion[] frames;
    private TextureRegion currentFrame;
    private float stateTime;
    private int speed;
    private float velX, velY;
    private int dir;
    private float scale;

    public Player(World world, Vector2 position) {
        super(world, position, new Texture2D("player"));
        this.speed = 2;
        this.scale = 2;
        this.dir = 0;
        TextureRegion[][] tmp = TextureRegion.split(texture.getTexture(), texture.getTexture().getWidth() / col,
                texture.getTexture().getHeight() / row);
        this.frames = new TextureRegion[col * row];

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

        this.animation = new Animation(1, frames);
        this.stateTime = 0f;
        this.currentFrame = animation.getKeyFrame(0);
        this.walkBounds = new Rectangle(position.x, position.y, currentFrame.getRegionWidth(),
                currentFrame.getRegionHeight());
        this.hitBox = new Rectangle(position.x, position.y, currentFrame.getRegionWidth() * scale,
                currentFrame.getRegionHeight() * scale);
    }

    @Override
    public void update() {
        xTile = (int) (position.x / 64);
        yTile = (int) (position.y / 64);
        velX = velY = 0;
        walkBounds.set(position.x + 17, position.y, currentFrame.getRegionWidth(),
                currentFrame.getRegionHeight() / 2);
        hitBox.set(position.x, position.y, currentFrame.getRegionWidth() * scale,
                currentFrame.getRegionHeight() * scale);
        stateTime += Gdx.graphics.getDeltaTime() * 8;
        if (stateTime > 3)
            stateTime = 0;

        int moveSpeed = speed;

        if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT))
            moveSpeed *= 2;

        if (Gdx.input.isKeyPressed(Keys.W))
            velY = moveSpeed;
        if (Gdx.input.isKeyPressed(Keys.A))
            velX = -moveSpeed;
        if (Gdx.input.isKeyPressed(Keys.S))
            velY = -moveSpeed;
        if (Gdx.input.isKeyPressed(Keys.D))
            velX = moveSpeed;

        position.add(velX, velY);

        if (velY == moveSpeed) {
            currentFrame = animation.getKeyFrame(9 + stateTime);
            dir = 1;
        }
        if (velX == -moveSpeed) {
            currentFrame = animation.getKeyFrame(3 + stateTime);
            dir = 2;
        }
        if (velY == -moveSpeed) {
            currentFrame = animation.getKeyFrame(0 + stateTime);
            dir = 0;
        }
        if (velX == moveSpeed) {
            currentFrame = animation.getKeyFrame(6 + stateTime);
            dir = 3;
        }

        if (velX == 0 && velY == 0) {
            switch (dir) {
            case 0:
                currentFrame = animation.getKeyFrame(1);
                break;
            case 1:
                currentFrame = animation.getKeyFrame(10);
                break;
            case 2:
                currentFrame = animation.getKeyFrame(4);
                break;
            case 3:
                currentFrame = animation.getKeyFrame(7);
                break;
            }
        }
    }

    public void readjust() {
        switch (dir) {
        case 0:
            position.add(0, speed);
            break;
        case 1:
            position.sub(0, speed);
            break;
        case 2:
            position.add(speed, 0);
            break;
        case 3:
            position.sub(speed, 0);
            break;
        default:
            LogHelper.warn("Unkown direction");
            break;
        }
    }

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

    @Override
    public void dispose() {
        savePlayer(this);
        super.dispose();
    }

    public static void savePlayer(Player player) {
        LogHelper.info("Saving player data");
        Preferences prefs = Gdx.app.getPreferences(Astilade.TITLE);
        prefs.putFloat("playerX", player.position.x);
        prefs.putFloat("playerY", player.position.y);

        prefs.flush();
        LogHelper.info("Player data saved");
    }

    public TextureRegion getCurrentFrame() {
        return currentFrame;
    }

}