@muvtacode/cli 0.1.8 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/loop.js +6 -0
- package/dist/agent/loop.js.map +1 -1
- package/dist/repl.js +94 -22
- package/dist/repl.js.map +1 -1
- package/dist/templates/game/adapt-audio-01/files/src/audio/audio.ts +192 -0
- package/dist/templates/game/adapt-audio-01/meta.json +34 -0
- package/dist/templates/game/adapt-camera-rig-01/files/src/camera/rig.ts +193 -0
- package/dist/templates/game/adapt-camera-rig-01/meta.json +38 -0
- package/dist/templates/game/adapt-controle-teclado-01/files/src/controles/teclado.ts +171 -0
- package/dist/templates/game/adapt-controle-teclado-01/meta.json +34 -0
- package/dist/templates/game/adapt-controle-toque-01/files/src/controles/toque.ts +291 -0
- package/dist/templates/game/adapt-controle-toque-01/meta.json +36 -0
- package/dist/templates/game/adapt-dia-noite-01/files/src/world/dianoite.ts +157 -0
- package/dist/templates/game/adapt-dia-noite-01/meta.json +38 -0
- package/dist/templates/game/adapt-hud-01/files/src/hud/hud.ts +263 -0
- package/dist/templates/game/adapt-hud-01/meta.json +36 -0
- package/dist/templates/game/adapt-menu-01/files/src/ui/menu.ts +273 -0
- package/dist/templates/game/adapt-menu-01/meta.json +36 -0
- package/dist/templates/game/adapt-particulas-01/files/src/vfx/particulas.ts +212 -0
- package/dist/templates/game/adapt-particulas-01/meta.json +38 -0
- package/dist/templates/game/adapt-perf-mobile-01/files/src/engine/performance.ts +145 -0
- package/dist/templates/game/adapt-perf-mobile-01/meta.json +35 -0
- package/dist/templates/game/adapt-save-01/files/src/save/save.ts +188 -0
- package/dist/templates/game/adapt-save-01/meta.json +34 -0
- package/dist/templates/game/carro-2d-01/files/src/models/carro2d.ts +253 -0
- package/dist/templates/game/carro-2d-01/meta.json +38 -0
- package/dist/templates/game/carro-3d-01/files/src/models/carro3d.ts +266 -0
- package/dist/templates/game/carro-3d-01/meta.json +13 -0
- package/dist/templates/game/game-design-tokens-01/files/src/design/tokens.ts +274 -0
- package/dist/templates/game/game-design-tokens-01/meta.json +13 -0
- package/dist/templates/game/game-engine-base-01/files/src/engine/device.ts +57 -0
- package/dist/templates/game/game-engine-base-01/files/src/engine/engine.ts +201 -0
- package/dist/templates/game/game-engine-base-01/files/src/engine/index.ts +48 -0
- package/dist/templates/game/game-engine-base-01/files/src/engine/input.ts +206 -0
- package/dist/templates/game/game-engine-base-01/meta.json +13 -0
- package/dist/templates/game/mundo-25d-01/files/src/world/mundo25d.ts +252 -0
- package/dist/templates/game/mundo-25d-01/meta.json +39 -0
- package/dist/templates/game/mundo-2d-01/files/src/world/mundo2d.ts +273 -0
- package/dist/templates/game/mundo-2d-01/meta.json +38 -0
- package/dist/templates/game/mundo-3d-01/files/src/world/mundo3d.ts +250 -0
- package/dist/templates/game/mundo-3d-01/meta.json +13 -0
- package/dist/templates/game/personagem-2d-01/files/src/models/personagem2d.ts +341 -0
- package/dist/templates/game/personagem-2d-01/meta.json +41 -0
- package/dist/templates/game/personagem-3d-01/files/src/models/personagem3d.ts +293 -0
- package/dist/templates/game/personagem-3d-01/meta.json +13 -0
- package/package.json +1 -1
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// Mundo 2D: tilemap, colisão, câmera e parallax.
|
|
3
|
+
//
|
|
4
|
+
// Duas coisas que quase todo platformer feito às pressas erra:
|
|
5
|
+
//
|
|
6
|
+
// 1. COLISÃO RESOLVIDA POR EIXO. Se você move em X e Y e testa uma vez só, o
|
|
7
|
+
// corpo gruda na quina ao pular encostado na parede. Aqui move X, resolve X,
|
|
8
|
+
// move Y, resolve Y. É a diferença entre pulo que funciona e pulo que trava.
|
|
9
|
+
//
|
|
10
|
+
// 2. CÂMERA COM ZONA MORTA. Câmera colada no jogador enjoa e treme a cada
|
|
11
|
+
// passo. Com uma zona morta no meio, ela só anda quando o jogador realmente
|
|
12
|
+
// se desloca — e o resultado é muito mais confortável de olhar.
|
|
13
|
+
//
|
|
14
|
+
// O mapa é ASCII no próprio código: editar a fase é editar strings.
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
import { getPalette, alpha } from '../design/tokens';
|
|
18
|
+
|
|
19
|
+
export const tuning = {
|
|
20
|
+
tamanhoTile: 32,
|
|
21
|
+
/** Metade da largura/altura da zona morta da câmera, em fração da tela. */
|
|
22
|
+
zonaMortaX: 0.18,
|
|
23
|
+
zonaMortaY: 0.22,
|
|
24
|
+
/** Suavização da câmera (1/s). Maior = mais colada. */
|
|
25
|
+
suavizacaoCamera: 6,
|
|
26
|
+
/** Velocidade de cada camada de parallax (0 = parada, 1 = junto com a câmera). */
|
|
27
|
+
parallax: [0.15, 0.35, 0.6],
|
|
28
|
+
/** Desenha as caixas de colisão. Ligue ao investigar bug de física. */
|
|
29
|
+
debug: false
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** Legenda do mapa ASCII. Adicione a sua trocando o caractere. */
|
|
33
|
+
export const TILES = {
|
|
34
|
+
'.': { solido: false, cor: null },
|
|
35
|
+
'#': { solido: true, cor: 'superficie' },
|
|
36
|
+
'=': { solido: true, cor: 'secundaria' }, // plataforma
|
|
37
|
+
'^': { solido: true, cor: 'perigo' }, // espinho — trate no gameplay
|
|
38
|
+
'*': { solido: false, cor: 'aviso' } // decoração/coletável
|
|
39
|
+
} as const;
|
|
40
|
+
|
|
41
|
+
export type Caractere = keyof typeof TILES;
|
|
42
|
+
|
|
43
|
+
export interface Corpo {
|
|
44
|
+
x: number;
|
|
45
|
+
y: number;
|
|
46
|
+
largura: number;
|
|
47
|
+
altura: number;
|
|
48
|
+
vx: number;
|
|
49
|
+
vy: number;
|
|
50
|
+
/** Preenchido pelo mover(): true quando encostou. */
|
|
51
|
+
noChao: boolean;
|
|
52
|
+
encostouParede: boolean;
|
|
53
|
+
encostouTeto: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface Mundo2D {
|
|
57
|
+
largura: number;
|
|
58
|
+
altura: number;
|
|
59
|
+
/** Câmera em px (canto superior esquerdo do que aparece). */
|
|
60
|
+
camX: number;
|
|
61
|
+
camY: number;
|
|
62
|
+
tileEm(col: number, lin: number): Caractere;
|
|
63
|
+
solidoEm(x: number, y: number): boolean;
|
|
64
|
+
/** Move o corpo resolvendo colisão eixo a eixo. Escreve nas flags do corpo. */
|
|
65
|
+
mover(corpo: Corpo, passo: number): void;
|
|
66
|
+
seguirCamera(alvoX: number, alvoY: number, passo: number, larguraTela: number, alturaTela: number): void;
|
|
67
|
+
desenharFundo(ctx: CanvasRenderingContext2D, larguraTela: number, alturaTela: number): void;
|
|
68
|
+
desenhar(ctx: CanvasRenderingContext2D, larguraTela: number, alturaTela: number): void;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Mapa de exemplo. Cada string é uma linha; todas com o mesmo comprimento.
|
|
73
|
+
* Troque livremente — a colisão e o desenho leem daqui.
|
|
74
|
+
*/
|
|
75
|
+
export const MAPA_EXEMPLO = [
|
|
76
|
+
'................................................',
|
|
77
|
+
'................................................',
|
|
78
|
+
'..........*.....................*...............',
|
|
79
|
+
'.........===...........===.....===..............',
|
|
80
|
+
'................................................',
|
|
81
|
+
'....*...................................*.......',
|
|
82
|
+
'...===.......===..............===......===......',
|
|
83
|
+
'................................................',
|
|
84
|
+
'..............................*.................',
|
|
85
|
+
'#####.......=====............====...............',
|
|
86
|
+
'#####...........................................',
|
|
87
|
+
'#####....^^^....................................',
|
|
88
|
+
'################################.....###########',
|
|
89
|
+
'################################.....###########'
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
export function criarMundo2D(mapa: string[] = MAPA_EXEMPLO): Mundo2D {
|
|
93
|
+
const T = tuning.tamanhoTile;
|
|
94
|
+
const linhas = mapa.length;
|
|
95
|
+
const colunas = Math.max(...mapa.map((l) => l.length));
|
|
96
|
+
|
|
97
|
+
const tileEm = (col: number, lin: number): Caractere => {
|
|
98
|
+
if (lin < 0 || lin >= linhas || col < 0 || col >= colunas) return '.';
|
|
99
|
+
const ch = mapa[lin]![col] as Caractere | undefined;
|
|
100
|
+
return ch && ch in TILES ? ch : '.';
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const solidoEm = (x: number, y: number): boolean => {
|
|
104
|
+
const col = Math.floor(x / T);
|
|
105
|
+
const lin = Math.floor(y / T);
|
|
106
|
+
return TILES[tileEm(col, lin)].solido;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/** Testa os 4 cantos da caixa. Suficiente para corpo menor que 1 tile. */
|
|
110
|
+
const colide = (x: number, y: number, l: number, a: number): boolean => {
|
|
111
|
+
const eps = 0.01; // evita pegar o tile vizinho ao encostar exatamente na borda
|
|
112
|
+
return (
|
|
113
|
+
solidoEm(x + eps, y + eps) ||
|
|
114
|
+
solidoEm(x + l - eps, y + eps) ||
|
|
115
|
+
solidoEm(x + eps, y + a - eps) ||
|
|
116
|
+
solidoEm(x + l - eps, y + a - eps)
|
|
117
|
+
);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const mundo: Mundo2D = {
|
|
121
|
+
largura: colunas * T,
|
|
122
|
+
altura: linhas * T,
|
|
123
|
+
camX: 0,
|
|
124
|
+
camY: 0,
|
|
125
|
+
tileEm,
|
|
126
|
+
solidoEm,
|
|
127
|
+
|
|
128
|
+
mover(corpo, passo) {
|
|
129
|
+
corpo.noChao = false;
|
|
130
|
+
corpo.encostouParede = false;
|
|
131
|
+
corpo.encostouTeto = false;
|
|
132
|
+
|
|
133
|
+
// --- eixo X
|
|
134
|
+
const dx = corpo.vx * passo;
|
|
135
|
+
corpo.x += dx;
|
|
136
|
+
if (colide(corpo.x, corpo.y, corpo.largura, corpo.altura)) {
|
|
137
|
+
// Empurra de volta até a borda do tile — sem "sub-passo", direto.
|
|
138
|
+
corpo.x = dx > 0
|
|
139
|
+
? Math.floor((corpo.x + corpo.largura) / T) * T - corpo.largura - 0.01
|
|
140
|
+
: Math.floor(corpo.x / T) * T + T + 0.01;
|
|
141
|
+
corpo.vx = 0;
|
|
142
|
+
corpo.encostouParede = true;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// --- eixo Y (separado: é isto que destrava o pulo junto à parede)
|
|
146
|
+
const dy = corpo.vy * passo;
|
|
147
|
+
corpo.y += dy;
|
|
148
|
+
if (colide(corpo.x, corpo.y, corpo.largura, corpo.altura)) {
|
|
149
|
+
if (dy > 0) {
|
|
150
|
+
corpo.y = Math.floor((corpo.y + corpo.altura) / T) * T - corpo.altura - 0.01;
|
|
151
|
+
corpo.noChao = true;
|
|
152
|
+
} else {
|
|
153
|
+
corpo.y = Math.floor(corpo.y / T) * T + T + 0.01;
|
|
154
|
+
corpo.encostouTeto = true;
|
|
155
|
+
}
|
|
156
|
+
corpo.vy = 0;
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
|
|
160
|
+
seguirCamera(alvoX, alvoY, passo, larguraTela, alturaTela) {
|
|
161
|
+
const zx = larguraTela * tuning.zonaMortaX;
|
|
162
|
+
const zy = alturaTela * tuning.zonaMortaY;
|
|
163
|
+
const centroX = mundo.camX + larguraTela / 2;
|
|
164
|
+
const centroY = mundo.camY + alturaTela / 2;
|
|
165
|
+
|
|
166
|
+
let desejadaX = mundo.camX;
|
|
167
|
+
let desejadaY = mundo.camY;
|
|
168
|
+
// Só move quando o alvo sai da zona morta.
|
|
169
|
+
if (alvoX < centroX - zx) desejadaX = alvoX + zx - larguraTela / 2;
|
|
170
|
+
else if (alvoX > centroX + zx) desejadaX = alvoX - zx - larguraTela / 2;
|
|
171
|
+
if (alvoY < centroY - zy) desejadaY = alvoY + zy - alturaTela / 2;
|
|
172
|
+
else if (alvoY > centroY + zy) desejadaY = alvoY - zy - alturaTela / 2;
|
|
173
|
+
|
|
174
|
+
const k = 1 - Math.exp(-passo * tuning.suavizacaoCamera);
|
|
175
|
+
mundo.camX += (desejadaX - mundo.camX) * k;
|
|
176
|
+
mundo.camY += (desejadaY - mundo.camY) * k;
|
|
177
|
+
|
|
178
|
+
// Trava nos limites: a câmera nunca mostra fora do nível.
|
|
179
|
+
mundo.camX = Math.max(0, Math.min(mundo.largura - larguraTela, mundo.camX));
|
|
180
|
+
mundo.camY = Math.max(0, Math.min(mundo.altura - alturaTela, mundo.camY));
|
|
181
|
+
if (mundo.largura < larguraTela) mundo.camX = (mundo.largura - larguraTela) / 2;
|
|
182
|
+
if (mundo.altura < alturaTela) mundo.camY = (mundo.altura - alturaTela) / 2;
|
|
183
|
+
},
|
|
184
|
+
|
|
185
|
+
desenharFundo(ctx, larguraTela, alturaTela) {
|
|
186
|
+
const p = getPalette();
|
|
187
|
+
const grad = ctx.createLinearGradient(0, 0, 0, alturaTela);
|
|
188
|
+
grad.addColorStop(0, p.fundo);
|
|
189
|
+
grad.addColorStop(1, p.superficie);
|
|
190
|
+
ctx.fillStyle = grad;
|
|
191
|
+
ctx.fillRect(0, 0, larguraTela, alturaTela);
|
|
192
|
+
|
|
193
|
+
// Parallax: colinas em 3 camadas, cada uma andando mais devagar que a
|
|
194
|
+
// câmera. Desenhadas por seno — nenhuma imagem necessária.
|
|
195
|
+
tuning.parallax.forEach((vel, camada) => {
|
|
196
|
+
const desloc = -mundo.camX * vel;
|
|
197
|
+
const baseY = alturaTela * (0.55 + camada * 0.12);
|
|
198
|
+
const amp = 26 + camada * 14;
|
|
199
|
+
ctx.beginPath();
|
|
200
|
+
ctx.moveTo(0, alturaTela);
|
|
201
|
+
for (let x = 0; x <= larguraTela; x += 12) {
|
|
202
|
+
const mundoX = x - desloc;
|
|
203
|
+
const y = baseY + Math.sin(mundoX * 0.006 + camada * 2.1) * amp
|
|
204
|
+
+ Math.sin(mundoX * 0.017 + camada) * amp * 0.35;
|
|
205
|
+
ctx.lineTo(x, y);
|
|
206
|
+
}
|
|
207
|
+
ctx.lineTo(larguraTela, alturaTela);
|
|
208
|
+
ctx.closePath();
|
|
209
|
+
ctx.fillStyle = alpha(p.secundaria, 0.14 + camada * 0.1);
|
|
210
|
+
ctx.fill();
|
|
211
|
+
});
|
|
212
|
+
},
|
|
213
|
+
|
|
214
|
+
desenhar(ctx, larguraTela, alturaTela) {
|
|
215
|
+
const p = getPalette();
|
|
216
|
+
ctx.save();
|
|
217
|
+
ctx.translate(-Math.round(mundo.camX), -Math.round(mundo.camY));
|
|
218
|
+
|
|
219
|
+
// Culling: só os tiles na tela. Sem isto, mapa grande = travamento.
|
|
220
|
+
const c0 = Math.max(0, Math.floor(mundo.camX / T));
|
|
221
|
+
const c1 = Math.min(colunas - 1, Math.ceil((mundo.camX + larguraTela) / T));
|
|
222
|
+
const l0 = Math.max(0, Math.floor(mundo.camY / T));
|
|
223
|
+
const l1 = Math.min(linhas - 1, Math.ceil((mundo.camY + alturaTela) / T));
|
|
224
|
+
|
|
225
|
+
for (let lin = l0; lin <= l1; lin++) {
|
|
226
|
+
for (let col = c0; col <= c1; col++) {
|
|
227
|
+
const ch = tileEm(col, lin);
|
|
228
|
+
const def = TILES[ch];
|
|
229
|
+
if (!def.cor) continue;
|
|
230
|
+
|
|
231
|
+
const x = col * T;
|
|
232
|
+
const y = lin * T;
|
|
233
|
+
ctx.fillStyle = p[def.cor as keyof typeof p] as string;
|
|
234
|
+
|
|
235
|
+
if (ch === '^') {
|
|
236
|
+
// Espinho: triângulo, não bloco — a forma comunica o perigo.
|
|
237
|
+
ctx.beginPath();
|
|
238
|
+
ctx.moveTo(x, y + T);
|
|
239
|
+
ctx.lineTo(x + T / 2, y + T * 0.25);
|
|
240
|
+
ctx.lineTo(x + T, y + T);
|
|
241
|
+
ctx.closePath();
|
|
242
|
+
ctx.fill();
|
|
243
|
+
} else if (ch === '*') {
|
|
244
|
+
ctx.beginPath();
|
|
245
|
+
ctx.arc(x + T / 2, y + T / 2, T * 0.22, 0, Math.PI * 2);
|
|
246
|
+
ctx.fill();
|
|
247
|
+
} else {
|
|
248
|
+
ctx.fillRect(x, y, T, T);
|
|
249
|
+
// Topo mais claro: dá volume sem textura.
|
|
250
|
+
if (!TILES[tileEm(col, lin - 1)].solido) {
|
|
251
|
+
ctx.fillStyle = alpha(p.texto, 0.14);
|
|
252
|
+
ctx.fillRect(x, y, T, 4);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (tuning.debug) {
|
|
259
|
+
ctx.strokeStyle = alpha(p.perigo, 0.4);
|
|
260
|
+
ctx.lineWidth = 1;
|
|
261
|
+
for (let lin = l0; lin <= l1; lin++) {
|
|
262
|
+
for (let col = c0; col <= c1; col++) {
|
|
263
|
+
if (TILES[tileEm(col, lin)].solido) ctx.strokeRect(col * T, lin * T, T, T);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
ctx.restore();
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
return mundo;
|
|
273
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"categoria": "game",
|
|
3
|
+
"framework": "canvas",
|
|
4
|
+
"linguagem": "typescript",
|
|
5
|
+
"dependencias": [],
|
|
6
|
+
"compatibilidade": {
|
|
7
|
+
"banco": [],
|
|
8
|
+
"auth": []
|
|
9
|
+
},
|
|
10
|
+
"versao": "1.0.0",
|
|
11
|
+
"id": "mundo-2d-01",
|
|
12
|
+
"nome": "Mundo 2D (tilemap + parallax + colisão)",
|
|
13
|
+
"tags": [
|
|
14
|
+
"game",
|
|
15
|
+
"jogo",
|
|
16
|
+
"mundo",
|
|
17
|
+
"mapa",
|
|
18
|
+
"tilemap",
|
|
19
|
+
"2d",
|
|
20
|
+
"canvas",
|
|
21
|
+
"plataforma",
|
|
22
|
+
"parallax",
|
|
23
|
+
"colisao",
|
|
24
|
+
"camera",
|
|
25
|
+
"nivel",
|
|
26
|
+
"fase"
|
|
27
|
+
],
|
|
28
|
+
"recursos_suportados": [
|
|
29
|
+
"tilemap",
|
|
30
|
+
"colisao-aabb",
|
|
31
|
+
"parallax-3-camadas",
|
|
32
|
+
"camera-com-zona-morta",
|
|
33
|
+
"culling",
|
|
34
|
+
"mapa-por-texto",
|
|
35
|
+
"autotile"
|
|
36
|
+
],
|
|
37
|
+
"descricao": "Mundo 2D em tiles com o que um platformer precisa de verdade: colisão AABB resolvida por eixo (evita grudar na parede ao pular), câmera com zona morta e limites do nível, fundo parallax de 3 camadas e culling que desenha só os tiles visíveis. O mapa é escrito como texto ASCII no próprio código — editar a fase é editar strings, sem editor externo."
|
|
38
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// Mundo 3D: terreno, céu, luz e vegetação.
|
|
3
|
+
//
|
|
4
|
+
// Três decisões que valem explicar:
|
|
5
|
+
//
|
|
6
|
+
// 1. Ruído próprio com SEED, não Math.random(). Mesma seed = mesmo mapa. Sem
|
|
7
|
+
// isso é impossível reproduzir um bug ("o jogador caiu pelo chão") ou fazer
|
|
8
|
+
// o mundo voltar igual depois de carregar um save.
|
|
9
|
+
//
|
|
10
|
+
// 2. A neblina usa a MESMA cor do céu. É o truque que faz o horizonte sumir sem
|
|
11
|
+
// a linha de corte feia onde o terreno acaba — em vez de esconder, integra.
|
|
12
|
+
//
|
|
13
|
+
// 3. Vegetação em InstancedMesh. 2000 árvores como Mesh separada = 2000 draw
|
|
14
|
+
// calls = travamento no celular. Como instância, é 1 draw call.
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
import * as THREE from 'three';
|
|
18
|
+
import { getPalette, hex } from '../design/tokens';
|
|
19
|
+
|
|
20
|
+
export const tuning = {
|
|
21
|
+
/** Lado do terreno em metros. */
|
|
22
|
+
tamanho: 400,
|
|
23
|
+
/** Divisões da malha. 128 = 16k vértices; 256 já pesa em celular fraco. */
|
|
24
|
+
resolucao: 128,
|
|
25
|
+
/** Altura máxima das colinas. */
|
|
26
|
+
altura: 18,
|
|
27
|
+
/** Escala do relevo. Menor = colinas largas; maior = terreno recortado. */
|
|
28
|
+
escalaRelevo: 0.012,
|
|
29
|
+
/** Camadas de ruído. Mais oitavas = mais detalhe, mais custo. */
|
|
30
|
+
oitavas: 4,
|
|
31
|
+
/** Densidade de props por 1000 m². */
|
|
32
|
+
densidadeProps: 3,
|
|
33
|
+
/** Distância em que a neblina fecha totalmente. */
|
|
34
|
+
alcanceVisao: 260,
|
|
35
|
+
sombras: true
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** Biomas: cada um é um conjunto de papéis de cor + parâmetros de relevo. */
|
|
39
|
+
export const BIOMAS = {
|
|
40
|
+
campo: { chao: 'sucesso', props: 'secundaria', relevo: 1.0, densidade: 1.0 },
|
|
41
|
+
deserto: { chao: 'aviso', props: 'primaria', relevo: 0.55, densidade: 0.25 },
|
|
42
|
+
neve: { chao: 'texto', props: 'contorno', relevo: 1.35, densidade: 0.5 },
|
|
43
|
+
vulcanico:{ chao: 'perigo', props: 'superficie', relevo: 1.6, densidade: 0.35 },
|
|
44
|
+
alien: { chao: 'primaria', props: 'acento', relevo: 1.2, densidade: 0.9 },
|
|
45
|
+
noturno: { chao: 'superficie',props: 'primaria', relevo: 0.9, densidade: 0.7 }
|
|
46
|
+
} as const;
|
|
47
|
+
|
|
48
|
+
export type NomeBioma = keyof typeof BIOMAS;
|
|
49
|
+
|
|
50
|
+
export interface Mundo3DOpts {
|
|
51
|
+
paleta?: string;
|
|
52
|
+
bioma?: NomeBioma;
|
|
53
|
+
/** Mesma seed = mesmo mundo. Guarde no save. */
|
|
54
|
+
seed?: number;
|
|
55
|
+
tamanho?: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface Mundo3D {
|
|
59
|
+
raiz: THREE.Group;
|
|
60
|
+
/** Adicione na cena junto com a raiz. */
|
|
61
|
+
luzes: THREE.Object3D[];
|
|
62
|
+
/** Altura do terreno num ponto — use para pousar personagem/inimigo no chão. */
|
|
63
|
+
alturaEm(x: number, z: number): number;
|
|
64
|
+
/** Normal do terreno — para alinhar o carro com a inclinação. */
|
|
65
|
+
normalEm(x: number, z: number): THREE.Vector3;
|
|
66
|
+
aplicarNeblina(cena: THREE.Scene): void;
|
|
67
|
+
dispose(): void;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Ruído de valor com interpolação suave. Determinístico a partir da seed —
|
|
72
|
+
* não é Perlin, mas para relevo de jogo o resultado é indistinguível e cabe
|
|
73
|
+
* em 20 linhas sem dependência.
|
|
74
|
+
*/
|
|
75
|
+
function criarRuido(seed: number) {
|
|
76
|
+
const aleatorio = (x: number, y: number): number => {
|
|
77
|
+
// Hash inteiro: mistura x, y e seed em um número 0..1 estável.
|
|
78
|
+
let h = x * 374761393 + y * 668265263 + seed * 1442695040888963407;
|
|
79
|
+
h = (h ^ (h >> 13)) * 1274126177;
|
|
80
|
+
return ((h ^ (h >> 16)) >>> 0) / 4294967295;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const suave = (t: number) => t * t * (3 - 2 * t); // smoothstep
|
|
84
|
+
|
|
85
|
+
const valor = (x: number, y: number): number => {
|
|
86
|
+
const xi = Math.floor(x), yi = Math.floor(y);
|
|
87
|
+
const xf = suave(x - xi), yf = suave(y - yi);
|
|
88
|
+
const a = aleatorio(xi, yi), b = aleatorio(xi + 1, yi);
|
|
89
|
+
const c = aleatorio(xi, yi + 1), d = aleatorio(xi + 1, yi + 1);
|
|
90
|
+
return (a * (1 - xf) + b * xf) * (1 - yf) + (c * (1 - xf) + d * xf) * yf;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/** Soma de oitavas (fBm): dá colina grande com detalhe pequeno em cima. */
|
|
94
|
+
return (x: number, y: number, oitavas: number): number => {
|
|
95
|
+
let total = 0, amplitude = 1, frequencia = 1, soma = 0;
|
|
96
|
+
for (let i = 0; i < oitavas; i++) {
|
|
97
|
+
total += valor(x * frequencia, y * frequencia) * amplitude;
|
|
98
|
+
soma += amplitude;
|
|
99
|
+
amplitude *= 0.5;
|
|
100
|
+
frequencia *= 2;
|
|
101
|
+
}
|
|
102
|
+
return total / soma;
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function createMundo3D(opts: Mundo3DOpts = {}): Mundo3D {
|
|
107
|
+
const palette = getPalette(opts.paleta);
|
|
108
|
+
const bioma = BIOMAS[opts.bioma ?? 'campo'];
|
|
109
|
+
const seed = opts.seed ?? 1337;
|
|
110
|
+
const tamanho = opts.tamanho ?? tuning.tamanho;
|
|
111
|
+
const ruido = criarRuido(seed);
|
|
112
|
+
|
|
113
|
+
const raiz = new THREE.Group();
|
|
114
|
+
raiz.name = 'mundo';
|
|
115
|
+
const descartaveis: Array<{ dispose(): void }> = [];
|
|
116
|
+
|
|
117
|
+
/** Altura do relevo — a MESMA função usada pela malha e por alturaEm(). */
|
|
118
|
+
const alturaBruta = (x: number, z: number): number =>
|
|
119
|
+
(ruido(x * tuning.escalaRelevo, z * tuning.escalaRelevo, tuning.oitavas) - 0.5) *
|
|
120
|
+
tuning.altura * bioma.relevo * 2;
|
|
121
|
+
|
|
122
|
+
// ---------------------------------------------------------------- terreno
|
|
123
|
+
const geo = new THREE.PlaneGeometry(tamanho, tamanho, tuning.resolucao, tuning.resolucao);
|
|
124
|
+
geo.rotateX(-Math.PI / 2);
|
|
125
|
+
const pos = geo.attributes.position as THREE.BufferAttribute;
|
|
126
|
+
for (let i = 0; i < pos.count; i++) {
|
|
127
|
+
pos.setY(i, alturaBruta(pos.getX(i), pos.getZ(i)));
|
|
128
|
+
}
|
|
129
|
+
// Sem isto a luz calcula errado e o terreno fica com faces chapadas.
|
|
130
|
+
geo.computeVertexNormals();
|
|
131
|
+
|
|
132
|
+
const matChao = new THREE.MeshStandardMaterial({
|
|
133
|
+
color: palette[bioma.chao as keyof typeof palette] as string,
|
|
134
|
+
roughness: 0.95,
|
|
135
|
+
metalness: 0,
|
|
136
|
+
flatShading: false
|
|
137
|
+
});
|
|
138
|
+
const chao = new THREE.Mesh(geo, matChao);
|
|
139
|
+
chao.receiveShadow = tuning.sombras;
|
|
140
|
+
raiz.add(chao);
|
|
141
|
+
descartaveis.push(geo, matChao);
|
|
142
|
+
|
|
143
|
+
// ------------------------------------------------------------------ props
|
|
144
|
+
const corProps = palette[bioma.props as keyof typeof palette] as string;
|
|
145
|
+
const quantidade = Math.floor((tamanho * tamanho / 1000) * tuning.densidadeProps * bioma.densidade);
|
|
146
|
+
|
|
147
|
+
const geoProp = new THREE.ConeGeometry(1.1, 4.5, 6);
|
|
148
|
+
const matProp = new THREE.MeshStandardMaterial({ color: corProps, roughness: 0.85 });
|
|
149
|
+
const props = new THREE.InstancedMesh(geoProp, matProp, quantidade);
|
|
150
|
+
props.castShadow = tuning.sombras;
|
|
151
|
+
|
|
152
|
+
const dummy = new THREE.Object3D();
|
|
153
|
+
const meio = tamanho / 2;
|
|
154
|
+
for (let i = 0; i < quantidade; i++) {
|
|
155
|
+
// Posição também derivada da seed: os props voltam no mesmo lugar.
|
|
156
|
+
const x = (ruido(i * 0.73, seed * 0.11, 1) - 0.5) * tamanho;
|
|
157
|
+
const z = (ruido(seed * 0.29, i * 0.53, 1) - 0.5) * tamanho;
|
|
158
|
+
const y = alturaBruta(x, z);
|
|
159
|
+
const escala = 0.6 + ruido(i * 1.7, i * 0.3, 1) * 0.9;
|
|
160
|
+
|
|
161
|
+
dummy.position.set(x, y + 2.2 * escala, z);
|
|
162
|
+
dummy.rotation.y = ruido(i, i, 1) * Math.PI * 2;
|
|
163
|
+
dummy.scale.setScalar(escala);
|
|
164
|
+
dummy.updateMatrix();
|
|
165
|
+
props.setMatrixAt(i, dummy.matrix);
|
|
166
|
+
}
|
|
167
|
+
props.instanceMatrix.needsUpdate = true;
|
|
168
|
+
raiz.add(props);
|
|
169
|
+
descartaveis.push(geoProp, matProp);
|
|
170
|
+
|
|
171
|
+
// ------------------------------------------------------------------- céu
|
|
172
|
+
// Esfera gigante com material pintado por dentro (BackSide) e gradiente do
|
|
173
|
+
// horizonte para o zênite. Mais barato que cubemap e sem asset.
|
|
174
|
+
const geoCeu = new THREE.SphereGeometry(tuning.alcanceVisao * 2.2, 24, 16);
|
|
175
|
+
const matCeu = new THREE.ShaderMaterial({
|
|
176
|
+
side: THREE.BackSide,
|
|
177
|
+
depthWrite: false,
|
|
178
|
+
uniforms: {
|
|
179
|
+
corBaixo: { value: new THREE.Color(hex(palette.neblina)) },
|
|
180
|
+
corAlto: { value: new THREE.Color(hex(palette.fundo)) }
|
|
181
|
+
},
|
|
182
|
+
vertexShader: `
|
|
183
|
+
varying vec3 vPos;
|
|
184
|
+
void main() {
|
|
185
|
+
vPos = position;
|
|
186
|
+
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
|
187
|
+
}`,
|
|
188
|
+
fragmentShader: `
|
|
189
|
+
uniform vec3 corBaixo;
|
|
190
|
+
uniform vec3 corAlto;
|
|
191
|
+
varying vec3 vPos;
|
|
192
|
+
void main() {
|
|
193
|
+
float t = clamp(normalize(vPos).y * 0.5 + 0.5, 0.0, 1.0);
|
|
194
|
+
gl_FragColor = vec4(mix(corBaixo, corAlto, pow(t, 0.75)), 1.0);
|
|
195
|
+
}`
|
|
196
|
+
});
|
|
197
|
+
const ceu = new THREE.Mesh(geoCeu, matCeu);
|
|
198
|
+
raiz.add(ceu);
|
|
199
|
+
descartaveis.push(geoCeu, matCeu);
|
|
200
|
+
|
|
201
|
+
// ------------------------------------------------------------------ luz
|
|
202
|
+
// Rig de 3 pontos: sol (direcional, faz sombra), hemisférica (céu x chão,
|
|
203
|
+
// evita a sombra virar preto morto) e um preenchimento fraco por trás.
|
|
204
|
+
const sol = new THREE.DirectionalLight(hex(palette.luzPrincipal), 2.1);
|
|
205
|
+
sol.position.set(tamanho * 0.3, tuning.altura * 6, tamanho * 0.2);
|
|
206
|
+
if (tuning.sombras) {
|
|
207
|
+
sol.castShadow = true;
|
|
208
|
+
sol.shadow.mapSize.set(1024, 1024);
|
|
209
|
+
const cam = sol.shadow.camera as THREE.OrthographicCamera;
|
|
210
|
+
cam.left = -meio * 0.5; cam.right = meio * 0.5;
|
|
211
|
+
cam.top = meio * 0.5; cam.bottom = -meio * 0.5;
|
|
212
|
+
cam.far = tuning.altura * 14;
|
|
213
|
+
cam.updateProjectionMatrix();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const hemi = new THREE.HemisphereLight(hex(palette.fundo), hex(palette.luzAmbiente), 1.1);
|
|
217
|
+
const preenchimento = new THREE.DirectionalLight(hex(palette.luzAmbiente), 0.45);
|
|
218
|
+
preenchimento.position.set(-tamanho * 0.3, tuning.altura * 3, -tamanho * 0.25);
|
|
219
|
+
|
|
220
|
+
const luzes: THREE.Object3D[] = [sol, hemi, preenchimento];
|
|
221
|
+
|
|
222
|
+
// -------------------------------------------------------------- consultas
|
|
223
|
+
const alturaEm = (x: number, z: number): number => alturaBruta(x, z);
|
|
224
|
+
|
|
225
|
+
const normalEm = (x: number, z: number): THREE.Vector3 => {
|
|
226
|
+
// Diferenças finitas: amostra vizinhos e monta a normal. Passo de 1m é o
|
|
227
|
+
// suficiente para alinhar um carro sem tremer.
|
|
228
|
+
const d = 1;
|
|
229
|
+
const hL = alturaBruta(x - d, z), hR = alturaBruta(x + d, z);
|
|
230
|
+
const hB = alturaBruta(x, z - d), hF = alturaBruta(x, z + d);
|
|
231
|
+
return new THREE.Vector3(hL - hR, 2 * d, hB - hF).normalize();
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const aplicarNeblina = (cena: THREE.Scene): void => {
|
|
235
|
+
cena.fog = new THREE.Fog(hex(palette.neblina), tuning.alcanceVisao * 0.35, tuning.alcanceVisao);
|
|
236
|
+
cena.background = new THREE.Color(hex(palette.neblina));
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
return {
|
|
240
|
+
raiz,
|
|
241
|
+
luzes,
|
|
242
|
+
alturaEm,
|
|
243
|
+
normalEm,
|
|
244
|
+
aplicarNeblina,
|
|
245
|
+
dispose() {
|
|
246
|
+
descartaveis.forEach((d) => d.dispose());
|
|
247
|
+
props.dispose();
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "mundo-3d-01",
|
|
3
|
+
"nome": "Mundo 3D (terreno + céu + iluminação)",
|
|
4
|
+
"categoria": "game",
|
|
5
|
+
"framework": "three",
|
|
6
|
+
"linguagem": "typescript",
|
|
7
|
+
"dependencias": ["three"],
|
|
8
|
+
"tags": ["game", "jogo", "mundo", "world", "cenario", "terreno", "3d", "three", "mapa", "ambiente", "iluminacao", "neblina", "skybox", "open-world"],
|
|
9
|
+
"compatibilidade": { "banco": [], "auth": [] },
|
|
10
|
+
"recursos_suportados": ["terreno-por-ruido", "6-biomas", "iluminacao-3-pontos", "neblina-casada", "props-instanciados", "altura-no-ponto", "sombras-opcionais"],
|
|
11
|
+
"versao": "1.0.0",
|
|
12
|
+
"descricao": "Mundo 3D gerado por ruído determinístico (mesma seed = mesmo mapa, então dá para reproduzir bug): terreno com colinas, 6 biomas prontos, céu em gradiente casado com a neblina (horizonte sem emenda), rig de luz de 3 pontos e vegetação/pedras via InstancedMesh — 2000 objetos em 1 draw call, que é o que permite rodar em celular. Expõe alturaEm(x,z) para posicionar personagem e inimigos no chão."
|
|
13
|
+
}
|