Skip to content

Instantly share code, notes, and snippets.

@sjakaus
Created September 29, 2010 20:36
Show Gist options
  • Save sjakaus/603511 to your computer and use it in GitHub Desktop.
Save sjakaus/603511 to your computer and use it in GitHub Desktop.
package
{
import net.flashpunk.*;
import net.flashpunk.masks.*;
import net.flashpunk.graphics.*;
import flash.geom.*;
public class Level extends Entity
{
[Embed(source = 'assets/bwtiles.png')] private const TILEIMAGE:Class;
[Embed(source='assets/yellowtile.png')] private const YELLOWTILE:Class;
public function Level()
{
var myTiles:Tilemap = new Tilemap(TILEIMAGE,1536,1536,16,16);
var yellowTiles:Tilemap = new Tilemap(YELLOWTILE,1536,1536,16,16);
x = 0;
y = 0;
type = "level";
myTiles.setRegion(0,0,96,96,1);
myTiles.setRegion(0,0,96,1,0);
myTiles.setRegion(0,0,1,96,0);
var roomtype:uint;
var roomx:uint;
var roomy:uint;
var rooms:uint = 0;
var maxrooms:uint = 60;
var looping:Boolean = true;
var startx:uint
var starty:uint;
var tiles:uint = 0;
var doors:uint;
var maxdoors:uint = 40;
var doorx:uint;
var doory:uint;
//generate the level
while(looping){
//spam tetraminos
rooms = 0;
while(rooms < maxrooms)
{
//pick a tetramino
roomtype = FP.rand(7);
roomx = FP.rand(8);
roomy = FP.rand(8);
switch(roomtype)
{
//I
case 0:
switch(FP.rand(2))
{
case 0:
myTiles.setRegion(roomx*8,roomy*8,33,9,0);
myTiles.setRegion(roomx*8+1,roomy*8+1,31,7,1);
break;
case 1:
myTiles.setRegion(roomx*8,roomy*8,9,33,0);
myTiles.setRegion(roomx*8+1,roomy*8+1,7,31,1);
break;
}
break;
//L
case 1:
switch(FP.rand(4))
{
case 0:
myTiles.setRegion(roomx*8,roomy*8,9,25,0);
myTiles.setRegion(roomx*8,roomy*8+16,17,9,0);
myTiles.setRegion(roomx*8+1,roomy*8+1,7,23,1);
myTiles.setRegion(roomx*8+1,roomy*8+17,15,7,1);
break;
case 1:
myTiles.setRegion(roomx*8,roomy*8,25,9,0);
myTiles.setRegion(roomx*8,roomy*8,9,17,0);
myTiles.setRegion(roomx*8+1,roomy*8+1,23,7,1);
myTiles.setRegion(roomx*8+1,roomy*8+1,7,15,1);
break;
case 2:
myTiles.setRegion(roomx*8,roomy*8,17,9,0);
myTiles.setRegion(roomx*8+8,roomy*8,9,25,0);
myTiles.setRegion(roomx*8+1,roomy*8+1,15,7,1);
myTiles.setRegion(roomx*8+9,roomy*8+1,7,23,1);
break;
case 3:
myTiles.setRegion(roomx*8,roomy*8+8,25,9,0);
myTiles.setRegion(roomx*8+24,roomy*8,9,9,0);
myTiles.setRegion(roomx*8+1,roomy*8+9,23,7,1);
myTiles.setRegion(roomx*8+25,roomy*8+1,7,8,1);
break;
}
break;
//J
case 2:
switch(FP.rand(4))
{
case 0:
myTiles.setRegion(roomx*8+8,roomy*8,9,25,0);
myTiles.setRegion(roomx*8,roomy*8+16,17,9,0);
myTiles.setRegion(roomx*8+9,roomy*8+1,7,23,1);
myTiles.setRegion(roomx*8+1,roomy*8+17,15,7,1);
break;
case 1:
myTiles.setRegion(roomx*8,roomy*8,9,17,0);
myTiles.setRegion(roomx*8,roomy*8+8,25,9,0);
myTiles.setRegion(roomx*8+1,roomy*8+1,7,15,1);
myTiles.setRegion(roomx*8+1,roomy*8+9,23,7,1);
break;
case 2:
myTiles.setRegion(roomx*8,roomy*8,9,25,0);
myTiles.setRegion(roomx*8,roomy*8,17,9,0);
myTiles.setRegion(roomx*8+1,roomy*8+1,7,23,1);
myTiles.setRegion(roomx*8+1,roomy*8+1,15,7,1);
break;
case 3:
myTiles.setRegion(roomx*8,roomy*8,25,9,0);
myTiles.setRegion(roomx*8+16,roomy*8,9,17,0);
myTiles.setRegion(roomx*8+1,roomy*8+1,23,7,1);
myTiles.setRegion(roomx*8+17,roomy*8+1,7,15,1);
break;
}
break;
//T
case 3:
switch(FP.rand(4))
{
case 0:
myTiles.setRegion(roomx*8,roomy*8+8,25,9,0);
myTiles.setRegion(roomx*8+8,roomy*8,9,17,0);
myTiles.setRegion(roomx*8+1,roomy*8+9,23,7,1);
myTiles.setRegion(roomx*8+9,roomy*8+1,7,15,1);
break;
case 1:
myTiles.setRegion(roomx*8,roomy*8,9,25,0);
myTiles.setRegion(roomx*8,roomy*8+8,17,9,0);
myTiles.setRegion(roomx*8+1,roomy*8+1,7,23,1);
myTiles.setRegion(roomx*8+1,roomy*8+9,15,7,1);
break;
case 2:
myTiles.setRegion(roomx*8,roomy*8,25,9,0);
myTiles.setRegion(roomx*8+8,roomy*8,9,17,0);
myTiles.setRegion(roomx*8+1,roomy*8+1,23,7,1);
myTiles.setRegion(roomx*8+9,roomy*8+1,7,15,1);
break;
case 3:
myTiles.setRegion(roomx*8+8,roomy*8,9,25,0);
myTiles.setRegion(roomx*8,roomy*8+8,17,9,0);
myTiles.setRegion(roomx*8+9,roomy*8+1,7,23,1);
myTiles.setRegion(roomx*8+1,roomy*8+9,15,7,1);
break;
}
break;
//S
case 4:
switch(FP.rand(2))
{
case 0:
myTiles.setRegion(roomx*8,roomy*8,9,17,0);
myTiles.setRegion(roomx*8+8,roomy*8+8,9,17,0);
myTiles.setRegion(roomx*8+1,roomy*8+1,7,15,1);
myTiles.setRegion(roomx*8+1,roomy*8+9,15,7,1);
myTiles.setRegion(roomx*8+9,roomy*8+9,7,15,1);
break;
case 1:
myTiles.setRegion(roomx*8+8,roomy*8,17,9,0);
myTiles.setRegion(roomx*8,roomy*8+8,17,9,0);
myTiles.setRegion(roomx*8+9,roomy*8+1,15,7,1);
myTiles.setRegion(roomx*8+1,roomy*8+9,15,7,1);
myTiles.setRegion(roomx*8+9,roomy*8+1,7,15,1);
break;
}
break;
//Z
case 5:
switch(FP.rand(2))
{
case 0:
myTiles.setRegion(roomx*8,roomy*8+8,9,17,0);
myTiles.setRegion(roomx*8+8,roomy*8,9,17,0);
myTiles.setRegion(roomx*8+9,roomy*8+1,7,15,1);
myTiles.setRegion(roomx*8+1,roomy*8+9,15,7,1);
myTiles.setRegion(roomx*8+1,roomy*8+9,7,15,1);
break;
case 1:
myTiles.setRegion(roomx*8,roomy*8,17,9,0);
myTiles.setRegion(roomx*8+8,roomy*8+8,17,9,0);
myTiles.setRegion(roomx*8+1,roomy*8+1,15,7,1);
myTiles.setRegion(roomx*8+9,roomy*8+1,7,15,1);
myTiles.setRegion(roomx*8+9,roomy*8+9,15,7,1);
break;
}
break;
//O
case 6:
myTiles.setRegion(roomx*8,roomy*8,17,17,0);
myTiles.setRegion(roomx*8+1,roomy*8+1,15,15,1);
break;
}
rooms++
}
//make sure edges are edges
myTiles.setRegion(64,0,31,96,0);
myTiles.setRegion(0,64,96,31,0);
//spam doors
doors = 0;
while(doors < maxdoors)
{
doorx = FP.rand(7);
doory = FP.rand(7);
if(FP.rand(2) == 1)
{
if(myTiles.getTile(4+doorx*8,8+doory*8) == 0)
{
myTiles.setTile(4+doorx*8,8+doory*8,1);
doors++;
}
}
else
{
if(myTiles.getTile(8+doorx*8,4+doory*8) == 0)
{
myTiles.setTile(8+doorx*8,4+doory*8,1);
doors++;
}
}
}
//create a yellow tilemap of everything connected to the starting position for reference
//switch out later for vector.<Point>?
//or a vector of vectors of booleans
startx = 4 + FP.rand(7)*8;
starty = 4 + FP.rand(7)*8;
yellowTiles.setRegion(0,0,96,96,0);
yellowTiles.setTile(startx,starty,1);
tiles = 0;
for(var i:uint = 0; i < 50; i++)
{
for(var j:uint = 0; j<96; j+=4)
{
for(var k:uint = 0; k<96; k+=4)
{
if((yellowTiles.getTile(j,k) == 0) && (myTiles.getTile(j,k) == 1))
{
if(yellowTiles.getTile(j-4,k) == 1)
{
yellowTiles.setTile(j,k,1);
tiles++;
}
else if(yellowTiles.getTile(j+4,k) == 1)
{
yellowTiles.setTile(j,k,1);
tiles++;
}
else if(yellowTiles.getTile(j,k-4) == 1)
{
yellowTiles.setTile(j,k,1);
tiles++;
}
else if(yellowTiles.getTile(j,k+4) == 1)
{
yellowTiles.setTile(j,k,1);
tiles++;
}
}
}
}
}
if(tiles>16){
trace("no longer looping");
looping = false;
}
else
{
trace("looping");
}
}
FP.world.add(new Player(startx*16,starty*16));
//create the collision grid from the wall/floor tilemap
var collisionGrid:Grid = new Grid(97,97,16,16);
collisionGrid.usePositions = false;
/*for(i = 0; i < 90; i++)
{
for(j = 0; j < 90; j++)
{
if(myTiles.getTile(i,j)==0)
{
collisionGrid.setCell(i,j,true);
}
else
{
collisionGrid.setCell(i,j,false);
}
}
}*/
collisionGrid.setRect(0,0,90,90,true);
mask = collisionGrid;
//this is how it's going to go down:
//pick a tetramino. rotate it randomly. pick a location on the map, stick it there.
//repeat a bunch
//draw surrounding walls
//place special rooms
//spam doorways (place doors at this time)
//pick a random spot for the entrance, flag eveything as connected
//make sure enough spots are flagged, otherwise repeat from the top?
//pick a random spot, check if it's flagged, place the exit
var graphics:Graphiclist = new Graphiclist(myTiles,yellowTiles);
graphic = graphics;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment