malc-game-engine 1.0.5 → 1.0.7

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 +98 -31
  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.5
3
+ * Version: 1.0.7
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.5", // Increment version
2578
+ version: "1.0.7", // Increment version
2561
2579
 
2562
2580
  // Core classes
2563
2581
  gameObject: gameObject,
@@ -2675,9 +2693,10 @@ const MALC = {
2675
2693
 
2676
2694
  // Initialize the engine
2677
2695
 
2696
+ // Initialize the engine
2678
2697
  init: function(canvasX, canvasY) {
2679
- // Use the current p5 instance (this) to create the canvas
2680
- this.createCanvas(canvasX, canvasY);
2698
+ // Use the p5 instance that was passed to the factory function
2699
+ _p5.prototype.createCanvas(canvasX, canvasY);
2681
2700
 
2682
2701
  this.time = new Date();
2683
2702
  this.startTime = this.time.getTime();
@@ -2696,7 +2715,8 @@ const MALC = {
2696
2715
  new Scene("blank", 70);
2697
2716
  new Scene("loading", 50, function(self) {
2698
2717
  try {
2699
- this.textSize(24);
2718
+ // Use _p5.prototype here too
2719
+ _p5.prototype.textSize(24);
2700
2720
  let timed = (self.timeActive / 250 % 4);
2701
2721
  let dots = "";
2702
2722
 
@@ -2704,16 +2724,16 @@ const MALC = {
2704
2724
  else if (timed < 2) dots = "..";
2705
2725
  else if (timed < 3) dots = "...";
2706
2726
 
2707
- coloredText(`\\lime|Loading Game${dots}| `, 120, 200, this.LEFT, this.CENTER);
2727
+ coloredText(`\\lime|Loading Game${dots}| `, 120, 200, _p5.prototype.LEFT, _p5.prototype.CENTER);
2708
2728
 
2709
- this.textSize(16);
2729
+ _p5.prototype.textSize(16);
2710
2730
 
2711
2731
  let num = (Math.floor(self.timeActive / 100) / 10);
2712
2732
  let percentText = `${ Math.round((10 - num) * 10) / 10 + ((num + "").length < 2 ? ".0" : "")}`;
2713
2733
 
2714
- coloredText(`\\red|${percentText}|`, 200, 225, this.CENTER, this.CENTER);
2734
+ coloredText(`\\red|${percentText}|`, 200, 225, _p5.prototype.CENTER, _p5.prototype.CENTER);
2715
2735
  } catch (e) {
2716
- this.text(`Loading Game...`, 120, 200);
2736
+ _p5.prototype.text(`Loading Game...`, 120, 200);
2717
2737
  }
2718
2738
  });
2719
2739
 
@@ -2724,30 +2744,75 @@ const MALC = {
2724
2744
  },
2725
2745
 
2726
2746
  // Update all systems (call in draw)
2727
- update: function() {
2728
- this.time = new Date();
2729
- this.timer = this.time - this.startTime;
2730
-
2731
- if (this.mouse) {
2732
- this.mouse.rawX = this.mouseX;
2733
- this.mouse.rawY = this.mouseY;
2734
- this.mouse.x = this.mouse.rawX + window.camera.getOrientation()[0];
2735
- this.mouse.y = this.mouse.rawY + window.camera.getOrientation()[1];
2736
- this.mouse.down = this.mouseIsPressed;
2737
- }
2738
-
2739
- controller.update();
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
+ }
2740
2762
 
2741
- gameObject.update();
2742
- 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
+ }
2743
2784
 
2744
- 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
+ }
2745
2811
 
2746
- if (typeof window.camera.render == "function") {
2747
- window.camera.render();
2812
+ // Helper method for radians conversion without p5
2813
+ _toRadians(degrees) {
2814
+ return degrees * Math.PI / 180;
2748
2815
  }
2749
- Scene.update();
2750
- }
2751
2816
  };
2752
2817
 
2753
2818
  // Initialize mouse and keyboard handlers
@@ -2757,3 +2822,5 @@ return MALC;
2757
2822
 
2758
2823
  }));
2759
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.5",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.7",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){this.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{this.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,this.LEFT,this.CENTER);this.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,this.CENTER,this.CENTER)}catch(e){this.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=this.mouseX;this.mouse.rawY=this.mouseY;this.mouse.x=this.mouse.rawX+window.camera.getOrientation()[0];this.mouse.y=this.mouse.rawY+window.camera.getOrientation()[1];this.mouse.down=this.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.5",
3
+ "version": "1.0.7",
4
4
  "description": "A comprehensive 2D game engine for p5.js",
5
5
  "main": "malc.js",
6
6
  "unpkg": "malc.min.js",