/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.theinvader360.tutorial.libgdx.gameservices;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import com.badlogic.gdx.Gdx;
public class Settings {
public static boolean soundEnabled = true;
public final static int[] highscores = new int[] {100, 80, 50, 30, 10};
public final static String file = ".superjumper";
public static void load () {
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(Gdx.files.external(file).read()));
soundEnabled = Boolean.parseBoolean(in.readLine());
for (int i = 0; i < 5; i++) {
highscores[i] = Integer.parseInt(in.readLine());
}
} catch (Throwable e) {
// :( It's ok we have defaults
} finally {
try {
if (in != null) in.close();
} catch (IOException e) {
}
}
}
public static void save () {
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(Gdx.files.external(file).write(false)));
out.write(Boolean.toString(soundEnabled));
for (int i = 0; i < 5; i++) {
out.write(Integer.toString(highscores[i]));
}
} catch (Throwable e) {
} finally {
try {
if (out != null) out.close();
} catch (IOException e) {
}
}
}
public static void addScore (int score) {
for (int i = 0; i < 5; i++) {
if (highscores[i] < score) {
for (int j = 4; j > i; j--)
highscores[j] = highscores[j - 1];
highscores[i] = score;
break;
}
}
}
}