Wednesday, April 29, 2020

Tutorial 3.4: Adding extra info to our maps

Before I dive too deep into making maps and drawing many different tiles, let’s see IF and HOW we can import this map into our game. And more importantly, if it contains all the features we need. I’m confident graphics wise (since I wasn’t planning spectacular features anyway), but we also need to make sure our physics and A.I. routines can deal with the map. I think so, but better be sure than sorry. As said, generic solutions often have restrictions. One way or another, there always seems to be a catch somewhere. Prince Charming with white socks. Princess Thai that happens to be a boy.

First we need to fill in some of the empty Java classes that were proposed in 3.2. And probably we can get rid of a few things as well. But let’s start with “”.

public class MapInfo {
    String  mapName;
    String  mapFileName;    // TMX file
    // Initial state
    int     time;           // Seconds to survive
    Vector2 orbStartPos;    // Initial plaement of orb
    float   orbStartSize;   // Radius meters
    // Music
    // ...
    // Enemy waves
    // ...
} // MapInfo

A few properties I can imagine right now, but sure there is more to come. Just like images and animations, Maps are yet another type of Resource. Owned by a “MapSystem”, filled by our Game Resource file. Same old story:

public class MapSystem {

    HashMap<String,MapInfo> maps; // Collection

    public MapSystem()
        this.maps = new HashMap();
    } // create

    public void addMap( String mapFilePath )
        // Use fileName as idName
        int start               = mapFilePath.lastIndexOf( '/' )+1;
        int end                 = mapFilePath.lastIndexOf( '.' );

        String idName           = mapFilePath.substring( start, end );

        // Make image and add to the list
        MapInfo map     = new MapInfo( mapFilePath );
        this.maps.put( idName, map );
    } // addImage

    public MapInfo get( String idName )
        return this.maps.get( idName );
    } // idName

} // MapLibrary

private void loadInitial_LevelPack()
    engine.getMapLibrary().addMap( "Maps/" );
} // loadInitial_LevelPack hu? Smells fishy. I just made up a file that doesn’t exist yet. We DO have a TMX file that we just generated with the Tiled editor. But we need more than just a map. As the mostly empty MapInfo class already reveals, we have extra options. Such as the background music, or start position of the Orb.

You can in fact add such custom properties to your map in Tiled. Those are limited to primitive types though, such as strings, Booleans or numbers. Which is actually sufficient for the properties we got there, so far. But there is one more advanced type of data: "Enemy Waves".

Not all enemies appear at once while playing a map. They come in waves, and/or partially random. For example, spawn 10 weak sissies in the first minute. Then 4 heavies in the second minute. This type of information doesn’t fit in a few simple map attributes, it needs a more delicate structure.

Buuutt… We may as well solve this by creating “spawners” in Tiled. Below I created a rectangle (object). Not for drawing, but to hold spawning information. The properties suggest that after 1,5 seconds playing, enemy “Dumbass01” appears. At 9 seconds, there is a 75% chance that another Dumbass appears. Note it could be any entity. “Health crate” or something. It’s still a somewhat crude way of defining your enemy waves, but at least it beats the crap out of Notepad.

So you know what? We’ll just refer to the TMX file instead, and see how far we get with that.

private void loadInitial_LevelPack()
    engine.getMapLibrary().addMap( "Maps/TestMap01.tmx" );
} // loadInitial_LevelPack