Skip to content

Commit

Permalink
v4: Oddly Shaped Rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
VolatileCoder committed Sep 29, 2024
1 parent 04100af commit c2569ef
Show file tree
Hide file tree
Showing 13 changed files with 882 additions and 21 deletions.
4 changes: 4 additions & 0 deletions assets/js/engine/Math.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ VC.Math = class {
return rangeMax - (percentage * (rangeMax-rangeMin));
}

static random(min, max){
return Math.floor(Math.random() * (max - min +1)) + min;
}

}
24 changes: 24 additions & 0 deletions assets/js/engine/Point.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//REQUIRES VC

VC.Point = class {
#element = null;
x = 0;
y = 0;
constructor (x,y){
this.x = x;
this.y = y;
}
render(screen){
if (this.#element){
this.remove();
}
this.#element = screen.drawRect(this.x-1,this.y-1, 3, 3, "#fff","#000",0);
screen.onClear(this.remove)
}
remove(){
if(this.#element){
this.#element.remove();
this.#element = null;
}
}
}
92 changes: 92 additions & 0 deletions assets/js/engine/Triangle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//REQUIRES VC
VC.Triangle = class {

#p1 = new VC.Point(0,0);
#p2 = new VC.Point(0,0);
#p3 = new VC.Point(0,0);
#elements = []
constructor(p1, p2, p3){
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}

get p1() {
return this.#p1;
}
set p1(value){
if(!(value instanceof VC.Point)){
throw ("VC.Point expected!")
}
this.#p1 = value;
}

get p2() {
return this.#p2;
}
set p2(value){
if(!(value instanceof VC.Point)){
throw ("VC.Point expected!")
}
this.#p2 = value;
}

get p3() {
return this.#p3;
}
set p3(value){
if(!(value instanceof VC.Point)){
throw ("VC.Point expected!")
}
this.#p3 = value;
}

render(screen){
if(this.#elements.length>0){
this.remove();
}
this.p1.render(screen);
this.p2.render(screen);
this.p3.render(screen);
this.#elements.push(screen.drawLine(this.p1.x, this.p1.y, this.p2.x, this.p2.y, "#00F", 1));
this.#elements.push(screen.drawLine(this.p2.x, this.p2.y, this.p3.x, this.p3.y ,"#00F", 1));
this.#elements.push(screen.drawLine(this.p3.x, this.p3.y, this.p1.x, this.p1.y, "#00F", 1));
screen.onClear(this.remove)
}

remove(){
if(this.#elements.length>0){
this.p1.remove();
this.p2.remove();
this.p3.remove();
this.#elements.forEach((element)=>element.remove());
this.#elements = [];
}
}

contains(obj){
if(obj instanceof VC.Point) {
var d1 = this.#sign(obj, this.p1, this.p2);
var d2 = this.#sign(obj, this.p2, this.p3);
var d3 = this.#sign(obj, this.p3, this.p1);

var has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0);
var has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0);

return !(has_neg && has_pos);
}
if (obj instanceof VC.Box){
return this.contains(new VC.Point(obj.x, obj.y)) && this.contains(new VC.Point(obj.x + obj.width, obj.y)) && this.contains(new VC.Point(obj.x + obj.width, obj.y + obj.height)) && this.contains(new VC.Point(obj.x, obj.y + obj.height))
}

if (obj instanceof VC.Triangle){
return this.contains(obj.p1) && this.contains(obj.p2) && this.contains(obj.p3)
}
return false;
}

#sign (p1,p2,p3)
{
return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
}
}
3 changes: 3 additions & 0 deletions assets/js/engine/Trig.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ VC.Trig = class {
return Math.tan(radians);
}
static pointToAngle(opposite, adjacent){
if(adjacent<0){
return Math.PI + Math.atan(opposite/adjacent);
}
return Math.atan(opposite/adjacent);
}
static distance (x1, y1, x2, y2){
Expand Down
2 changes: 2 additions & 0 deletions assets/js/goldruin/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Game extends VC.Game {
this.player.tntCount = 5;

this.currentScene = new TitleScreen(this.screen, this.infoScreen);
//this.currentScene = new PolygonalRoom(0,0,dimensions.width-200, dimensions.width-200, 5);
//this.currentScene.finalize()
this.currentScene.preDisplay();
}

Expand Down
8 changes: 4 additions & 4 deletions assets/js/goldruin/level/BossLevelFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class BossLevelFactory {
var currentPalette = LevelFactory.getWorldPalette(level.world);
var nextPalette = LevelFactory.getWorldPalette(level.world + 1);

var startingRoom = level.getRoom(0,0, 7 * constants.brickWidth, 10 * constants.brickWidth);
var startingRoom = level.getRoom(0,0, 7 * constants.brickWidth, 10 * constants.brickWidth, null, true);
startingRoom.palette = currentPalette;

//startingRoom.box.width = 7 * constants.brickWidth;
Expand All @@ -31,7 +31,7 @@ class BossLevelFactory {

}

var bossRoom = level.getRoom(0,-1, 13 * constants.brickWidth, 13 * constants.brickWidth, 7);
var bossRoom = level.getRoom(0,-1, 13 * constants.brickWidth, 13 * constants.brickWidth, 7, true);
bossRoom.isBossRoom = true;

//TODO:change depending on boss
Expand All @@ -48,7 +48,7 @@ class BossLevelFactory {
rewardDoor.stabilize();
bossRoom.doors = [entranceDoor, rewardDoor];

var treasureRoom = level.getRoom(0,-2, 5 * constants.brickWidth, 5 * constants.brickWidth);
var treasureRoom = level.getRoom(0,-2, 5 * constants.brickWidth, 5 * constants.brickWidth, null, true);
treasureRoom.palette = nextPalette;
var chest = new TreasureChest(treasureRoom, Treasure.HEARTCONTAINER);
chest.box.x = treasureRoom.box.width/2 + treasureRoom.box.x - chest.box.width/2;
Expand All @@ -60,7 +60,7 @@ class BossLevelFactory {
exitDoor.stabilize();
treasureRoom.doors = [entranceDoor, exitDoor];

var exitRoom = level.getRoom(0,-3, 5 * constants.brickWidth, 10 * constants.brickWidth);
var exitRoom = level.getRoom(0,-3, 5 * constants.brickWidth, 10 * constants.brickWidth, null, true);
var exit = new Exit(exitRoom);
exitRoom.palette = nextPalette;

Expand Down
31 changes: 23 additions & 8 deletions assets/js/goldruin/level/Level.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ class Level extends VC.Scene {

set currentRoom(nextRoom){
if(this.#currentRoom != nextRoom){
if(this.#currentRoom != null && this.#currentRoom instanceof Room){
if(this.#currentRoom != null && (this.#currentRoom instanceof Room || this.#currentRoom instanceof PolygonalRoom)){
this.#currentRoom.doors.forEach((d)=>{
var n = this.findNeighbor(this.#currentRoom, d.wall);
if(n && n instanceof Room){
if(n && (n instanceof Room || n instanceof PolygonalRoom)){
n.volume = 0;
}
})
}
this.#currentRoom = nextRoom;
if(this.#currentRoom != null && this.#currentRoom instanceof Room){
if(this.#currentRoom != null && (this.#currentRoom instanceof Room || this.#currentRoom instanceof PolygonalRoom)){
this.#currentRoom.doors.forEach((d)=>{
var n = this.findNeighbor(this.#currentRoom, d.wall);
if(n && n instanceof Room){
if(n && (n instanceof Room || n instanceof PolygonalRoom)){
n.volume = .25;
switch(d.wall){
case Direction.EAST:
Expand Down Expand Up @@ -115,7 +115,7 @@ class Level extends VC.Scene {
var pc = game.player.box.center();
this.currentRoom.doors.forEach((d)=>{
var n = this.findNeighbor(this.currentRoom, d.wall);
if(n && n instanceof Room){
if(n && (n instanceof Room || n instanceof PolygonalRoom)){
if(d.isEntrance){
n.volume = 0;
return
Expand All @@ -129,7 +129,7 @@ class Level extends VC.Scene {
d = VC.Math.constrain(0, d, maxDistance);

n.volume = VC.Math.inversePercentToRange(d/maxDistance, .1, .66);
console.log(n.volume);

n.preRender(deltaT);
}
});
Expand Down Expand Up @@ -213,10 +213,24 @@ class Level extends VC.Scene {
}
}

getRoom(x, y, w, h, wallHeightInBricks){
getRoom(x, y, w, h, wallHeightInBricks, forceSquare){
var foundRoom = this.findRoom(x,y);
if(foundRoom) return foundRoom;
var room = new Room(x, y, w, h, wallHeightInBricks)
if (!w && !h) {
w = Math.round((((constants.roomMaxWidthInBricks - constants.roomMinWidthInBricks) * Math.random()) + constants.roomMinWidthInBricks)) * constants.brickWidth;
h = Math.round((((constants.roomMaxHeightInBricks - constants.roomMinHeightInBricks) * Math.random()) + constants.roomMinHeightInBricks)) * constants.brickWidth;
}
var room = null;
if ((w * h) < 160000){
room = new Room(x, y, w, h, wallHeightInBricks)
} else {
if(VC.Math.random(0,2) == 0 && !forceSquare){
room = new PolygonalRoom(x, y, w, h, wallHeightInBricks)
}else {
//room = new PolygonalRoom(x, y, w, h, wallHeightInBricks)
room = new Room(x, y, w, h, wallHeightInBricks)
}
}
room.palette = this.palette;
this.rooms.push(room);
return room;
Expand Down Expand Up @@ -253,6 +267,7 @@ class Level extends VC.Scene {
openNextRoom(direction){
var nextRoom = game.level.findNeighbor(this.currentRoom, direction);
if(nextRoom.opened){
console.log("Room Area:", nextRoom.box.width * nextRoom.box.height)
nextRoom.visited = 1;
game.player.room = nextRoom;
this.currentRoom = nextRoom;
Expand Down
13 changes: 8 additions & 5 deletions assets/js/goldruin/level/LevelFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,16 @@ class LevelFactory {
var extent = extents[direction];
switch(direction){
case Direction.NORTH:
exitRoom = level.getRoom(extent.x, extent.y - 1);
exitRoom = level.getRoom(extent.x, extent.y - 1, null, null, null, true);
break;
case Direction.EAST:
exitRoom = level.getRoom(extent.x + 1, extent.y);
exitRoom = level.getRoom(extent.x + 1, extent.y, null, null, null, true);
break;
case Direction.SOUTH:
exitRoom = level.getRoom(extent.x, extent.y + 1);
exitRoom = level.getRoom(extent.x, extent.y + 1, null, null, null, true);
break;
case Direction.WEST:
exitRoom = level.getRoom(extent.x - 1, extent.y);
exitRoom = level.getRoom(extent.x - 1, extent.y, null, null, null, true);
break;
}
//regionRooms.push(entrance);
Expand Down Expand Up @@ -275,7 +275,10 @@ class LevelFactory {
}


level.rooms.forEach((r)=>{r.doors.forEach((d)=>d.stabilize())});
level.rooms.forEach((r)=>{
r.doors.forEach((d)=>d.stabilize())
r.finalize();
});
//set exit
var exit = new Exit(exitRoom);
exit.box.x = exitRoom.box.x + exitRoom.box.width/2 - exit.box.width/2
Expand Down
Loading

0 comments on commit c2569ef

Please sign in to comment.