## 🧱 Granularidade
Funções pequenas (bom) vs função gigante (ruim)
```python
# ruim (tudo junto)
def processar_usuario(usuario):
print(usuario["nome"])
if usuario["idade"] > 18:
print("adulto")
# bom (granular)
def mostrar_nome(usuario):
print(usuario["nome"])
def eh_adulto(usuario):
return usuario["idade"] > 18
```
---
## 🧩 Modularidade
Separar em arquivos
```python
# arquivo: usuario.py
def criar(nome):
return {"nome": nome}
# arquivo: main.py
from usuario import criar
u = criar("Will")
```
---
## 🧠 Coesão
Cada função faz UMA coisa
```python
# ruim
def usuario_tudo(usuario):
print(usuario["nome"])
salvar_no_banco(usuario)
# bom
def mostrar(usuario):
print(usuario["nome"])
def salvar(usuario):
salvar_no_banco(usuario)
```
---
## 🔗 Acoplamento
Evitar dependência forte
```python
# ruim (acoplado)
def enviar_email():
servidor = Gmail() # preso nisso
# bom (desacoplado)
def enviar_email(servico_email):
servico_email.enviar()
```
---
## 🎭 Abstração
Esconder complexidade
```python
def salvar_usuario(usuario):
banco.inserir(usuario) # você não precisa saber como funciona
```
---
## 🔒 Encapsulamento
Proteger dados internos
```python
class Conta:
def __init__(self):
self._saldo = 0 # "protegido"
def depositar(self, valor):
self._saldo += valor
def ver_saldo(self):
return self._saldo
```
---
## 🧬 Composição
Construir com partes
```python
class Motor:
def ligar(self):
print("motor ligado")
class Carro:
def __init__(self):
self.motor = Motor() # composição
def ligar(self):
self.motor.ligar()
```
---
## 🏭 Pipeline
Etapas em sequência
```python
def etapa1(x): return x + 1
def etapa2(x): return x * 2
def etapa3(x): return x - 3
def pipeline(x):
x = etapa1(x)
x = etapa2(x)
x = etapa3(x)
return x
```
---
## 🔧 Extensibilidade
Adicionar sem quebrar
```python
class Plugin:
def executar(self):
pass
class PluginA(Plugin):
def executar(self):
print("A")
class PluginB(Plugin):
def executar(self):
print("B")
```
---
## 🛠️ Manutenibilidade
Código limpo = fácil de mexer
```python
# ruim
def calc(x,y):return x+y if x>0 else y
# bom
def somar(a, b):
return a + b
```
---
## 📊 Data-Driven
Dados controlam comportamento
```python
config = {
"multiplicador": 10
}
def calcular(x):
return x * config["multiplicador"]
```
---
## 👾 ECS (Entity Component System)
```python
# componentes
posicao = {"x": 10, "y": 20}
vida = {"hp": 100}
# entidade = conjunto
entidade = {
"posicao": posicao,
"vida": vida
}
```
---
## 🔄 State Machine
```python
estado = "parado"
def atualizar(entrada):
global estado
if estado == "parado" and entrada == "andar":
estado = "andando"
elif estado == "andando" and entrada == "pular":
estado = "pulando"
```
---
## 🎯 Determinismo
```python
def soma(x, y):
return x + y
# sempre igual
print(soma(2, 3)) # sempre 5
```
---
## 🧠 Cognitive Load
Código fácil de ler
```python
# ruim
x=1;y=2;z=x+y
# bom
numero1 = 1
numero2 = 2
resultado = numero1 + numero2
```
---
## 📍 Local Reasoning
Entender sem olhar tudo
```python
def dobrar(x):
return x * 2 # você entende só olhando aqui
```
---
## 🫗 Leaky Abstraction
Abstração “vazando”
```python
# parece simples
def ler_arquivo():
return open("file.txt").read()
# problema: pode dar erro se arquivo não existir 😬
```
Projeto tá tão grande que cada compilada é 1 min. [hj são mais 5/6 min]
The function below uses Perlin noise octaves to calculate the height of a vertex based on its coordinates and the number of octaves to be applied. It is a slightly modified version of the original code introduced on TTG’s generation step 3. The values of the base frequency, persistence and lacunarity are constant for demonstration purposes.
static float GetHeight(float x, float y, uint octaves, Vector2[] offsets)
{
float height = 0;
var amplitude = 1f;
var frequency = 0.055f;
const float persistence = 0.5f;
const float lacunarity = 2.5f;
for (var i = 0; i < octaves; i++)
{
var offset = offsets[i];
var filterX = x * frequency + offset.x;
var filterY = y * frequency + offset.y;
var noise = Mathf.PerlinNoise(filterX, filterY);
height += amplitude * noise;
frequency *= lacunarity;
amplitude *= persistence;
}
return height;
}
A fairly similar function is used in TTG’s code, with parameterized values instead of hard-coded ones.
The outcome
The GIF below displays the usage of Perlin noise octaves, with four terrains: the terrain we’ve been using as an example in this section, and three other ones; each one with an increasing number of Perlin noise octaves, from 1 to 3 (check the left bottom corner of the image for octave count). All terrains were generated with a persistence of 0.375 and a lacunarity of 2.52.
The value of the octaves is clear: they add detail without changing the scale of the terrain, delivering a more natural-looking outcome. That’s exactly what we were looking for.
Perlin noise octaves are a simple, yet quite satisfactory solution for adding more detail to terraced terrains. This feature shipped as part of TTG 1.2.0, where both the API and the helper component were updated to support Perlin noise octaves. On the next release, I plan to add added (2.0.0) spheres as a basic terrain shape, allowing terraced planets to be created using TTG.
Estou aprendendo e desenvolvendo com geração de terreno usando duas coisas simples e poderosas: funções senoidais e Perlin Noise.
A senoidal uso como “estrutura base”: ela cria ondas grandes e suaves, tipo colinas e vales. Regiões mais altas, mais baixas, transições previsíveis. É matemática pura: sin(x) + sin(z) já te dá uma paisagem básica.
Por cima disso eu jogo Perlin Noise, que é um tipo de ruído gradiente, criado pelo Ken Perlin nos anos 80. A graça dele é que, diferente de um ruído aleatório comum, ele muda suavemente, criando padrões naturais: montes, erosões, irregularidades — tudo com “cara de natureza”.
O pipeline é basicamente:
height = baseSine(x, z)
+ perlin(x * freqLow)*ampLow
+ perlin(x * freqHigh)*ampHigh
a senoidal define o terreno macro
o Perlin de baixa frequência cria grandes formas orgânicas
o Perlin de alta frequência adiciona detalhes finos
O resultado final é um terreno que parece natural, mas nasce 100% de matemática e ruído controlado. Zero heightmap externo, zero modelos prontos: só funções.
[Em [Eterna] Reforma]