Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.horn.NuclearHolocaust.saving;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.utils.Array;
- import com.esotericsoftware.kryo.Kryo;
- import com.esotericsoftware.kryo.io.Input;
- import com.esotericsoftware.kryo.io.Output;
- import com.horn.NuclearHolocaust.Items.Item;
- import com.horn.NuclearHolocaust.spells.Spell;
- public class GameState {
- String saveName;
- Kryo kryo = new Kryo();
- public String currentMap;
- public Vector2 position;
- public int playerHealth;
- public int playerMana;
- public Spell[] selectedSpells = new Spell[10];
- public Array<Spell> allSpells;
- public Array<Item> inventory;
- public GameState(String saveName){
- this.saveName = saveName;
- }
- public void save(){
- try {
- Output output = new Output(new FileOutputStream(saveName + ".bin"));
- //Position and angle
- output.writeFloat(position.x);
- output.writeFloat(position.y);
- //Health mana and xp
- output.writeInt(playerHealth);
- output.writeInt(playerMana);
- //All spells
- for (Spell spell : allSpells)
- kryo.writeObject(output, spell);
- //Selected spells
- for (int i=0; i<10; i++){
- if (selectedSpells[i]!=null){
- output.writeInt(allSpells.indexOf(selectedSpells[i], true)+1);
- }else{
- output.writeInt(0);
- }
- }
- output.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- public void load(){
- try {
- Input input = new Input(new FileInputStream(saveName + ".bin"));
- //Position and angle
- position = new Vector2();
- position.x = input.readFloat();
- position.y = input.readFloat();
- //Health, mana and xp
- playerHealth = input.readInt();
- playerMana = input.readInt();
- //All spells
- allSpells = new Array<Spell>();
- Spell spell =kryo.readObjectOrNull(input, Spell.class);
- while (spell!=null){
- allSpells.add(spell);
- spell = kryo.readObjectOrNull(input, Spell.class);
- }
- //Selected spells
- for (int i = 0; i<10; i++){
- int spellIndex = input.readInt();
- if (spellIndex != 0){
- selectedSpells[i] = allSpells.get(spellIndex-1);
- }
- }
- input.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement