// Разместить и раскрасить чанк MapChunk BuildChunk(Vector2 position) { MapChunk chunk = null; if (chunksPool.Count > 0) { chunk = chunksPool[0]; chunksPool.Remove(chunk); chunk.gameObject.SetActive(true); } else { GameObject chunkObject = Instantiate(chunkPrefab); chunkObject.transform.SetParent(transform); chunk = chunkObject.GetComponent <MapChunk>(); chunk.BuildMesh(chunkSize); } chunk.name = "Chunk_" + position.x + "_" + position.y; chunk.position = position; MDTile[] tiles = map.GetTilesAt((int)position.x, (int)position.y, chunkSize, chunkSize); GDTile[] textures = new GDTile[tiles.Length]; for (int t = 0; t < tiles.Length; t++) { textures[t] = tiles[t].texture; } chunk.SetTextures(textures); return(chunk); }
public MDTile(string type, bool solid = false, bool liquid = false, float speed = 1.0f) { this.type = type; this.texture = ResManager.shared.TextureForTile(type); this.solid = solid; this.liquid = liquid; this.speed = speed; }
void LoadTiles() { this.atlasWidth = this.tilesTexture.width / SettingsManager.shared.tileResolution; this.atlasHeight = this.tilesTexture.height / SettingsManager.shared.tileResolution; JSONObject root = new JSONObject(this.tilesDatabase.text); // Переберем тайлы for (int k = 0; k < root.list.Count; k++) { JSONObject jsonTile = root.list[k]; GDTile[] textures = new GDTile[jsonTile.list.Count]; string name = root.keys[k]; tiles[name] = textures; // Переберем варианты текстур тайла for (int t = 0; t < jsonTile.list.Count; t++) { JSONObject jsonTexture = jsonTile.list[t]; // Если текстура анимированая, переберем координаты кадров и добавим в массив разверток if (jsonTexture.HasField(jAnimation)) { JSONObject jsonFrames = jsonTexture.GetField(jAnimation); Vector2[][] uvs = new Vector2[jsonFrames.list.Count][]; for (int f = 0; f < jsonFrames.list.Count; f++) { JSONObject jsonFrame = jsonFrames.list[t]; uvs[f] = GetUV(jsonFrame.GetField(jX).f, jsonFrame.GetField(jY).f); } GDTile texture = new GDTile(uvs); if (jsonTexture.HasField(jSync)) { texture.sync = jsonTexture.GetField(jSync).b; } textures[t] = texture; // Иначе текстура не анимированая, просто добавим координату одной развертки } else { Vector2[] uv = GetUV(jsonTile.GetField(jX).f, jsonTile.GetField(jY).f); GDTile texture = new GDTile(uv); if (jsonTile.HasField(jWeight)) { texture.weight = (int)jsonTile.GetField(jWeight).f; } textures[t] = texture; } } } }
public void SetTextures(GDTile[] textures) { this.textures = textures; animations.Clear(); int frame; for (int i = 0; i < textures.Length; i++) { GDTile tile = textures[i]; Vector2[] uv = tile.uvs[0]; frame = 0; if (tile.isAnimated) { if (!tile.sync) { frame = Random.Range(0, tile.uvs.Length); uv = tile.uvs[frame]; } animations[i] = frame; } SetUV(i, uv); } mesh.uv = this.uv; }
// Возвращает текстуру по имени тайла public GDTile TextureForTile(string type) { GDTile texture = null; float total = 0; GDTile[] textures = this.tiles[type]; for (int t = 0; t < textures.Length; t++) { total += textures[t].weight; } float random = Random.Range(0, total); float position = 0; for (int p = 0; p < textures.Length; p++) { position += textures[p].weight; if (position > random) { texture = textures[p]; break; } } return(texture); }