Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.me.FirstProject;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.OutputStream;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input.Keys;
- import com.badlogic.gdx.files.FileHandle;
- import com.badlogic.gdx.graphics.Texture;
- 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.graphics.Texture;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.utils.Json;
- import com.badlogic.gdx.utils.Json.Serializable;
- import com.badlogic.gdx.utils.JsonValue;
- public class Player implements Serializable{
- private static final long serialVersionIID = 1L;
- Vector2 position;
- String textureLoc;
- private static final int col = 4;
- private static final int row = 2;
- Animation animation;
- Texture player;
- TextureRegion[] frames;
- TextureRegion currentFrame;
- float stateTime;
- Rectangle bounds;
- String movement;
- public Player(Vector2 position, String textureLoc){
- this.position = position;
- movement = "";
- player = new Texture(Gdx.files.internal("SpriteSheet.png"));
- TextureRegion[][] tr = TextureRegion.split(player, player.getWidth() / col, player.getHeight() / row);
- frames = new TextureRegion[col * row];
- int index = 0;
- for(int i = 0; i < row; i++){
- for(int j = 0; j < col; j++){
- frames[index++] = tr [i][j];
- }
- }
- animation = new Animation(0.025f, frames);
- stateTime = 0f;
- currentFrame = animation.getKeyFrame(0);
- bounds = new Rectangle(position.x, position.y, currentFrame.getRegionWidth(), currentFrame.getRegionHeight());
- }
- public void update(){
- bounds.set(position.x, position.y, currentFrame.getRegionWidth(), currentFrame.getRegionHeight());
- if(stateTime > 4){
- stateTime += Gdx.graphics.getDeltaTime();
- }else{
- stateTime = 0;
- }
- //moving up and down
- if(Gdx.input.isKeyPressed(Keys.W)){
- position.y += 1f;
- currentFrame = animation.getKeyFrame(12 + stateTime / 2);
- movement = "Up";
- }
- if(Gdx.input.isKeyPressed(Keys.S)){
- position.y -= 1f;
- currentFrame = animation.getKeyFrame(0 + stateTime / 2);
- movement = "Down";
- }
- //moving left and right
- if(Gdx.input.isKeyPressed(Keys.A)){
- position.x -= 1f;
- currentFrame = animation.getKeyFrame(4 + stateTime / 2);
- movement = "Left";
- }
- if(Gdx.input.isKeyPressed(Keys.D)){
- position.x += 1f;
- currentFrame = animation.getKeyFrame(8 + stateTime / 2);
- movement = "Right";
- }
- }
- public void redirect(){
- if(movement == "Up"){
- position.y -= 1f;
- }
- if(movement == "Down"){
- position.y += 1f;
- }
- if(movement == "Left"){
- position.x -= 1f;
- }
- if(movement == "Right"){
- position.x += 1f;
- }
- }
- //where the file will be saved
- public static void savePlayer(Player playerPosition)throws IOException{
- FileHandle file = Gdx.files.local("player.dat");
- OutputStream out = null;
- try{
- file.writeBytes(serialize(playerPosition.getPosition()), false);
- }catch(Exception ex){
- System.out.println(ex.toString());
- }finally{
- if(out != null) try{out.close();} catch(Exception ex){}
- }
- System.out.println("Saving Player");
- }
- public static Vector2 readPlayer() throws IOException, ClassNotFoundException{
- Vector2 playerPosition = null;
- FileHandle file = Gdx.files.local("player.dat");
- playerPosition = (Vector2) deserialize(file.readBytes());
- return playerPosition;
- }
- //to write to a file in bytes
- public static byte[] serialize(Object obj)throws IOException{
- ByteArrayOutputStream b = new ByteArrayOutputStream();
- ObjectOutputStream o = new ObjectOutputStream(b);
- o.writeObject(obj);
- return b.toByteArray();
- }
- //to read from a file
- public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException{
- ByteArrayInputStream b = new ByteArrayInputStream(bytes);
- ObjectInputStream o = new ObjectInputStream(b);
- return o.readObject();
- }
- /*public Texture getTexture() {
- return texture;
- }
- public void setTexture(Texture texture) {
- this.texture = texture;
- }*/
- public Vector2 getPosition(){
- return position;
- }
- public void setPosition(Vector2 position){
- this.position = position;
- }
- public String getTextureLoc() {
- return textureLoc;
- }
- public void setTextureLoc(String textureLoc) {
- this.textureLoc = textureLoc;
- }
- public Texture getPlayer() {
- return player;
- }
- public void setPlayer(Texture player) {
- this.player = player;
- }
- @Override
- public void write(Json json) {
- }
- @Override
- public void read(Json json, JsonValue jsonData) {
- }
- public Animation getAnimation() {
- return animation;
- }
- public void setAnimation(Animation animation) {
- this.animation = animation;
- }
- public TextureRegion[] getFrames() {
- return frames;
- }
- public void setFrames(TextureRegion[] frames) {
- this.frames = frames;
- }
- public TextureRegion getCurrentFrame() {
- return currentFrame;
- }
- public void setCurrentFrame(TextureRegion currentFrame) {
- this.currentFrame = currentFrame;
- }
- public float getStateTime() {
- return stateTime;
- }
- public void setStateTime(float stateTime) {
- this.stateTime = stateTime;
- }
- public Rectangle getBounds() {
- return bounds;
- }
- public void setBounds(Rectangle bounds) {
- this.bounds = bounds;
- }
- public static long getSerialversioniid() {
- return serialVersionIID;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement