0
\$\begingroup\$

I'm coding a space shooter on three.js (WebGL) and since the game I'm doing is rather simple, plus I want to keep controll of the structure, I'm not using a game engine.

I'm using oop classes and inheritance to optimize code reusability and maintenability.

The classes so far look like the following :

MovingObject, the base class possessing the attributes : position, speed, weapon, mesh, etc.

Asteroid, inheriting MovingObject

PlayerShip, inheriting MovingObject

Weapons, Base class of other weapons, possessiong the class shoot, that every other inheriting class redefines in order to shoot the projectile it wants at the position and frquency it wants

MovingSystem, Base class of other moving classes, possessing the function update(inputs,time) that move the MovingObject according to the class behaviour

What I'm searchign is a common architecture / data-model of how an AI controlling an enemy space ship should look like.

So far I've only found algorithms of A* search, a following / escaping path search, etc.

However I don't need the logic code, I want the structure of HOW and WHERE to execute the AI. (If possible in an oop model, so there can be inheritances with slight variations)

I'll post an example of code here, so you see how the code actually use the different classes and inhertance :

//MovingObject class
MovingObject.prototype.update = function(time, inputs)
{
    this.lastPos = this.mesh.position.clone();
    //console.log("this: ",this);
    //console.log("inputs: ", inputs);
    this.movingSys.move(this, time, inputs);

    this.particularUpdateFunctions(time);
};
//PlayerShip class
function PlayerShip(MAX_SPEED, speed, COEFF_ACCEL, mesh)
{
    this.particularUpdateFunctions = function(time){
        this.weapons[this.currentWeap].update(this,time, this.shooting);
    };
}
\$\endgroup\$
  • 2
    \$\begingroup\$ Friendly tip: You are talking about inheritance far too much. All the worse for the fact that JS is designed specifically to favour composition over inheritance, which is what you should be doing when writing games. Inheritance will rapidly lock you in, and cause proliferation of classes to accommodate minor variations in entity design. There is much Q&A on this site about said topic, search on "entity", "inheritance" and/or "composition". \$\endgroup\$ – Engineer May 21 '14 at 14:24
1
\$\begingroup\$

Disclaimer: "typically" applies to all of the following since no two game architectures are the same.

AI is executed near or at the start of the game loop, usually just after or before user input. Why? Because both player(s) and AI(s) are entities, and they each have their own way of providing input. For the player, your code extracts actions from human interface devices, and for the AIs your code does the thinking to decide what moves to make. Both should result in a command or command queue, which will then be enacted in the next phase of the current game loop iteration, or in future iterations.

"HOW to execute" your AI is not something we will be able to help you with, I'm afraid, since there is no single way of doing this... it is a very broad question and depends on what you're trying to achieve. Architecturally though, suffice to say that if your game is quite simple, you may want to use an FSM to produce your commands to be stored in and executed from the command queue.

Commands would generally be things like, "move forward", "fire", "jump" and so on. These could be enacted without knowing or caring whether the entity is player- or AI-controlled. These would typically have a duration, which could be a single frame / update, or more often, several updates before completion. On completion, we'd check the command queue for the next command, and enact it, etc.

\$\endgroup\$
  • \$\begingroup\$ Thanks, I've done some AI's for a game of mine last year... I've actually coded a command and a command queue system all slopily, Having an actual pattern for there will help me a lot! \$\endgroup\$ – Ludovic Migneault May 21 '14 at 18:45
  • \$\begingroup\$ @LudovicMigneault Ah, yes, I think we all have those discarded bits of code lying about that end up being very handy indeed later on. :) \$\endgroup\$ – Engineer May 21 '14 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.