示例#1
0
    private void OnTriggerStay2D(Collider2D collision)
    {
        var entity = collision.gameObject.GetComponent <AbstractEntity>();

        if (entity == null)
        {
            return;
        }

        var entityBounds = collision.bounds;

        // Check bottom edge
        if (!tilemapCollider.OverlapPoint(new Vector2(entityBounds.min.x, entityBounds.min.y)))
        {
            return;
        }
        if (!tilemapCollider.OverlapPoint(new Vector2(entityBounds.max.x, entityBounds.min.y)))
        {
            return;
        }

        // Check top edge
        if (!tilemapCollider.OverlapPoint(new Vector2(entityBounds.min.x, entityBounds.max.y)))
        {
            return;
        }
        if (!tilemapCollider.OverlapPoint(new Vector2(entityBounds.max.x, entityBounds.max.y)))
        {
            return;
        }

        entity.Fall();
    }
示例#2
0
        private void TakeStep(Vector2 movement)
        {
            var halfCollider = movementCollider * 0.5f;

            bool willCollide = false;

            foreach (var corner in new Vector2[]
            {
                new Vector2(halfCollider.x, halfCollider.y),
                new Vector2(-halfCollider.x, halfCollider.y),
                new Vector2(halfCollider.x, -halfCollider.y),
                new Vector2(-halfCollider.x, -halfCollider.y)
            })
            {
                var samplePoint        = Position + corner + movement;
                var sampleTilePosition = Vector3Int.FloorToInt(samplePoint);

                if (terrainCollider.OverlapPoint(samplePoint))
                {
                    willCollide = true;
                }
                if (decorationCollider.OverlapPoint(samplePoint))
                {
                    willCollide = true;
                }
            }

            if (!willCollide)
            {
                transform.position         = transform.position + new Vector3(movement.x, movement.y, 0.0f);
                game.State.Player.Position = new Vector2(transform.position.x, transform.position.y);
            }
        }
示例#3
0
    public void placeBuilding(collectable building)
    {
        if (Inventory.buildings.Contains(building))
        {
            Vector3 spawnPos = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y));
            if (!prohibitedArea.OverlapPoint(spawnPos))
            {
                Instantiate(building.building, new Vector3(spawnPos.x, spawnPos.y, 0), Quaternion.identity);

                Inventory.buildings.Remove(building);

                building = null;
            }
        }
        else
        {
            Debug.Log("ERROR: This shouldn't be happening! Something's wrong! panic! AAAAA!");
        }
    }
示例#4
0
    // Begins hovering on the tile at pos if it is placeable
    void CheckTile(Vector2 pos)
    {
        // The tile at the cursor
        Vector2 cursorTile = new Vector2((Mathf.Floor(pos.x) + 0.5f), (Mathf.Floor(pos.y) + 0.5f));

        // If this is the same tile from the last check, no need to update anything!
        if (cursorTile == tilePos)
        {
            return;
        }


        bool updateHover = hovering;

        // If the cursor position is over the Env collider, check for placeable tile
        if (env.OverlapPoint(pos))
        {
            // Snap tile position to grid
            tilePos.x = cursorTile.x;
            tilePos.y = cursorTile.y;

            // If placeable tile, indicate that hovering and show the highlight
            if (IsPlaceable(tilePos))
            {
                // Move the highlight onto this tile
                selectButton.transform.position = tilePos;
                updateHover = true;

                // If hovering on a rune, change the highlight sprite to the same type as that rune
                bool onRune = false;
                foreach (PlacedRune p in placedRuneList)
                {
                    if (p.tile == tilePos)
                    {
                        selectButton.GetComponent <Image>().sprite = p.icon.GetComponent <SpriteRenderer>().sprite;
                        onRune = true;
                        break;
                    }
                }
                // Otherwise, reset to current placing-type (if need to)
                if (!onRune)
                {
                    if (selectButton.GetComponent <Image>().sprite != highlightTileSprite)
                    {
                        selectButton.GetComponent <Image>().sprite = highlightTileSprite;
                    }
                }
            }
            // Indicate else if otherwise
            else
            {
                updateHover = false;
            }
        }
        else
        {
            updateHover = false;
        }

        // Turn off button if just stopped hovering
        if (!updateHover && (updateHover != hovering))
        {
            selectButton.SetActive(false);
        }
        // Turn on if just started hovering
        else if (updateHover && (updateHover != hovering))
        {
            selectButton.SetActive(true);
        }

        hovering = updateHover;
    }
示例#5
0
    /// <summary>
    /// Translates input into an action target such as a movement destination.
    /// </summary>
    /// <param name="input">The InpurDir object from the current frame.</param>
    private void SetInputTarget(InputDir input)
    {
        // If we've arrived at a movement target and no input is being given,
        // force our state to idle
        if (playerArrived && input == InputDir.NONE)
        {
            state = PlayerState.IDLE;
            return;
        }

        if ((state == PlayerState.IDLE || playerArrived) &&
            !levelChanger.InputLocked)
        {
            // Handle direction change
            if (input.HasFlag(InputDir.LEFT))
            {
                if (faceDirection == PlayerDirection.LEFT)
                {
                    sameDirectionFrameCount++;
                }
                else
                {
                    sameDirectionFrameCount = 0;
                    faceDirection           = PlayerDirection.LEFT;
                }
            }
            else if (input.HasFlag(InputDir.RIGHT))
            {
                if (faceDirection == PlayerDirection.RIGHT)
                {
                    sameDirectionFrameCount++;
                }
                else
                {
                    sameDirectionFrameCount = 0;
                    faceDirection           = PlayerDirection.RIGHT;
                }
            }
            else if (input.HasFlag(InputDir.UP))
            {
                if (faceDirection == PlayerDirection.UP)
                {
                    sameDirectionFrameCount++;
                }
                else
                {
                    sameDirectionFrameCount = 0;
                    faceDirection           = PlayerDirection.UP;
                }
            }
            else if (input.HasFlag(InputDir.DOWN))
            {
                if (faceDirection == PlayerDirection.DOWN)
                {
                    sameDirectionFrameCount++;
                }
                else
                {
                    sameDirectionFrameCount = 0;
                    faceDirection           = PlayerDirection.DOWN;
                }
            }

            // Prevent any kind of crazy overflow nonsense
            sameDirectionFrameCount = Mathf.Clamp(sameDirectionFrameCount, 0, 1024);

            // Move if we're able
            if (sameDirectionFrameCount >= moveFrameDelay || (playerArrived && state == PlayerState.MOVING))
            {
                Vector3 potentialMovementTarget = transform.position;

                if (input.HasFlag(InputDir.LEFT))
                {
                    potentialMovementTarget = grid.CellToWorld(new Vector3Int(cellPos.x - 1, cellPos.y, cellPos.z)) + cellOffset;
                }
                else if (input.HasFlag(InputDir.RIGHT))
                {
                    potentialMovementTarget = grid.CellToWorld(new Vector3Int(cellPos.x + 1, cellPos.y, cellPos.z)) + cellOffset;
                }
                else if (input.HasFlag(InputDir.UP))
                {
                    potentialMovementTarget = grid.CellToWorld(new Vector3Int(cellPos.x, cellPos.y + 1, cellPos.z)) + cellOffset;
                }
                else if (input.HasFlag(InputDir.DOWN))
                {
                    potentialMovementTarget = grid.CellToWorld(new Vector3Int(cellPos.x, cellPos.y - 1, cellPos.z)) + cellOffset;
                }

                // Check we're not trying to move into a collidable cell
                if (!tilemapCollider.OverlapPoint(potentialMovementTarget) &&
                    potentialMovementTarget != transform.position)
                {
                    movementTarget = potentialMovementTarget;
                    movementOrigin = transform.position;
                    state          = PlayerState.MOVING;
                    playerArrived  = false;
                }
                else
                {
                    // Moving into a collidable should still be animated as
                    // walking on the spot
                    state = PlayerState.KABEDON;
                }
            }
        }
    }
示例#6
0
文件: Player.cs 项目: atil/ggj21
    private IEnumerator MoveCoroutine(Vector2Int dir)
    {
        if (_isMoving)
        {
            yield break;
        }

        _isMoving = true;
        Vector2 src = transform.position;

        Vector3 offset         = new Vector3(0.49f, -0.49f, 0.0f); // From top left to the middle
        Vector3 offsetPosition = transform.position + offset;

        offsetPosition.x = Mathf.RoundToInt(offsetPosition.x);
        offsetPosition.y = Mathf.RoundToInt(offsetPosition.y);
        offsetPosition.z = Mathf.RoundToInt(offsetPosition.z);

        Vector3Int srcCellPos    = Game.Grid.WorldToCell(offsetPosition); // Get the cellPos using the middle
        Vector3Int targetCellPos = srcCellPos + (Vector3Int)dir;
        Vector2    target        = Game.Grid.CellToWorld(targetCellPos) - offset;

        TilemapCollider2D currentCollider = Game.CurrentRoom.transform.Find("Collision").GetComponent <TilemapCollider2D>();

        if (currentCollider.OverlapPoint(target))
        {
            _isMoving = false; // Can't move into the collider
            yield break;
        }

        TilemapCollider2D currentCollider2 = Game.CurrentRoom.transform.Find("Doors").GetComponent <TilemapCollider2D>();

        if (currentCollider2.OverlapPoint(target))
        {
            _isMoving = false; // Can't move into the collider
            yield break;
        }

        if (target.x > 4f || target.x < -6 || target.y > 5 || target.y < -5)
        {
            _isMoving = false; // Out of bounds
            yield break;
        }

        StartCoroutine(PlayFootstepParticleCoroutine());
        Game.Sfx.PlayFootstep();

        Animator.speed = 0;

        const float duration = 0.15f;

        for (float f = 0; f < duration; f += Time.deltaTime)
        {
            float t = MovementCurve.Evaluate(f / duration);
            transform.position = Vector2.Lerp(src, target, t);
            yield return(null);
        }

        Animator.speed = 1;

        transform.position = target;
        _isMoving          = false;
        ClampIntoBounds();
    }