Skip to content

Commit

Permalink
Merge pull request #3 from Br4ggs/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Br4ggs authored Dec 9, 2020
2 parents e13af99 + 08caeb3 commit 8e089ff
Show file tree
Hide file tree
Showing 12 changed files with 453 additions and 73 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
Rogue written in javascript to get more familiar with the language

Playable demo [here!](https://br4ggs.github.io/JSrogue/)

Trello board [here!](https://trello.com/b/0nokBzgm)
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<script src="src/generatorController.js"></script>
<script src="src/IOController.js"></script>
<script src="src/uiRenderer.js"></script>
<script src="src/item.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
Expand Down
23 changes: 16 additions & 7 deletions src/IOController.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var uiState;
var xCursorPos;
var yCursorPos;

function initialize() {
function ioInit() {
document.onkeydown = function (keyEvent) {
var key = String.fromCharCode(keyEvent.keyCode);
if (keyMap.has(key)) {
Expand All @@ -19,8 +19,6 @@ function initialize() {
setMoveMode();
}

window.addEventListener('load', initialize);

function registerKey(key, func, descr) {
keyMap.set(key, func);
keyDescriptions.set(key, descr);
Expand Down Expand Up @@ -89,13 +87,18 @@ function moveCursor(yDir, xDir) {
drawDisplay();
}

//TODO: rework chests into simple items laying on the ground
//you can pick them up manually but they will automatically be picked up as soon as you walk over them

//TODO: this could be turned into a tryMoveAction object
function tryPlayerMove(yDir, xDir) {
refreshPathing = false;

var result = layer3Generator.moveEntity(yDir, xDir);
if (result) {
if (layer2Generator.isOccupied(layer3Generator.player.yPos, layer3Generator.player.xPos)) {
const obj = layer2Generator.getObject(layer3Generator.player.yPos, layer3Generator.player.xPos);
if (obj instanceof Item) {
playerInteract(layer3Generator.player.yPos, layer3Generator.player.xPos);
}
}
moveGoblins();
drawDisplay();
drawHealthIndicator();
Expand Down Expand Up @@ -123,13 +126,17 @@ function tryPlayerMove(yDir, xDir) {
}

function playerInspect(yPos, xPos) {
refreshPathing = false;

var result = layer3Generator.inspect(yPos, xPos);
if (!result) {
writeToConsole("Nothing to see here...");
}
}

function playerInteract(yPos, xPos) {
refreshPathing = false;

if (distance(layer3Generator.player.yPos, layer3Generator.player.xPos, yPos, xPos) > 1.5) {
writeToConsole("That object is too far away...");
return;
Expand All @@ -146,6 +153,8 @@ function playerInteract(yPos, xPos) {
}

function playerAttack(yPos, xPos) {
refreshPathing = false;

if (distance(layer3Generator.player.yPos, layer3Generator.player.xPos, yPos, xPos) > 1.5) {
writeToConsole("That is too far away...");
return;
Expand All @@ -168,7 +177,7 @@ function showInventory() {

function gameOver() {
clearMap();
registerKey('R', () => null, "Restart");
registerKey('R', () => restart(), "Restart");
writeToConsole("You died, press R to restart");
drawDisplay();
drawHealthIndicator();
Expand Down
2 changes: 2 additions & 0 deletions src/chest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//deprecated, do not use

var Chest = function (yPos, xPos, item) {
this.yPos = yPos;
this.xPos = xPos;
Expand Down
5 changes: 5 additions & 0 deletions src/door.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ Door.prototype.interact = function () {
this.open = !this.open;

writeToConsole("You " + (this.open ? "open" : "close") + " the door.");
refreshPathing = true;
};

Door.prototype.isOpaque = function () {
return !this.open
};
33 changes: 28 additions & 5 deletions src/generatorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ var layer1Generator = new Layer1Generator();
var layer2Generator = new Layer2Generator();
var layer3Generator = new Layer3Generator();

var refreshPathing = false;

/**
* Checks whether the given tile is traverseable by entities.
* @param {int} y The row of the tile.
* @param {int} x The column of the tile.
* @returns {boolean} Whether this tile is traverseable or not.
*/
function isTraverseable(y, x) {
if (y < 0 || y > layer1Generator.rows || x < 0 || x > layer1Generator.columns)
if (y < 0 || y > layer1Generator.rows - 1 || x < 0 || x > layer1Generator.columns - 1)
return false;

if (layer2Generator.isOccupied(y, x)) {
Expand All @@ -34,6 +36,20 @@ function isTraverseable(y, x) {
return layer1Generator.grid[y][x].symbol === '.';
}

function isOpaque(y, x) {
if (y < 0 || y > layer1Generator.rows - 1 || x < 0 || x > layer1Generator.columns - 1)
return false;

if (layer2Generator.isOccupied(y, x)) {
var object = layer2Generator.getObject(y, x);
if (object instanceof Door) {
return object.isOpaque();
}
}

return layer1Generator.grid[y][x].symbol === '#';
}

/**
* Initial function that generates the maze.
* Gets called when the window is completely loaded.
Expand All @@ -43,26 +59,33 @@ function generateLevel() {
layer2Generator.generateLayer();
layer3Generator.generateLayer();

uiInit();
ioInit();
drawDisplay();
drawHealthIndicator();
}

//Really lazy but this is gonna get overhauled anyway
function nextLevel() {
const playerData = layer3Generator.player;
console.log(playerData);
layer1Generator = new Layer1Generator();
layer2Generator = new Layer2Generator();
layer3Generator = new Layer3Generator();

generateLevel();
console.log(playerData);


layer3Generator.player.id = playerData.id;
layer3Generator.player.health = playerData.health;
layer3Generator.player.gold = playerData.gold;
writeToConsole("The staircase locks behind you.")
writeToConsole("The staircase locks behind you.");
}

function restart() {
layer1Generator = new Layer1Generator();
layer2Generator = new Layer2Generator();
layer3Generator = new Layer3Generator();
generateLevel();
setMoveMode();
}

window.addEventListener('load', generateLevel);
55 changes: 42 additions & 13 deletions src/goblin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ var Goblin = function(yPos,xPos) {
this.health = this.maxHealth;
this.attackPwr = 1;

this.chasingPlayer = false;
this.roamTargetY = yPos;
this.roamTargetX = xPos;

this.setNewRoamTarget();
this.pathToRoamTarget = this.getPathTo(this.roamTargetY, this.roamTargetX);

this.blockCounter = 0;
this.maxBlockCounter = 3;
}
Expand Down Expand Up @@ -96,32 +100,59 @@ Goblin.prototype.getReachableTiles = function(yPos, xPos) {
}

Goblin.prototype.act = function() {
const {yPos, xPos} = layer3Generator.player;
let path = this.getPathTo(yPos, xPos);
let nextStep = path[[this.yPos, this.xPos]];
let path;
let nextStep;

if(refreshPathing)
{
const {yPos, xPos} = layer3Generator.player;
path = this.getPathTo(yPos, xPos);
nextStep = path[[this.yPos, this.xPos]];
if(nextStep === undefined)
{
this.chasingPlayer = false;
}
else
{
this.chasingPlayer = true;
}

if(!this.chasingPlayer)
{
this.pathToRoamTarget = this.getPathTo(this.roamTargetY, this.roamTargetX);
nextStep = this.pathToRoamTarget[[this.yPos, this.xPos]];
if(nextStep === undefined)
{
this.setNewRoamTarget();
this.pathToRoamTarget = this.getPathTo(this.roamTargetY, this.roamTargetX);
}
}
}

if(this.chasingPlayer)
{
const {yPos, xPos} = layer3Generator.player;
path = this.getPathTo(yPos, xPos); //this only needs to be checked when a player opens/closes a door
nextStep = path[[this.yPos, this.xPos]];

if(nextStep !== undefined) {
if(distance(this.yPos, this.xPos, layer3Generator.player.yPos, layer3Generator.player.xPos) === 1){
layer3Generator.attack(this, layer3Generator.player.yPos, layer3Generator.player.xPos);
}
else if (!layer3Generator.isOccupied(nextStep.yPos, nextStep.xPos)){
({yPos : this.yPos, xPos : this.xPos} = nextStep);
}
}
else {
console.log("cannot find path to player");
// OR getpathto target returns undefined
//TODO: this path should only be calculated once
path = this.getPathTo(this.roamTargetY, this.roamTargetX);
nextStep = path[[this.yPos, this.xPos]];
else
{
nextStep = this.pathToRoamTarget[[this.yPos, this.xPos]];

if(distance(this.yPos, this.xPos, this.roamTargetY, this.roamTargetX) <= 1 || nextStep === undefined || this.blockCounter >= this.maxBlockCounter) {
this.blockCounter = 0;
this.setNewRoamTarget();
this.pathToRoamTarget = this.getPathTo(this.roamTargetY, this.roamTargetX);
}
else if(layer3Generator.isOccupied(nextStep.yPos, nextStep.xPos)) {
this.blockCounter++;
console.log("added to counter");
}
else {
({yPos : this.yPos, xPos : this.xPos} = nextStep);
Expand All @@ -131,7 +162,5 @@ Goblin.prototype.act = function() {

//TODO: move to ai actor class or something?
function moveGoblins() {
console.log("doot");
layer3Generator.goblins.forEach(goblin => goblin.act());
drawDisplay();
}
54 changes: 54 additions & 0 deletions src/item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var Item = function (yPos, xPos, desc) {
this.id = generateId();
this.desc = desc;
this.yPos = yPos;
this.xPos = xPos;
//this.action = function
};

Item.prototype.inspect = function () {
writeToConsole(this.desc);
};

Item.prototype.interact = function () {
this.function();
layer2Generator.removeObject(this);
}

//How do i get all item variables and functions in item instead of potion
var Potion = function (yPos, xPos, hp) {
Item.call(this, yPos, xPos, "a health potion healing " + hp + " hitpoints.");
this.hp = hp;
this.function = () => {
writeToConsole("you pick up the potion");
writeToConsole(layer3Generator.healPlayer(this.hp));
};
};

Potion.prototype = Object.create(Item.prototype);
Potion.prototype.constructor = Potion;


var GoldSack = function (yPos, xPos, amount) {
Item.call(this, yPos, xPos, "a sack of gold containing " + amount + " gold.");
this.amount = amount;
this.function = () => {
writeToConsole("you pick up the goldsack");
writeToConsole(layer3Generator.addPlayerGold(this.amount));
}
}

GoldSack.prototype = Object.create(Item.prototype);
GoldSack.prototype.constructor = GoldSack;


var Key = function (yPos, xPos) {
Item.call(this, yPos, xPos, "the key to the next floor.");
this.function = () => {
writeToConsole("you pick up the key");
writeToConsole(layer3Generator.setPlayerKey(true));
}
}

Key.prototype = Object.create(Item.prototype);
Key.prototype.constructor = Key;
Loading

0 comments on commit 8e089ff

Please sign in to comment.