Skip to content

Commit 998f124

Browse files
committed
scale exit meta to final ascii
1 parent 76833ea commit 998f124

File tree

2 files changed

+120
-4
lines changed

2 files changed

+120
-4
lines changed

lib/mazes/Metroidvania.js

+69-4
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,8 @@ function initialize(width, height) {
590590
return map;
591591
}
592592

593-
export default function ALGORITHM_METROIDVANIA(tileMap, options) {
593+
export default function ALGORITHM_METROIDVANIA(tileMap, options={}) {
594+
try{
594595
tileMap.fill(0); // Fill with walls
595596
const maxDimension = Math.max(tileMap.width, tileMap.height);
596597
if(maxDimension <= 5){
@@ -622,13 +623,77 @@ export default function ALGORITHM_METROIDVANIA(tileMap, options) {
622623
roomDiff: doorDiff, // When adding a new door, room ID distance
623624
roomDiffOdds: 1/2 // Odds of inserting a new door on opportunity
624625
});
625-
const built = generator.build(()=>{
626-
return tileMap.random();
627-
});
626+
let built = null;
627+
if(options.retries){
628+
let generated = false;
629+
let currentTry = 0;
630+
while(currentTry < options.retries && !generated){
631+
try{
632+
currentTry++
633+
built = generator.build(()=>{
634+
return tileMap.random();
635+
});
636+
generated = true;
637+
}catch(ex){}
638+
}
639+
}else{
640+
built = generator.build(()=>{
641+
return tileMap.random();
642+
});
643+
}
628644
const flattened = built.world.reduce(((agg, line)=> agg.concat(line)), []);
629645
built.world = null;
646+
built.scaledExits = {};
647+
let chr = null;
648+
['north', 'south', 'east', 'west'].forEach((direction)=>{
649+
chr = direction[0];
650+
const scaled = {
651+
x: built.exits[chr].x * roomSizeWidth,
652+
y: (built.exits[chr].y * roomSizeHeight)
653+
};
654+
built.scaledExits[direction] = [];
655+
switch(direction){
656+
case 'north':
657+
scaled.x += Math.floor(roomSizeWidth/2);
658+
built.scaledExits[direction].push(scaled);
659+
built.scaledExits[direction].push({
660+
x: scaled.x-1,
661+
y: scaled.y
662+
});
663+
break;
664+
case 'south':
665+
scaled.x += Math.floor(roomSizeWidth/2);
666+
scaled.y += roomSizeHeight-1;
667+
built.scaledExits[direction].push(scaled);
668+
built.scaledExits[direction].push({
669+
x: scaled.x-1,
670+
y: scaled.y
671+
});
672+
break;
673+
case 'east':
674+
scaled.y += Math.floor(roomSizeHeight/2);
675+
scaled.x += roomSizeWidth-1;
676+
built.scaledExits[direction].push(scaled);
677+
built.scaledExits[direction].push({
678+
x: scaled.x,
679+
y: scaled.y-1
680+
});
681+
break;
682+
case 'west':
683+
scaled.y += Math.floor(roomSizeHeight/2);
684+
built.scaledExits[direction].push(scaled);
685+
built.scaledExits[direction].push({
686+
x: scaled.x,
687+
y: scaled.y-1
688+
});
689+
break;
690+
}
691+
});
630692
tileMap.world = built;
631693
for(let lcv=0; lcv < tileMap.data.length; lcv++){
632694
tileMap.data[lcv] = flattened[lcv];
633695
}
696+
}catch(ex){
697+
console.log(ex);
698+
}
634699
}

test/metroidvania-test.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import tap from 'tape';
2+
import TileMap from '../lib/TileMap.js';
3+
import metroidvania from '../lib/mazes/Metroidvania.js';
4+
5+
6+
const twoDTileData = (tileMap)=>{
7+
const data = tileMap.data;
8+
const twoD = [];
9+
while(data.length) twoD.push(data.splice(0, tileMap.width));
10+
return twoD;
11+
};
12+
13+
// Test querying by coordinates for 2D map
14+
tap.test('meta matches output', (t) => {
15+
16+
const tileMap = new TileMap({ width: 128, height: 128 });
17+
tileMap.fill2D(1); // Fill the entire map with '1's for simplicity
18+
metroidvania(tileMap, {retries: 5});
19+
20+
21+
const twoD = twoDTileData(tileMap);
22+
23+
//overwrite all exits
24+
twoD[tileMap.world.scaledExits.north[0].y][tileMap.world.scaledExits.north[0].x] = 'X';
25+
twoD[tileMap.world.scaledExits.north[1].y][tileMap.world.scaledExits.north[1].x] = 'X';
26+
27+
twoD[tileMap.world.scaledExits.south[0].y][tileMap.world.scaledExits.south[0].x] = 'X';
28+
twoD[tileMap.world.scaledExits.south[1].y][tileMap.world.scaledExits.south[1].x] = 'X';
29+
30+
twoD[tileMap.world.scaledExits.east[0].y][tileMap.world.scaledExits.east[0].x] = 'X';
31+
twoD[tileMap.world.scaledExits.east[1].y][tileMap.world.scaledExits.east[1].x] = 'X';
32+
33+
twoD[tileMap.world.scaledExits.west[0].y][tileMap.world.scaledExits.west[0].x] = 'X';
34+
twoD[tileMap.world.scaledExits.west[1].y][tileMap.world.scaledExits.west[1].x] = 'X';
35+
36+
37+
const oneD = twoD.reduce(((agg, line)=> agg.concat(line)), []);
38+
const index = {};
39+
40+
console.log(twoD.map(line => line.join('')).join('\n'));
41+
oneD.forEach((type)=>{
42+
if(!index[type]){
43+
index[type] = 1;
44+
}else{
45+
index[type]++;
46+
}
47+
});
48+
t.same(index['X'], 8, 'Wrote in 8 options');
49+
t.same(index[6], undefined, 'Has no exits after being overwritten');
50+
t.end();
51+
});

0 commit comments

Comments
 (0)