Jump trees

This commit is contained in:
austin 2022-04-02 20:38:39 -04:00
parent 3724f2dcf8
commit 4965c8f1a9
19 changed files with 476 additions and 31 deletions

View File

@ -64,7 +64,7 @@
{
"type" : "GROUND",
"acceleration" : 1000.0,
"maxVelocity" : 2.0,
"maxVelocity" : 2.5,
"animationStartup" : {
"name" : "Armature|Jog",
"length" : 1,
@ -94,6 +94,29 @@
"loops" : false
}
}
},
{
"type" : "JUMP",
"jumpFrames" : 30,
"jumpForce" : 0.05,
"animationJump" : {
"name" : "Armature|Jump",
"length" : 1,
"loops" : false
}
},
{
"type" : "FALL",
"animationFall" : {
"name" : "Armature|Fall",
"length" : 1,
"loops" : true
},
"animationLand" : {
"name" : "Armature|Land",
"length" : 1,
"loops" : true
}
}
],
"rotatorSystem" : {

Binary file not shown.

View File

@ -12,6 +12,7 @@ import electrosphere.entity.state.inventory.InventoryUtils;
import electrosphere.entity.state.inventory.UnrelationalInventoryState;
import electrosphere.entity.state.ironsight.IronSightTree;
import electrosphere.entity.state.movement.GroundMovementTree;
import electrosphere.entity.state.movement.JumpTree;
import electrosphere.entity.state.movement.GroundMovementTree.MovementRelativeFacing;
import electrosphere.entity.state.movement.GroundMovementTree.MovementTreeState;
import electrosphere.entity.state.movement.SprintTree;
@ -586,6 +587,19 @@ public class ControlHandler {
}
}});
/*
Jump
DATA_STRING_INPUT_CODE_MOVEMENT_JUMP
*/
mainGameControlList.add(controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_JUMP));
controls.get(DATA_STRING_INPUT_CODE_MOVEMENT_JUMP).setOnPress(new ControlMethod(){public void execute(){
if(Globals.playerCharacter != null){
JumpTree jumpTree = JumpTree.getJumpTree(Globals.playerCharacter);
if(jumpTree != null){
jumpTree.start();
}
}
}});
/*
Sprint
*/
mainGameControlList.add(controls.get(INPUT_CODE_SPRINT));

View File

@ -43,6 +43,8 @@ public class EntityDataStrings {
public static final String DATA_STRING_MAX_NATURAL_VELOCITY = "velocityMaxNatural";
public static final String CREATURE_ATTRIBUTE_VARIANT = "creatureAttributeVariant";
public static final String ROTATOR_TREE = "rotatorTree";
public static final String JUMP_TREE = "jumpTree";
public static final String FALL_TREE = "fallTree";
/*

View File

@ -115,7 +115,7 @@ public class CollidableTree {
// // Globals.controlHandler.showMouse();
// Vector3d pos = impulse.getWorldPoint();
// // pos = new Vector3d(position).add(impulse.getCollisionPoint()).mul(1,0,1);
// DebugVisualizerUtils.spawnVectorVisualizer(pos, new Vector3d(torqueVec));
// DebugVisualizerUtils.spawnVectorVisualizer(impulse.getWorldPoint(), new Vector3d(torqueVec));
// }
// System.out.println("Impulse: " + torqueVec + " " + torqueMag);
}

View File

@ -4,6 +4,8 @@ import electrosphere.collision.dispatch.CollisionObject;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.collidable.Impulse;
import electrosphere.entity.state.movement.FallTree;
import electrosphere.entity.state.movement.JumpTree;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.game.collision.PhysicsUtils;
import electrosphere.game.collision.collidable.Collidable;
@ -31,6 +33,9 @@ public class GravityTree {
Entity parent;
int frameCurrent = 0;
int fallFrame = 1;
float gravityVelocity = 0;
float gravityAccel = 0.0002f;
@ -39,11 +44,12 @@ public class GravityTree {
CopyOnWriteArrayList<EntityMessage> networkMessageQueue = new CopyOnWriteArrayList();
public GravityTree(Entity e, Collidable collidable, CollisionObject body){
public GravityTree(Entity e, Collidable collidable, CollisionObject body, int fallFrame){
state = GravityTreeState.ACTIVE;
parent = e;
this.body = body;
this.collidable = collidable;
this.fallFrame = fallFrame;
}
// public void setCollisionObject(CollisionObject body, Collidable collidable){
@ -58,6 +64,9 @@ public class GravityTree {
public void start(){
//TODO: check if can start moving
state = GravityTreeState.ACTIVE;
if(state == GravityTreeState.NOT_ACTIVE){
frameCurrent = 0;
}
}
public void interrupt(){
@ -110,8 +119,27 @@ public class GravityTree {
if(!hadStructureCollision()){
// position.set(new Vector3d(position.x,Globals.commonWorldData.getElevationAtPoint(position) + 0.0001f,position.z));
}
JumpTree jumpTree;
if((jumpTree = JumpTree.getJumpTree(parent))!=null){
jumpTree.land();
}
FallTree fallTree;
if((fallTree = FallTree.getFallTree(parent))!=null){
fallTree.land();
}
frameCurrent = 0;
gravityVelocity = 0;
} else {
//animation nonsense
frameCurrent++;
if(frameCurrent == fallFrame){
FallTree fallTree;
if((fallTree = FallTree.getFallTree(parent))!=null){
fallTree.start();
}
}
//actual gravity calculations
if(gravityVelocity < gravityConstant){
gravityVelocity = gravityVelocity + gravityAccel;
}

View File

@ -0,0 +1,101 @@
package electrosphere.entity.state.movement;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.BehaviorTree;
import electrosphere.renderer.actor.Actor;
public class FallTree implements BehaviorTree {
static enum FallState {
ACTIVE,
INACTIVE,
}
FallState state = FallState.INACTIVE;
String animationFall = "Armature|Fall";
String animationLand = "Armature|Land";
Entity parent;
JumpTree jumpTree;
public FallTree(Entity parent){
this.parent = parent;
}
@Override
public void simulate() {
Actor entityActor = EntityUtils.getActor(parent);
switch(state){
case ACTIVE:
if(entityActor != null){
String animationToPlay = determineCorrectAnimation();
if(
!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(animationToPlay) &&
(jumpTree == null || !jumpTree.isJumping())
){
entityActor.playAnimation(animationToPlay,1);
entityActor.incrementAnimationTime(0.01);
}
}
break;
case INACTIVE:
break;
}
}
public void start(){
state = FallState.ACTIVE;
}
public boolean isFalling(){
return state == FallState.ACTIVE;
}
public void land(){
if(state != FallState.INACTIVE){
state = FallState.INACTIVE;
Actor entityActor = EntityUtils.getActor(parent);
if(entityActor != null){
String animationToPlay = determineCorrectAnimation();
if(
!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(animationToPlay)
){
entityActor.playAnimation(animationToPlay,1);
entityActor.incrementAnimationTime(0.01);
}
}
}
}
public static FallTree getFallTree(Entity parent){
return (FallTree)parent.getData(EntityDataStrings.FALL_TREE);
}
String determineCorrectAnimation(){
switch(state){
case ACTIVE:
return animationFall;
case INACTIVE:
return animationLand;
default:
return animationLand;
}
}
public void setJumpTree(JumpTree jumpTree){
this.jumpTree = jumpTree;
}
public void setAnimationFall(String animationName){
animationFall = animationName;
}
public void setAnimationLand(String animationName){
animationLand = animationName;
}
}

View File

@ -69,6 +69,8 @@ public class GroundMovementTree {
MovementRelativeFacing facing;
SprintTree sprintTree;
JumpTree jumpTree;
FallTree fallTree;
Entity parent;
@ -221,7 +223,11 @@ public class GroundMovementTree {
case STARTUP:
if(entityActor != null){
String animationToPlay = determineCorrectAnimation();
if(!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(animationToPlay)){
if(
!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(animationToPlay) &&
(jumpTree == null || !jumpTree.isJumping()) &&
(fallTree == null || !fallTree.isFalling())
){
entityActor.playAnimation(animationToPlay,1);
entityActor.incrementAnimationTime(0.01);
}
@ -302,7 +308,11 @@ public class GroundMovementTree {
//if yes, restart animation
if(entityActor != null){
String animationToPlay = determineCorrectAnimation();
if(!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(animationToPlay)){
if(
!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(animationToPlay) &&
(jumpTree == null || !jumpTree.isJumping()) &&
(fallTree == null || !fallTree.isFalling())
){
entityActor.playAnimation(animationToPlay,1);
entityActor.incrementAnimationTime(0.01);
}
@ -377,7 +387,11 @@ public class GroundMovementTree {
//run slowdown code
if(entityActor != null){
String animationToPlay = determineCorrectAnimation();
if(!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(animationToPlay)){
if(
!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(animationToPlay) &&
(jumpTree == null || !jumpTree.isJumping()) &&
(fallTree == null || !fallTree.isFalling())
){
entityActor.playAnimation(animationToPlay,1);
entityActor.incrementAnimationTime(0.01);
}
@ -510,6 +524,14 @@ public class GroundMovementTree {
this.sprintTree = sprintTree;
}
public void setJumpTree(JumpTree jumpTree){
this.jumpTree = jumpTree;
}
public void setFallTree(FallTree fallTree){
this.fallTree = fallTree;
}
public MovementRelativeFacing getFacing(){
return facing;
}

View File

@ -0,0 +1,102 @@
package electrosphere.entity.state.movement;
import org.joml.Vector3d;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.BehaviorTree;
import electrosphere.entity.state.collidable.Impulse;
import electrosphere.entity.state.gravity.GravityUtils;
import electrosphere.entity.types.collision.CollisionObjUtils;
import electrosphere.game.collision.collidable.Collidable;
import electrosphere.renderer.actor.Actor;
public class JumpTree implements BehaviorTree {
static enum JumpState {
INACTIVE,
ACTIVE,
AWAITING_LAND,
}
JumpState state = JumpState.INACTIVE;
String animationJump = "Armature|Jump";
Entity parent;
int jumpFrames = 0;
int currentFrame = 0;
float jumpForce = 1.0f;
float currentJumpForce = jumpForce;
static final float jumpFalloff = 0.99f;
public JumpTree(Entity parent, int jumpFrames, float jumpForce){
this.parent = parent;
this.jumpFrames = jumpFrames;
this.jumpForce = jumpForce;
}
public void start(){
if(state == JumpState.INACTIVE){
state = JumpState.ACTIVE;
currentFrame = 0;
currentJumpForce = jumpForce;
GravityUtils.attemptActivateGravity(parent);
}
}
@Override
public void simulate() {
Actor entityActor = EntityUtils.getActor(parent);
switch(state){
case ACTIVE:
if(entityActor != null){
String animationToPlay = determineCorrectAnimation();
if(!entityActor.isPlayingAnimation() || !entityActor.isPlayingAnimation(animationToPlay)){
entityActor.playAnimation(animationToPlay,1);
entityActor.incrementAnimationTime(0.01);
}
}
currentFrame++;
currentJumpForce = currentJumpForce * jumpFalloff;
//push parent up
CollisionObjUtils.getCollidable(parent).addImpulse(new Impulse(new Vector3d(0,1,0), new Vector3d(0,0,0), new Vector3d(EntityUtils.getPosition(parent)), currentJumpForce, Collidable.TYPE_FORCE));
//potentially disable
if(currentFrame >= jumpFrames){
state = JumpState.AWAITING_LAND;
GravityUtils.attemptActivateGravity(parent);
}
break;
case INACTIVE:
break;
case AWAITING_LAND:
break;
}
}
public static JumpTree getJumpTree(Entity parent){
return (JumpTree)parent.getData(EntityDataStrings.JUMP_TREE);
}
public void land(){
if(state != JumpState.INACTIVE && currentFrame > 2){
state = JumpState.INACTIVE;
}
}
public boolean isJumping(){
return state == JumpState.ACTIVE;
}
String determineCorrectAnimation(){
return animationJump;
}
public void setAnimationJump(String animationName){
animationJump = animationName;
}
}

View File

@ -5,11 +5,12 @@ import electrosphere.dynamics.RigidBody;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.movement.FallTree;
import electrosphere.entity.state.movement.GroundMovementTree;
import electrosphere.entity.state.movement.JumpTree;
import electrosphere.entity.types.hitbox.HitboxData;
import electrosphere.entity.types.hitbox.HitboxUtils;
import electrosphere.game.data.creature.type.CreatureType;
import electrosphere.game.data.creature.type.MovementSystem;
import electrosphere.entity.state.AttackTree;
import electrosphere.entity.state.BehaviorTree;
import electrosphere.entity.state.IdleTree;
@ -27,6 +28,10 @@ import electrosphere.game.data.creature.type.CollidableTemplate;
import electrosphere.game.data.creature.type.SprintSystem;
import electrosphere.game.data.creature.type.attack.AttackMove;
import electrosphere.game.data.creature.type.equip.EquipPoint;
import electrosphere.game.data.creature.type.movement.FallMovementSystem;
import electrosphere.game.data.creature.type.movement.GroundMovementSystem;
import electrosphere.game.data.creature.type.movement.JumpMovementSystem;
import electrosphere.game.data.creature.type.movement.MovementSystem;
import electrosphere.game.data.creature.type.rotator.RotatorItem;
import electrosphere.game.data.creature.type.rotator.RotatorSystem;
import electrosphere.game.data.creature.type.visualattribute.AttributeVariant;
@ -153,19 +158,20 @@ public class CreatureUtils {
}
for(MovementSystem movementSystem : rawType.getMovementSystems()){
switch(movementSystem.getType()){
case "GROUND":
case GroundMovementSystem.GROUND_MOVEMENT_SYSTEM:
GroundMovementSystem groundMovementSystem = (GroundMovementSystem)movementSystem;
GroundMovementTree moveTree = new GroundMovementTree(rVal,CollisionObjUtils.getCollidable(rVal));
if(movementSystem.getAnimationStartup() != null){
moveTree.setAnimationStartUp(movementSystem.getAnimationStartup().getName());
if(groundMovementSystem.getAnimationStartup() != null){
moveTree.setAnimationStartUp(groundMovementSystem.getAnimationStartup().getName());
}
if(movementSystem.getAnimationLoop() != null){
moveTree.setAnimationMain(movementSystem.getAnimationLoop().getName());
if(groundMovementSystem.getAnimationLoop() != null){
moveTree.setAnimationMain(groundMovementSystem.getAnimationLoop().getName());
}
if(movementSystem.getAnimationWindDown()!= null){
moveTree.setAnimationSlowDown(movementSystem.getAnimationWindDown().getName());
if(groundMovementSystem.getAnimationWindDown()!= null){
moveTree.setAnimationSlowDown(groundMovementSystem.getAnimationWindDown().getName());
}
if(movementSystem.getSprintSystem() != null){
SprintSystem sprintSystem = movementSystem.getSprintSystem();
if(groundMovementSystem.getSprintSystem() != null){
SprintSystem sprintSystem = groundMovementSystem.getSprintSystem();
SprintTree sprintTree = new SprintTree(rVal,sprintSystem.getMaxVelocity(),sprintSystem.getStaminaMax());
if(sprintSystem.getAnimationStartUp()!= null){
moveTree.setAnimationSprintStartUp(sprintSystem.getAnimationStartUp().getName());
@ -181,13 +187,47 @@ public class CreatureUtils {
rVal.putData(EntityDataStrings.SPRINT_TREE,sprintTree);
Globals.entityManager.registerSprintableEntity(rVal);
}
//round out end of move system
rVal.putData(EntityDataStrings.DATA_STRING_MOVEMENT_BT, moveTree);
rVal.putData(EntityDataStrings.DATA_STRING_FACING_VECTOR, new Vector3f(0,0,0));
rVal.putData(EntityDataStrings.DATA_STRING_MAX_NATURAL_VELOCITY, movementSystem.getMaxVelocity());
rVal.putData(EntityDataStrings.DATA_STRING_ACCELERATION, movementSystem.getAcceleration());
rVal.putData(EntityDataStrings.DATA_STRING_MAX_NATURAL_VELOCITY, groundMovementSystem.getMaxVelocity());
rVal.putData(EntityDataStrings.DATA_STRING_ACCELERATION, groundMovementSystem.getAcceleration());
rVal.putData(EntityDataStrings.DATA_STRING_VELOCITY, 0f);
Globals.entityManager.registerMoveableEntity(rVal);
break;
case JumpMovementSystem.JUMP_MOVEMENT_SYSTEM:
JumpMovementSystem jumpMovementSystem = (JumpMovementSystem)movementSystem;
JumpTree jumpTree = new JumpTree(rVal, jumpMovementSystem.getJumpFrames(), jumpMovementSystem.getJumpForce());
if(jumpMovementSystem.getAnimationJump() != null){
jumpTree.setAnimationJump(jumpMovementSystem.getAnimationJump().getName());
}
if(CreatureUtils.getEntityMovementTree(rVal) != null){
CreatureUtils.getEntityMovementTree(rVal).setJumpTree(jumpTree);
}
if(FallTree.getFallTree(rVal)!=null){
FallTree.getFallTree(rVal).setJumpTree(jumpTree);
}
rVal.putData(EntityDataStrings.JUMP_TREE, jumpTree);
Globals.entityManager.registerBehaviorTree(jumpTree);
break;
case FallMovementSystem.FALL_MOVEMENT_SYSTEM:
FallMovementSystem fallMovementSystem = (FallMovementSystem)movementSystem;
FallTree fallTree = new FallTree(rVal);
if(fallMovementSystem.getAnimationFall()!=null){
fallTree.setAnimationFall(fallMovementSystem.getAnimationFall().getName());
}
if(fallMovementSystem.getAnimationLand()!=null){
fallTree.setAnimationLand(fallMovementSystem.getAnimationLand().getName());
}
if(CreatureUtils.getEntityMovementTree(rVal) != null){
CreatureUtils.getEntityMovementTree(rVal).setFallTree(fallTree);
}
if(JumpTree.getJumpTree(rVal)!=null){
fallTree.setJumpTree(JumpTree.getJumpTree(rVal));
}
rVal.putData(EntityDataStrings.FALL_TREE, fallTree);
Globals.entityManager.registerBehaviorTree(fallTree);
break;
}
}
if(rawType.getEquipPoints() != null && rawType.getEquipPoints().size() > 0){
@ -222,7 +262,7 @@ public class CreatureUtils {
case "GRAVITY":
Collidable collidable = (Collidable)rVal.getData(EntityDataStrings.PHYSICS_COLLIDABLE);
CollisionObject collisionObject = (CollisionObject)rVal.getData(EntityDataStrings.PHYSICS_COLLISION_BODY);
GravityTree gravityTree = new GravityTree(rVal,collidable,collisionObject);
GravityTree gravityTree = new GravityTree(rVal,collidable,collisionObject,30);
// gravityTree.setCollisionObject(collisionObject, collidable);
rVal.putData(EntityDataStrings.GRAVITY_ENTITY, true);
rVal.putData(EntityDataStrings.GRAVITY_TREE, gravityTree);

View File

@ -123,7 +123,7 @@ public class ItemUtils {
case "GRAVITY":
Collidable collidable = (Collidable)rVal.getData(EntityDataStrings.PHYSICS_COLLIDABLE);
CollisionObject collisionObject = (CollisionObject)rVal.getData(EntityDataStrings.PHYSICS_COLLISION_BODY);
GravityTree gravityTree = new GravityTree(rVal,collidable,collisionObject);
GravityTree gravityTree = new GravityTree(rVal,collidable,collisionObject,30);
// gravityTree.setCollisionObject(collisionObject, collidable);
rVal.putData(EntityDataStrings.GRAVITY_ENTITY, true);
rVal.putData(EntityDataStrings.GRAVITY_TREE, gravityTree);

View File

@ -27,6 +27,7 @@ public class Collidable {
public static final String TYPE_CREATURE = "creature";
public static final String TYPE_STRUCTURE = "structure";
public static final String TYPE_ITEM = "item";
public static final String TYPE_FORCE = "force";
public Collidable(Entity parent, String type){

View File

@ -4,6 +4,7 @@ import electrosphere.entity.types.hitbox.HitboxData;
import electrosphere.game.data.creature.type.attack.AttackMove;
import electrosphere.game.data.creature.type.attack.AttackMoveResolver;
import electrosphere.game.data.creature.type.equip.EquipPoint;
import electrosphere.game.data.creature.type.movement.MovementSystem;
import electrosphere.game.data.creature.type.rotator.RotatorSystem;
import electrosphere.game.data.creature.type.visualattribute.VisualAttribute;

View File

@ -0,0 +1,27 @@
package electrosphere.game.data.creature.type.movement;
import electrosphere.game.data.creature.type.Animation;
public class FallMovementSystem implements MovementSystem {
public static final String FALL_MOVEMENT_SYSTEM = "FALL";
String type;
Animation animationFall;
Animation animationLand;
public Animation getAnimationFall(){
return animationFall;
}
public Animation getAnimationLand(){
return animationLand;
}
@Override
public String getType() {
return type;
}
}

View File

@ -1,7 +1,14 @@
package electrosphere.game.data.creature.type;
package electrosphere.game.data.creature.type.movement;
import electrosphere.game.data.creature.type.Animation;
import electrosphere.game.data.creature.type.SprintSystem;
public class GroundMovementSystem implements MovementSystem {
public static final String GROUND_MOVEMENT_SYSTEM = "GROUND";
public class MovementSystem {
String type;
float acceleration;
float maxVelocity;
Animation animationStartup;
@ -9,9 +16,6 @@ public class MovementSystem {
Animation animationWindDown;
SprintSystem sprintSystem;
public String getType() {
return type;
}
public float getAcceleration() {
return acceleration;
@ -37,5 +41,8 @@ public class MovementSystem {
return sprintSystem;
}
@Override
public String getType() {
return type;
}
}

View File

@ -0,0 +1,33 @@
package electrosphere.game.data.creature.type.movement;
import electrosphere.game.data.creature.type.Animation;
public class JumpMovementSystem implements MovementSystem {
public static final String JUMP_MOVEMENT_SYSTEM = "JUMP";
String type;
Animation animationJump;
int jumpFrames;
float jumpForce;
public int getJumpFrames(){
return jumpFrames;
}
public float getJumpForce(){
return jumpForce;
}
public Animation getAnimationJump(){
return animationJump;
}
@Override
public String getType() {
return type;
}
}

View File

@ -0,0 +1,10 @@
package electrosphere.game.data.creature.type.movement;
import electrosphere.game.data.creature.type.Animation;
import electrosphere.game.data.creature.type.SprintSystem;
public interface MovementSystem {
public String getType();
}

View File

@ -0,0 +1,27 @@
package electrosphere.game.data.creature.type.movement;
import java.lang.reflect.Type;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
public class MovementSystemSerializer implements JsonDeserializer<MovementSystem> {
@Override
public MovementSystem deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
// TODO Auto-generated method stub
switch(json.getAsJsonObject().get("type").getAsString()){
case GroundMovementSystem.GROUND_MOVEMENT_SYSTEM:
return context.deserialize(json, GroundMovementSystem.class);
case JumpMovementSystem.JUMP_MOVEMENT_SYSTEM:
return context.deserialize(json, JumpMovementSystem.class);
case FallMovementSystem.FALL_MOVEMENT_SYSTEM:
return context.deserialize(json, FallMovementSystem.class);
}
return null;
}
}

View File

@ -1,6 +1,10 @@
package electrosphere.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import electrosphere.game.data.creature.type.movement.MovementSystem;
import electrosphere.game.data.creature.type.movement.MovementSystemSerializer;
import electrosphere.main.Main;
import java.io.BufferedReader;
import java.io.File;
@ -18,7 +22,13 @@ import java.util.logging.Logger;
public class FileUtils {
static {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(MovementSystem.class, new MovementSystemSerializer());
gson = gsonBuilder.create();
}
static Gson gson;
static final int maxReadFails = 3;
static final int READ_TIMEOUT_DURATION = 5;
@ -129,7 +139,6 @@ public class FileUtils {
public static void serializeObjectToFilePath(String filePath, Object object){
Path path = new File(filePath).toPath();
Gson gson = new Gson();
try {
Files.write(path, gson.toJson(object).getBytes());
} catch (IOException ex) {
@ -167,7 +176,6 @@ public class FileUtils {
public static <T>T loadObjectFromAssetPath(String pathName, Class<T> className){
T rVal = null;
String sanitizedFilePath = sanitizeFilePath(pathName);
Gson gson = new Gson();
try {
rVal = gson.fromJson(Files.newBufferedReader(getAssetFile(sanitizedFilePath).toPath()), className);
} catch (IOException ex) {
@ -185,7 +193,6 @@ public class FileUtils {
public static <T>T loadObjectFromSavePath(String saveName, String pathName, Class<T> className){
T rVal = null;
String sanitizedFilePath = sanitizeFilePath(pathName);
Gson gson = new Gson();
try {
rVal = gson.fromJson(Files.newBufferedReader(getSaveFile(saveName,sanitizedFilePath).toPath()), className);
} catch (IOException ex) {