Command system in RTS game

Started by
1 comment, last by rmxhaha 9 years, 12 months ago

Say I have this class called Spaceship

Now off coarse I have to give command to this Spaceship like go here and go there

I have fully implemented steering behavior like seek, flee, avoid,

but I am having a hard time deciding whether I should put something like

the command system inside the class or outside the class

something like

var ship = new Spaceship( param );
 
io.on('connection', function( socket ){
    socket.on('command', function( cmd ){
       if( cmd.instruction == 'move' )
           ship.moveTo( cmd.x, cmd.y );
    }
});
 

There are some command that need interaction with other classes not just one class something like shooting missile

when shooting missile it have to know the location of the enemy which need a pointer to the enemy

and also it need to spawn missile class and push them to missiles array which is concealed deep inside another class

so should I just make this class something like Commander

to tell each unit what to do

There will be one Commander class per player

What do you think ?

is there a major flaw both of these ?

Advertisement

Your question is hard to answer without knowing how the rest of your game is structured. The short answer is, both ways might work.

The longer answer is encapsulation. Ideally, each object should only know about the stuff it actually needs. So an object shooting a missile doesn't necessarily need to know any of that; it just needs a function that takes a target as an argument that spawns another object (the missile). The spaceship doesn't actually have to know anything about missiles or enemies; it just needs to know how to spawn an object and give the spawned object a target.

If you need more information, it would help to know how your current classes are structured.

My classes are structured something like this

I will make this brief :

 
var HealthModule = {  
     number : 1,
     shield : 1,
     hit : function( dmg ){ ... }
}
 
var EventListener = function(){
     call : function( eventName ){ ... },
     on : function( eventname, callback ){ ... }
}
 
var Spaceship = function( opt ){
     this.extend( opt );
}
 
// now spaceship have those module property and function
Spaceship.prototype.extend( HealthModule );
 
// so ship = new Spaceship( ...);
// ship.on('death', function(){} );
// is possible
Spaceship.prototype.extend( EventListener('death','under-attack') );
var Warhead = function(){ ... }
 
// this is I think most people call entity manager 
var Galaxy =  function(){
    var Legion = function(){
        this.units = [];
        this.heads = [];
    }
    var legions = [];
 
    this.addLegion = function(){};
    
    this.update = function(){}
}
 
var galaxy = new Galaxy;
 
setInterval( galaxy.update, 50 );
 
// socket and data transfer between client server
 
var express = ...;
var io = ...;
 
io.on('connection', function( socket ){
    var legion = ...;
    socket.on('identification', function( data ) { legion = spawnLegion( data ) } );
 
   function sendData(){
     socket.emit( galaxy.getData( legion.id ) );
   }
   
   setInterval( sendData, 100 );
});

I skip a lot of internal physic, health, collision system and this is just pseudocode that I type to describe the structure I am using

so can you tell

This topic is closed to new replies.

Advertisement