Пример #1
0
    public static UnitAction ActionFromJson(JsonElement elem)
    {
        JsonElement jsonData       = elem.GetProperty(nameof(JsonData));
        string      unitGuid       = elem.GetProperty(nameof(UnitGuid)).GetString();
        string      unitActionType = jsonData.GetProperty("UnitActionType").GetString();

        switch (unitActionType)
        {
        case nameof(MoveAction):
            return(MoveAction.FromJson(unitGuid, jsonData));

        case nameof(FightAction):
            return(FightAction.FromJson(unitGuid, jsonData));

        case nameof(SpawnAction):
            return(SpawnAction.FromJson(unitGuid, jsonData));

        case nameof(DieAction):
        // Do nothing for now. If a unit dies, it was probably the
        // result of some other action (fight, etc)
        // therefore, we don't need to DieAction because it will happen
        // automatically
        default:
            throw Global.Error(string.Format("{0} is not a valid UnitActionType", unitActionType));
        }
    }
Пример #2
0
    public static List <UnitAction> TransformFromJson(List <JsonElement> unitActions)
    {
        List <UnitAction> actions = new List <UnitAction>();

        foreach (JsonElement elem in unitActions)
        {
            JsonElement jsonData       = elem.GetProperty(nameof(JsonData));
            string      unitGuid       = elem.GetProperty(nameof(UnitGuid)).GetString();
            string      unitActionType = jsonData.GetProperty("UnitActionType").GetString();
            switch (unitActionType)
            {
            case nameof(MoveAction):
                actions.Add(MoveAction.FromJson(unitGuid, jsonData));
                break;

            case nameof(FightAction):
                actions.Add(FightAction.FromJson(unitGuid, jsonData));
                break;

            case nameof(SpawnAction):
                actions.Add(SpawnAction.FromJson(unitGuid, jsonData));
                break;

            case nameof(DieAction):
                // Do nothing for now. If a unit dies, it was probably the
                // result of some other action (fight, etc)
                // therefore, we don't need to DieAction because it will happen
                // automatically
                break;

            default:
                throw Global.Error(string.Format("{0} is not a valid UnitActionType", unitActionType));
            }
        }

        return(actions);
    }