gpt4 book ai didi

java - libGdx 中的 GameState 到 JSON 的序列化不起作用,我该如何修复它?

转载 作者:行者123 更新时间:2023-11-30 07:29:05 25 4
gpt4 key购买 nike

我在将 GameState 保存为 JSON 时遇到问题。

最初我将 GameState 保存为 JSON,如下所示:

Json json = new JSON();
String save = json.prettyPrint(state);

我加载它的方式如下:

json.fromJson(GameState.class, save);

但是,状态加载不正确,所以我进行了一些测试。我做了以下事情:

class Car
{
public int id;
public Color color;
public Speed speed;

public Car(int id, Color color, Speed speed)
{
this.color = color;
this.id = id;
this.speed = speed;
}
}

class Speed
{
enum ASpeed
{
FAST, MEDIUM, SLOW;
}

ASpeed speed;

public Speed()
{
speed = ASpeed.FAST;
}
}

并在创建

@Override
public void create()
{
Json json = new Json();
Car car = new Car(1, Color.RED, new Speed());
String carSave = json.prettyPrint(car);

System.out.println(carSave);
}

我得到的结果是这样的:

{
id:1
color:{
r:1
a:1
}
speed:{}
}

我明白为什么我的游戏无法正确加载;它没有正确序列化。我的 GameState 几乎是一个包含 3 个需要在游戏之间进行的数据数组的类。我有常规的 getter 和 setter 方法以及几十个实用程序方法来访问相关数据,如下所示:

//TODO: Possibly add an Array of Event instances to fire for each player when certain circumstances are met as well as a string-based flagging system that can be used in the events.
//TODO: Research threading API in libGdx and see about moving the game logic over to a seperate thread - since this class is used for game logic, consider moving this with it.
public class GameState
{
private Array<Player> players;
private Array<Planet> planets;
private Array<Fleet> fleets;

public GameState(Array<Player> players, Array<Planet> planets, Array<Fleet> fleets)
{
this.players = players;
this.planets = planets;
this.fleets = fleets;
}

public Fleet getFleet(int id)
{
if(id <= fleets.size)
{
return fleets.get(id);
}
return null;
}

public Planet getPlanet(int id)
{
if(id <= planets.size)
{
return planets.get(id);
}
return null;
}

public Player getPlayer(int id)
{
if(id <= players.size)
{
return players.get(id);
}
return null;
}

当然,这不是最好的方法,但我每个字段只有大约 100 个,并且不打算添加更多,所以这种方法很好地满足了我的目的。每个播放器都包含一个用于获取相关数据的Integer值数组。一个例子;

public class Player
{
private final int id;

//libGdx Color used by ShapeRenderer to display planets in the right political color
private Color color;

//Things that the game logic demands be accessible from each Player instance
//TODO: Move these over to the libGdx IntegerArray when you have the time - this will avoid the boxing/unboxing penalty when accessing members.
Array<Integer> allies;
Array<Integer>desiredAllies;
Array<Integer> enemies:
Array<Integer> neutrals;
Array<Integer> planets;
Array<Integer> fleets;

public Player(int id, Color color)
{
this.id = id;
this.color = color;
allies = new Array<Integer>();
planets = new Array<Integer>();
fleets = new Array<Integer>();
neutrals = new Array<Integer>();
enemies = new Array<Integer>();
}

//Getters, Setters, and many, many utility methods.
}

Planet、Player 和 Fleet 类都具有相似的结构,甚至实现了相同的接口(interface):

public interface IndexedGameObject extends GameObject
{
int getID();
}

我的游戏设计和结构在连载之前都运行良好。我专门学习 Json 来保存我的游戏,我可能错过了一些东西。 为什么这个设计在序列化时不起作用?有什么办法可以让它发挥作用吗?如果没有,除了编写我自己的保存格式之外,还有什么可能的解决方案?我对 XML 缺乏经验,这行得通吗?

注释:

我愿意并且能够编写自己的保存格式,但是以一致的、人眼可读的方式这样做会增加大量的开销和维护,并且会浪费时间(因为它类似于编写一个非常小的、不完全函数式的语言(我需要做大量的一般测试以确保它正常工作),并且将精力花在开发和维护更直观的用户界面上。如果 JSON 无法满足我的目的,有什么可以吗?

我基本上必须重新制定我的整个时间表(是的,我保留了一份我需要做的事情以及何时应该做的 list ,例如;星期二 - 修复那个烦人的、几乎不可能在放学后重新创建按钮消失的错误,以及作业)。

理论上,我的 JSON 应该具有这样的结构;

GameState ->
fleets ->
fleet ->
owner: integer id of the player that owns this fleet.
id: the index of this fleet in the Array
locationID: integer id of the province this fleet is located in.
originalTransports: final integer used for reinforcing.
originalFighters: final integer used for reinforcing.
originalFrigates: final integer used for reinforcing.
originalDreadnaughts: final integer used for reinforcing.
transports: number of troop transports currently in the fleet.
fighters: the number of fighters currently in fleet.
frigates: the number of frigates in this fleet.
dreadnaughts: number of dreadnaughts in the fleet.
players ->
player ->
id: int representing the index of this player in the GameState
provinces: Integer Array representing the provinces owned by this player.
allies: Integer Array representing the index of the other Players allied to this one.
enemies: Integer Array representing the indices of the other players at war with this one.
neutrals: Integer Array representing the players whom this player is neutral to
desiredAllies: Integer Array representing the players who this player desires for allies - used mainly by the ai to determine who to send alliance offers to and who to accept them from.
fleets: Integer Array representing the indices of the fleets of this player
provinces ->
province ->
id: int representing the index of this province
owner: int id of the owning player in the GameState
name: String value. Text displayed on the screen when province selected.
isUnderSiege: boolean. Read the variable name.
colonies ->
colony ->
name: String value. Displayed in the menu when the city is selected from the province info screen that pops up when the province is selected.
controller: The index of the controlling Player in the GameState.
population: int number of people who reside here. Used to calculate things such as how many ships this planet provides, or how much tax it gives to the player.
fortLevel: double representing the amount of organized military resistance this colony will give an attacker before surrendering.
growthRate: the number of births occurring in this colony every 60 ticks.

编辑:

玩家阵列和舰队正常工作。当我到达省份数组时,事情变得很奇怪;

{
planets:[
//There should be 300 provinces. Instead 300 of these
colonies: [ {},{},{},{},{},{},{} ]
]
}

我的相关字段类看起来像这样;

public class Planet implements IndexedGameObject, Modifier
{
private int id;

private Array<Colony> colonies;

public Planet(int id, Array<Colony> colonies)
{
this.id = id;
this.colonies = colonies;
}

//Utility methods + Getters/Setters
}

class Colony
{
private int parentProvinceID;
private ColonyType type;

public Colony(int parentProvinceID, ColonyType type)
{
this.parentProvinceID = parentProvinceID;
this.type = type;
}

//Some more irrelevant methods
}

enum ColonyType
{
ADMINISTRATIVE, MILITARY, POPULATION;
}

最佳答案

您的问题是由通用数组引起的。我认为 JSON 不可能序列化,例如 Array<Player>

解决方案是包装通用数组 - take a look at this thread或者在可能的情况下使用非通用数组(例如 FloatArray 而不是 Array)。

<小时/>

另一方面,你的逻辑结构似乎是关系型的 - 也许使用一些 SQL 技术而不是将其保留在对象中并序列化/反序列化会更方便? Here you have some nutshell info

关于java - libGdx 中的 GameState 到 JSON 的序列化不起作用,我该如何修复它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36403339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com