Skip to content

Instantly share code, notes, and snippets.

@campbecf
Last active August 29, 2015 14:23
Show Gist options
  • Save campbecf/ca3e2342dae35a0b3c28 to your computer and use it in GitHub Desktop.
Save campbecf/ca3e2342dae35a0b3c28 to your computer and use it in GitHub Desktop.
package com.howeszy.neighborhood.pcg.tileroom
import com.howeszy.neighborhood.pcg.TileRoom
import com.howeszy.neighborhood.pcg.TileMap
import java.awt.Color
import java.util.Random
class Corridor extends TileRoom{
Corridor(Integer x1, Integer y1, Integer x2, Integer y2 ){
this(x1, y1, x2, y2, null, null, Color.WHITE.getRGB())
}
Corridor(Integer x1, Integer y1, Integer x2, Integer y2, Color primaryColor ){
this(x1, y1, x2, y2, null, null, primaryColor.getRGB())
}
Corridor(Integer x1, Integer y1, Integer x2, Integer y2, Integer primaryColor ){
this(x1, y1, x2, y2, null, null, primaryColor)
}
Corridor(Integer x1, Integer y1, Integer x2, Integer y2, Integer x3, Integer y3){
this(x1, y1, x2, y2, x3, y3, Color.WHITE.getRGB())
}
Corridor(Integer x1, Integer y1, Integer x2, Integer y2, Integer x3, Integer y3, Color primaryColor ){
this(x1, y1, x2, y2, x3, y3, primaryColor.getRGB())
}
Corridor(Integer x1, Integer y1, Integer x2, Integer y2, Integer x3, Integer y3, Integer primaryColor ){
if((x3 && !y3) || (!x3 && y3)){
throw new CorridorException("Both sets of third coordinates must be provided if one is")
}
if(x1 != x2 && y1 != y2){
throw new CorridorException("A corridor can only be 1 unit wide")
}
if(x3 || y3){
if((x1 == x2 && y2 != y3) || (y1 == y2 && x2 != x3)){
throw new CorridorException("Both sections of a corridor can only be 1 unit wide")
}
}
this.connections = []
this.start = false
this.primaryColor = primaryColor
this.points = [:]
List xi1 = (x1 .. x2), yi1 = (y1 .. y2),
xi2 = (x2 .. x3), yi2 = (y2 .. y3)
xi1.each{ x ->
points[x] = [:]
yi1.each{ y ->
points[x][y] = primaryColor
}
}
xi2.each{ x ->
if(!points?.get(x)){
points[x] = [:]
}
yi2.each{ y ->
points[x][y] = primaryColor
}
}
}
static void connect(TileMap map, TileRoom room1, TileRoom room2){
println 'entering connect'
List room1x = room1.xlist, room1y = room1.ylist,
room2x = room2.xlist, room2y = room2.ylist
if(room1.intersects(room2)){
println 'room intersects'
room1.connect(room2)
return
} else if(room1.connections.contains(room2)){
println 'connection already exists'
return
} else if( room1x.collect{it + 1}.intersect(room2x) && room1y.intersect(room2y) ||
room1x.collect{it - 1}.intersect(room2x) && room1y.intersect(room2y) ||
room1y.collect{it + 1}.intersect(room2y) && room1x.intersect(room2x) ||
room1y.collect{it - 1}.intersect(room2y) && room1x.intersect(room2x)){
println "room connects"
room1.connect(room2)
return
}
Random rand = new Random()
Integer nx1, ny1, nx2, ny2, nx3, ny3
List room1coords = room1.toList(),
room2coords = room2.toList(),
room1rand = room1coords[rand.nextInt(room1coords.size())],
room2rand = room2coords[rand.nextInt(room2coords.size())]
Integer coinflip = rand.nextInt(1)
nx1 = room1rand[0]
ny1 = room1rand[1]
nx3 = room2rand[0]
ny3 = room2rand[1]
if(coinflip == 0){
nx2 = room1rand[0]
ny2 = room2rand[1]
}else{
nx2 = room2rand[0]
ny2 = room1rand[1]
}
Corridor corridor = new Corridor(nx1, ny1, nx2, ny2, nx3, ny3)
map.rooms << corridor
room1.connect(corridor)
room2.connect(corridor)
}
void trim(TileRoom room){
}
}
class CorridorException extends Exception {
CorridorException(){ super(); }
CorridorException(String message ){ super(message); }
CorridorException(String message, Throwable cause){ super(message, cause); }
CorridorException(Throwable cause){ super(cause); }
}
package com.howeszy.neighborhood.pcg
import java.awt.Color
import java.util.Random
class TileRoom{
Map<Integer, Map<Integer, Integer>> points
List<TileRoom> connections
Boolean start
Integer primaryColor
TileRoom(){}
TileRoom(Integer x1, Integer y1, Integer x2, Integer y2){
this(x1, y1, x2, y2, false, Color.WHITE.getRGB())
}
TileRoom(Integer x1, Integer y1, Integer x2, Integer y2, Boolean start){
this(x1, y1, x2, y2, start, Color.WHITE.getRGB())
}
TileRoom(Integer x1, Integer y1, Integer x2, Integer y2, Color primaryColor){
this(x1, y1, x2, y2, false, primaryColor.getRGB())
}
TileRoom(Integer x1, Integer y1, Integer x2, Integer y2, Integer primaryColor){
this(x1, y1, x2, y2, false, primaryColor)
}
TileRoom(Integer x1, Integer y1, Integer x2, Integer y2, Boolean start, Color primaryColor){
this(x1, y1, x2, y2, start, primaryColor.getRGB())
}
TileRoom(Integer x1, Integer y1, Integer x2, Integer y2, Boolean start, Integer primaryColor){
this.primaryColor = primaryColor
this.connections = []
this.start = start
this.points =[:]
(x1 .. x2).each{ x ->
points[x] = [:]
(y1 .. y2).each{ y ->
points[x][y] = primaryColor
}
}
}
List toList(){
List pointslist = []
for(int x = 0; x < points.keySet().size(); x++){
Integer xkey = points.keySet()[x]
for(int y = 0; y < points[xkey].keySet().size(); y++){
Integer ykey = points[xkey].keySet()[y]
pointslist << [xkey,ykey]
}
}
return pointslist
}
void connect(TileRoom room){
this.connections.add(room)
room.connections.add(this)
}
Double distance(Integer x, Integer y){
Math.sqrt( Math.pow(x - getCenterX(), 2) + Math.pow(y - getCenterY(), 2) )
}
Double distance(TileRoom room){
Math.sqrt( Math.pow(room.getCenterX() - getCenterX(), 2) + Math.pow(room.getCenterY() - getCenterY(), 2) )
}
Boolean intersects(TileRoom room){
for(int x = 0; x < points.keySet().size(); x++){
Integer xkey = points.keySet()[x]
if(room.points?.get(xkey)){
for(int y = 0; y < points[xkey].keySet().size(); y++){
Integer ykey = points[xkey].keySet()[y]
if(room.points[xkey]?.get(ykey)){
return true
}
}
}
}
return false
}
List getCenter(){
[getCenterX(), getCenterY()]
}
Integer getCenterX(){
List xlist = getXlist()
xlist[(Integer)(xlist.size()/2)]
}
Integer getCenterY(){
List ylist = getYlist()
ylist[(Integer)(ylist.size()/2)]
}
List getYlist(){
def ylist = []
points.each{ ylist = ylist.plus(it.value.keySet()).unique() }
return ylist.sort()
}
List getXlist(){
def xlist = points.keySet()
return xlist.toArray().sort()
}
Integer getHeight(){
getYlist().size()
}
Integer getWidth(){
getXlist().size()
}
Integer getArea(){
getWidth() * getHeight()
}
}
class RoomException extends Exception{
RoomException(){ super(); }
RoomException(String message ){ super(message); }
RoomException(String message, Throwable cause){ super(message, cause); }
RoomException(Throwable cause){ super(cause); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment