Tiles
From SemanticLab
Contents |
GameObject vs. Tile
- Tiles are the building block of the game map. It is possible to change their appearance and to trigger actions if GameCharacters enter a Tile or the player clicks on a Tile. Tiles are aligned on the game map and identified by a unique position. The document Navigation helps you in obtaining a Tiles position.
- GameObjects are able to move on the game map and are highly extensible. The document on configuration files describes how to add your own GameObjects to the game.
Register Custom Tile Types
You may register your own Tile types using Tile.registerTileType(package, name, img).
The method m.getArea() returns a handle on the game area which may be used together with the method setTileType(package, name, x, y) to set a specific Tile type to a certain position.
In most cases it is sensible to initialize the new Tile types in your plug-in's init method.
// register new Tile type (wall) Tile.registerTileType(getClass().getPackage(), "wall", "/at/ac/wu/moelltal/plugins/w10/example/wall.png"); // set the tiles on position (10,10) and (10,11) to the new Tile type m.getArea().setTileType(getClass().getPackage(), "wall", 10, 10); m.getArea().setTileType(getClass().getPackage(), "wall", 10, 11);
The first argument of the function call adds the plug-in package name to the tile type which prevents ambiguities if different packages define Tiles with the same name (e.g. wall). Please consider the information regarding file names when organizing your Tile's images.
Register your Own Maps
Creating more complex objects using setTileType is quite cumbersome. Therefore, we have created a mechanism to include custom maps into the games area.
The class at.ac.wu.moelltal.examples.LavaTile demonstrates the use of this mechanism.
At first, you need to define a map-file which contains your custom map:
- every line which starts with an asterisk assigns a Tile type to the letter following the asterisk (these Tile types need to be defined using
registerTileTypeas outlined above). - the other lines specify the alignment of the tiles. A space indicates that the tile will not be changed on this position.
*a=lava *b=lava_accessible *c=stoneelement b a a ccccc a a b
You may use your map-file using the at.ac.wu.moelltal.driver.util.MapReader object as outlined below:
// register new tile types Tile.registerTileType(getClass().getPackage(), "lava", "/at/ac/wu/moelltal/plugins/w10/myproject/lava.png"); Tile.registerTileType(getClass().getPackage(), "lava_accessible", "/at/ac/wu/moelltal/plugins/w10/myproject/lava.png"); Tile.registerTileType(getClass().getPackage(), "stoneelement", "/at/ac/wu/moelltal/plugins/w10/myproject/stone.png"); // paint your map MapReader lavaBridge = new MapReader(m, getClass().getResource("/at/ac/wu/moelltal/plugin/examples/myMap.map")); laveBridge.drawMap(getClass().getPackage(), x,y);
You may reuse the MapReader object to include your map in multiple places in the game area.
Trigger a Function Call on Accessing a Tile
addTileAccessFunction adds classes which implement the TileCallBack interface to a particular Tile. Once a GameCharacter accesses this Tile, the Manager will call the tileAccessed method of all classes associated with it.
// get the handle of the Tile to which we want to // add a tile access method Tile t = m.getArea().getTile(x,y); // define the TileCallBack object and add it to that Tile t.addTileAccessFunction( new TileCallBack() { public void tileAccessed(Tile t, GameCharacter c) { System.out.println("GameCharacter "+c+" has entered Tile "+t); } });
or
Tile t = m.getArea().getTile( x, y); t.addTileAccessFunction( this ); // add //* called whe the tile where the GameCharacter is on is access *// public void tileAccessed(Tile t, GameCharacter c) { // do what you want to do in here ... }
Execute a Function Once a Tile Gets Clicked
The addTileClickedFunction method adds objects implementing the TileClicked interface to a Tile. Ones a user clicks on that object the tileClicked methods of its associated TileClicked objects get called.
// get the handle of the Tile to which we want to // add a tileClicked method Tile t = m.getArea().getTile(x,y); // define the TileClicked object and add it to that Tile t.addTileClickedFunction( new TileClicked() { public void tileClicked(Tile t) { System.out.println("You have clicked Tile "+t); } });

