Commit 438dfd68 authored by Fardale's avatar Fardale

Merge branch 'master' of gitlab.crans.org:genie_logiciel_2015/the_dungeon_project

parents d584d2df acbf2482
......@@ -31,6 +31,7 @@ public class TileSpriteLinker
spriteMap.put(TileType.EMPTY, new GroundTileSprite(SpriteLoader.getSpriteFromPath("/assets/Top_wall.png").getSpriteImage()));
spriteMap.put(TileType.STAIRS , new GroundTileSprite(SpriteLoader.getSpriteFromPath("/assets/dungeon_set_1/stairs/stairs2.bmp").getSpriteImage()));
spriteMap.put(TileType.WATER , new GroundTileSprite(SpriteLoader.getSpriteFromPath("/assets/Lava.png").getSpriteImage()));
spriteMap.put(TileType.TORCH , new GroundTileSprite(SpriteLoader.getSpriteFromPath("/assets/Torch.png").getSpriteImage()));
}
public Displayable[][] getSpriteOfTile(TileType type, TileSprite.TileAbstractType[][] mask)
......
......@@ -2,14 +2,13 @@ package map_generation.map;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import map_generation.tiles.TileBuilder;
import map_generation.tiles.TileType;
import core.gamestate.Entity;
import core.gamestate.SpeciesArray;
import core.zone.Point;
import map_generation.tiles.TileBuilder;
import map_generation.tiles.TileType;
/**
* Builder for the class Map
......@@ -94,23 +93,19 @@ public final class MapBuilder implements Serializable{
TileBuilder[][] map = SurfacesMapGeneration.emptyTiles(height, width);
for (RoomBuilder room : rooms) {
room.getSurface().surroundWithWalls(map);
room.getSurface().surroundWith(map, TileType.WALL);
}
for (Surface surface: corridors) {
surface.surroundWithWalls(map);
surface.surroundWith(map, TileType.WALL);
}
for (RoomBuilder room : rooms) {
room.build(map);
room.build(map, this);
}
for (Surface corridor : corridors) {
corridor.fillSurfaceWith(map, TileType.GROUND);
}
generateEntities(map);
map[stairsPosition.getI()][stairsPosition.getJ()].setType(TileType.STAIRS);
//TODO uses doors
//Surface[] doors = this.doors.toArray(new Surface[this.doors.size()]);
Surface[] rooms = new Surface[this.rooms.size()];
for (int i = 0; i < rooms.length; i++) {
rooms[i] = this.rooms.get(i).getSurface();
......
......@@ -7,7 +7,7 @@ import map_generation.tiles.TileType;
public final class RoomBuilder {
public static enum RoomType{START, END, NORMAL, TORCHES, LAVA};
public static enum RoomType{START, END, NORMAL, TORCHES, FULL_LAVA, LAVA_ISLAND};
private Surface surface;
private RoomType type;
......@@ -25,27 +25,54 @@ public final class RoomBuilder {
return surface;
}
private void addLava(TileBuilder[][] map){
private void addFullLava(TileBuilder[][] map){
Random r = new Random();
int dw = r.nextInt((surface.width-2)/2)+1, dh = r.nextInt((surface.height-2)/2)+1;
Surface s = new Surface(surface.i1+dh, surface.j1+dw, surface.i2-dh, surface.j2-dw);
s.fillSurfaceWith(map, TileType.WATER);
}
private void addStairs(TileBuilder[][] map, MapBuilder mapB){
Random r = new Random();
int posY=r.nextInt(surface.j2-surface.j1+1)+surface.j1;
int posX=r.nextInt(surface.i2-surface.i1+1)+surface.i1;
map[posY][posX].setType(TileType.STAIRS);
mapB.setStairPosition(new MapPoint(posY, posX));
}
private void addTorches(TileBuilder[][] map){
map[surface.j1 + 2][surface.i1 + 2].setType(TileType.TORCH);
map[surface.j2 - 2][surface.i1 + 2].setType(TileType.TORCH);
map[surface.j1 + 2][surface.i2 - 2].setType(TileType.TORCH);
map[surface.j2 - 2][surface.i2 - 2].setType(TileType.TORCH);
}
public void setRandomlySpecial(double p){
Random r = new Random();
if(r.nextDouble() <= p){
if(r.nextDouble() <= 0.5) setType(RoomType.LAVA);
if(type == RoomType.NORMAL && r.nextDouble() <= p){
double pp = r.nextDouble();
if( pp <= 0.5 )setType(RoomType.FULL_LAVA);
else setType(RoomType.TORCHES);
}
}
public void build(TileBuilder[][] map){
public void build(TileBuilder[][] map, MapBuilder mapB){
surface.fillSurfaceWith(map, TileType.GROUND);
switch (type){
case LAVA :
addLava(map);
case FULL_LAVA :
addFullLava(map);
break;
case START :
int posY=(surface.j1+surface.j2)/2;
int posX=(surface.i1+surface.i2)/2;
mapB.setPositionPlayerAtStart(new MapPoint(posY, posX));
break;
case TORCHES:
addTorches(map);
break;
case END:
addStairs(map, mapB);
break;
default :
}
......
......@@ -36,14 +36,14 @@ public class Surface implements Serializable{
* @param xb
* @param yb
*/
private static void linkNodes(TileBuilder[][] tilebs, int xa, int ya, int xb, int yb){
private static void linkNodes(TileBuilder[][] tilebs, TileType type, int xa, int ya, int xb, int yb){
int d = 0;
int dy = Math.abs(yb - ya), dx = Math.abs(xb - xa), dy2 = (dy << 1), dx2 = (dx << 1);
int ix = xa < xb ? 1 : -1, iy = ya < yb ? 1 : -1;
if (dy <= dx) {
for (;;) {
tilebs[xa][ya].setType(TileType.WALL);
tilebs[xa][ya].setType(type);
if (xa == xb)
break;
xa += ix;
......@@ -55,7 +55,7 @@ public class Surface implements Serializable{
}
} else {
for (;;) {
tilebs[xa][ya].setType(TileType.WALL);
tilebs[xa][ya].setType(type);
if (ya == yb)
break;
ya += iy;
......@@ -72,14 +72,14 @@ public class Surface implements Serializable{
* @param map
* @param surface
*/
public void surroundWithWalls(TileBuilder[][] map){
public void surroundWith(TileBuilder[][] map, TileType type){
final int x1 = i1 - 1, x2 = i2 +1;
final int y1 = j1 - 1, y2 = j2 +1;
linkNodes(map, y1, x1, y2, x1);
linkNodes(map, y2, x1, y2, x2);
linkNodes(map, y2, x2, y1, x2);
linkNodes(map, y1, x2, y1, x1);
linkNodes(map, type, y1, x1, y2, x1);
linkNodes(map, type, y2, x1, y2, x2);
linkNodes(map, type, y2, x2, y1, x2);
linkNodes(map, type, y1, x2, y1, x1);
}
......
......@@ -8,7 +8,6 @@ import java.util.Random;
import map_generation.map.RoomBuilder.RoomType;
import map_generation.tiles.TileBuilder;
import map_generation.tiles.TileType;
/**
*
* This class contains methods that generates simple map to help for tests
......@@ -30,22 +29,17 @@ public class SurfacesMapGeneration implements Serializable{
return tilebs;
}
private static MapPoint defineStartPosition(RoomBuilder[] rooms) {
private static RoomBuilder defineStartPosition(RoomBuilder[] rooms) {
Random r = new Random();
int idRoom=r.nextInt(rooms.length);
//int posY=(rooms[idRoom].j1+rooms[idRoom].j2)/2;
//int posX=(rooms[idRoom].i1+rooms[idRoom].i2)/2;
int posX = rooms[idRoom].getSurface().i1;
int posY = rooms[idRoom].getSurface().j1;
rooms[idRoom].setType(RoomType.START);
MapPoint startPosition=new MapPoint(posY, posX);
return startPosition;
return rooms[idRoom];
}
private static MapPoint defineStairsPosition(MapBuilder map,int height,int width,RoomBuilder[] rooms,MapPoint startPosition) {
private static void defineStairsPosition(MapBuilder map,int height,int width,RoomBuilder[] rooms, RoomBuilder startingRoom) {
// This method chooses, once the structure of the map is done and the start position of the player is choosed, where the stairs should be
// First I do a BFS (Breadth First Search) starting at the start position of the player that I put in distanceToStart
MapPoint startPosition = new MapPoint(startingRoom.getSurface().j1, startingRoom.getSurface().i1);
int[][] distanceToStart=new int[height][width];
for(int i=0;i<height;i++) {
for(int j=0;j<width;j++) {
......@@ -100,8 +94,6 @@ public class SurfacesMapGeneration implements Serializable{
int posX=0;
while (bb) { // I choose a random spot for the stairs
idRoom=r.nextInt(rooms.length);
// posY=r.nextInt(rooms[idRoom].j2-rooms[idRoom].j1+1)+rooms[idRoom].j1;
// posX=r.nextInt(rooms[idRoom].i2-rooms[idRoom].i1+1)+rooms[idRoom].i1;
posX = rooms[idRoom].getSurface().i1;
posY = rooms[idRoom].getSurface().j1;
if (3*distanceToStart[posY][posX]>2*max_dist){
......@@ -109,8 +101,6 @@ public class SurfacesMapGeneration implements Serializable{
bb=false; // If the distance from the start position to the stairs position is large enough I choose it (compared to the farest point the player can reach)
}
}
MapPoint stairsPosition=new MapPoint(posY, posX);
return stairsPosition;
}
public static int distanceBetweenTwoSurface(Surface surface1,Surface surface2) {
......@@ -268,16 +258,14 @@ public class SurfacesMapGeneration implements Serializable{
map.addRoom(rooms[i]);
}
addCorridorsByMST(rooms, map); // Creation of the set of corridors
MapPoint startPosition=defineStartPosition(rooms);
MapPoint stairsPosition=defineStairsPosition(map,heightMap,widthMap,rooms,startPosition);
RoomBuilder startRoom=defineStartPosition(rooms);
defineStairsPosition(map,heightMap,widthMap,rooms,startRoom);
map.setHeight(heightMap);
map.setWidth(widthMap);
map.setStairPosition(stairsPosition);
map.setPositionPlayerAtStart(startPosition);
for (RoomBuilder room : rooms) {
room.setRandomlySpecial(0.4);
room.setRandomlySpecial(0.3);
}
return map.build();
......@@ -293,10 +281,8 @@ public class SurfacesMapGeneration implements Serializable{
RoomBuilder[] rooms=new RoomBuilder[1];
rooms[0]= new RoomBuilder(room, RoomType.NORMAL);
addCorridorsByMST(rooms, map);
MapPoint startPosition=defineStartPosition(rooms);
MapPoint stairsPosition=defineStairsPosition(map,size,size,rooms,startPosition);
map.setStairPosition(stairsPosition);
map.setPositionPlayerAtStart(startPosition);
RoomBuilder startRoom=defineStartPosition(rooms);
defineStairsPosition(map,size,size,rooms,startRoom);
return(map.build());
}
......
......@@ -27,6 +27,12 @@ public class MapTests {
}
}
@Test
public void testMapHasStartEnd(){
Map map = SurfacesMapGeneration.roomsRandomGeneration(20, 500);
assertFalse("The Map doesn't cointains any stairs !", map.getPositionStairs() == null && map.getPositionPlayerStart() == null);
}
@Test
public void testPlayerNotOnWater(){
Map map = SurfacesMapGeneration.roomsRandomGeneration(20, 500);
......
......@@ -8,7 +8,7 @@ import map_generation.tiles.TilePropertyVector.TileProperty;
*
*/
public enum TileType{
EMPTY, GROUND, WALL, STAIRS, WATER;
EMPTY, GROUND, WALL, STAIRS, WATER, TORCH;
private TilePropertyVector tpv;
private String shortName;
......@@ -24,6 +24,8 @@ public enum TileType{
STAIRS.shortName="S";
WATER.tpv=new TilePropertyVector().addProperty(TileProperty.LIQUID);
WATER.shortName="W";
TORCH.tpv=new TilePropertyVector().addProperty(TileProperty.IMPASSABLE);
TORCH.shortName="T";
}
public TilePropertyVector getTilePropertyVector(){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment