Skip to content

How to use haxe metazelda

Julien Samama edited this page Apr 23, 2014 · 2 revisions

First of all, everything you need for your own project is in the src/ folder.

##1. Generation

###1.1. Seed First of all, we have to make a seed for our generator. Let's make a random one :

var seed = Std.random(metazelda.util.Utils.MAX_VALUE - 1);

One of the restriction right now is that this number must be positive.

###1.2. Constraints Our generator also needs constraints (e.g. number of rooms, of keys...). There's 2 types of constraints.

####1.2.1. Count Constraints Limits the number of keys, switches and rooms the IDungeonGenerator is allowed to place.

// limit it to 25 rooms, 4 keys and 1 switch
var constraints = new CountConstraints(25, 4, 1);

NB: currently, the DungeonGenerator can only manage 1 switch maximum.

####1.2.2. Space Constraints Constrains the coordinates where Rooms may be placed to be only those within the SpaceMap. The SpaceMap is a image we'll read, white pixels will be the rooms, other pixels will be null. See examples of image in assets. Here's an example on how to do it :

var spaceMap:SpaceMap = new SpaceMap();

// get the turtle shape image
var img:BitmapData = Assets.getBitmapData('img/spacemaps/turtle.png');

// loop to each pixel
for (x in 0...img.width) {
	for (y in 0...img.height) {
		// if the current pixel is white :
		if (img.getPixel(x, y) & 0xFFFFFF != 0) {
			// then we set the current coordinate in the spacemap as a true (will be a room)
			spaceMap.set(new Coords(x, y), true);
		}
	}
}
// feed the SpaceConstraints with the SpaceMap
constraints = new SpaceConstraints(spaceMap);

###1.3. Generators

####1.3.1. DungeonGenerator DungeonGenerator is the base generator.

var dungeonGen = new DungeonGenerator(seed, constraints);
dungeonGen.generate();

####1.3.2. LinearDungeonGenerator LinearDungeonGenerator is an improvement on DungeonGenerator by trying to make it more linear and by doing so, saving a lot of backtracking for the player. It ignores switches for now.

var dungeonGen = new LinearDungeonGenerator(seed, constraints);
dungeonGen.generate();

###1.4. Full example

var seed = Std.random(metazelda.util.Utils.MAX_VALUE - 1);
var constraints:CountConstraints = new CountConstraints(25, 4, 0);
var dungeonGen:IDungeonGenerator = new LinearDungeonGenerator(seed, constraints);
dungeonGen.generate();

##2. Dungeon We'll be working with the dungeon from now on.

var dungeon:IDungeon = dungeonGen.getDungeon();

###2.1. Map bounding box dungeon.getExtentBounds() gives a rectangle enclosing every room within the dungeon.

var bounds:Bounds = dungeon.getExtentBounds();
bounds.left;     // left side (x) position of the rectangle
bounds.top;      // top side (y) position of the rectangle
bounds.width();  // width of the rectangle
bounds.height(); // height of the rectangle

###2.2. Rooms access

####2.2.1. Get a specific room Dungeon has some methods to help us get a specific room. Here's how to get a room based on its position:

// using Coords
var room1 = dungeon.get(new Coords(-5, 2));
// using (x, y) position
var room2 = dungeon.getBy(10, -3);

It returns null if there's no room at this position. Always think of testing if it's not null!

Dungeon also has some other helpful methods:

dungeon.findStart();  // returns the starting room
dungeon.findBoss();   // returns the boss room
dungeon.findGoal();   // returns the goal room
dungeon.findSwitch(); // returns the switch room

####2.2.2. Looping Let's loop through all our rooms:

for (room in dungeon.getRooms()) {
	// do stuff
}

###2.3. Link between rooms Testing if 2 given rooms are linked:

dungeon.roomsAreLinked(room1, room2);

##3. Rooms functionalities

//Loop through all rooms:
for (room in dungeon.getRooms())
{
	// get room Coords
	room.coords;
	
	// get the precondition. It can be a switch on a given state, or it could be a key:
	room.getPrecond();
	
	// returns null if this room has no item, else it returns the item:
	room.getItem();
	
	// get intensity of the room. Can be used to put dangerous group of enemies when the intensity is high
	// and small group of easy enemies when intensity is low.
	room.getIntensity();
}

##4. Directions and Edges

//Loop through all rooms:
for (room in dungeon.getRooms())
{
	// loop through all directions (North, East, South, West):
	for (d in Direction.values())
	{
		// get the coords next to the current one in the diven direction:
		var nextCoords:Coords = room.coords.nextInDirection(d);
		
		// try to get the room next to the current room:
		var nextRoom:Room = dungeon.get(nextCoords);
		
		
		// there's 2 ways to check if the rooms are connected:
		
		// 1st solution:
		if (nextRoom != null && !dungeon.roomsAreLinked(room, nextRoom)) {
			// rooms are linked
		}
		
		// 2nd solution:
		if (room.getEdge(d) != null) {
			// rooms are linked
		}
		
		// get the edge of the current direction:
		var edge:Edge = room.getEdge(d);
		
		if (edge != null)
		{
			if (edge.hasSymbol())
			{
				// get the symbol of the edge:
				var symbol:Symbol = edge.getSymbol();
				// a Symbol can be either a key, or a switch state. or it can be
				// null which means the player won't need anything to go through the door.
				// 
				if (symbol.getValue() == 0) {
					// it needs the key 0 (or A in the Viewer) to be opened
				} else if (symbol.getValue() == 1) {
					// it needs the key 1 (or B in the Viewer) to be opened
				}
				// and so on...
			}
		}
	}
}

Clone this wiki locally