Advertisement
Guest User

Player Class

a guest
Sep 2nd, 2014
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.67 KB | None | 0 0
  1. package com.me.FirstProject;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.io.OutputStream;
  9.  
  10. import com.badlogic.gdx.Gdx;
  11. import com.badlogic.gdx.Input.Keys;
  12. import com.badlogic.gdx.files.FileHandle;
  13. import com.badlogic.gdx.graphics.Texture;
  14. import com.badlogic.gdx.graphics.g2d.Animation;
  15. //import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  16. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  17. import com.badlogic.gdx.math.Rectangle;
  18. //import com.badlogic.gdx.graphics.Texture;
  19. import com.badlogic.gdx.math.Vector2;
  20. import com.badlogic.gdx.utils.Json;
  21. import com.badlogic.gdx.utils.Json.Serializable;
  22. import com.badlogic.gdx.utils.JsonValue;
  23.  
  24. public class Player implements Serializable{
  25.        
  26.         private static final long serialVersionIID = 1L;
  27.        
  28.         Vector2 position;
  29.         String textureLoc;
  30.        
  31.         private static final int col = 4;
  32.         private static final int row = 2;
  33.        
  34.         Animation animation;
  35.         Texture player;
  36.         TextureRegion[] frames;
  37.         TextureRegion currentFrame;
  38.         float stateTime;
  39.         Rectangle bounds;
  40.         String movement;
  41.        
  42.         public Player(Vector2 position, String textureLoc){
  43.                 this.position = position;
  44.                
  45.                 movement = "";
  46.                 player = new Texture(Gdx.files.internal("SpriteSheet.png"));
  47.                 TextureRegion[][] tr = TextureRegion.split(player, player.getWidth() / col, player.getHeight() / row);
  48.                
  49.                 frames = new TextureRegion[col * row];
  50.                 int index = 0;
  51.                 for(int i = 0; i < row;  i++){
  52.                         for(int j = 0; j < col; j++){
  53.                                 frames[index++] = tr [i][j];
  54.                         }
  55.                 }
  56.                 animation =  new Animation(0.025f, frames);
  57.                 stateTime = 0f;
  58.                 currentFrame = animation.getKeyFrame(0);
  59.                 bounds = new Rectangle(position.x, position.y, currentFrame.getRegionWidth(), currentFrame.getRegionHeight());
  60.                
  61.                
  62.         }
  63.         public void update(){
  64.                 bounds.set(position.x, position.y, currentFrame.getRegionWidth(), currentFrame.getRegionHeight());
  65.                
  66.                 if(stateTime > 4){
  67.                         stateTime += Gdx.graphics.getDeltaTime();
  68.                         }else{
  69.                         stateTime = 0;
  70.                 }      
  71.                
  72.                 //moving up and down
  73.                 if(Gdx.input.isKeyPressed(Keys.W)){
  74.                         position.y += 1f;
  75.                         currentFrame = animation.getKeyFrame(12 + stateTime / 2);
  76.                         movement = "Up";
  77.                 }
  78.                 if(Gdx.input.isKeyPressed(Keys.S)){
  79.                         position.y -= 1f;
  80.                         currentFrame = animation.getKeyFrame(0 + stateTime / 2);
  81.                         movement = "Down";
  82.                 }
  83.                 //moving left and right
  84.                 if(Gdx.input.isKeyPressed(Keys.A)){
  85.                         position.x -= 1f;
  86.                         currentFrame = animation.getKeyFrame(4 + stateTime / 2);
  87.                         movement = "Left";
  88.                 }
  89.                 if(Gdx.input.isKeyPressed(Keys.D)){
  90.                         position.x += 1f;
  91.                         currentFrame = animation.getKeyFrame(8 + stateTime / 2);
  92.                         movement = "Right";
  93.                         }
  94. }
  95.         public void redirect(){
  96.                 if(movement == "Up"){
  97.                         position.y -= 1f;
  98.                 }
  99.                 if(movement == "Down"){
  100.                         position.y += 1f;
  101.                 }
  102.                 if(movement == "Left"){
  103.                         position.x -= 1f;
  104.                 }
  105.                 if(movement == "Right"){
  106.                         position.x += 1f;
  107.                 }
  108.         }
  109.        
  110.         //where the file will be saved
  111.         public static void savePlayer(Player playerPosition)throws IOException{
  112.                 FileHandle file = Gdx.files.local("player.dat");
  113.                 OutputStream out = null;
  114.                 try{
  115.                         file.writeBytes(serialize(playerPosition.getPosition()), false);
  116.                 }catch(Exception ex){
  117.                         System.out.println(ex.toString());
  118.                        
  119.                 }finally{
  120.                         if(out != null) try{out.close();} catch(Exception ex){}
  121.                 }
  122.                 System.out.println("Saving Player");
  123.         }
  124.        
  125.         public static Vector2 readPlayer() throws IOException, ClassNotFoundException{
  126.                 Vector2 playerPosition = null;
  127.                 FileHandle file = Gdx.files.local("player.dat");
  128.                 playerPosition = (Vector2) deserialize(file.readBytes());
  129.                
  130.                 return playerPosition;
  131.         }
  132.  
  133.         //to write to a file in bytes
  134.         public static byte[] serialize(Object obj)throws IOException{
  135.                 ByteArrayOutputStream b = new ByteArrayOutputStream();
  136.                 ObjectOutputStream o = new ObjectOutputStream(b);
  137.                 o.writeObject(obj);
  138.                 return b.toByteArray();
  139.         }
  140.         //to read from a file
  141.         public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException{
  142.                
  143.                 ByteArrayInputStream b = new ByteArrayInputStream(bytes);
  144.                 ObjectInputStream o = new ObjectInputStream(b);
  145.                 return o.readObject();
  146.         }
  147.         /*public Texture getTexture() {
  148.                 return texture;
  149.         }
  150.         public void setTexture(Texture texture) {
  151.                 this.texture = texture;
  152.         }*/
  153.         public Vector2 getPosition(){
  154.             return position;
  155.     }
  156.     public void setPosition(Vector2 position){
  157.             this.position = position;
  158.     }
  159.         public String getTextureLoc() {
  160.                 return textureLoc;
  161.         }
  162.         public void setTextureLoc(String textureLoc) {
  163.                 this.textureLoc = textureLoc;
  164.         }
  165.     public Texture getPlayer() {
  166.                 return player;
  167.         }
  168.         public void setPlayer(Texture player) {
  169.                 this.player = player;
  170.         }
  171.         @Override
  172.     public void write(Json json) {    
  173.     }
  174.     @Override
  175.     public void read(Json json, JsonValue jsonData) {      
  176.     }
  177.     public Animation getAnimation() {
  178.                 return animation;
  179.         }
  180.         public void setAnimation(Animation animation) {
  181.                 this.animation = animation;
  182.         }
  183.         public TextureRegion[] getFrames() {
  184.                 return frames;
  185.         }
  186.         public void setFrames(TextureRegion[] frames) {
  187.                 this.frames = frames;
  188.         }
  189.         public TextureRegion getCurrentFrame() {
  190.                 return currentFrame;
  191.         }
  192.         public void setCurrentFrame(TextureRegion currentFrame) {
  193.                 this.currentFrame = currentFrame;
  194.         }
  195.         public float getStateTime() {
  196.                 return stateTime;
  197.         }
  198.         public void setStateTime(float stateTime) {
  199.                 this.stateTime = stateTime;
  200.         }
  201.         public Rectangle getBounds() {
  202.                 return bounds;
  203.         }
  204.         public void setBounds(Rectangle bounds) {
  205.                 this.bounds = bounds;
  206.         }
  207.         public static long getSerialversioniid() {
  208.             return serialVersionIID;
  209.         }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement