dimanche 3 mai 2015

Save/load function using serialization

I'm currently working on a GUI for my text turnbased fighting game, which has a player fight a random opponent. I've been trying to make a save function for the game, where the progress of the fight is saved i.e. the player's hp, the opponent's hp etc. as a "savegame.obj" file, and then load it, continuing where the fight was saved. So far I've been using serialization, and watched a lot of videos on youtube on the topic. But I'm almost clueless as to how to do this exactly, since the videos on youtube only deal with simple files like textfiles. I believe I have managed to save the game's progress, since I have created a save file. But whenever I load it, it doesn't really do anything. I'm pretty sure what I'm doing is wrong, but if anyone could show me how to do this the right way I'd be greatful. Here is my Game class with the main method(irrelevant code omitted):

import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Collections;
import java.util.Comparator;
import java.io.*;
/**
 * The Game class includes solely a main-method for starting the game
 * FieldOfHonor.
 *
 * @author
 * @version 0.5
 *
 */
public class Game {

    /**
     * Starts the game - creates a player and three items, adds the items to
     * players inventory. Also generates a list of opponents, a Field Of Honor
     * and starts the battle.
     *
     * @param args
     *            the system arguments
     */
    public static void main(String[] args) {
        final Player player1 = new Player("John Marston", "Gunslinger", "marston.png", 50, 20, 50, 100);

        GeneralStore store = new GeneralStore();

        System.out.println(player1);

        final ArrayList<Opponent> opponents = new ArrayList<Opponent>();
        opponents.add(new Opponent("Billy the Kid", "Gang Leader", "billy.png", 30, 30));
        opponents.add(new Opponent("Joe Dalton", "Robber", "joe_dalton.png", 35, 45));
        opponents.add(new Opponent("Everett Murdoch", "Nemesis", "murdoch.png", 50, 100));
        opponents.add(new Opponent("Marty McFly", "FutureMan", "mcfly.png", 22, 10));
        opponents.add(new Opponent("Rattata", "Dog", "rattata.png", 21, 5));


        final FieldOfHonor fieldOfHonor = new FieldOfHonor(player1, opponents, store);
        Log.p("Welcome to the game!");
        Log.p("Do you want to fight, go to the store or quit?");

        final GUI gui = new GUI(player1, fieldOfHonor.selectRandomOpponent());      

        gui.getSaveItem().addActionListener(new ActionListener()
            {
                @Override
                /**
                 * ActionListener for the save item
                 */
                public void actionPerformed(ActionEvent e) {

                    String fileName = "savegame.obj";
                    try {

                        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(fileName));
                        os.writeObject(fieldOfHonor); //write object
                        os.close();
                    } catch (FileNotFoundException f) {
                        // TODO Auto-generated catch block
                        f.printStackTrace();
                    } catch (IOException f) {
                        // TODO Auto-generated catch block
                        f.printStackTrace();
                    }

                    System.out.println("Game saved.");
                }
            });

        gui.getLoadItem().addActionListener(new ActionListener()
            {
                @Override
                /**
                 * ActionListener for the load item
                 */
                public void actionPerformed(ActionEvent e) {
                    String fileName = "savegame.obj";
                    try {
                        ObjectInputStream is = new ObjectInputStream(new FileInputStream(fileName));
                        FieldOfHonor foh = (FieldOfHonor) is.readObject(); //read object

                        is.close();
                    } catch (FileNotFoundException f) {
                        // TODO Auto-generated catch block
                        f.printStackTrace();
                    } catch (IOException f) {
                        f.printStackTrace();
                    } catch (ClassNotFoundException f) {
                        // TODO Auto-generated catch block
                        f.printStackTrace();
                    }

                    System.out.println("Game loaded.");
                }
            });




    }


}

Aucun commentaire:

Enregistrer un commentaire