package projectzombie.tiles;

import gl_engine.vec.Vec2i;
import projectzombie.init.Models;
import projectzombie.init.Tiles;
import projectzombie.model.Model;
import projectzombie.util.TileState;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;

public class TileGrass extends Tile
{

	@Override
	public Model getModel(byte meta) {
		return Models.TILE_GRASS;
	}
	
	@Override
	public void tickRandomly(Layer layer, Chunk chunk, TileState state, Vec2i pos) {
		super.tickRandomly(layer, chunk, state, pos);
		
		if(Math.random() > 0.8)
		{
			Vec2i cpos = pos.add(new Vec2i(
					(int)Math.floor(Math.random() * 3) - 1,
					(int)Math.floor(Math.random() * 3) - 1));
			
			if(layer.getBackTile(cpos).tile == Tiles.DIRT) {
				layer.setBackTile(Tiles.GRASS.getDefaultState(), cpos);
			}
		}
		
		if(Math.random() > 0.998)
		{
			if(layer.getFrontTile(pos).tile == Tiles.VOID)
			{
				int grass_count = 0;
				int d = 3;
				
				for(int x=-d;x<=d;x++) {
					for(int y=-d;y<=d;y++)
					{
						if(layer.getFrontTile(pos.add(new Vec2i(x, y))).tile == Tiles.TALL_GRASS) {
							grass_count += 1;
						}
					}
				}
				
				if(grass_count < 2)
				{
					layer.setFrontTile(Tiles.TALL_GRASS.getDefaultState(), pos);
				}
			}
		}
	}

}
