Programmation Orientée Jeux

Jeu de Platforme - Des monstres

Quelques monstres

La dernière grande étape consiste à ajouter des monstres qui rendront le jeu plus difficile et amusant.

1) Ajout d'un nouveau type

Nous allons maintenant ajouter une nouvelle entitée. Pour cela nous devrons effectuer les changements suivants en ajoutant la classe Goomba avec son graphisme et ses données:

2) Système d'Handler

Et maintenant, ajoutez la classe HandlerEntity héritant de HandlerEntityPlatform:

/**
 * Handler implementation. All of our entity will be handled here.
 *
 * @author Pierre-Alexandre (contact@b3dgs.com)
 */
final class HandlerEntity
        extends HandlerEntityPlatform<Entity>
{
    /** Mario reference. */
    private final Entity mario;

    /**
     * Constructor.
     *
     * @param camera The camera reference.
     * @param mario The mario reference.
     */
    HandlerEntity(CameraPlatform camera, Entity mario)
    {
        super(camera);
        this.mario = mario;
    }

    /*
     * HandlerEntityPlatform
     */

    @Override
    protected boolean canUpdateEntity(Entity entity)
    {
        return true;
    }

    @Override
    protected void updatingEntity(Entity entity, double extrp)
    {
        if (!(mario.isDead() || entity.isDead()) && entity.collide(mario))
        {
            // Mario hit entity if coming from its top
            if (mario.isFalling() && mario.getLocationOldY() > entity.getLocationOldY())
            {
                entity.onHurtBy(mario);
                mario.onHitThat(entity);
            }
            else
            {
                mario.onHurtBy(entity);
                entity.onHitThat(mario);
            }
        }
    }

    @Override
    protected void renderingEntity(Entity entity, Graphic g, CameraPlatform camera)
    {
        // Nothing to do
    }
}

Et enfin, mettez à jour la classe World pour créer deux Goomba:

/**
 * World implementation.
 *
 * @author Pierre-Alexandre (contact@b3dgs.com)
 */
final class World
        extends WorldGame
{
    /** Background color. */
    private static final ColorRgba BACKGROUND_COLOR = new ColorRgba(107, 136, 255);
    /** Camera reference. */
    private final CameraPlatform camera;
    /** Map reference. */
    private final Map map;
    /** Factory reference. */
    private final FactoryEntity factory;
    /** Mario reference. */
    private final Mario mario;
    /** Handler reference. */
    private final HandlerEntity handler;

    /**
     * @see WorldGame#WorldGame(Sequence)
     */
    World(Sequence sequence)
    {
        super(sequence);
        camera = new CameraPlatform(width, height);
        map = new Map();
        factory = new FactoryEntity(map, source.getRate());
        mario = factory.create(EntityType.MARIO);
        handler = new HandlerEntity(camera, mario);
    }

    /*
     * WorldGame
     */

    @Override
    public void update(double extrp)
    {
        mario.updateControl(keyboard);
        mario.update(extrp);
        handler.update(extrp);
        camera.follow(mario);
    }

    @Override
    public void render(Graphic g)
    {
        g.setColor(World.BACKGROUND_COLOR);
        g.drawRect(0, 0, width, height, true);
        // Draw the map
        map.render(g, camera);
        // Draw the mario
        mario.render(g, camera);
        handler.render(g);
    }

    @Override
    protected void saving(FileWriting file) throws IOException
    {
        map.save(file);
    }

    @Override
    protected void loading(FileReading file) throws IOException
    {
        map.load(file);
        camera.setLimits(map);
        camera.setIntervals(16, 0);
        map.adjustCollisions();
        mario.respawn();

        // Create two goombas
        for (int i = 0; i < 2; i++)
        {
            final Goomba goomba = factory.create(EntityType.GOOMBA);
            goomba.setLocation(532 + i * 24, 32);
            handler.add(goomba);
        }
    }
}

Et voilà, Mario peut maintenant interagir avec des Goombas !

Un Goomba !Un Goomba !

Lire la suite: Jeu de Platforme - Son et Musique

Programmation Orientée Jeux ©2010-2014
Byron 3D Games Studio