malc-game-engine 1.0.4 → 1.0.6

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.
Files changed (3) hide show
  1. package/malc.js +118 -54
  2. package/malc.min.js +12 -5
  3. package/package.json +1 -1
package/malc.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * MALC Game Engine Library
3
- * Version: 1.0.3
3
+ * Version: 1.0.6
4
4
  * Description: A comprehensive 2D game engine for p5.js
5
5
  */
6
6
 
@@ -909,7 +909,20 @@ class gameObject {
909
909
  directionTo(x, y, err = 0.5) {
910
910
  let pa = [x - this.x, y - this.y];
911
911
 
912
- let angle = (x && y) ? _p5.prototype.atan(pa[1]/pa[0]) : this.rotation;
912
+ let angle = this.rotation;
913
+
914
+ // Safely calculate angle
915
+ try {
916
+ if (x && y) {
917
+ angle = _p5.prototype.atan(pa[1]/pa[0]);
918
+ }
919
+ } catch (e) {
920
+ // Fallback to native Math.atan
921
+ if (x && y) {
922
+ angle = Math.atan(pa[1]/pa[0]);
923
+ }
924
+ }
925
+
913
926
  var quads = [
914
927
  pa[0] < -err && pa[1] > err,
915
928
  pa[0] < -err && pa[1] < -err,
@@ -919,7 +932,12 @@ class gameObject {
919
932
  (pa[1] < err && pa[1] > -err),
920
933
  ];
921
934
 
922
- let da = (_p5.prototype.atan(pa[1]/pa[0]) * 180)/Math.PI;
935
+ let da = 0;
936
+ try {
937
+ da = (_p5.prototype.atan(pa[1]/pa[0]) * 180)/Math.PI;
938
+ } catch (e) {
939
+ da = (Math.atan(pa[1]/pa[0]) * 180)/Math.PI;
940
+ }
923
941
 
924
942
  if((pa[1] > -err && pa[1] < err) && (pa[0] > -err && pa[0] < err)){
925
943
  angle = NaN;
@@ -2557,7 +2575,7 @@ const helpDocs = {
2557
2575
 
2558
2576
  // ========== MALC MAIN OBJECT ==========
2559
2577
  const MALC = {
2560
- version: "1.0.4", // Increment version
2578
+ version: "1.0.6", // Increment version
2561
2579
 
2562
2580
  // Core classes
2563
2581
  gameObject: gameObject,
@@ -2673,8 +2691,11 @@ const MALC = {
2673
2691
  return topics;
2674
2692
  },
2675
2693
 
2694
+ // Initialize the engine
2695
+
2676
2696
  // Initialize the engine
2677
2697
  init: function(canvasX, canvasY) {
2698
+ // Use the p5 instance that was passed to the factory function
2678
2699
  _p5.prototype.createCanvas(canvasX, canvasY);
2679
2700
 
2680
2701
  this.time = new Date();
@@ -2687,71 +2708,111 @@ const MALC = {
2687
2708
  this.mouse = new MouseHandler();
2688
2709
  window.mouse = this.mouse;
2689
2710
 
2690
-
2691
-
2692
2711
  // Start FPS tracking
2693
2712
  refreshLoop();
2694
2713
 
2695
2714
  // Create default scenes
2696
- // Create default scenes
2697
- new Scene("blank", 70);
2698
- new Scene("loading", 50, function(self) {
2699
- try {
2700
- _p5.prototype.textSize(24);
2701
- let timed = (self.timeActive / 250 % 4);
2702
- let dots = "";
2703
-
2704
- if (timed < 1) dots = ".";
2705
- else if (timed < 2) dots = "..";
2706
- else if (timed < 3) dots = "...";
2707
-
2708
- // Use standalone coloredText function
2709
- coloredText(`\\lime|Loading Game${dots}| `, 120, 200, _p5.prototype.LEFT, _p5.prototype.CENTER);
2710
-
2711
- _p5.prototype.textSize(16);
2712
-
2713
- let num = (Math.floor(self.timeActive / 100) / 10);
2714
- let percentText = `${ Math.round((10 - num) * 10) / 10 + ((num + "").length < 2 ? ".0" : "")}`;
2715
-
2716
- // Use standalone coloredText function
2717
- coloredText(`\\red|${percentText}|`, 200, 225, _p5.prototype.CENTER, _p5.prototype.CENTER);
2718
- } catch (e) {
2719
- // Fallback if coloredText fails
2720
- _p5.prototype.text(`Loading Game...`, 120, 200);
2721
- }
2722
- });
2715
+ new Scene("blank", 70);
2716
+ new Scene("loading", 50, function(self) {
2717
+ try {
2718
+ // Use _p5.prototype here too
2719
+ _p5.prototype.textSize(24);
2720
+ let timed = (self.timeActive / 250 % 4);
2721
+ let dots = "";
2722
+
2723
+ if (timed < 1) dots = ".";
2724
+ else if (timed < 2) dots = "..";
2725
+ else if (timed < 3) dots = "...";
2726
+
2727
+ coloredText(`\\lime|Loading Game${dots}| `, 120, 200, _p5.prototype.LEFT, _p5.prototype.CENTER);
2728
+
2729
+ _p5.prototype.textSize(16);
2730
+
2731
+ let num = (Math.floor(self.timeActive / 100) / 10);
2732
+ let percentText = `${ Math.round((10 - num) * 10) / 10 + ((num + "").length < 2 ? ".0" : "")}`;
2733
+
2734
+ coloredText(`\\red|${percentText}|`, 200, 225, _p5.prototype.CENTER, _p5.prototype.CENTER);
2735
+ } catch (e) {
2736
+ _p5.prototype.text(`Loading Game...`, 120, 200);
2737
+ }
2738
+ });
2723
2739
 
2724
2740
  Scene.activeScene = "loading";
2725
2741
 
2726
2742
  console.log("MALC Game Engine initialized v" + this.version);
2727
2743
  console.log("Type MALC.help() for documentation");
2728
2744
  },
2729
-
2730
- // Update all systems (call in draw)
2731
- update: function() {
2732
- this.time = new Date();
2733
- this.timer = this.time - this.startTime;
2734
-
2735
- if (this.mouse) {
2736
- this.mouse.rawX = _p5.prototype.mouseX;
2737
- this.mouse.rawY = _p5.prototype.mouseY;
2738
- this.mouse.x = this.mouse.rawX + window.camera.getOrientation()[0];
2739
- this.mouse.y = this.mouse.rawY + window.camera.getOrientation()[1];
2740
- this.mouse.down = _p5.prototype.mouseIsPressed;
2741
- }
2742
2745
 
2743
- controller.update();
2746
+ // Update all systems (call in draw)
2747
+ update() {
2748
+ if (!this.active) return;
2749
+
2750
+ // Apply gravity if enabled
2751
+ this.applyGravity();
2752
+
2753
+ let vel = this.velocity[0];
2754
+ let angle = this.velocity[1];
2755
+
2756
+ if (this.velocityMode == "polar") {
2757
+ let linked = false;
2758
+ if (!/unlinked/i.test(this.rvm) && /linked/i.test(this.rvm)) {
2759
+ this.velocity[1] = this.rotation;
2760
+ linked = true;
2761
+ }
2744
2762
 
2745
- gameObject.update();
2746
- Button.updateButton();
2763
+ let rot = linked ?
2764
+ (this.rotationMode == "degrees" ? (this.rotation) : this._toRadians(this.rotation)) :
2765
+ (this.rotationMode == "degrees" ? (this.velocity[1]) : (this.velocity[1]));
2766
+
2767
+ if(isNaN(rot)){
2768
+ vel = 0;
2769
+ rot = 0;
2770
+ }
2771
+
2772
+ // SAFELY call p5 math functions
2773
+ let vx = 0;
2774
+ let vy = 0;
2775
+
2776
+ try {
2777
+ vx = vel * _p5.prototype.cos(rot);
2778
+ vy = vel * _p5.prototype.sin(rot);
2779
+ } catch (e) {
2780
+ // Fallback if p5 isn't ready
2781
+ vx = vel * Math.cos(rot);
2782
+ vy = vel * Math.sin(rot);
2783
+ }
2747
2784
 
2748
- this.fps = fps;
2785
+ this.velocityMatrix = [vx, vy];
2786
+
2787
+ // Don't apply horizontal movement if gravity is enabled and we're grounded with friction
2788
+ if (!(this.gravity.enabled && this.gravity.grounded && this.gravity.friction > 0)) {
2789
+ this.x += vx;
2790
+ }
2791
+
2792
+ // Vertical movement is handled by gravity when enabled
2793
+ if (!this.gravity.enabled) {
2794
+ this.y += vy;
2795
+ }
2796
+ } else {
2797
+ // Cartesian velocity mode
2798
+ if (!(this.gravity.enabled && this.gravity.grounded && this.gravity.friction > 0)) {
2799
+ this.x += vel;
2800
+ }
2801
+ if (!this.gravity.enabled) {
2802
+ this.y += angle;
2803
+ }
2804
+ }
2805
+
2806
+ // Update parent scene reference
2807
+ this.updateParentScene();
2808
+
2809
+ MALCgameObjects[this.objectInstance] = this;
2810
+ }
2749
2811
 
2750
- if (typeof window.camera.render == "function") {
2751
- window.camera.render();
2812
+ // Helper method for radians conversion without p5
2813
+ _toRadians(degrees) {
2814
+ return degrees * Math.PI / 180;
2752
2815
  }
2753
- Scene.update();
2754
- }
2755
2816
  };
2756
2817
 
2757
2818
  // Initialize mouse and keyboard handlers
@@ -2760,3 +2821,6 @@ MALC.mouse = new MouseHandler();
2760
2821
  return MALC;
2761
2822
 
2762
2823
  }));
2824
+
2825
+
2826
+
package/malc.min.js CHANGED
@@ -99,7 +99,9 @@ removeFromAllScenes(){this.scenes.forEach(sceneId=>{let scene=Scene.getSceneById
99
99
  updateParentScene(){if(Scene.activeScene){let activeScene=Scene.getSceneById(Scene.activeScene);if(activeScene&&this.belongsToScene(activeScene.id)){this.parentScene=activeScene}}}
100
100
  distanceTo(target){let dx=target.x!==undefined?target.x-this.x:target[0]-this.x;let dy=target.y!==undefined?target.y-this.y:target[1]-this.y;return Math.sqrt(dx*dx+dy*dy)}
101
101
  collidesWith(other){return(this.x<other.x+other.width&&this.x+this.width>other.x&&this.y<other.y+other.height&&this.y+this.height>other.y)}
102
- directionTo(x,y,err=0.5){let pa=[x-this.x,y-this.y];let angle=(x&&y)?_p5.prototype.atan(pa[1]/pa[0]):this.rotation;var quads=[pa[0]<-err&&pa[1]>err,pa[0]<-err&&pa[1]<-err,pa[0]>err&&pa[1]>err,pa[0]>err&&pa[1]<-err,(pa[0]<err&&pa[0]>-err),(pa[1]<err&&pa[1]>-err),];let da=(_p5.prototype.atan(pa[1]/pa[0])*180)/Math.PI;if((pa[1]>-err&&pa[1]<err)&&(pa[0]>-err&&pa[0]<err)){angle=NaN}else if(quads[0]){angle=da+180}else if(quads[1]){angle=da+180}else if(quads[2]){angle=da}else if(quads[3]){angle=da}else if(quads[4]){if(pa[1]<-err){angle=-90}else if(pa[1]>err){angle=90}}else if(quads[5]){if(pa[0]<-err){angle=180}else if(pa[0]>err){angle=0}}
102
+ directionTo(x,y,err=0.5){let pa=[x-this.x,y-this.y];let angle=this.rotation;try{if(x&&y){angle=_p5.prototype.atan(pa[1]/pa[0])}}catch(e){if(x&&y){angle=Math.atan(pa[1]/pa[0])}}
103
+ var quads=[pa[0]<-err&&pa[1]>err,pa[0]<-err&&pa[1]<-err,pa[0]>err&&pa[1]>err,pa[0]>err&&pa[1]<-err,(pa[0]<err&&pa[0]>-err),(pa[1]<err&&pa[1]>-err),];let da=0;try{da=(_p5.prototype.atan(pa[1]/pa[0])*180)/Math.PI}catch(e){da=(Math.atan(pa[1]/pa[0])*180)/Math.PI}
104
+ if((pa[1]>-err&&pa[1]<err)&&(pa[0]>-err&&pa[0]<err)){angle=NaN}else if(quads[0]){angle=da+180}else if(quads[1]){angle=da+180}else if(quads[2]){angle=da}else if(quads[3]){angle=da}else if(quads[4]){if(pa[1]<-err){angle=-90}else if(pa[1]>err){angle=90}}else if(quads[5]){if(pa[0]<-err){angle=180}else if(pa[0]>err){angle=0}}
103
105
  return angle}
104
106
  pointTo(target){this.rotation=this.directionTo(target);return this}
105
107
  setPosition(x,y){this.x=x;this.y=y;return this}
@@ -282,7 +284,7 @@ const helpDocs={overview:`
282
284
  function draw() {
283
285
  MALC.update(); // Updates all MALC systems
284
286
  }
285
- `};const MALC={version:"1.0.4",gameObject:gameObject,Button:Button,Scene:Scene,UIPlane:UIPlane,Camera:Camera,mouse:null,keyboard:keyboard,controller:controller,fps:fps,getFPS:getFPS,time:new Date(),startTime:new Date().getTime(),timer:0,generateId:generateId,getTimestamp:getTimestamp,coloredText:coloredText,GRAVITY:GRAVITY,TERMINAL_VELOCITY:TERMINAL_VELOCITY,help:function(topic="overview"){if(topic==="overview"){console.log(helpDocs.overview);return helpDocs.overview}
287
+ `};const MALC={version:"1.0.6",gameObject:gameObject,Button:Button,Scene:Scene,UIPlane:UIPlane,Camera:Camera,mouse:null,keyboard:keyboard,controller:controller,fps:fps,getFPS:getFPS,time:new Date(),startTime:new Date().getTime(),timer:0,generateId:generateId,getTimestamp:getTimestamp,coloredText:coloredText,GRAVITY:GRAVITY,TERMINAL_VELOCITY:TERMINAL_VELOCITY,help:function(topic="overview"){if(topic==="overview"){console.log(helpDocs.overview);return helpDocs.overview}
286
288
  if(helpDocs.classes[topic]){console.log(`=== ${topic.toUpperCase()} ===`);console.log(helpDocs.classes[topic].description);console.log("\nConstructor:",helpDocs.classes[topic].constructor);if(helpDocs.classes[topic].properties){console.log("\nProperties:");Object.entries(helpDocs.classes[topic].properties).forEach(([prop,desc])=>{console.log(` ${prop}: ${desc}`)})}
287
289
  if(helpDocs.classes[topic].methods){console.log("\nMethods:");Object.entries(helpDocs.classes[topic].methods).forEach(([method,desc])=>{console.log(` ${method}: ${desc}`)})}
288
290
  if(helpDocs.classes[topic].staticMethods){console.log("\nStatic Methods:");Object.entries(helpDocs.classes[topic].staticMethods).forEach(([method,desc])=>{console.log(` static ${method}: ${desc}`)})}
@@ -291,6 +293,11 @@ if(helpDocs.input[topic]){console.log(`=== ${topic.toUpperCase()} ===`);console.
291
293
  return helpDocs.input[topic]}
292
294
  if(helpDocs.utilities[topic]){console.log(`=== ${topic.toUpperCase()} ===`);console.log(helpDocs.utilities[topic]);return helpDocs.utilities[topic]}
293
295
  if(topic==="quickStart"||topic==="start"){console.log(helpDocs.quickStart);return helpDocs.quickStart}
294
- console.log(`Help topic "${topic}" not found. Try: overview, classes (gameObject, Button, Scene, UIPlane, Camera), input (mouse, keyboard, controller), utilities (coloredText, getFPS), quickStart`);return null},helpTopics:function(){const topics=["overview","classes: "+Object.keys(helpDocs.classes).join(", "),"input: "+Object.keys(helpDocs.input).join(", "),"utilities: "+Object.keys(helpDocs.utilities).join(", "),"quickStart"];console.log("Available help topics:\n"+topics.join("\n"));return topics},init:function(canvasX,canvasY){_p5.prototype.createCanvas(canvasX,canvasY);this.time=new Date();this.startTime=this.time.getTime();window.camera=new Camera(canvasX,canvasY);this.mouse=new MouseHandler();window.mouse=this.mouse;refreshLoop();new Scene("blank",70);new Scene("loading",50,function(self){try{_p5.prototype.textSize(24);let timed=(self.timeActive/250%4);let dots="";if(timed<1)dots=".";else if(timed<2)dots="..";else if(timed<3)dots="...";coloredText(`\\lime|Loading Game${dots}| `,120,200,_p5.prototype.LEFT,_p5.prototype.CENTER);_p5.prototype.textSize(16);let num=(Math.floor(self.timeActive/100)/10);let percentText=`${ Math.round((10 - num) * 10) / 10 + ((num + "").length < 2 ? ".0" : "")}`;coloredText(`\\red|${percentText}|`,200,225,_p5.prototype.CENTER,_p5.prototype.CENTER)}catch(e){_p5.prototype.text(`Loading Game...`,120,200)}});Scene.activeScene="loading";console.log("MALC Game Engine initialized v"+this.version);console.log("Type MALC.help() for documentation")},update:function(){this.time=new Date();this.timer=this.time-this.startTime;if(this.mouse){this.mouse.rawX=_p5.prototype.mouseX;this.mouse.rawY=_p5.prototype.mouseY;this.mouse.x=this.mouse.rawX+window.camera.getOrientation()[0];this.mouse.y=this.mouse.rawY+window.camera.getOrientation()[1];this.mouse.down=_p5.prototype.mouseIsPressed}
295
- controller.update();gameObject.update();Button.updateButton();this.fps=fps;if(typeof window.camera.render=="function"){window.camera.render()}
296
- Scene.update()}};MALC.mouse=new MouseHandler();return MALC}))
296
+ console.log(`Help topic "${topic}" not found. Try: overview, classes (gameObject, Button, Scene, UIPlane, Camera), input (mouse, keyboard, controller), utilities (coloredText, getFPS), quickStart`);return null},helpTopics:function(){const topics=["overview","classes: "+Object.keys(helpDocs.classes).join(", "),"input: "+Object.keys(helpDocs.input).join(", "),"utilities: "+Object.keys(helpDocs.utilities).join(", "),"quickStart"];console.log("Available help topics:\n"+topics.join("\n"));return topics},init:function(canvasX,canvasY){_p5.prototype.createCanvas(canvasX,canvasY);this.time=new Date();this.startTime=this.time.getTime();window.camera=new Camera(canvasX,canvasY);this.mouse=new MouseHandler();window.mouse=this.mouse;refreshLoop();new Scene("blank",70);new Scene("loading",50,function(self){try{_p5.prototype.textSize(24);let timed=(self.timeActive/250%4);let dots="";if(timed<1)dots=".";else if(timed<2)dots="..";else if(timed<3)dots="...";coloredText(`\\lime|Loading Game${dots}| `,120,200,_p5.prototype.LEFT,_p5.prototype.CENTER);_p5.prototype.textSize(16);let num=(Math.floor(self.timeActive/100)/10);let percentText=`${ Math.round((10 - num) * 10) / 10 + ((num + "").length < 2 ? ".0" : "")}`;coloredText(`\\red|${percentText}|`,200,225,_p5.prototype.CENTER,_p5.prototype.CENTER)}catch(e){_p5.prototype.text(`Loading Game...`,120,200)}});Scene.activeScene="loading";console.log("MALC Game Engine initialized v"+this.version);console.log("Type MALC.help() for documentation")},update(){if(!this.active)return;this.applyGravity();let vel=this.velocity[0];let angle=this.velocity[1];if(this.velocityMode=="polar"){let linked=!1;if(!/unlinked/i.test(this.rvm)&&/linked/i.test(this.rvm)){this.velocity[1]=this.rotation;linked=!0}
297
+ let rot=linked?(this.rotationMode=="degrees"?(this.rotation):this._toRadians(this.rotation)):(this.rotationMode=="degrees"?(this.velocity[1]):(this.velocity[1]));if(isNaN(rot)){vel=0;rot=0}
298
+ let vx=0;let vy=0;try{vx=vel*_p5.prototype.cos(rot);vy=vel*_p5.prototype.sin(rot)}catch(e){vx=vel*Math.cos(rot);vy=vel*Math.sin(rot)}
299
+ this.velocityMatrix=[vx,vy];if(!(this.gravity.enabled&&this.gravity.grounded&&this.gravity.friction>0)){this.x+=vx}
300
+ if(!this.gravity.enabled){this.y+=vy}}else{if(!(this.gravity.enabled&&this.gravity.grounded&&this.gravity.friction>0)){this.x+=vel}
301
+ if(!this.gravity.enabled){this.y+=angle}}
302
+ this.updateParentScene();MALCgameObjects[this.objectInstance]=this}
303
+ _toRadians(degrees){return degrees*Math.PI/180}};MALC.mouse=new MouseHandler();return MALC}))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "malc-game-engine",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "A comprehensive 2D game engine for p5.js",
5
5
  "main": "malc.js",
6
6
  "unpkg": "malc.min.js",