Esempio n. 1
0
 private void Awake()
 {
     playerReference = GetComponent <ObjectGridInteraction>();
     playerAnimation = GetComponent <Animator>();
     PlayerFinishedMove();
 }
Esempio n. 2
0
    public bool AskToMove(Vector2 direction)
    {
        if (!isMoving)
        {
            if (CanMoveOnGrid(direction))
            {
                if (!isSpeedOverwritten)
                {
                    newMovementSpeedCalculated = myData.moveSpeed;
                }
                newMovePosition = transform.position + (Vector3)direction;
                GameObject[] objectsOnTileInDirection = CheckForObjectsOnTargetPosition(newMovePosition);
                // Set new position to move to and check if other GameObjects are on this position
                if (objectsOnTileInDirection.Length == 0)
                {
                    isMoving = true;

                    if (myData.isPlayable)
                    {
                        AudioManager.current.Play("CatMoving");
                    }
                    else if (myData.isPushable)
                    {
                        if (!myData.isHeavy)
                        {
                            AudioManager.current.Play("BoxPush", 0.8f);
                        }
                        else
                        {
                            AudioManager.current.Play("BoxPush", 0.5f);
                        }
                    }

                    EventManager.current.AddMovingEntity();
                    if (myData.isPlayable)
                    {
                        EventManager.current.PlayerStartMove();
                    }
                    return(true);
                }

                // One or more objects found in front of me
                else
                {
                    if (direction != oldDirection && myData.isPlayable /* || myData.isNPC */)
                    {
                        // Get list of gameobjects in line of sight
                        List <GameObject[]> listOfObjectsInPath = ObjectsInMovePath(transform.position, direction);

                        // Filter for movable objects, and check for heavy things
                        List <GameObject> movableObjects = GetMovableObjectsFromList(listOfObjectsInPath);

                        if (movableObjects.Count > 0)
                        {
                            // look for maximum movement speed of all objects
                            foreach (GameObject go in movableObjects)
                            {
                                ObjectGridInteraction tmp = go.GetComponent <ObjectGridInteraction>();
                                newMovementSpeedCalculated = Mathf.Min(myData.moveSpeed, tmp.Data.moveSpeed);
                                tmp.OverwriteMovementSpeed(newMovementSpeedCalculated);
                            }
                            // Move all objects
                            foreach (GameObject go in movableObjects)
                            {
                                go.GetComponent <ObjectGridInteraction>().AskToMove(direction);
                            }
                            isMoving = true;
                            pushing  = true;
                            return(true);
                        }

                        // Switch to walk on / push objects on it
                        else if (myData.isPlayable && listOfObjectsInPath[0][0].GetComponent <ObjectGridInteraction>().Data.isTrigger)
                        {
                            isMoving = true;
                        }
                        else
                        {
                            oldDirection = direction;
                        }
                    }

                    else if (myData.isPushable || myData.isHeavy)
                    {
                        isMoving = true;
                        pushing  = true;
                    }
                }
            }
        }
        return(false);
    }
Esempio n. 3
0
 void Awake()
 {
     gridInteraction = GetComponent <ObjectGridInteraction>();
 }
Esempio n. 4
0
    void SearchForSpawners(Tilemap tileMap, SpawnableObjectBehaviour[] spawnableObjects)
    {
        // Are spawnable scripted objects added to the array in the inspector?
        if (spawnableGameObjects.Length > 0)
        {
            foreach (SpawnableObjectBehaviour spawner in spawnableObjects)
            {
                // Skip empty entries
                if (spawner != null)
                {
                    // validate basic spanwer values
                    if (spawner.NameOfEntity == "" ||
                        spawner.spawnableGameObject == null ||
                        spawner.spawnerTile == null ||
                        spawner.MoveSpeed < 0f)
                    {
                        Debug.LogWarning("Spawner \"" + spawner.NameOfEntity + "\" übersprungen, fehlerhafte Einträge im Inspector!");
                        continue;
                    }

                    // Everything ok with the spawner values
                    int countTiles = 0;
                    //Debug.Log("Objekte vom Typ " + spawner.NameOfEntity + " werden gesucht ...");

                    if (tileMap != null && spawner != null)
                    {
                        // check if tilemap has tiles of the spawner type
                        if (tileMap.ContainsTile(spawner.spawnerTile))
                        {
                            //Instanciate one parent GameObject to sort all spawned children
                            GameObject tmpParent = new GameObject
                            {
                                name = spawner.name + "Container"
                            };

                            BoundsInt  bounds   = tileMap.cellBounds;
                            TileBase[] allTiles = tileMap.GetTilesBlock(bounds);

                            for (int x = 0; x < bounds.size.x; x++)
                            {
                                for (int y = 0; y < bounds.size.y; y++)
                                {
                                    TileBase tile = allTiles[x + y * bounds.size.x];
                                    if (tile != null)
                                    {
                                        if (tile == spawner.spawnerTile)
                                        {
                                            countTiles++;
                                            //Debug.Log("x:" + x + " y:" + y + " Objekt: " + spawner.nameOfEntity);
                                            GameObject tmpChild = Instantiate(spawner.spawnableGameObject, new Vector3(tileMap.origin.x + x + tileMap.tileAnchor.x, tileMap.origin.y + y + tileMap.tileAnchor.y, 0f), Quaternion.identity);
                                            tmpChild.name = spawner.name + countTiles;

                                            // Set spawner properties to instance properties
                                            if (tmpChild.GetComponent <ObjectGridInteraction>())
                                            {
                                                ObjectGridInteraction tmpGridInteraction = tmpChild.GetComponent <ObjectGridInteraction>();
                                                tmpGridInteraction.Data = new SpawnableObjectBehaviour.PublicProperties(
                                                    spawner.NameOfEntity.Replace(" ", ""),
                                                    spawner.MoveSpeed,
                                                    spawner.IsPlayable,
                                                    spawner.IsVulnerable,
                                                    spawner.IsPushable,
                                                    spawner.IsHeavy,
                                                    spawner.IsHole,
                                                    spawner.IsTrigger);
                                            }

                                            // Add child object to parent
                                            tmpChild.transform.parent = tmpParent.transform;
                                        }
                                    }
                                }
                            }
                            // normal (ignore maximum spawn value, you can add as many spawners as you like)
                            if (spawner.minimumSpawners >= 0 && spawner.maximumSpawners == 0)
                            {
                                if (countTiles >= spawner.minimumSpawners)
                                {
                                    //Debug.Log(countTiles + "x " + spawner.NameOfEntity + " gespawnt");
                                }
                            }
                            // spawn count has to be specific (e.g. start tile)
                            else if (spawner.minimumSpawners > 0 && spawner.maximumSpawners > 0)
                            {
                                // Nothing is ok
                                if (countTiles < spawner.minimumSpawners || countTiles > spawner.maximumSpawners)
                                {
                                    Debug.LogError(countTiles + "x " + spawner.NameOfEntity + " gespawnt, erlaubt sind Min "
                                                   + spawner.minimumSpawners + "x und Max " + spawner.maximumSpawners);
                                }
                                // Everything is ok
                                else if (countTiles >= spawner.minimumSpawners && countTiles <= spawner.maximumSpawners)
                                {
                                    //Debug.Log(countTiles + "x " + spawner.NameOfEntity + " gespawnt");
                                }
                            }
                        }
                    }
                    else if (spawner == null)
                    {
                        Debug.LogError("Spawner existiert nicht!");
                    }
                    else if (tileMap == null)
                    {
                        Debug.LogError("Tilemap existiert nicht!");
                    }
                }
            }
            Debug.Log("Spawner-Suche beendet");
            spawnMap.ClearAllTiles();
            EventManager.current.SpawnerFinished();
            EventManager.current.EnablePlayerMovement();
        }
        else
        {
            Debug.LogError("Keine Einträge für Spawner im Level Approver gefunden. Bitte ergänzen!");
        }
    }