Exemple #1
0
    private void UpdateBulletsPhysics(BulletController bullet)
    {
        Vector2 position    = bullet.GetPosition();
        Vector2 velocity    = bullet.GetVelocity();
        Vector2 newPosition = position + velocity * Time.fixedDeltaTime;

        bullet.SetPosition(newPosition);
        Rect collider = bullet.GetCollider();

        int minX = Mathf.FloorToInt(collider.xMin + 0.5f);
        int minY = Mathf.FloorToInt(collider.yMin + 0.5f);
        int maxX = Mathf.FloorToInt(collider.xMax + 0.5f);
        int maxY = Mathf.FloorToInt(collider.yMax + 0.5f);

        for (int x = minX; x <= maxX; ++x)
        {
            for (int y = minY; y <= maxY; ++y)
            {
                Coords2    coords = new Coords2(x, y);
                GameObject wall   = map.GetWallTile(coords);
                if (wall == null)
                {
                    continue;
                }
                WallTileController controller = wall.GetComponent <WallTileController>();
                if (controller == null)
                {
                    continue;
                }
                if (controller.HasCollider())
                {
                    bullet.OnHitWall(controller);
                    return;
                }
            }
        }
    }