Beispiel #1
0
    public override void Init(bool destroyed)
    {
        SetCollidersInChilds(false);
        var trunk = transform.GetChild(0);

        trunk.GetComponent <CircleCollider2D>().enabled = true;

        var spriteRenderer = GetComponent <SpriteRenderer>();

        spriteRenderer.enabled = true;
        GetComponent <DropScript>().enabled = true;
        GetComponent <Tree>().enabled       = true;

        // ZlayerManager.SetSortingOrder(transform, spriteRenderer);

        var rigid = trunk.GetComponent <Rigidbody2D>();

        rigid.simulated = true;
        rigid.bodyType  = RigidbodyType2D.Kinematic;

        dead = destroyed;
        if (destroyed)
        {
            type = ResourceManager.Instance.GetTrunk(type);
            StubInit();
        }

        spriteRenderer.sortingOrder = 0;

        float z = ZlayerManager.GetZFromY(transform.position);

        transform.position = new Vector3(transform.position.x, transform.position.y, z);

        //// Hyi vittu
        //if (transform.childCount == 3)
        //{
        //    GameObject shadowGo = new GameObject("shadow");
        //    shadowGo.transform.parent = transform;
        //    var shadowRenderer = shadowGo.AddComponent<SpriteRenderer>();
        //    shadowRenderer.sortingLayerID = SortingLayer.NameToID("ObjectShadow");
        //    shadowRenderer.sprite = _treeShadows[ResourceManager.TreeToShadow(type)];
        //    shadowGo.transform.position = transform.position;
        //}
        //else
        //{
        //    var shadow = transform.GetChild(3);
        //    var shadowRenderer = shadow.gameObject.GetComponent<SpriteRenderer>();
        //    shadowRenderer.sprite = _treeShadows[ResourceManager.TreeToShadow(type)];
        //    shadowRenderer.sortingLayerID = SortingLayer.NameToID("ObjectShadow");
        //    shadowRenderer.enabled = true;

        //    shadow.transform.position = transform.position;
        //}
    }
Beispiel #2
0
    void Update()
    {
        if (Mathf.Abs(transform.position.x - _player.transform.position.x) > Chunk.CHUNK_SIZE * 2 ||
            Mathf.Abs(transform.position.y - _player.transform.position.y) > Chunk.CHUNK_SIZE * 2)
        {
            Destroy(this.gameObject);
        }

        // Khää Khää!!
        float z = ZlayerManager.GetZFromY(transform.position);

        transform.position = new Vector3(transform.position.x, transform.position.y, z);
    }
Beispiel #3
0
    public void SpawnObject(Vector3 spawnPosition, Transform parent, TileType type, Chunk chunk, int x, int y)
    {
        if (!IsImplementedSetting(type))
        {
            type = TileType.GrassLand; // atm kaikki grasslandikisi jos ei löydy
        }

        // choose object to spawn
        TilemapObjectSpawnSettings setting;

        if (TileSpawnSettings.TryGetValue(type, out setting))
        {
            // var setting = TileSpawnSettings[type];

            if (setting.SpawnableObjects.Length != 0)
            {
                float roll       = Random.Range(0f, 100f);
                float totalCount = 0;

                for (int i = 0; i < setting.SpawnableObjects.Length; i++)
                {
                    float spawnRate = setting.SpawnableObjects[i].SpawnRate;

                    GameObject prefab = setting.SpawnableObjects[i].ObjectPrefab;
                    if (totalCount + spawnRate >= roll)
                    {
                        var go = ObjectPool.instance.GetObjectForType(prefab.name, false); // ??????????????????

                        go.transform.localScale *= Random.Range(0.90f, 1.10f);

                        float z = ZlayerManager.GetZFromY(spawnPosition); // eessä olevat puut eteen ja takana taakse
                        go.transform.position = new Vector3(spawnPosition.x, spawnPosition.y, z);

                        go.GetComponent <Resource>().Init(false);

                        chunk.AddObject(x, y, go);

                        break; // ei montaa samaan kohtaan
                    }
                    totalCount += spawnRate;
                }
            }
        }
    }
Beispiel #4
0
    void Update()
    {
        fakeZAxel = ZlayerManager.GetZFromY(transform.position);

        // onko kaikki pakollisia
        TryToSetShader(new Vector3(1.0f, 1.0f, 0f));
        TryToSetShader(new Vector3(1.0f, 0f, 1.0f));

        TryToSetShader(new Vector3(0f, 0f, 0f));
        TryToSetShader(new Vector3(1f, 0f, 0f));

        //if (_timer < Time.time)
        //{
        //    print("ef");
        //    var type = _tileMap.GetTile(transform.position + new Vector3(0.0f, -0.5f, 0f));
        //    if (type == TileType.Water)
        //    {
        //        // var go = Instantiate(WaterEffect);
        //        // go.transform.position = transform.position + new Vector3(0.0f, -0.5f, 0.0f);
        //    }
        //    _timer = Time.time + 1f;
        //}
    }
Beispiel #5
0
    void Update()
    {
        float z = ZlayerManager.GetZFromY(transform.position);

        transform.position = new Vector3(transform.position.x, transform.position.y, z);
    }
Beispiel #6
0
    public bool TrySpawnRares(Chunk chunk)
    {
        if (AllSpawnded)
        {
            return(false);
        }

        // choose index
        int index = ChooseIndex(SingleInstanceSpawnSettings.Spawns.Count);

        if (index == -1)
        {
            AllSpawnded = true;
        }

        if (0 == Random.Range(0, SingleInstanceSpawnSettings.SpawnRate))
        {
            int tries = 0;
            do
            {
                int x = Random.Range(0, Chunk.CHUNK_SIZE);
                int y = Random.Range(0, Chunk.CHUNK_SIZE);

                if (chunk.AreaClear(x, y))
                {
                    if (chunk.GetTileOnTileGameObject(x, y) == null)
                    {
                        var go = Instantiate(SingleInstanceSpawnSettings.Spawns[index].Spawn);
                        _spawnsLeft[index]--;

                        go.transform.position = chunk.GetGameObject(x, y).transform.position;
                        go.transform.position = new Vector3(go.transform.position.x, go.transform.position.y, ZlayerManager.GetZFromY(go.transform.position));

                        chunk.AddObject(x, y, go);
                        Debug.Log("rare spawned!");

                        return(true);
                    }
                }
                tries++;
            } while (tries < SPAWN_MAX_TRIES);
        }
        return(false);
    }
Beispiel #7
0
    public bool SpawnRares(Chunk chunk) // Spawned y/n
    {
        if (_currentSpawn >= SingleInstanceSpawnSettings.Spawns.Count || AllSpawnded)
        {
            return(false);
        }

        if (0 == Random.Range(0, SingleInstanceSpawnSettings.SpawnRate))
        {
            int  tries   = 0;
            bool Success = false;

            do
            {
                int x = Random.Range(0, Chunk.CHUNK_SIZE);
                int y = Random.Range(0, Chunk.CHUNK_SIZE);

                if (chunk.AreaClear(x, y))
                {
                    if (chunk.GetTileOnTileGameObject(x, y) == null)
                    {
                        bool searchingForSpawn = true;
                        int  index             = Random.Range(0, SingleInstanceSpawnSettings.Spawns.Count);
                        int  startIndex        = index;

                        while (searchingForSpawn)
                        {
                            if (_spawnsLeft[index] > 0)
                            {
                                var go = Instantiate(SingleInstanceSpawnSettings.Spawns[index].Spawn);
                                _spawnsLeft[index]--;

                                go.transform.position = chunk.GetGameObject(x, y).transform.position;
                                go.transform.position = new Vector3(go.transform.position.x, go.transform.position.y, ZlayerManager.GetZFromY(go.transform.position));

                                chunk.AddObject(x, y, go);
                                Success = true;
                                Debug.Log("rare spawned!");
                                // _currentSpawn++;

                                searchingForSpawn = false;
                                break;
                            }

                            index++;
                            if (index == startIndex)
                            {
                                AllSpawnded = true;
                                break;
                            }

                            if (index == SingleInstanceSpawnSettings.Spawns.Count)
                            {
                                index = 0;
                            }
                        }
                    }
                }
                tries++;
            } while (tries < SPAWN_MAX_TRIES && !Success);
            return(Success);
        }
        return(false);
    }
Beispiel #8
0
    public void Load()
    {
        Dictionary <Vec2, ResourceType> types = new Dictionary <Vec2, ResourceType>();

        SetTypes(TestWriter.Load(ChunkSaveName(), out types)); // lataa tiilet

        // lataa tiilien resurssit
        foreach (var keyvaluepair in types)
        {
            Vec2         v    = keyvaluepair.Key;
            ResourceType type = keyvaluepair.Value;

            GameObject go = null;

            if (ResourceManager.Instance.IsTrunkType(type)) // kaikki destroyed
            {
                go = ObjectPool.instance.GetObjectForType(Resource.GetResourcePrefabName(type), false);
                go.gameObject.GetComponent <Resource>().Init(true);

                go.transform.position          = new Vector3(v.X + offsetX * CHUNK_SIZE, v.Y + offsetY * CHUNK_SIZE, ZlayerManager.GetZFromY(go.transform.position));
                go.transform.parent            = _parent.transform;
                worldObjects[keyvaluepair.Key] = go;
            }
            else // #HaCK ctrl_c_ctrl_v
            {
                go = ObjectPool.instance.GetObjectForType(Resource.GetResourcePrefabName(type), false);
                if (go != null)
                {
                    go.gameObject.GetComponent <Resource>().Init(false);

                    go.transform.position          = new Vector3(v.X + offsetX * CHUNK_SIZE, v.Y + offsetY * CHUNK_SIZE, ZlayerManager.GetZFromY(go.transform.position));
                    go.transform.parent            = _parent.transform;
                    worldObjects[keyvaluepair.Key] = go;
                }
            }
        }
    }
Beispiel #9
0
    public void SetTileSprites(int width, int height, ITileMap tilemap, int startX, int startY)
    {
        // int tempIndex = 0;
        float offsetX = tilemap.GetTileGameObject(0, 0).transform.position.x;
        float offsetY = tilemap.GetTileGameObject(0, 0).transform.position.y;

        // sstring currentPool = GetCurrentPool();
        string currentPool = "Border";

        for (int y = startY; y < height; y++)
        {
            for (int x = startX; x < width; x++)
            {
                TileType type = tilemap.GetTile(x, y);

#if true
                if (type == TileType.Water || type == TileType.DeepWater)
                {
                    var anime = tilemap.GetTileGameObject(x, y).GetComponent<TileAnime>();
                    anime.enabled = true;
                    anime.SetSprites(type);
                    anime.Continue(); // maybe redudant 

                    continue;
                }
                else
                {
                    var tileAnime = tilemap.GetTileGameObject(x, y).GetComponent<TileAnime>();
                    if (tileAnime != null)
                        tileAnime.enabled = false;
                }
#endif

                if (type == TileType.Mountain) // outdoor mountain ===??)==??=9
                {
                    int tile = GetAssetNameBitmaskNoStr(x, y, tilemap, type);
                    if (tile == 15)
                    {
                        tilemap.GetTileGameObject(x, y).GetComponent<SpriteRenderer>().sprite = FlatMountains[Random.Range(0, FlatMountains.Length - 1)];
                    }
                    else
                    {
                        tilemap.GetTileGameObject(x, y).GetComponent<SpriteRenderer>().sprite = Mountains[tile];
                    }

                    if (Perlin.CanPlaceDoor && (tile == (int)TileDir.N || tile == (int)TileDir.S || tile == (int)TileDir.E || tile == (int)TileDir.W))
                    {
                        var go = Instantiate(Perlin.DoorHackGameObject);
                        go.transform.position = tilemap.GetTileGameObject(x, y).transform.position;
                        go.transform.position = new Vector3(go.transform.position.x, go.transform.position.y, ZlayerManager.GetZFromY(go.transform.position));
                        // tilemap.AddLater_hack(x, y, go);
                        print("door placed");
                        Perlin.CanPlaceDoor = false;

                        // translate hack
                        switch (tile)
                        {
                            case (int)TileDir.N:
                                go.transform.Translate(Vector3.up);
                                break;
                            case (int)TileDir.S:
                                go.transform.Translate(Vector3.down);
                                break;
                            case (int)TileDir.E:
                                go.transform.Translate(Vector3.left);
                                break;
                            case (int)TileDir.W:
                                go.transform.Translate(Vector3.right);
                                break;
                            default:
                                break;
                        }
                    }
                    continue;
                }

                // normals:
                int value = GetAssetNameBitmaskNoStr(x, y, tilemap, type);

                if (IsImplemented(type))
                    tilemap.GetTileGameObject(x, y).GetComponent<SpriteRenderer>().sprite = _textures2[type][(Random.Range(0, GetAssetCount(type)))];

                if (value == 15)    // ei voi tulla enää reunoja
                {
                    continue;
                }

                // lisää: reunat
                // 4-bit directions
                // North, west, east, south
#if false
                if (tempIndex < temporarySecondLayer.Length - 1)
                {
                    ObjectPool.instance.GetObjectForType("Border", true);

                    string border = "GrassLand_";

                    if (IsImplemented(type))
                    {
                        border = GetEnumName(type);
                    }

                    if ((value & (1 << 1 - 1)) == 0)            // TODO: TEXTURES2 TÄNNE
                    {
                        temporarySecondLayer[tempIndex].GetComponent<SpriteRenderer>().sprite = _textures[border + "N0"];
                        temporarySecondLayer[tempIndex].SetActive(true);
                        temporarySecondLayer[tempIndex].transform.position = new Vector3(x + offsetX, y + offsetY + 1);
                        tempIndex++;
                    }
                    if ((value & (1 << 2 - 1)) == 0)
                    {
                        temporarySecondLayer[tempIndex].GetComponent<SpriteRenderer>().sprite = _textures[border + "W0"];
                        temporarySecondLayer[tempIndex].transform.position = new Vector3(x + offsetX - 1, y + offsetY);
                        temporarySecondLayer[tempIndex].SetActive(true);
                        tempIndex++;
                    }
                    if ((value & (1 << 3 - 1)) == 0)
                    {
                        temporarySecondLayer[tempIndex].GetComponent<SpriteRenderer>().sprite = _textures[border + "E0"];
                        temporarySecondLayer[tempIndex].transform.position = new Vector3(x + offsetX + 1, y + offsetY);
                        temporarySecondLayer[tempIndex].SetActive(true);
                        tempIndex++;
                    }
                    if ((value & (1 << 4 - 1)) == 0)
                    {
                        temporarySecondLayer[tempIndex].GetComponent<SpriteRenderer>().sprite = _textures[border + "S0"];
                        temporarySecondLayer[tempIndex].transform.position = new Vector3(x + offsetX, y + offsetY - 1);
                        temporarySecondLayer[tempIndex].SetActive(true);
                        tempIndex++;
                    }
#else

                string border = "GrassLand_";

                if (IsImplemented(type))
                {
                    border = GetEnumName(type);
                }

                if ((value & (1 << 1 - 1)) == 0)            // TODO: TEXTURES2 TÄNNE
                {
                    var go = ObjectPool.instance.GetObjectForType(currentPool, false);
                    go.GetComponent<SpriteRenderer>().sprite = _textures[border + "N0"];
                    go.transform.position = new Vector3(x + offsetX, y + offsetY + 1);
                    go.transform.parent = _borders.transform;
                    borders.Add(go);
                }

                if ((value & (1 << 2 - 1)) == 0)
                {
                    var go = ObjectPool.instance.GetObjectForType(currentPool, false);
                    go.GetComponent<SpriteRenderer>().sprite = _textures[border + "W0"];
                    go.transform.position = new Vector3(x + offsetX - 1, y + offsetY);
                    go.transform.parent = _borders.transform;
                    borders.Add(go);
                }

                if ((value & (1 << 3 - 1)) == 0)
                {
                    var go = ObjectPool.instance.GetObjectForType(currentPool, false);
                    go.GetComponent<SpriteRenderer>().sprite = _textures[border + "E0"];
                    go.transform.position = new Vector3(x + offsetX + 1, y + offsetY);
                    go.transform.parent = _borders.transform;
                    borders.Add(go);
                }

                if ((value & (1 << 4 - 1)) == 0)
                {
                    var go = ObjectPool.instance.GetObjectForType(currentPool, false);
                    go.GetComponent<SpriteRenderer>().sprite = _textures[border + "S0"];
                    go.transform.position = new Vector3(x + offsetX, y + offsetY - 1);
                    go.transform.parent = _borders.transform;
                    borders.Add(go);
                }
#endif
            }
        }

        // tarkista rajat
        for (int i = 0; i < borders.Count; i++)
        {
            var border = borders[i];
            if (offsetX > border.transform.position.x ||
                offsetX + TileMap.TotalWidth < border.transform.position.x ||
                offsetY > border.transform.position.y ||
                offsetY + TileMap.TotalHeight < border.transform.position.y)
            {
                // border.transform.parent = null;
                border.transform.parent = null;
                ObjectPool.instance.PoolObject(border);
                borders.RemoveAt(i);
                i--;
            }
        }
        // f**k f**k
    }