Creating the Server (2)

Back to the Server

Now we've created our entities, we can get back to creating the server.  The next thing we need to do is fill in the CreateGame() method in our MTDServer class.  This is the method that will be called whenever a new game starts, and initially all it will do is create a floor for the avatars to walk on:-

    @Override
    protected void createGame() {
        Floor floor = new Floor(this, getNextEntityID(), 0, 0, 0, 20f, .5f, 20f, "Textures/mud.png");        this.actuallyAddEntity(floor);
    }

   
This will create a floor of size 20 x 20.  In the future we'll create a wall around the edge to stop things falling off, but for now we'll just throw Health & Safety out the window.

We also need to tell the server what entity to create for a player when they connect, so add/replace the following method:

    @Override
    protected AbstractServerAvatar createPlayersAvatarEntity(ClientData client, int entityid) {
        return new WizardServerAvatar(this, client, client.remoteInput, entityid);
    }


Next, we need to tell the server where to start a new Avatar when it appears (either when a new game starts, or if it is resurrected during the game).

    @Override
    public void moveAvatarToStartPosition(AbstractAvatar avatar) {
        avatar.setWorldTranslation(3+(avatar.getSide()*3), 3f, 3f);
    }

This ia a bit of a hack, since it just takes the players side number, and uses it to generate some unique co-ords so two players don't start in the same place.  We can come up with something better later.

Finally, 2 more methods which we need:-

    @Override
    protected byte getWinningSideAtEnd() {
        return -1; // Todo later
    }

    @Override
    public byte getSideForPlayer(ClientData client) {
        return (byte) client.getPlayerID(); // Take the player's ID as their side
    }

This will get us up-and-running quickly; we can improve them later.

And that concludes version 0.000001 (alpha) of the server!  It should run without errors, although currently we don't have any clients that can connect to it.  We will soon though, as we'll create one in the next section.

Comments