Monday, April 25, 2011

Simple Entity Engine

What

EntityEngine is a nice pattern for externalizing the different components (logic) of an entity (object). And particularly effective in games, where features of an object is shared among many different types. This way the behavior of the object is put into separate components. The idea is that an entity is pretty much just an ID, and the components are referenced via. the entity ID.
$engine = new EntityEngine();
$entity = new SimpleEntity($engine);
$entity->setComponent(new DialogComponent());
$entity->getComponent('Dialog')->talk();
Alternative:
$engine->getComponent($entity, 'Dialog')->talk();

Why

Entity engines works very well as an alternative (and improvement) to deep inheritance trees. Inheritance can fall short when, for example you want a Rock to be able to speak (wouldn't want a Rock to inherit from Human, right?).
Another advantage is that the engine, keep the components in an array based on their type. This means that we can fetch all components of a given type, without knowing (or caring) which entities own them.
$engine = new EntityEngine();
$entity = new SimpleEntity($engine);
$entity->setComponent(new CollisionComponent());
foreach($engine->getAll('Collision') as $comp) {
    $comp->update();
}
Note: The only reason these examples are in PHP is for prototyping.

EntityEngine Interfaces and Implementation

class EntityEngine
{
    private $components = array();
    
    /**
     * @param Entity $entity
     * @param string $componentName
     * @return Component
     */
    public function getComponent(Entity $entity, $componentName)
    {
        if($this->hasComponent($entity, $componentName)) {
            return $this->components[$componentName][$entity->getId()];
        }
        
        return null;
    }
    
    /**
     * @param Entity $entity
     * @param string $componentName
     * @return boolean
     */
    public function hasComponent(Entity $entity, $componentName)
    {
        return isset($this->components[$componentName][$entity->getId()]);
    }
    
    /**
     * @param Entity $entity
     * @param Component $component 
     */
    public function setComponent(Entity $entity, Component $component)
    {
        $this->components[$component->getName()][$entity->getId()] = $component;
    }
    
    /**
     * @param string $componentName
     * @return array of Component
     */
    public function getAll($componentName)
    {
        if(! isset($this->components[$componentName])) {
            return array();
        }
        
        return $this->components[$componentName];
    }
}

interface Entity
{
    /**
     * @return unique identifier for this instance
     */
    public function getId();
    
    /**
     * @param string $component
     * @return Component
     */
    public function getComponent($componentName);
    
    /**
     * @param Component $component
     */
    public function setComponent(Component $component);
}

abstract class SimpleEntity implements Entity
{
    private $engine;
    
    /**
     * @param EntityEngine $engine 
     */
    public function __construct(EntityEngine $engine)
    {
        $this->engine = $engine;
    }
    
    public function getId()
    {
        return spl_object_hash($this);
    }
    
    public function getComponent($componentName)
    {
        return $this->engine->getComponent($this, $componentName);
    }
    
    public function setComponent(Component $component)
    {
        $this->engine->setComponent($this, $component);
    }
}

interface Component
{
    public function getName();
}

Component Implementations of the Examples

class DialogComponent implements Component
{
    public function getName() 
    {
        return "Dialog";
    }
    
    public function moveRight();
}

class CollisionComponent implements Component
{
    public function getName() 
    {
        return "Collision";
    }

    public function update();
}

Tuesday, April 19, 2011

Ogre3d in Netbeans 7

Recently me and a friend of mine started talking about creating a game using C++. After a little looking around, we decided on the OGRE game engine as the framework of choice and me being a fan of Netbeans, we choose that as IDE. Here comes a little guide to setting up an OGRE 1.7.2 project in Netbeans 7 RC using MinGW on Windows 7.

Netbeans
Install Netbeans IDE 7 RC remember to select the all inclusive or c++ versions.

MinGW
Install MinGW using the mingw-get-inst installer. For this guide we install it at the default location (C:\MinGW).

During installation you should select the C++ component, as well as the two MSYS components.

After installation you need to add C:\MinGW\bin to your system Path.

Now you should be able to load up Netbeans:
  1. Select Tools -> Options -> C/C++ Tab.
  2. Press "Add..."
  3. Now select your base directory C:\MinGW\bin (same as Path), the rest should be auto-detected.
Create a new project, choose Samples -> C/C++ -> Welcome and try to build (you should succeed).

OGRE
Install OGRE 1.7.2 SDK for MinGW the self extractor will place it self where ever you download it, but copy the extracted folder to your root (C:\OgreSDK).

Here i basically followed this Ogre guide.

Only thing i changed was:
  • Project Properties -> Build -> Linker -> Output path to "C:/OgreSDK/bin/Debug/ogregame1". Switch out ogregame1 with your name of choice.
  • Project Properties -> Run -> Console Type: External Terminal.

NOTE: OgreMain_d.dll and OIS_d.dll is in the bin/Debug directory.
NOTE2: Executar is Run in English.

Now everything builds and runs. You can test it your self, with the main.cpp file from MadMarx tutorial One.