Search in sources :

Example 1 with ObjectInFront

use of com.states.ObjectInFront in project anki-battle-showcase by adessoAG.

the class Vehicle method convertFactsToMessage.

public String convertFactsToMessage() {
    ObjectMapper objMapper = new ObjectMapper();
    JSONObject json = new JSONObject();
    JSONArray arr = new JSONArray();
    try {
        json.put("speed", this.speed);
        for (GameState gameState : this.factsRoad) {
            arr.put(gameState.getClass().getSimpleName());
        }
        json.put("nextRoadPiece", arr);
        arr = new JSONArray();
        for (GameState gameState : this.factsInventory) {
            arr.put(gameState.getClass().getSimpleName());
        }
        json.put("inv", arr);
        // 1 ebene tiefer die einzelnen attribute
        JSONArray allObstacleFacts = new JSONArray();
        for (GameState gameState : this.factsObstacles) {
            JSONArray factArr = new JSONArray();
            if (gameState instanceof ObjectBehind) {
                factArr.put("Behind");
                // obstacle type
                factArr.put(((ObjectBehind) gameState).getType());
                factArr.put(((ObjectBehind) gameState).getMetersBehind());
            }
            if (gameState instanceof ObjectInFront) {
                factArr.put("Front");
                // obstacle type
                factArr.put(((ObjectInFront) gameState).getType());
                factArr.put(((ObjectInFront) gameState).getMetersInFront());
            }
            allObstacleFacts.put(factArr);
        }
        json.put("obstacles", allObstacleFacts);
        return json.toString();
    } catch (JSONException e) {
        log.error("Constructing Json error", e);
    }
    return "";
}
Also used : ObjectInFront(com.states.ObjectInFront) JSONObject(org.json.JSONObject) ObjectBehind(com.states.ObjectBehind) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) GameState(com.states.GameState) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 GameState (com.states.GameState)1 ObjectBehind (com.states.ObjectBehind)1 ObjectInFront (com.states.ObjectInFront)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1