Inheritance: MonoBehaviour
        public async Task SpawnPlayer(SpawnAction spawnAction)
        {
            bool   res;
            string responseString;

            if (_interpolator != null)
            {
                try
                {
                    res = _gameLogic.IsPlayerSpawned(spawnAction.Username);
                    if (!res)
                    {
                        _gameLogic.HandleSpawnPlayer(spawnAction.Username, spawnAction.PlayerClass);
                    }
                    responseString = res ? "Player Already Spawned." :
                                     "Player wasnt already spawned. Added Player to spawn queue.";
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    responseString = e.ToString();
                }
            }
            else
            {
                responseString = "Error: Interpolator not setup";
            }


            await Clients.Caller.ReceiveString(responseString);
        }
Esempio n. 2
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));
        }
    }
        public void Test_ShouldSpawnStructure_WhenExecuteInvoked(int x, int y)
        {
            //    Arrange
            Mock <EntitySpawner> spawner = new Mock <EntitySpawner>(MockBehavior.Loose, new object[] { gameController });

            spawner.Setup(entitySpawner => entitySpawner.SpawnStructure(It.IsAny <Coords>(), It.IsAny <IStructure <IStructureDef> >())).Verifiable();

            gameController.Spawner = spawner.Object;

            Coords coords = new Coords()
            {
                x = x,
                y = y
            };

            BuildingDef buildingDef = Mock.Of <BuildingDef>();

            Mock <SpawnActionDef> actionDef = new Mock <SpawnActionDef>(new object[] { (uint)10, "image", buildingDef });

            actionDef.SetupGet(def => def.SpawnableDef).Returns(() => buildingDef);

            Mock <ITargetable> target = new Mock <ITargetable>();

            target.Setup(targetable => targetable.FloatCoords).Returns(() => (FloatCoords)coords);

            SpawnAction spawnAction = new SpawnAction(actionDef.Object, gameController, factionController);

            //    Act
            spawnAction.Execute(target.Object);

            //    Assert

            spawner.VerifyAll();
        }
Esempio n. 4
0
    public static SpawnAction FromJson(string unitGuid, JsonElement jsonData)
    {
        SpawnAction action = new SpawnAction();

        JsonElement spawnWorldPosition = jsonData.GetProperty(nameof(SpawnWorldPosition));
        JsonElement unitType           = jsonData.GetProperty(nameof(UnitType));

        float x = spawnWorldPosition.GetProperty("x").GetSingle();
        float y = spawnWorldPosition.GetProperty("x").GetSingle();

        action.UnitGuid           = unitGuid;
        action.SpawnWorldPosition = new Vector2(x, y);
        action.UnitType           = unitType.GetString();

        return(action);
    }
        protected override void OnEnable()
        {
            base.OnEnable();

            m_SpawnAction = (SpawnAction)m_Action;

            m_ModelProp                = serializedObject.FindProperty("m_Model");
            m_SpawnMethodProp          = serializedObject.FindProperty("m_SpawnMethod");
            m_EffectProp               = serializedObject.FindProperty("m_Effect");
            m_SpawnAreaShapeProp       = serializedObject.FindProperty("m_SpawnAreaShape");
            m_SpawnAreaCenterProp      = serializedObject.FindProperty("m_SpawnAreaCenter");
            m_SpawnAreaSizeProp        = serializedObject.FindProperty("m_SpawnAreaSize");
            m_SpawnAreaRadiusProp      = serializedObject.FindProperty("m_SpawnAreaRadius");
            m_SnapToGroundProp         = serializedObject.FindProperty("m_SnapToGround");
            m_SpawnOrientationTypeProp = serializedObject.FindProperty("m_SpawnOrientationType");
            m_SpawnOrientationProp     = serializedObject.FindProperty("m_SpawnOrientation");
            m_CollideProp              = serializedObject.FindProperty("m_Collide");
            m_BuildTimeProp            = serializedObject.FindProperty("m_BuildTime");
        }
Esempio n. 6
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);
    }
Esempio n. 7
0
 public static async Task Exec(GameScene scene,
                               SpawnAction act,
                               Tween tween)
 {
     await new SpawnAct(scene, act, tween).Exec();
 }
Esempio n. 8
0
 private SpawnAct(GameScene scene, SpawnAction action, Tween tween)
 {
     _scene = scene;
     _act   = action;
     _tween = tween;
 }
Esempio n. 9
0
    private GameObject GetHighlight(int step)
    {
        ZAction = null;
        XAction = null;

        HighlightedIndex += step;
        if (HighlightedIndex >= IM.ItemList.Count)
        {
            HighlightedIndex = -IM.IngredientSpawners.Count;
        }
        else if (HighlightedIndex < -IM.IngredientSpawners.Count)
        {
            HighlightedIndex = IM.ItemList.Count - 1;
        }

        if (HighlightedIndex >= 0)
        {
            if (!PlayerRef.IsHolding)
            {
                PrepareAction prepAction = null;
                if (IM.ItemList[HighlightedIndex].MyItemType == ItemType.INGREDIENT)
                {
                    foreach (int boardID in IM.BoardIndexList)
                    {
                        Board b = IM.ItemList[boardID] as Board;
                        if (b.HoldingItem != null && b.HoldingItem.ID == HighlightedIndex)
                        {
                            prepAction = new PrepareAction(boardID);
                            XAction    = prepAction;
                            break;
                        }
                    }
                }

                PickUpAction puAction = new PickUpAction(HighlightedIndex);
                if (puAction.isValid(CurrentState))
                {
                    ZAction = puAction;
                }
                else
                {
                    puAction = null;
                }

                if (puAction != null || prepAction != null)
                {
                    return(IM.ItemList[HighlightedIndex].gameObject);
                }
                else
                {
                    return(GetHighlight(step));
                }
            }
            else
            {
                SubmitOrderAction soAction = new SubmitOrderAction();
                if (soAction.isValid(CurrentState))
                {
                    XAction = soAction;
                }

                DropOffAction doAction = new DropOffAction(HighlightedIndex);
                if (doAction.isValid(CurrentState))
                {
                    ZAction = doAction;
                    return(IM.ItemList[HighlightedIndex].gameObject);
                }

                TransferAction transAction = new TransferAction(HighlightedIndex);
                if (transAction.isValid(CurrentState))
                {
                    ZAction = transAction;
                    return(IM.ItemList[HighlightedIndex].gameObject);
                }

                return(GetHighlight(step));
            }
        }
        else
        {
            int         spawnerIndex = ~HighlightedIndex;
            SpawnAction sAction      = new SpawnAction(IM.IngredientSpawners[spawnerIndex].MyIngredientType);
            if (sAction.isValid(CurrentState))
            {
                ZAction = sAction;
                return(IM.IngredientSpawners[spawnerIndex].gameObject);
            }
            else
            {
                return(GetHighlight(step));
            }
        }
    }
Esempio n. 10
0
    private void ApplyAction(Action a)
    {
        if (a is SpawnAction)
        {
            SpawnAction       spawn   = a as SpawnAction;
            IngredientSpawner spawner = IM.IngredientSpawners.Find(x => x.SpawnedIngredients.Exists(y => !y.IsSpawned && y.MyIngredientType == spawn.spawnType));
            HighlightedObject = spawner.gameObject;
        }
        if (a is PickUpAction)
        {
            PickUpAction pickup = a as PickUpAction;
            HighlightedObject = IM.ItemList[pickup.id].gameObject;
        }
        if (a is DropOffAction)
        {
            DropOffAction dropOff = a as DropOffAction;
            HighlightedObject = IM.ItemList[dropOff.id].gameObject;
        }
        if (a is TransferAction)
        {
            TransferAction transfer = a as TransferAction;
            HighlightedObject = IM.ItemList[transfer.id].gameObject;
        }
        if (a is PrepareAction)
        {
            PrepareAction prep = a as PrepareAction;
            HighlightedObject = IM.ItemList[prep.id].gameObject;
        }
        if (a is SubmitOrderAction)
        {
            HighlightedObject = IM.SubmittedTableRef.gameObject;
        }

        if (a is IdleAction)
        {
            HighlightedObject = PlayerStartLocation;
            Highlighter.transform.position = new Vector3(
                HighlightedObject.transform.position.x,
                Highlighter.transform.position.y,
                HighlightedObject.transform.position.z);
            PlayerRef.transform.position = Vector3.right * Highlighter.transform.position.x
                                           + Vector3.forward * Highlighter.transform.position.z;
        }
        else
        {
            Highlighter.transform.position = new Vector3(
                HighlightedObject.transform.position.x,
                Highlighter.transform.position.y,
                HighlightedObject.transform.position.z);
            PlayerRef.transform.position = Vector3.right * Highlighter.transform.position.x
                                           + Vector3.forward * (Highlighter.transform.position.z + 1);
        }


        Debug.Log("Applying action: " + a.ToString(CurrentState));
        if (!a.isValid(CurrentState))
        {
            Debug.LogError("Action is not valid. Ignoring.");
            return;
        }

        CurrentState = a.ApplyAction(CurrentState);
        IM.LoadWorldState(CurrentState);
        Debug.Log("Action applied. History size: " + observedStates.Count);
        Debug.Log(CurrentHeuristic + " = " + CurrentHeuristic.GetHeuristic(CurrentState));
    }
Esempio n. 11
0
    /// <summary>
    /// Used to get all of the valid actions for the current state
    /// </summary>
    /// <returns></returns>
    public List <Action> GetValidActions(AIState state)
    {
        List <Action> validActions = new List <Action>();

        //Waiting around
        validActions.Add(new IdleAction());

        //Things you can do when your hands are free
        if (state.CurrentPlayerState.HandsFree())
        {
            //Spawning items
            foreach (IngredientType type in System.Enum.GetValues(typeof(IngredientType)))
            {
                SpawnAction spawnAction = new SpawnAction(type);
                if (spawnAction.isValid(state))
                {
                    validActions.Add(spawnAction);
                }
            }

            PickUpAction pickupAction;
            //Picking up everything
            //Ingredients
            foreach (int ingredientID in state.IngredientStateIndexList)
            {
                IngredientState ingredient = state.ItemStateList[ingredientID] as IngredientState;
                if (ingredient.IsSpawned && !ingredient.IsInMeal)
                {
                    pickupAction = new PickUpAction(ingredient.ID);
                    validActions.Add(pickupAction);
                }
            }

            //Pots
            foreach (int potID in state.PotStateIndexList)
            {
                PotState pot = state.ItemStateList[potID] as PotState;
                pickupAction = new PickUpAction(pot.ID);
                validActions.Add(pickupAction);
            }

            //Plates
            foreach (int plateID in state.PlateStateIndexList)
            {
                PlateState plate = state.ItemStateList[plateID] as PlateState;
                pickupAction = new PickUpAction(plate.ID);
                validActions.Add(pickupAction);
            }

            PrepareAction prepAction;
            foreach (int boardID in state.BoardStateIndexList)
            {
                BoardState bState = state.ItemStateList[boardID] as BoardState;
                if (!bState.IsFree())
                {
                    IngredientState iState = state.ItemStateList[bState.HoldingItemID] as IngredientState;
                    if (iState != null && !iState.IsPrepared)
                    {
                        prepAction = new PrepareAction(boardID);
                        validActions.Add(prepAction);
                    }
                }
            }
        }

        //Things you can do when you have something in hand
        else
        {
            DropOffAction  dropoffAction;
            TransferAction transferAction;
            ItemState      itemState = state.ItemStateList[state.CurrentPlayerState.HoldingItemID];
            ItemType       type      = itemState.MyItemType;

            if (type == ItemType.INGREDIENT)
            {
                //Putting things on the table
                foreach (int tableID in state.TableStateIndexList)
                {
                    TableSpace table = state.ItemStateList[tableID] as TableSpace;
                    if (table.IsFree())
                    {
                        dropoffAction = new DropOffAction(table.ID);
                        validActions.Add(dropoffAction);
                    }
                }

                //Moving ingredients to a cutting board
                foreach (int boardID in state.BoardStateIndexList)
                {
                    BoardState board = state.ItemStateList[boardID] as BoardState;
                    if (board.IsFree())
                    {
                        dropoffAction = new DropOffAction(board.ID);
                        validActions.Add(dropoffAction);
                    }
                }

                if ((itemState as IngredientState).IsPrepared)
                {
                    //Moving ingredients to a pot
                    foreach (int potID in state.PotStateIndexList)
                    {
                        PotState  pot  = state.ItemStateList[potID] as PotState;
                        MealState meal = state.ItemStateList[pot.mealID] as MealState;
                        if (meal.MealSize() + 1 <= PotState.MAX_ITEMS_PER_POT && !meal.IsBurnt())
                        {
                            dropoffAction = new DropOffAction(pot.ID);
                            validActions.Add(dropoffAction);
                        }
                    }

                    //Moving ingredients to a plate
                    foreach (int plateID in state.PlateStateIndexList)
                    {
                        PlateState plate = state.ItemStateList[plateID] as PlateState;
                        MealState  meal  = state.ItemStateList[plate.mealID] as MealState;
                        if (!plate.IsSubmitted && !meal.IsBurnt())
                        {
                            dropoffAction = new DropOffAction(plate.ID);
                            validActions.Add(dropoffAction);
                        }
                    }
                }
            }

            if (type == ItemType.POT)
            {
                PotState  pot  = itemState as PotState;
                MealState meal = state.ItemStateList[pot.mealID] as MealState;

                //Putting the pot on the table
                foreach (int tableID in state.TableStateIndexList)
                {
                    TableSpace table = state.ItemStateList[tableID] as TableSpace;
                    if (table.IsFree())
                    {
                        dropoffAction = new DropOffAction(table.ID);
                        validActions.Add(dropoffAction);
                    }
                }

                //Moving meal to a pot
                transferAction = new TransferAction(Item.NOTHING_ID);
                foreach (int potID in state.PotStateIndexList)
                {
                    transferAction.id = potID;
                    if (transferAction.isValid(state))
                    {
                        validActions.Add(transferAction);
                        transferAction = new TransferAction(Item.NOTHING_ID);
                    }
                }

                //Moving the meal to another plate
                foreach (int plateID in state.PlateStateIndexList)
                {
                    transferAction.id = plateID;
                    if (transferAction.isValid(state))
                    {
                        validActions.Add(transferAction);
                        transferAction = new TransferAction(Item.NOTHING_ID);
                    }
                }
            }

            if (type == ItemType.PLATE)
            {
                PlateState plate = itemState as PlateState;

                //Putting things on the table
                foreach (int tableID in state.TableStateIndexList)
                {
                    TableSpace table = state.ItemStateList[tableID] as TableSpace;
                    if (table.IsFree())
                    {
                        dropoffAction = new DropOffAction(table.ID);
                        validActions.Add(dropoffAction);
                    }
                }

                //If the plate is non-empty
                MealState heldMeal = state.ItemStateList[plate.mealID] as MealState;
                if (heldMeal.IsSpawned())
                {
                    //Submitting the meal
                    if (heldMeal.IsCooked())
                    {
                        SubmitOrderAction submitAction = new SubmitOrderAction();
                        validActions.Add(submitAction);
                    }

                    //Moving meal to a pot
                    transferAction = new TransferAction(Item.NOTHING_ID);
                    foreach (int potID in state.PotStateIndexList)
                    {
                        transferAction.id = potID;
                        if (transferAction.isValid(state))
                        {
                            validActions.Add(transferAction);
                            transferAction = new TransferAction(Item.NOTHING_ID);
                        }
                    }

                    //Moving the meal to another plate
                    foreach (int plateID in state.PlateStateIndexList)
                    {
                        transferAction.id = plateID;
                        if (transferAction.isValid(state))
                        {
                            validActions.Add(transferAction);
                            transferAction = new TransferAction(Item.NOTHING_ID);
                        }
                    }
                }
            }
        }


        return(validActions);
    }