Best way to implement a path system

Started by
1 comment, last by jakson15 8 years, 11 months ago

So I'm making a web game using HTML5 / Javascript.

I have an Enemy class and would like to know the best way to implement a path system.

I currently have something, but it doesn't seem too efficient for me (Compared to programming I've done in C++).


function Enemy(){
   
   this.currentPath = [];
   
   this.currentPath.push({x: 5, y: 9});
   this.currentPath.push({x: 5, y: 1});
   // e.t.c ...
   this.currentPath.push({x: ..., y: ...}); 

   this.move = function(){
      
      if(atNextNode){
      //Not actual code, but relevant
         this.currentPath.shift();
      }
   };
}

For every enemy I create, I push a whole load of new data into each class.

Compared to C++ where I could just have a pointer to a Path class and then increment where in the path it is, saving memory.

I know computing power is very fast but I want to know if there are better ways to implement this sort of thing in JavaScript or I should just not worry about minuscule optimisations like this.

Advertisement
The similar approach is to pass an object by reference.

Similar to C#, Primitives are always passed by Value. Objects are always passed by reference.

I have searched passing by reference but I don't quite understand how that should be implemented.

Will the Enemy class still have a variable that is the path array?

I think a little example would really benefit me right now, in context of my path trouble and not a generic passing by reference.

This topic is closed to new replies.

Advertisement