LordXido commited on
Commit
a69d494
·
verified ·
1 Parent(s): 508f83a

Update main.js

Browse files
Files changed (1) hide show
  1. main.js +84 -66
main.js CHANGED
@@ -1,3 +1,7 @@
 
 
 
 
1
  const canvas = document.getElementById("gl");
2
  const gl = canvas.getContext("webgl");
3
  const log = document.getElementById("log");
@@ -5,87 +9,110 @@ const input = document.getElementById("input");
5
 
6
  function print(m){ log.innerHTML += m+"<br/>"; log.scrollTop=log.scrollHeight; }
7
 
8
- canvas.width = window.innerWidth - 420;
9
  canvas.height = window.innerHeight;
10
 
11
- // ===== Camera =====
12
- let camPos = [0, 0, -4];
13
- let yaw = 0, pitch = 0;
14
  const keys = {};
15
-
16
- window.addEventListener("keydown", e=>keys[e.key]=true);
17
- window.addEventListener("keyup", e=>keys[e.key]=false);
18
- canvas.addEventListener("mousemove", e=>{
19
  if(e.buttons===1){
20
- yaw += e.movementX * 0.002;
21
- pitch += e.movementY * 0.002;
22
  }
23
  });
24
-
25
  function camRot(){
26
  const cy=Math.cos(yaw), sy=Math.sin(yaw);
27
  const cp=Math.cos(pitch), sp=Math.sin(pitch);
28
  return [
29
- cy, 0, -sy,
30
- sy*sp, cp, cy*sp,
31
- sy*cp, -sp, cy*cp
32
  ];
33
  }
34
 
35
- // ===== Load Shader =====
36
- async function loadShader(){
37
- return await (await fetch("shader.glsl")).text();
38
- }
39
 
40
- let power = 8.0;
41
- let energy = 1.0;
42
-
43
- // Ω branch graph
44
- const branches = {};
45
- let currentBranch = "root";
46
-
47
- (async ()=>{
48
- const frag = await loadShader();
49
- const vert = `
50
- attribute vec2 p;
51
- void main(){ gl_Position=vec4(p,0,1); }
52
- `;
53
- function compile(t,s){
54
- const sh=gl.createShader(t);
55
- gl.shaderSource(sh,s);
56
- gl.compileShader(sh);
57
- return sh;
 
 
 
 
 
58
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
 
 
 
 
 
60
  const prog=gl.createProgram();
61
  gl.attachShader(prog,compile(gl.VERTEX_SHADER,vert));
62
  gl.attachShader(prog,compile(gl.FRAGMENT_SHADER,frag));
63
- gl.linkProgram(prog);
64
- gl.useProgram(prog);
65
 
66
  const buf=gl.createBuffer();
67
  gl.bindBuffer(gl.ARRAY_BUFFER,buf);
68
  gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),gl.STATIC_DRAW);
69
-
70
  const loc=gl.getAttribLocation(prog,"p");
71
  gl.enableVertexAttribArray(loc);
72
  gl.vertexAttribPointer(loc,2,gl.FLOAT,false,0,0);
73
 
74
  const uRes=gl.getUniformLocation(prog,"u_res");
75
- const uTime=gl.getUniformLocation(prog,"u_time");
76
  const uPow=gl.getUniformLocation(prog,"u_power");
77
  const uEng=gl.getUniformLocation(prog,"u_energy");
78
  const uCam=gl.getUniformLocation(prog,"u_camPos");
79
  const uRot=gl.getUniformLocation(prog,"u_camRot");
80
 
81
- function loop(t){
82
  if(keys["w"]) camPos[2]+=0.05;
83
  if(keys["s"]) camPos[2]-=0.05;
84
  if(keys["a"]) camPos[0]-=0.05;
85
  if(keys["d"]) camPos[0]+=0.05;
86
 
 
 
87
  gl.uniform2f(uRes,canvas.width,canvas.height);
88
- gl.uniform1f(uTime,t*0.001);
89
  gl.uniform1f(uPow,power);
90
  gl.uniform1f(uEng,energy);
91
  gl.uniform3fv(uCam,camPos);
@@ -93,41 +120,32 @@ let currentBranch = "root";
93
  gl.drawArrays(gl.TRIANGLE_STRIP,0,4);
94
  requestAnimationFrame(loop);
95
  }
96
- loop(0);
97
  })();
98
 
99
- // ===== CLI (CCL-v1) =====
100
  function handle(cmd){
 
101
  const p=cmd.split(" ");
102
  switch(p[0]){
103
  case "help":
104
  print("inject <v> | superposition <v>");
105
- print("branch <name> | switch <name>");
106
- print("move x y z");
107
  break;
108
  case "inject":
109
  energy=parseFloat(p[1])||energy;
110
- print("Ψ="+energy);
111
- break;
112
  case "superposition":
113
  power=parseFloat(p[1])||power;
114
- print("σ="+power);
115
- break;
116
- case "branch":
117
- branches[p[1]]={power,energy,cam:[...camPos]};
118
- print("Ω branch created");
119
- break;
120
- case "switch":
121
- if(branches[p[1]]){
122
- ({power,energy}=branches[p[1]]);
123
- camPos=[...branches[p[1]].cam];
124
- currentBranch=p[1];
125
- print("Ω switched");
126
- }
127
- break;
128
- case "move":
129
- camPos=[+p[1],+p[2],+p[3]];
130
- break;
131
  }
132
  }
133
 
@@ -139,5 +157,5 @@ input.addEventListener("keydown",e=>{
139
  }
140
  });
141
 
142
- print("CodexReality3D v6Open World Engine");
143
  print("Type `help`");
 
1
+ // ================================
2
+ // CodexReality3D v7 — Living World
3
+ // ================================
4
+
5
  const canvas = document.getElementById("gl");
6
  const gl = canvas.getContext("webgl");
7
  const log = document.getElementById("log");
 
9
 
10
  function print(m){ log.innerHTML += m+"<br/>"; log.scrollTop=log.scrollHeight; }
11
 
12
+ canvas.width = window.innerWidth - 440;
13
  canvas.height = window.innerHeight;
14
 
15
+ // ---------- Camera ----------
16
+ let camPos = [0,0,-4];
17
+ let yaw=0, pitch=0;
18
  const keys = {};
19
+ window.addEventListener("keydown",e=>keys[e.key]=true);
20
+ window.addEventListener("keyup",e=>keys[e.key]=false);
21
+ canvas.addEventListener("mousemove",e=>{
 
22
  if(e.buttons===1){
23
+ yaw += e.movementX*0.002;
24
+ pitch += e.movementY*0.002;
25
  }
26
  });
 
27
  function camRot(){
28
  const cy=Math.cos(yaw), sy=Math.sin(yaw);
29
  const cp=Math.cos(pitch), sp=Math.sin(pitch);
30
  return [
31
+ cy,0,-sy,
32
+ sy*sp,cp,cy*sp,
33
+ sy*cp,-sp,cy*cp
34
  ];
35
  }
36
 
37
+ // ---------- GPU Init ----------
38
+ async function loadShader(){ return await (await fetch("shader.glsl")).text(); }
39
+
40
+ let power=8.0, energy=1.0;
41
 
42
+ // ---------- World Systems ----------
43
+ const agents = [];
44
+ const economy = { gold:1000, price:1.0 };
45
+ const ledger = []; // Ω command log
46
+
47
+ class Agent {
48
+ constructor(id){
49
+ this.id=id;
50
+ this.pos=[Math.random()*2-1,0,Math.random()*2-1];
51
+ this.vel=[0,0,0];
52
+ this.energy=1.0;
53
+ }
54
+ step(){
55
+ // simple policy: wander + trade
56
+ this.vel[0]+= (Math.random()-0.5)*0.01;
57
+ this.vel[2]+= (Math.random()-0.5)*0.01;
58
+ this.pos[0]+=this.vel[0];
59
+ this.pos[2]+=this.vel[2];
60
+ this.energy -= 0.001;
61
+ if(this.energy<0.5 && economy.gold>1){
62
+ economy.gold-=1;
63
+ this.energy+=0.5;
64
+ }
65
  }
66
+ }
67
+
68
+ // ---------- Persistence ----------
69
+ function saveWorld(){
70
+ localStorage.setItem("codex_v7", JSON.stringify({
71
+ power, energy, camPos, economy, agents
72
+ }));
73
+ print("Ω world saved");
74
+ }
75
+ function loadWorld(){
76
+ const d=JSON.parse(localStorage.getItem("codex_v7"));
77
+ if(!d) return;
78
+ power=d.power; energy=d.energy; camPos=d.camPos;
79
+ economy.gold=d.economy.gold; economy.price=d.economy.price;
80
+ agents.length=0; d.agents.forEach(a=>agents.push(Object.assign(new Agent(a.id),a)));
81
+ print("Ω world restored");
82
+ }
83
 
84
+ // ---------- Render Loop ----------
85
+ (async()=>{
86
+ const frag=await loadShader();
87
+ const vert=`attribute vec2 p;void main(){gl_Position=vec4(p,0,1);}`;
88
+ function compile(t,s){const sh=gl.createShader(t);gl.shaderSource(sh,s);gl.compileShader(sh);return sh;}
89
  const prog=gl.createProgram();
90
  gl.attachShader(prog,compile(gl.VERTEX_SHADER,vert));
91
  gl.attachShader(prog,compile(gl.FRAGMENT_SHADER,frag));
92
+ gl.linkProgram(prog); gl.useProgram(prog);
 
93
 
94
  const buf=gl.createBuffer();
95
  gl.bindBuffer(gl.ARRAY_BUFFER,buf);
96
  gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),gl.STATIC_DRAW);
 
97
  const loc=gl.getAttribLocation(prog,"p");
98
  gl.enableVertexAttribArray(loc);
99
  gl.vertexAttribPointer(loc,2,gl.FLOAT,false,0,0);
100
 
101
  const uRes=gl.getUniformLocation(prog,"u_res");
 
102
  const uPow=gl.getUniformLocation(prog,"u_power");
103
  const uEng=gl.getUniformLocation(prog,"u_energy");
104
  const uCam=gl.getUniformLocation(prog,"u_camPos");
105
  const uRot=gl.getUniformLocation(prog,"u_camRot");
106
 
107
+ function loop(){
108
  if(keys["w"]) camPos[2]+=0.05;
109
  if(keys["s"]) camPos[2]-=0.05;
110
  if(keys["a"]) camPos[0]-=0.05;
111
  if(keys["d"]) camPos[0]+=0.05;
112
 
113
+ agents.forEach(a=>a.step());
114
+
115
  gl.uniform2f(uRes,canvas.width,canvas.height);
 
116
  gl.uniform1f(uPow,power);
117
  gl.uniform1f(uEng,energy);
118
  gl.uniform3fv(uCam,camPos);
 
120
  gl.drawArrays(gl.TRIANGLE_STRIP,0,4);
121
  requestAnimationFrame(loop);
122
  }
123
+ loop();
124
  })();
125
 
126
+ // ---------- CLI (CCL-v2) ----------
127
  function handle(cmd){
128
+ ledger.push(cmd);
129
  const p=cmd.split(" ");
130
  switch(p[0]){
131
  case "help":
132
  print("inject <v> | superposition <v>");
133
+ print("spawn_agent | save | load");
134
+ print("economy");
135
  break;
136
  case "inject":
137
  energy=parseFloat(p[1])||energy;
138
+ print("Ψ="+energy); break;
 
139
  case "superposition":
140
  power=parseFloat(p[1])||power;
141
+ print("σ="+power); break;
142
+ case "spawn_agent":
143
+ agents.push(new Agent(agents.length));
144
+ print("Agent spawned"); break;
145
+ case "economy":
146
+ print("Gold="+economy.gold); break;
147
+ case "save": saveWorld(); break;
148
+ case "load": loadWorld(); break;
 
 
 
 
 
 
 
 
 
149
  }
150
  }
151
 
 
157
  }
158
  });
159
 
160
+ print("CodexReality3D v7Living World Engine");
161
  print("Type `help`");