@erik9994857/cag 1.0.3 → 2.0.2

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/defaults/main.cag CHANGED
@@ -1,3 +1,8 @@
1
- Depency Model.API
2
- Depency WorldGen.API
3
- Depency Pull.API
1
+ Dependency Model.API
2
+ Dependency WorldGen.API
3
+ Dependency Pull.API
4
+ Dependency Chunk.API
5
+ Dependency Player.API
6
+ Dependency Camera.API
7
+ Dependency Render.API
8
+ Dependency Window.API
@@ -1,5 +1,39 @@
1
+ // Default Game - CaG Engine Demo
2
+ // This script sets up a flat 3D world with grass blocks and chunk loading
3
+
1
4
  Import Model.API
2
5
  Import WorldGen.API
6
+ Import Chunk.API
7
+ Import Player.API
8
+ Import Camera.API
9
+ Import Render.API
10
+ Import Window.API
11
+
12
+ // Configure the window
13
+ {function use Window.API SetTitle Default Game
14
+ {function use Window.API SetSize 1280 720
15
+
16
+ // Configure rendering
17
+ {function use Render.API SetSkyColor 0.53 0.81 0.92
18
+ {function use Render.API SetFog 0.53 0.81 0.92 64 128
19
+ {function use Render.API SetSunDirection 0.5 1.0 0.3
20
+ {function use Render.API SetAmbientLight 0.3 0.3 0.4
21
+
22
+ // Configure camera
23
+ {function use Camera.API SetFOV 70
24
+ {function use Camera.API SetMode first-person
3
25
 
26
+ // Load the grass block model
4
27
  {function use Model.API UseModel Grass
28
+
29
+ // Configure chunks - 16x16 blocks, render 1 chunk in every direction
30
+ {function use Chunk.API SetSize 16
31
+ {function use Chunk.API SetRenderDistance 1
32
+
33
+ // Generate flat world with grass on top
5
34
  {function use WorldGen.API generate world top layer = Grass
35
+
36
+ // Configure and spawn the player
37
+ {function use Player.API SetSpeed 0.08
38
+ {function use Player.API SetJumpHeight 0.18
39
+ {function use Player.API Spawn 8 2 8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@erik9994857/cag",
3
- "version": "1.0.3",
3
+ "version": "2.0.2",
4
4
  "description": "CaG — A code library and custom language for building 3D worlds with .cagc files, .bbmodel support, and auto UV mapping",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -30,8 +30,6 @@
30
30
  "engines": {
31
31
  "node": ">=18.0.0"
32
32
  },
33
- "dependencies": {
34
- "@electron/packager": "^18.3.0"
35
- },
33
+ "dependencies": {},
36
34
  "devDependencies": {}
37
35
  }
@@ -1,6 +1,11 @@
1
1
  const ModelAPI = require("./model-api");
2
2
  const WorldGenAPI = require("./worldgen-api");
3
3
  const PullAPI = require("./pull-api");
4
+ const ChunkAPI = require("./chunk-api");
5
+ const PlayerAPI = require("./player-api");
6
+ const CameraAPI = require("./camera-api");
7
+ const RenderAPI = require("./render-api");
8
+ const WindowAPI = require("./window-api");
4
9
 
5
10
  class APIRegistry {
6
11
  constructor() {
@@ -24,6 +29,21 @@ class APIRegistry {
24
29
  var pullAPI = new PullAPI(resourceMap, resourcesPath);
25
30
  this.register("Pull.API", pullAPI);
26
31
 
32
+ var chunkAPI = new ChunkAPI();
33
+ this.register("Chunk.API", chunkAPI);
34
+
35
+ var playerAPI = new PlayerAPI();
36
+ this.register("Player.API", playerAPI);
37
+
38
+ var cameraAPI = new CameraAPI();
39
+ this.register("Camera.API", cameraAPI);
40
+
41
+ var renderAPI = new RenderAPI();
42
+ this.register("Render.API", renderAPI);
43
+
44
+ var windowAPI = new WindowAPI();
45
+ this.register("Window.API", windowAPI);
46
+
27
47
  this.initialized = true;
28
48
  return this;
29
49
  }
@@ -0,0 +1,75 @@
1
+ class CameraAPI {
2
+ constructor() {
3
+ this.fov = 70;
4
+ this.near = 0.1;
5
+ this.far = 300;
6
+ this.mode = "first-person";
7
+ this.sensitivity = 0.002;
8
+ }
9
+
10
+ execute(action, params) {
11
+ switch (action) {
12
+ case "SetFOV":
13
+ return this.setFOV(params);
14
+ case "SetNearFar":
15
+ return this.setNearFar(params);
16
+ case "SetMode":
17
+ return this.setMode(params);
18
+ case "SetSensitivity":
19
+ return this.setSensitivity(params);
20
+ case "GetSettings":
21
+ return this.getSettings();
22
+ default:
23
+ throw new Error("Unknown Camera.API action: " + action);
24
+ }
25
+ }
26
+
27
+ setFOV(params) {
28
+ if (params && params.length > 0) {
29
+ var val = parseFloat(params[0]);
30
+ if (!isNaN(val) && val >= 30 && val <= 120) {
31
+ this.fov = val;
32
+ }
33
+ }
34
+ return { fov: this.fov };
35
+ }
36
+
37
+ setNearFar(params) {
38
+ if (params && params.length >= 2) {
39
+ var n = parseFloat(params[0]);
40
+ var f = parseFloat(params[1]);
41
+ if (!isNaN(n)) this.near = n;
42
+ if (!isNaN(f)) this.far = f;
43
+ }
44
+ return { near: this.near, far: this.far };
45
+ }
46
+
47
+ setMode(params) {
48
+ if (params && params.length > 0) {
49
+ this.mode = params[0];
50
+ }
51
+ return { mode: this.mode };
52
+ }
53
+
54
+ setSensitivity(params) {
55
+ if (params && params.length > 0) {
56
+ var val = parseFloat(params[0]);
57
+ if (!isNaN(val) && val > 0) {
58
+ this.sensitivity = val;
59
+ }
60
+ }
61
+ return { sensitivity: this.sensitivity };
62
+ }
63
+
64
+ getSettings() {
65
+ return {
66
+ fov: this.fov,
67
+ near: this.near,
68
+ far: this.far,
69
+ mode: this.mode,
70
+ sensitivity: this.sensitivity
71
+ };
72
+ }
73
+ }
74
+
75
+ module.exports = CameraAPI;
@@ -0,0 +1,75 @@
1
+ class ChunkAPI {
2
+ constructor() {
3
+ this.chunkSize = 16;
4
+ this.renderDistance = 1;
5
+ this.loadedChunks = {};
6
+ this.terrain = "flat";
7
+ }
8
+
9
+ execute(action, params) {
10
+ switch (action) {
11
+ case "SetSize":
12
+ return this.setSize(params);
13
+ case "SetRenderDistance":
14
+ return this.setRenderDistance(params);
15
+ case "Generate":
16
+ return this.generate(params);
17
+ case "Unload":
18
+ return this.unload(params);
19
+ case "ListLoaded":
20
+ return this.listLoaded();
21
+ default:
22
+ throw new Error("Unknown Chunk.API action: " + action);
23
+ }
24
+ }
25
+
26
+ setSize(params) {
27
+ if (params && params.length > 0) {
28
+ var size = parseInt(params[0], 10);
29
+ if (!isNaN(size) && size >= 4 && size <= 64) {
30
+ this.chunkSize = size;
31
+ }
32
+ }
33
+ return { chunkSize: this.chunkSize };
34
+ }
35
+
36
+ setRenderDistance(params) {
37
+ if (params && params.length > 0) {
38
+ var dist = parseInt(params[0], 10);
39
+ if (!isNaN(dist) && dist >= 1 && dist <= 16) {
40
+ this.renderDistance = dist;
41
+ }
42
+ }
43
+ return { renderDistance: this.renderDistance };
44
+ }
45
+
46
+ generate(params) {
47
+ var cx = 0;
48
+ var cz = 0;
49
+ if (params && params.length >= 2) {
50
+ cx = parseInt(params[0], 10) || 0;
51
+ cz = parseInt(params[1], 10) || 0;
52
+ }
53
+ var key = cx + "," + cz;
54
+ this.loadedChunks[key] = { cx: cx, cz: cz, generated: true };
55
+ return { chunkKey: key, cx: cx, cz: cz };
56
+ }
57
+
58
+ unload(params) {
59
+ var cx = 0;
60
+ var cz = 0;
61
+ if (params && params.length >= 2) {
62
+ cx = parseInt(params[0], 10) || 0;
63
+ cz = parseInt(params[1], 10) || 0;
64
+ }
65
+ var key = cx + "," + cz;
66
+ delete this.loadedChunks[key];
67
+ return { unloaded: key };
68
+ }
69
+
70
+ listLoaded() {
71
+ return Object.keys(this.loadedChunks);
72
+ }
73
+ }
74
+
75
+ module.exports = ChunkAPI;
@@ -72,14 +72,26 @@ class ModelAPI {
72
72
  valid: false
73
73
  };
74
74
 
75
+ let embeddedTextures = [];
76
+
75
77
  if (resource.model) {
76
78
  const modelData = this.parseBBModel(resource.model);
77
79
  result.geometry = modelData.geometry;
78
80
  result.uvMappings = modelData.uvMappings;
81
+ embeddedTextures = modelData.embeddedTextures || [];
79
82
  }
80
83
 
81
84
  if (resource.texture) {
82
85
  result.texture = this.loadTexture(resource.texture);
86
+ } else if (embeddedTextures.length > 0) {
87
+ result.texture = {
88
+ path: null,
89
+ width: result.geometry ? result.geometry.resolution.width : 16,
90
+ height: result.geometry ? result.geometry.resolution.height : 16,
91
+ format: "png",
92
+ size: 0,
93
+ dataURL: embeddedTextures[0].source
94
+ };
83
95
  }
84
96
 
85
97
  if (result.geometry && result.texture && result.uvMappings) {
@@ -148,7 +160,21 @@ class ModelAPI {
148
160
  geometry.bones = this.parseOutliner(data.outliner);
149
161
  }
150
162
 
151
- return { geometry, uvMappings };
163
+ const embeddedTextures = [];
164
+ if (data.textures && Array.isArray(data.textures)) {
165
+ for (let i = 0; i < data.textures.length; i++) {
166
+ const tex = data.textures[i];
167
+ if (tex.source) {
168
+ embeddedTextures.push({
169
+ index: tex.id !== undefined ? tex.id : i,
170
+ name: tex.name || "texture_" + i,
171
+ source: tex.source
172
+ });
173
+ }
174
+ }
175
+ }
176
+
177
+ return { geometry, uvMappings, embeddedTextures };
152
178
  }
153
179
 
154
180
  parseOutliner(outliner) {
@@ -191,12 +217,16 @@ class ModelAPI {
191
217
  }
192
218
  }
193
219
 
220
+ const base64 = buffer.toString("base64");
221
+ const mime = ext === ".png" ? "image/png" : (ext === ".jpg" || ext === ".jpeg") ? "image/jpeg" : "image/" + ext.substring(1);
222
+
194
223
  return {
195
224
  path: filePath,
196
225
  width: width,
197
226
  height: height,
198
227
  format: ext.substring(1),
199
- size: buffer.length
228
+ size: buffer.length,
229
+ dataURL: "data:" + mime + ";base64," + base64
200
230
  };
201
231
  }
202
232
 
@@ -269,6 +299,10 @@ class ModelAPI {
269
299
  isModelLoaded(name) {
270
300
  return this.loadedModels[name] !== undefined;
271
301
  }
302
+
303
+ getLoadedModels() {
304
+ return this.loadedModels;
305
+ }
272
306
  }
273
307
 
274
308
  module.exports = ModelAPI;
@@ -0,0 +1,74 @@
1
+ class PlayerAPI {
2
+ constructor() {
3
+ this.spawnPoint = { x: 8, y: 2, z: 8 };
4
+ this.position = { x: 8, y: 2, z: 8 };
5
+ this.speed = 0.08;
6
+ this.jumpHeight = 0.18;
7
+ this.gravity = 0.015;
8
+ }
9
+
10
+ execute(action, params) {
11
+ switch (action) {
12
+ case "Spawn":
13
+ return this.spawn(params);
14
+ case "SetSpeed":
15
+ return this.setSpeed(params);
16
+ case "SetJumpHeight":
17
+ return this.setJumpHeight(params);
18
+ case "SetGravity":
19
+ return this.setGravity(params);
20
+ case "GetPosition":
21
+ return this.getPosition();
22
+ default:
23
+ throw new Error("Unknown Player.API action: " + action);
24
+ }
25
+ }
26
+
27
+ spawn(params) {
28
+ if (params && params.length >= 3) {
29
+ this.spawnPoint.x = parseFloat(params[0]) || 0;
30
+ this.spawnPoint.y = parseFloat(params[1]) || 0;
31
+ this.spawnPoint.z = parseFloat(params[2]) || 0;
32
+ this.position.x = this.spawnPoint.x;
33
+ this.position.y = this.spawnPoint.y;
34
+ this.position.z = this.spawnPoint.z;
35
+ }
36
+ return { spawnPoint: this.spawnPoint };
37
+ }
38
+
39
+ setSpeed(params) {
40
+ if (params && params.length > 0) {
41
+ var val = parseFloat(params[0]);
42
+ if (!isNaN(val) && val > 0) {
43
+ this.speed = val;
44
+ }
45
+ }
46
+ return { speed: this.speed };
47
+ }
48
+
49
+ setJumpHeight(params) {
50
+ if (params && params.length > 0) {
51
+ var val = parseFloat(params[0]);
52
+ if (!isNaN(val) && val > 0) {
53
+ this.jumpHeight = val;
54
+ }
55
+ }
56
+ return { jumpHeight: this.jumpHeight };
57
+ }
58
+
59
+ setGravity(params) {
60
+ if (params && params.length > 0) {
61
+ var val = parseFloat(params[0]);
62
+ if (!isNaN(val) && val > 0) {
63
+ this.gravity = val;
64
+ }
65
+ }
66
+ return { gravity: this.gravity };
67
+ }
68
+
69
+ getPosition() {
70
+ return { position: this.position };
71
+ }
72
+ }
73
+
74
+ module.exports = PlayerAPI;
@@ -0,0 +1,80 @@
1
+ class RenderAPI {
2
+ constructor() {
3
+ this.skyColor = { r: 0.53, g: 0.81, b: 0.92 };
4
+ this.fogColor = { r: 0.53, g: 0.81, b: 0.92 };
5
+ this.fogNear = 64;
6
+ this.fogFar = 128;
7
+ this.sunDirection = { x: 0.5, y: 1.0, z: 0.3 };
8
+ this.ambientLight = { r: 0.3, g: 0.3, b: 0.4 };
9
+ this.renderDistance = 128;
10
+ }
11
+
12
+ execute(action, params) {
13
+ switch (action) {
14
+ case "SetFog":
15
+ return this.setFog(params);
16
+ case "SetSkyColor":
17
+ return this.setSkyColor(params);
18
+ case "SetSunDirection":
19
+ return this.setSunDirection(params);
20
+ case "SetAmbientLight":
21
+ return this.setAmbientLight(params);
22
+ case "SetRenderDistance":
23
+ return this.setRenderDist(params);
24
+ default:
25
+ throw new Error("Unknown Render.API action: " + action);
26
+ }
27
+ }
28
+
29
+ setFog(params) {
30
+ if (params && params.length >= 3) {
31
+ this.fogColor.r = parseFloat(params[0]) || 0;
32
+ this.fogColor.g = parseFloat(params[1]) || 0;
33
+ this.fogColor.b = parseFloat(params[2]) || 0;
34
+ if (params.length >= 5) {
35
+ this.fogNear = parseFloat(params[3]) || 64;
36
+ this.fogFar = parseFloat(params[4]) || 128;
37
+ }
38
+ }
39
+ return { fogColor: this.fogColor, fogNear: this.fogNear, fogFar: this.fogFar };
40
+ }
41
+
42
+ setSkyColor(params) {
43
+ if (params && params.length >= 3) {
44
+ this.skyColor.r = parseFloat(params[0]) || 0;
45
+ this.skyColor.g = parseFloat(params[1]) || 0;
46
+ this.skyColor.b = parseFloat(params[2]) || 0;
47
+ }
48
+ return { skyColor: this.skyColor };
49
+ }
50
+
51
+ setSunDirection(params) {
52
+ if (params && params.length >= 3) {
53
+ this.sunDirection.x = parseFloat(params[0]) || 0;
54
+ this.sunDirection.y = parseFloat(params[1]) || 0;
55
+ this.sunDirection.z = parseFloat(params[2]) || 0;
56
+ }
57
+ return { sunDirection: this.sunDirection };
58
+ }
59
+
60
+ setAmbientLight(params) {
61
+ if (params && params.length >= 3) {
62
+ this.ambientLight.r = parseFloat(params[0]) || 0;
63
+ this.ambientLight.g = parseFloat(params[1]) || 0;
64
+ this.ambientLight.b = parseFloat(params[2]) || 0;
65
+ }
66
+ return { ambientLight: this.ambientLight };
67
+ }
68
+
69
+ setRenderDist(params) {
70
+ if (params && params.length > 0) {
71
+ var val = parseFloat(params[0]);
72
+ if (!isNaN(val) && val > 0) {
73
+ this.renderDistance = val;
74
+ }
75
+ }
76
+ return { renderDistance: this.renderDistance };
77
+ }
78
+ }
79
+
80
+ module.exports = RenderAPI;
@@ -0,0 +1,69 @@
1
+ class WindowAPI {
2
+ constructor() {
3
+ this.title = "CaG Engine";
4
+ this.width = 1280;
5
+ this.height = 720;
6
+ this.fullscreen = false;
7
+ this.background = "#1a1a2e";
8
+ }
9
+
10
+ execute(action, params) {
11
+ switch (action) {
12
+ case "SetTitle":
13
+ return this.setTitle(params);
14
+ case "SetSize":
15
+ return this.setSize(params);
16
+ case "SetFullscreen":
17
+ return this.setFullscreen(params);
18
+ case "SetBackground":
19
+ return this.setBackground(params);
20
+ case "GetSettings":
21
+ return this.getSettings();
22
+ default:
23
+ throw new Error("Unknown Window.API action: " + action);
24
+ }
25
+ }
26
+
27
+ setTitle(params) {
28
+ if (params && params.length > 0) {
29
+ this.title = params.join(" ");
30
+ }
31
+ return { title: this.title };
32
+ }
33
+
34
+ setSize(params) {
35
+ if (params && params.length >= 2) {
36
+ var w = parseInt(params[0], 10);
37
+ var h = parseInt(params[1], 10);
38
+ if (!isNaN(w) && w > 0) this.width = w;
39
+ if (!isNaN(h) && h > 0) this.height = h;
40
+ }
41
+ return { width: this.width, height: this.height };
42
+ }
43
+
44
+ setFullscreen(params) {
45
+ if (params && params.length > 0) {
46
+ this.fullscreen = params[0] === "true" || params[0] === true;
47
+ }
48
+ return { fullscreen: this.fullscreen };
49
+ }
50
+
51
+ setBackground(params) {
52
+ if (params && params.length > 0) {
53
+ this.background = params[0];
54
+ }
55
+ return { background: this.background };
56
+ }
57
+
58
+ getSettings() {
59
+ return {
60
+ title: this.title,
61
+ width: this.width,
62
+ height: this.height,
63
+ fullscreen: this.fullscreen,
64
+ background: this.background
65
+ };
66
+ }
67
+ }
68
+
69
+ module.exports = WindowAPI;