spawnFromWorld question

42 views
Skip to first unread message

Matty Moriates

unread,
Nov 21, 2013, 9:06:41 PM11/21/13
to replica-island-...@googlegroups.com
Hi there,

First, I want to say that this project is incredible. I have learned so much from studying this code and reading the posts on this forum. I have been diligently trying to understand the code and I am still (slowly) working my way through it. One of the biggest challenges I have come across is understanding level creation and how game objects are spawned. Generating collisions and the binary files from photoshop is something I do not fully understand yet. But, at this point I am not trying to actually design my own levels, just trying to understand how this code works.

To me it seems like the LevelSystem starts reading the .bin file and calls spawnFromWorld in the GameObjectFactory when it gets to an object. This method then calls the spawn method and passes the type of object from the tile that is being analyzed. What I am wondering is how does it know what the index of the tile is? Where is this defined? Is it in the .bin file itself where a 16 represents a Brobot for example?

Here is the code that I am talking about:

public void spawnFromWorld(TiledWorld world, int tileWidth, int tileHeight) {

        // Walk the world and spawn objects based on tile indexes.

        final float worldHeight = world.getHeight() * tileHeight;

        GameObjectManager manager = sSystemRegistry.gameObjectManager;

        if (manager != null) {

            for (int y = 0; y < world.getHeight(); y++) {

                for (int x = 0; x < world.getWidth(); x++) {

                    int index = world.getTile(x, y);

                    if (index != -1) {

                        GameObjectType type = GameObjectType.indexToType(index);

                        if (type != GameObjectType.INVALID) {

                            final float worldX = x * tileWidth;

                            final float worldY = worldHeight - ((y + 1) * tileHeight);

                            GameObject object = spawn(type, worldX, worldY, false);

                            if (object != null) {

                                if (object.height < tileHeight) {

                                    // make sure small objects are vertically centered in their

                                    // tile.

                                    object.getPosition().y += (tileHeight - object.height) / 2.0f;

                                }

                                if (object.width < tileWidth) {

                                    object.getPosition().x += (tileWidth - object.width) / 2.0f;

                                } else if (object.width > tileWidth) {

                                    object.getPosition().x -= (object.width - tileWidth) / 2.0f;

                                }

                                manager.add(object);

                                if (type == GameObjectType.PLAYER) {

                                    manager.setPlayer(object);

}}}}}

Thank you for any help I can get.


Chris Pruett

unread,
Nov 22, 2013, 12:52:42 AM11/22/13
to replica-island-...@googlegroups.com
Hi Matty,

Thanks for the kind words!

If I remember correctly, the map data stores indexes as "tiles" in several layers.  The code you pasted takes a TiledWorld, which is a single layer, loaded from the map file, that contains a bunch of indexes at specific tile offsets.  There is a method referenced in that code: GameObjectType.indexToType() that converts indexes to actual game object types so that they can be spawned.

So to answer your question: the actual value of what to spawn where comes from the map file.  But it is remapped from an integer index into a game type b the GameObjectType method.

Hope that helps!

Chris

--
You received this message because you are subscribed to the Google Groups "ReplicaIsland Coding Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to replica-island-coding...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Matty Moriates

unread,
Nov 22, 2013, 10:49:42 AM11/22/13
to replica-island-...@googlegroups.com
Chris,

Thank you for your quick response. I really appreciate it.

That does help. That's what I figured, but I still don't understand how the actual layers of tile are created so that's where I'm at a loss. I know there have been a couple of posts about it, so I'll look into those more. Thanks again for your help.
Reply all
Reply to author
Forward
0 new messages