diff options
| author | Matt Kohls <mattkohls13@gmail.com> | 2019-12-09 23:27:55 -0500 | 
|---|---|---|
| committer | Matt Kohls <mattkohls13@gmail.com> | 2019-12-09 23:27:55 -0500 | 
| commit | 421d3cee6dfad58fd48c2684da7494e730a16830 (patch) | |
| tree | aa88ef6301ade2b1fbaf873e7a374a20e2947a38 | |
| parent | c8a5d3a7597fc5b943a04e13aa8f3b01afdb3c48 (diff) | |
| download | rogue.js-421d3cee6dfad58fd48c2684da7494e730a16830.tar.gz rogue.js-421d3cee6dfad58fd48c2684da7494e730a16830.tar.bz2 rogue.js-421d3cee6dfad58fd48c2684da7494e730a16830.zip | |
| -rw-r--r-- | public/index.html | 2 | ||||
| -rw-r--r-- | public/js/rogue.js | 25 | ||||
| -rw-r--r-- | rogue-server.js | 87 | 
3 files changed, 101 insertions, 13 deletions
| diff --git a/public/index.html b/public/index.html index 47bcec0..5369ebd 100644 --- a/public/index.html +++ b/public/index.html @@ -16,7 +16,7 @@  	<canvas id="gameboard" class="center" width="512" height="416"></canvas>  	<div class="center">  	  <table class="center"> -		<tr><td><button id="joingame" type="button" title="Join Game">Join Game</button></td><td></td><td><button id="up">Up</button></td><td></td><td></td> +		<tr><td><button id="joingame" type="button" title="Join Game">Join Game</button></td><td></td><td><button id="up">Up</button></td><td></td><td><button id="wait">Wait</button></td>  		</tr>  		<tr><td><button id="leavegame" type="button" title="Leave Game">Leave Game</button></td><td><button id="left">Left</button></td><td><button id="stairs">Stairs</button></td><td><button id="right">Right</button></td><td></td>  		</tr> diff --git a/public/js/rogue.js b/public/js/rogue.js index 3564103..bd14c21 100644 --- a/public/js/rogue.js +++ b/public/js/rogue.js @@ -142,6 +142,12 @@ function parseMessage(message) {  	}  	drawMap();  	drawPlayerInfo(); +	if(msg.msg === "You exit the dungeon, wielding THE STAFF!") { +		drawMessageBox(scroll, "Congrats! You win!", 8, 19); +	} +	if(player.hp <= 0) { +		drawMessageBox(tomb, "G a m e   O v e r", 8, 19); +	}  }  joinGame.onclick = function() { @@ -226,6 +232,15 @@ right.onclick = function() {      websocket.send("{\"action\":\"move\", \"message\":\"d\"}");  } +wait.onclick = function() { +	if (!websocket) { +		showMessage('Connection error'); +		return; +    } + +    websocket.send("{\"action\":\"wait\", \"message\":\" \"}"); +} +  const canvas = document.getElementById('gameboard');  const context = canvas.getContext('2d');  const spritemap = new Image(320, 1264); @@ -397,13 +412,9 @@ function drawPlayerInfo() {  	renderText(player.staff.toString(), 31 - boxWidth + 5, 13);  	renderText(player.armor.toString(), 31 - boxWidth + 5, 14);  	renderText(player.potions.toString(), 31 - boxWidth + 5, 15); - -	if(player.hp <= 0) { -		drawGameOver(); -	}  } -function drawGameOver() { +function drawMessageBox(sprite, message, x, y) {  	for(var i = 0; i < 14; i++) {  		for(var j = 0; j < 5; j++) {  			drawSprite(nothing, 5 + i, 17 + j, false); @@ -419,8 +430,8 @@ function drawGameOver() {  	drawSprite(border[0], 19, 17, false);  	drawSprite(border[3], 5, 21, false);  	drawSprite(border[2], 19, 21, false); -	drawSprite(tomb, 11, 13, false); -	renderText("G a m e   O v e r", 8, 19); +	drawSprite(sprite, 11, 13, false); +	renderText(message, x, y);  	if(websocket) {  		showMessage('Disconnecting from game'); diff --git a/rogue-server.js b/rogue-server.js index 717669b..95aaffa 100644 --- a/rogue-server.js +++ b/rogue-server.js @@ -298,6 +298,7 @@ class Mob {  /** Turn generation **/ +var npc = [0, 0, 0]; // Number of NPCs per floor (more will be allowed on lower floors)  var mobs = [];  var dead = [];  const floors = require('./floors.json'); @@ -391,6 +392,8 @@ function move(mob, dir) {  			}  			mobs[mob].location = new Location(floors.stairs[newfloor].dx, floors.stairs[newfloor].dy, newfloor);  			return "Go up the stairs"; +		} else { +			return "You don't see any stairs here";  		}  		break;  	default: @@ -428,7 +431,7 @@ function performActions(init) {  				if(mobs[mob].action == null) {  					message = "You sit";  				} -				if(typeof mobs[mob].action !== 'undefined' || mobs[mob] != null) { +				if(mobs[mob] != null && typeof mobs[mob].action !== 'undefined') {  					switch(mobs[mob].action.type) {  					case 'wait':  						message = "You sit"; @@ -509,11 +512,79 @@ function sendResults() {   */  function cleanDungeon() {  	for(var i = 0; i < dead.length; i++) { +		if(mobs[dead[i]] != null && mobs[dead[i]].type !== 'undefined' && mobs[dead[i]].type !== "player") { +			npc[mobs[dead[i]].location.floor] = npc[mobs[dead[i]].location.floor] - 1; +		}  		mobs.splice(dead[i], 1);   	}  }  /** + * Gives non-player character actions + */ +function moveNPC(mob) { +	if(mobs[mob] != null && mobs[mob].type !== 'undefined') { +		var action = null; +		switch(mobs[mob].type) { +		case "slime": +			action = new Action("wait", ""); +			break; +		case "zombie": +			switch(Math.floor(Math.random() * 5)) { +			case 0: +				action = new Action("wait", ""); +				break; +			case 1: +				action = new Action("move", "w"); +				break; +			case 2: +				action = new Action("move", "a"); +				break; +			case 3: +				action = new Action("move", "s"); +				break; +			case 4: +				action = new Action("move", "d"); +				break; +			} +			break; +		default: +			mobs[mob].action = new Action("wait", ""); +			break; +		} +		mobs[mob].action = action; +	} +} + +/** + * Spawns in new non-player characters on floors that have room for them + */ +function spawnNPC() { +	for(var level = 0; level < floors.maps.length; level++) { +		if(npc[level] < 15 + level * 5 && Math.floor(Math.random() * 6) > 3) { +			// Find a spawn location that is not a wall +			var x = Math.floor(Math.random() * 100); +			var y = Math.floor(Math.random() * 40); +			while(floors.maps[level].charAt(y * 100 + x) == '*') { +				x = Math.floor(Math.random() * 100); +				y = Math.floor(Math.random() * 40); +			} +			// Decide what type of NPC to spawn +			var temp = Math.floor(Math.random() * 6); +			var type = "slime"; +			if(temp > 2) { +				type = "zombie"; +			} +			var local = new Location(x, y, level); +			var monster = new Mob(genStat(), genStat(), genStat(), genStat(), genStat(), 1, local, uuid(), type); +			mobs.push(monster); +			npc[level] += 1; +			console.log(new Date().toUTCString() + ' | new monster spawned: ' + type + " floor: " + level); +		} +	} +} + +/**   * All the steps that need to be taken in a turn   */  function genTurn() { @@ -522,13 +593,19 @@ function genTurn() {  	}  	console.log(new Date().toUTCString() + ' | generating turn...');  	dead = new Array(); +	for(var i = 0; i < mobs.length; i++) { +		if(mobs[i] != null && mobs[i].type !== 'undefined' && mobs[i].type != "player") { +			moveNPC(i); +		} +	} +	spawnNPC();  	var init = getInititive();  	performActions(init);  	sendResults();  	cleanDungeon();  	console.log(new Date().toUTCString() + ' | turn generation done'); -	if(mobs.length > 0) { -		clock = setInterval(genTurn, 30000); +	if(players.size > 0) { +		clock = setInterval(genTurn, 5000);  		console.log(new Date().toUTCString() + ' | resetting clock...');  	}  } @@ -592,7 +669,7 @@ wsServer.on('connection', function connection(ws, request) {  		genTurn();  		if(clock == null) {  			console.log(new Date().toUTCString() + ' | starting clock...'); -			clock = setInterval(genTurn, 30000); +			clock = setInterval(genTurn, 5000);  		}  	}); @@ -602,7 +679,7 @@ wsServer.on('connection', function connection(ws, request) {  		players.delete(id);  		device.delete(ws);  		for(var i = 0; i < mobs.length; i++) { -			if(mobs[i].uuid = id) { +			if(mobs[i].uuid == id) {  				dead.push(i);  			}  		} | 
