Skip to content

Commit

Permalink
Moved some logic in game db
Browse files Browse the repository at this point in the history
  • Loading branch information
macbury committed May 11, 2015
1 parent c33438c commit 9d20e8a
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ html/war/WEB-INF/classes/
gwt-unitCache/
www-test/
.gwt-tmp/

game.config
## Android Studio and Intellij and Android in general
android/libs/armeabi/
android/libs/armeabi-v7a/
Expand Down
2 changes: 1 addition & 1 deletion core/assets/db/entities/barell.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
{
class: "renderable",
path: "raw-models/barell.g3db"
path: "graphics/models/barell.g3db"
},

{
Expand Down
2 changes: 1 addition & 1 deletion core/assets/db/entities/crate.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
{
class: "renderable",
path: "raw-models/Crate.g3db"
path: "graphics/models/Crate.g3db"
},

{
Expand Down
2 changes: 1 addition & 1 deletion core/assets/db/entities/ham.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
{
class: "renderable",
path: "raw-models/Ham.g3db"
path: "graphics/models/Ham.g3db"
},

{
Expand Down
Binary file added core/assets/graphics/models/Barrel texture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added core/assets/graphics/models/Crate Texture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added core/assets/graphics/models/Crate.g3db
Binary file not shown.
Binary file added core/assets/graphics/models/Ham Texture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added core/assets/graphics/models/Ham.g3db
Binary file not shown.
Binary file added core/assets/graphics/models/barell.g3db
Binary file not shown.
33 changes: 26 additions & 7 deletions core/src/macbury/forge/db/GameDatabase.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package macbury.forge.db;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import macbury.forge.ForgE;
import macbury.forge.db.models.PlayerStartPosition;
import macbury.forge.db.models.BaseModel;
import macbury.forge.db.models.Teleport;
import macbury.forge.utils.Vector3i;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
* Created by macbury on 07.11.14.
*/
public class GameDatabase {
public static final String FILE_NAME = "game.db";
public static final String FILE_NAME = "db/game.config";
private static final String TAG = "GameDatabase";
public String title;
public int currentyUID = 0;
public long build = 0;
public int lastOpenedMapId = -1;
public PlayerStartPosition startPosition;
public Teleport startPosition;

/**
* Create int uuid
* @return unique id
Expand All @@ -34,11 +43,21 @@ public void bootstrap() {

public void setStartPosition(int levelId, Vector3i voxelPosition) {
Gdx.app.log(TAG, "New player start position: " + levelId + " at " + voxelPosition.toString());
startPosition = new PlayerStartPosition();
startPosition.mapId = levelId;
startPosition.voxelPosition = new Vector3i(voxelPosition);
this.startPosition = new Teleport(voxelPosition, levelId);
save();
}

ForgE.storage.saveDB(this);
public void save(BaseModel model) {
Kryo kryo = ForgE.storage.begin(); {
FileHandle modelFile = model.getFileHandle();
try {
Output output = new Output(new FileOutputStream(modelFile.file(), false));
kryo.writeObject(output, model);
output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} ForgE.storage.end(kryo);
}

public void save() {
Expand Down
18 changes: 18 additions & 0 deletions core/src/macbury/forge/db/models/BaseModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package macbury.forge.db.models;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;

/**
* Created by macbury on 11.05.15.
*/
public abstract class BaseModel {
private static final String DB_PATH = "db/";

public abstract String getFilename();
public abstract String getStorageDir();

public FileHandle getFileHandle() {
return Gdx.files.internal(DB_PATH+ getStorageDir() + getFilename());
}
}
16 changes: 0 additions & 16 deletions core/src/macbury/forge/db/models/PlayerStartPosition.java

This file was deleted.

5 changes: 3 additions & 2 deletions core/src/macbury/forge/db/models/Teleport.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
/**
* Created by macbury on 24.03.15.
*/
public class Teleport {
public class Teleport {
public final Vector3i voxelPosition;
public final int mapId;

public Teleport(Vector3i voxelPosition, int mapId) {
this.voxelPosition = voxelPosition;
this.voxelPosition = new Vector3i(voxelPosition);
this.mapId = mapId;
}

}
5 changes: 0 additions & 5 deletions core/src/macbury/forge/screens/LoadingScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.PerformanceCounter;
import macbury.forge.ForgE;
import macbury.forge.db.models.PlayerStartPosition;
import macbury.forge.db.models.Teleport;
import macbury.forge.level.loader.AsyncLevelLoader;
import macbury.forge.level.Level;
Expand All @@ -37,10 +36,6 @@ public class LoadingScreen extends AbstractScreen implements Promise<Level> {
private Matrix4 boxTransMat;
private float indicatorRotation;

public LoadingScreen(PlayerStartPosition startPosition) {
this(startPosition.toTeleport());
}

public LoadingScreen(Teleport teleport) {
super();
this.teleport = teleport;
Expand Down
11 changes: 9 additions & 2 deletions core/src/macbury/forge/storage/StorageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import macbury.forge.assets.assets.ModelAsset;
import macbury.forge.assets.assets.TextureAsset;
import macbury.forge.db.GameDatabase;
import macbury.forge.db.models.PlayerStartPosition;
import macbury.forge.db.models.Teleport;
import macbury.forge.graphics.batch.renderable.VoxelChunkRenderable;
import macbury.forge.level.LevelEnv;
import macbury.forge.level.LevelState;
Expand Down Expand Up @@ -62,7 +62,7 @@ public Kryo create () {
kryo.register(VoxelChunkRenderable.class, new VoxelFaceRenderableSerializer());
kryo.register(Matrix4.class, new Matrix4Serializer());
kryo.register(Vector3i.class, new Vector3iSerializer());
kryo.register(PlayerStartPosition.class, new PlayerStartPositionSerializer());
kryo.register(Teleport.class, new PlayerStartPositionSerializer());
kryo.register(TextureAsset.class, new AssetSerializer());
kryo.register(ModelAsset.class, new AssetSerializer());
kryo.setDefaultSerializer(TaggedFieldSerializer.class);
Expand Down Expand Up @@ -104,4 +104,11 @@ public void saveDB(GameDatabase db) {
}


public Kryo begin() {
return pool.borrow();
}

public void end(Kryo kryo) {
pool.release(kryo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import macbury.forge.db.GameDatabase;
import macbury.forge.db.models.PlayerStartPosition;
import macbury.forge.db.models.Teleport;

/**
* Created by macbury on 10.11.14.
Expand All @@ -17,7 +17,7 @@ public void write(Kryo kryo, Output output, GameDatabase gameDatabase) {
output.writeInt(gameDatabase.currentyUID);
output.writeString(gameDatabase.title);
output.writeLong(gameDatabase.build++);
kryo.writeObjectOrNull(output, gameDatabase.startPosition, PlayerStartPosition.class);
kryo.writeObjectOrNull(output, gameDatabase.startPosition, Teleport.class);
output.writeInt(gameDatabase.lastOpenedMapId);
}

Expand All @@ -28,7 +28,7 @@ public GameDatabase read(Kryo kryo, Input input, Class<GameDatabase> type) {
db.currentyUID = input.readInt();
db.title = input.readString();
db.build = input.readLong();
db.startPosition = kryo.readObjectOrNull(input, PlayerStartPosition.class);
db.startPosition = kryo.readObjectOrNull(input, Teleport.class);
db.lastOpenedMapId = input.readInt();
return db;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import macbury.forge.db.models.PlayerStartPosition;
import macbury.forge.db.models.Teleport;
import macbury.forge.utils.Vector3i;

/**
* Created by macbury on 24.03.15.
*/
public class PlayerStartPositionSerializer extends Serializer<PlayerStartPosition> {
public class PlayerStartPositionSerializer extends Serializer<Teleport> {
@Override
public void write(Kryo kryo, Output output, PlayerStartPosition object) {
public void write(Kryo kryo, Output output, Teleport object) {
kryo.writeObject(output, object.voxelPosition);
output.writeInt(object.mapId, true);
}

@Override
public PlayerStartPosition read(Kryo kryo, Input input, Class<PlayerStartPosition> type) {
PlayerStartPosition startPosition = new PlayerStartPosition();
startPosition.voxelPosition = kryo.readObject(input, Vector3i.class);
startPosition.mapId = input.readInt(true);
public Teleport read(Kryo kryo, Input input, Class<Teleport> type) {
Teleport startPosition = new Teleport(kryo.readObject(input, Vector3i.class), input.readInt(true));
return startPosition;
}
}

0 comments on commit 9d20e8a

Please sign in to comment.