/// <summary>
    /// Scans the tiles in the room and uses their tile properties objects to
    /// determine if they should have a collider or not. If so, adds one and
    /// sets its properties according to the tile's requirement.
    /// </summary>
    public void Generate()
    {
        Assert.IsFalse(hasColliders);

        Vec2i worldPos = room.Pos * new Vec2i(Room.Width, Room.Height);

        for (int y = 0; y < Room.Height; y++)
        {
            for (int x = 0; x < Room.Width; x++)
            {
                Tile           tile  = room.GetTile(x, y);
                TileProperties props = tile.Properties;

                if (props.hasCollider)
                {
                    TileCollider col = pool.Get();
                    col.SetPosition(new Vector2(worldPos.x + x, worldPos.y + y) + props.colliderOffset);
                    col.SetSize(props.colliderSize);
                    col.SetTile(tile);
                    col.SetTrigger(props.trigger);
                    colliders.Add(col);
                }
            }
        }

        hasColliders = true;
    }
Beispiel #2
0
    /// <summary>
    /// Gets a list of all environmental colliders hit given the following parameters.
    /// </summary>
    /// <param name="point1"></param>
    /// <param name="point2"></param>
    /// <param name="directionToCastRay"></param>
    /// <param name="distanceToCastRay"></param>
    /// <param name="totalPointsToCheck"></param>
    /// <returns></returns>
    private List <TileCollider> GetAllTilesHitFromRayCasts(Vector2 point1, Vector2 point2, Vector2 directionToCastRay, float distanceToCastRay, int totalPointsToCheck)
    {
        List <TileCollider> allCollidersThatWereHit = new List <TileCollider>();

        Vector2 segmentDistance = (point2 - point1) / (totalPointsToCheck - 1);

        Vector2 originPointForRaycast = point1;

        for (int i = 0; i < totalPointsToCheck; i++)
        {
            RaycastHit2D rayHit = Physics2D.Raycast(originPointForRaycast, directionToCastRay, distanceToCastRay, LayerMask.GetMask("Environment"));
            if (rayHit)
            {
                TileCollider tileColliderThatWasHit = rayHit.collider.GetComponent <TileCollider>();
                if (tileColliderThatWasHit)
                {
                    allCollidersThatWereHit.Add(tileColliderThatWasHit);
                }
            }

            DebugSettings.DrawLineDirection(originPointForRaycast, directionToCastRay, distanceToCastRay, Color.red);
            originPointForRaycast += segmentDistance;
        }

        return(allCollidersThatWereHit);
    }
    private void OnTriggerEnter(Collider col)
    {
        TileCollider tC = col.GetComponent <TileCollider>();

        if (tC != null)
        {
            switch (tC.tile.id)
            {
            // Generate a new floor when colliding with stairs.
            case TileType.Stair:
                Floor.Instance.Generate();
                break;
            }
        }
    }
    // Setup
    void createBoard(GameObject Board)
    {
        int   width  = 4;
        int   height = 4;
        float tilew  = 1.6f;
        float tileh  = 1.6f;

        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                GameObject   obj = Instantiate(tile, new Vector3((j - width / 2) * tilew, (i - height / 2) * tileh, 0f), Quaternion.identity);
                TileCollider tc  = obj.GetComponent <TileCollider> ();
                int          id  = i * height + j;

                // Weighted with vowels
                // 97, 101, 105, 111, 117 ~121
                // a    e   i    o     u    y
                int  weight = Random.Range(0, 4);
                char c      = (char)Random.Range(97, 122);

                char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };
                // Change to vowel
                if (weight == 0)
                {
                    int rd = Random.Range(0, 4);
                    c = vowels[rd];
                }


                // tc.setId (i * height + j, (char)('a'+ i * height + j)); // For now just c, will do random
                tc.setId(id, c);
                obj.transform.parent = Board.gameObject.transform;
            }
        }
    }
Beispiel #5
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //testImage = Content.Load<Texture2D>("Stupid");

            testScene = new Scene(GraphicsDevice, spriteBatch);
            layer1    = new SpriteLayer();
            layer2    = new SpriteLayer();

            Line     = Content.Load <Texture2D>("Line");
            testMap  = new TileMap("TileMaps/Tramtunnel1.tmx", 1, Content);
            testMap1 = new TileMap("TileMaps/Tramtunnel1.tmx", 3, Content);

            testMap1.Parent = layer2;
            testMap.Parent  = layer1;
            testMap.Scale   = new Vector2(1, 1);
            testMap1.Scale  = new Vector2(1, 1);

            testScene.Layers.AddLast(layer1);
            testScene.Layers.AddLast(layer2);

            Polygon testPolygon = new Polygon();

            testPolygon.addPoint(new Vector2(0, 100));
            testPolygon.addPoint(new Vector2(-50, -2));
            testPolygon.addPoint(new Vector2(50, -2));

            Polygon testPolygon2 = new Polygon();

            testPolygon2.addPoint(new Vector2(-40, -20));
            testPolygon2.addPoint(new Vector2(30, -10));
            testPolygon2.addPoint(new Vector2(30, 50));
            testPolygon2.addPoint(new Vector2(-40, 35));

            testPolygon.updateBounds();
            testPolygon2.updateBounds();

            testPoly  = new PolygonCollider(testPolygon);
            testPoly2 = new PolygonCollider(testPolygon2);

            container1 = new GameObject();
            container2 = new GameObject();

            obj1 = new StaticObject(testPoly, container1);
            b1   = obj1.BoundingShape;

            obj2 = new StaticObject(testPoly2, container2);
            b2   = obj2.BoundingShape;

            physicsTree = new Quadtree(Vector2.Zero, 1000, 7);

            physicsTree.InsertBound(b2);
            physicsTree.InsertBound(b1);

            container1.Collider = obj1;
            container2.Collider = obj2;

            TestCollider          = new TileCollider();
            TestCollider.Map      = testMap;
            TestCollider.TileSize = testMap.TileSets[0].TileHeight;

            audioManager.LoadContent(Content);
            SoundEffect         windHowl     = audioManager.LoadSoundEffect("wind_howl_1", Content);
            SoundEffectInstance windInstance = audioManager.MakeEmitter(windHowl, new Vector3(699f, 312f, 0f));

            windInstance.IsLooped = true;
            windInstance.Play();
            //test spatial audio emmiters
        }