@psikosen/

AggravatingOilyThing

Java

No description

fork
loading
Files
  • Main.java
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class Main {
  public static void main(String[] args) {
    System.out.prln("Hello world!");
  }
}


class Room extends Sprite {
    // these values hold grid coordinates for each corner of the room
    public int x1;
    public int x2;
    public int y1;
    public int y2;
 
    // width and height of room in terms of grid
    public int w;
    public int h;
 
    // center po of the room
    public Po center;
 
    // constructor for creating new rooms
    public  Room(int x,int y,int w,int h) {
        super();
 
        x1 = x;
        x2 = x + w;
        y1 = y;
        y2 = y + h;
        this.x = x * Main.TILE_WIDTH;
        this.y = y * Main.TILE_HEIGHT;
        this.w = w;
        this.h = h;
        center = new Po(Math.floor((x1 + x2) / 2),
            Math.floor((y1 + y2) / 2));
    }
 
    // return true if this room ersects provided room
    public boolean ersects(Room room) {
        return (x1 <= room.x2 && x2 >= room.x1 &&
            y1 <= room.y2 && room.y2 >= room.y1);
    }
}

private function placeRooms() {
// store rooms in an array for easy access
rooms = new Array();
 
// variable for tracking center of each room
var newCenter = null;
 
// randomize values for each room
for (int r = 0 ; r < maxRooms;r++) {
    double w = minRoomSize + Std.random(maxRoomSize - minRoomSize + 1);
    double h = minRoomSize + Std.random(maxRoomSize - minRoomSize + 1);
    double x = Std.random(MAP_WIDTH - w - 1) + 1;
    double y = Std.random(MAP_HEIGHT - h - 1) + 1;
 
    // create room with randomized values
    Room newRoom = new Room(x, y, w, h);
 
    int failed = false;

    for (int otherRoom = 0;i < room;i++) {
        if (newRoom.ersects(otherRoom)) {
            failed = true;
            break;
        }
    }
    if (!failed) {
        // local function to carve out new room
        createRoom(newRoom);
 
        // store center for new room
        newCenter = newRoom.center;
 
        if(rooms.length != 0){
            // store center of previous room
            var prevCenter = rooms[rooms.length - 1].center;
 
            // carve out corridors between rooms based on centers
            // randomly start with horizontal or vertical corridors
            if (Std.random(2) == 1) {
                hCorridor(Std.(prevCenter.x), Std.(newCenter.x),
                    Std.(prevCenter.y));
                vCorridor(Std.(prevCenter.y), Std.(newCenter.y),
                    Std.(newCenter.x));
            } else {
                vCorridor(Std.(prevCenter.y), Std.(newCenter.y),
                    Std.(prevCenter.x));
                hCorridor(Std.(prevCenter.x), Std.(newCenter.x),
                    Std.(newCenter.y));
                }
            }
        }
    if(!failed) rooms.push(newRoom);
    }
}

private function hCorridor(x1:, x2:, y) {
        for (x in Std.(Math.min(x1, x2))...Std.(Math.max(x1, x2)) + 1) {
            // destory the tiles to "carve" out corridor
            map[x][y].parent.removeChild(map[x][y]);
 
            // place a new unblocked tile
            map[x][y] = new Tile(Tile.DARK_GROUND, false, false);
 
            // add tile as a new game object
            addChild(map[x][y]);
 
            // set the location of the tile appropriately
            map[x][y].setLoc(x, y);
        }
    }
 
    // create vertical corridor to connect rooms
    private function vCorridor(y1:, y2:, x) {
        for (y in Std.(Math.min(y1, y2))...Std.(Math.max(y1, y2)) + 1) {
            // destroy the tiles to "carve" out corridor
            map[x][y].parent.removeChild(map[x][y]);
 
            // place a new unblocked tile
            map[x][y] = new Tile(Tile.DARK_GROUND, false, false);
 
            // add tile as a new game object
            addChild(map[x][y]);
 
            // set the location of the tile appropriately
            map[x][y].setLoc(x, y);
        }
    }