Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. package com.horn.NuclearHolocaust.saving;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6.  
  7. import com.badlogic.gdx.math.Vector2;
  8. import com.badlogic.gdx.utils.Array;
  9. import com.esotericsoftware.kryo.Kryo;
  10. import com.esotericsoftware.kryo.io.Input;
  11. import com.esotericsoftware.kryo.io.Output;
  12. import com.horn.NuclearHolocaust.Items.Item;
  13. import com.horn.NuclearHolocaust.spells.Spell;
  14.  
  15. public class GameState {
  16.     String saveName;
  17.     Kryo kryo = new Kryo();
  18.     public String currentMap;
  19.     public Vector2 position;
  20.     public int playerHealth;
  21.     public int playerMana;
  22.     public Spell[] selectedSpells = new Spell[10];
  23.     public Array<Spell> allSpells;
  24.     public Array<Item> inventory;
  25.    
  26.     public GameState(String saveName){
  27.         this.saveName = saveName;
  28.     }
  29.    
  30.     public void save(){
  31.         try {
  32.             Output output = new Output(new FileOutputStream(saveName + ".bin"));
  33.            
  34.             //Position and angle
  35.             output.writeFloat(position.x);
  36.             output.writeFloat(position.y);
  37.            
  38.             //Health mana and xp
  39.             output.writeInt(playerHealth);
  40.             output.writeInt(playerMana);
  41.  
  42.             //All spells
  43.             for (Spell spell : allSpells)
  44.                 kryo.writeObject(output, spell);
  45.            
  46.             //Selected spells
  47.             for (int i=0; i<10; i++){
  48.                 if (selectedSpells[i]!=null){
  49.                     output.writeInt(allSpells.indexOf(selectedSpells[i], true)+1);
  50.                 }else{
  51.                     output.writeInt(0);
  52.                 }
  53.             }
  54.            
  55.             output.close();
  56.         } catch (FileNotFoundException e) {
  57.             e.printStackTrace();
  58.         }
  59.     }
  60.    
  61.     public void load(){
  62.         try {
  63.             Input input = new Input(new FileInputStream(saveName + ".bin"));
  64.             //Position and angle
  65.             position = new Vector2();
  66.             position.x = input.readFloat();
  67.             position.y = input.readFloat();
  68.            
  69.             //Health, mana and xp
  70.             playerHealth = input.readInt();
  71.             playerMana = input.readInt();
  72.            
  73.             //All spells
  74.             allSpells = new Array<Spell>();
  75.             Spell spell =kryo.readObjectOrNull(input, Spell.class);
  76.             while (spell!=null){
  77.                 allSpells.add(spell);
  78.                 spell = kryo.readObjectOrNull(input, Spell.class);
  79.             }
  80.            
  81.             //Selected spells
  82.             for (int i = 0; i<10; i++){
  83.                 int spellIndex = input.readInt();
  84.                 if (spellIndex != 0){
  85.                     selectedSpells[i] = allSpells.get(spellIndex-1);
  86.                 }
  87.             }
  88.             input.close();
  89.         } catch (FileNotFoundException e) {
  90.             e.printStackTrace();
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement