mirror of
https://github.com/jwinterm/rotsock.git
synced 2026-10-23 06:38:35 +02:00
rebase to coding cookies style
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
Glyph = function(chr, foreground, background) {
|
||||
// Instantiate properties to default if they weren't passed
|
||||
this._char = chr || ' ';
|
||||
this._foreground = foreground || 'white';
|
||||
this._background = background || 'black';
|
||||
};
|
||||
|
||||
// Create standard getters for glyphs
|
||||
Glyph.prototype.getChar = function(){
|
||||
return this._char;
|
||||
}
|
||||
Glyph.prototype.getBackground = function(){
|
||||
return this._background;
|
||||
}
|
||||
Glyph.prototype.getForeground = function(){
|
||||
return this._foreground;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
Glyph,
|
||||
};
|
||||
@@ -0,0 +1,72 @@
|
||||
// map.js
|
||||
// =========
|
||||
var ROT = require('rot-js');
|
||||
var Glyph = require('./glyph.js');
|
||||
var Tile = require('./tile.js');
|
||||
|
||||
var w = 80;
|
||||
var h = 40;
|
||||
|
||||
|
||||
function Map(tiles) {
|
||||
this._tiles = tiles;
|
||||
// cache the width and height based
|
||||
// on the length of the dimensions of
|
||||
// the tiles array
|
||||
this._width = tiles.length;
|
||||
this._height = tiles[0].length;
|
||||
};
|
||||
|
||||
// Standard getters
|
||||
Map.prototype.getWidth = function() {
|
||||
return this._width;
|
||||
};
|
||||
Map.prototype.getHeight = function() {
|
||||
return this._height;
|
||||
};
|
||||
|
||||
// Gets the tile for a given coordinate set
|
||||
Map.prototype.getTile = function(x, y) {
|
||||
// Make sure we are inside the bounds. If we aren't, return
|
||||
// null tile.
|
||||
if (x < 0 || x >= this._width || y < 0 || y >= this._height) {
|
||||
return Tile.nullTile;
|
||||
} else {
|
||||
return this._tiles[x][y] || Tile.nullTile;
|
||||
}
|
||||
};
|
||||
|
||||
Map.prototype.consoleDisplay = function() {
|
||||
for (var x = 0; x < this.getWidth(); x++) {
|
||||
for (var y = 0; y < this.getHeight(); y++) {
|
||||
console.log(this.getTile(x, y));
|
||||
}
|
||||
console.log('\n');
|
||||
}
|
||||
}
|
||||
|
||||
function createArena(w, h) {
|
||||
var tilearray = []
|
||||
for (var x = 0; x < w; x++) {
|
||||
tilearray.push([]);
|
||||
for (var y = 0; y < h; y++) {
|
||||
tilearray[x].push(Tile.Tile.nullTile);
|
||||
}
|
||||
}
|
||||
console.log(tilearray);
|
||||
var generator = new ROT.Map.Arena(w, h);
|
||||
generator.create(function(x, y, v) {
|
||||
if (v === 1) {
|
||||
tilearray[x][y] = Tile.Tile.floorTile;
|
||||
} else {
|
||||
tilearray[x][y] = Tile.Tile.wallTile;
|
||||
}
|
||||
});
|
||||
return new Map(tilearray);
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
Map,
|
||||
createArena,
|
||||
};
|
||||
@@ -0,0 +1,58 @@
|
||||
var express = require('express');
|
||||
var app = express();
|
||||
var http = require('http').Server(app);
|
||||
var io = require('socket.io')(http);
|
||||
var ROT = require('rot-js');
|
||||
|
||||
|
||||
app.use(express.static(__dirname)); // Current directory is root
|
||||
|
||||
// var Players = [];
|
||||
|
||||
var Game = {
|
||||
screenW: 80,
|
||||
screenH: 25,
|
||||
mapW: 8,
|
||||
mapH: 8,
|
||||
}
|
||||
|
||||
|
||||
app.get('/', function(req, res){
|
||||
res.sendFile(__dirname + '/index.html');
|
||||
});
|
||||
|
||||
|
||||
io.on('connection', function(socket){
|
||||
var id = socket.id;
|
||||
var ip = socket.request.connection.remoteAddress;
|
||||
var player = new Player('jw', 'addy', id, ip)
|
||||
console.log("Client connected with id %s and ip %s".format(id, ip));
|
||||
//Players.push(new Player('jw', 'addy', id, ip));
|
||||
|
||||
io.emit('welcome', "You are now connected.");
|
||||
console.log("Player id is %s".format(player.id));
|
||||
//console.log(Players[0].sendData);
|
||||
|
||||
socket.on('info', function(data){
|
||||
console.log(data);
|
||||
player.nick = data.split(',')[0];
|
||||
player.addy = data.split(',')[1];
|
||||
io.emit('display', JSON.stringify(player.sendData));
|
||||
})
|
||||
|
||||
socket.on('key', function(key) {
|
||||
var dir = null;
|
||||
if (dir) {
|
||||
console.log(dir);
|
||||
var moveResult = player.tryMove(dir);
|
||||
//console.log(moveResult);
|
||||
socket.emit('display', JSON.stringify(moveResult[0]));
|
||||
socket.emit('move_message', moveResult[1]);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
http.listen(8001, function(){
|
||||
console.log('listening on *:8001');
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
// Player.js
|
||||
// ============
|
||||
|
||||
|
||||
function Player(nick, addy, id, ip) {
|
||||
// Setup the bookkeeping stuff
|
||||
this._nick = nick || "Anon";
|
||||
this._addy = addy || "MLaMWLvFJMai3czhrK6peenVT2S2hFgmUi";
|
||||
this._id = id;
|
||||
this._ip = ip;
|
||||
this._jointime = Date.now();
|
||||
this._x = 0;
|
||||
this._y = 0;
|
||||
return this;
|
||||
};
|
||||
|
||||
Player.prototype.getNick = function() {
|
||||
return this._nick;
|
||||
}
|
||||
|
||||
Player.prototype.processInput = function(key) {
|
||||
if (key == "VK_K" || key == "VK_NUMPAD8" || key == "VK_UP") {
|
||||
dir = ROT.DIRS["8"][0];
|
||||
}
|
||||
else if (key == "VK_U" || key == "VK_NUMPAD9") {
|
||||
dir = ROT.DIRS["8"][1];
|
||||
}
|
||||
else if (key == "VK_L" || key == "VK_NUMPAD6" || key == "VK_RIGHT") {
|
||||
dir = ROT.DIRS["8"][2];
|
||||
}
|
||||
else if (key == "VK_N" || key == "VK_NUMPAD3") {
|
||||
dir = ROT.DIRS["8"][3];
|
||||
}
|
||||
else if (key == "VK_J" || key == "VK_NUMPAD2" || key == "VK_DOWN") {
|
||||
dir = ROT.DIRS["8"][4];
|
||||
}
|
||||
else if (key == "VK_B" || key == "VK_NUMPAD1") {
|
||||
dir = ROT.DIRS["8"][5];
|
||||
}
|
||||
else if (key == "VK_H" || key == "VK_NUMPAD4" || key == "VK_LEFT") {
|
||||
dir = ROT.DIRS["8"][6];
|
||||
}
|
||||
else if (key == "VK_Y" || key == "VK_NUMPAD7") {
|
||||
dir = ROT.DIRS["8"][7];
|
||||
}
|
||||
else {
|
||||
dir = null;
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
Player,
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
|
||||
|
||||
|
||||
function Player (nick, addy, id, ip) {
|
||||
// Setup the bookkeeping stuff
|
||||
this.nick = nick || "Anon";
|
||||
this.addy = addy || "MLaMWLvFJMai3czhrK6peenVT2S2hFgmUi";
|
||||
this.id = id;
|
||||
this.ip = ip;
|
||||
this.starttime = Date.now();
|
||||
|
||||
// Setup the full map
|
||||
this.map = new ROT.Map.Arena(Game.mapW, Game.mapH);
|
||||
// this.map = new ROT.Map.Rogue(Game.mapW, Game.mapH);
|
||||
this.mapData = {};
|
||||
this.fovData = {};
|
||||
this.sendData = {};
|
||||
this.mapgen = function(map, mapData) {
|
||||
var mapCallback = function(x, y, value) {
|
||||
mapData[x+','+y] = value;
|
||||
}
|
||||
map.create(mapCallback);
|
||||
}
|
||||
this.mapgen(this.map, this.mapData);
|
||||
//console.log(this.mapData);
|
||||
|
||||
// Create the escape, orbs, and enemies
|
||||
this.entities = {};
|
||||
|
||||
// Setup the player initial position
|
||||
while (true) {
|
||||
var x = Math.floor(ROT.RNG.getUniform()*Game.mapW);
|
||||
var y = Math.floor(ROT.RNG.getUniform()*Game.mapH);
|
||||
if (this.mapData[x+','+y] == 0) {
|
||||
this.player_x = x;
|
||||
this.player_y = y;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Set the entities
|
||||
while (true) {
|
||||
var x = Math.floor(ROT.RNG.getUniform()*Game.mapW);
|
||||
var y = Math.floor(ROT.RNG.getUniform()*Game.mapH);
|
||||
if (this.mapData[x+','+y] == 0
|
||||
&& !(this.player_x == x && this.player_y == y )) {
|
||||
this.escape = x+','+y;
|
||||
console.log("This escape is: " + this.escape);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Create list to store orbs
|
||||
for (i=0; i<8; i++) {
|
||||
while (true) {
|
||||
var x = Math.floor(ROT.RNG.getUniform()*Game.mapW);
|
||||
var y = Math.floor(ROT.RNG.getUniform()*Game.mapH);
|
||||
if (this.mapData[x+','+y] == 0
|
||||
&& !(this.player_x == x && this.player_y == y )) {
|
||||
this.escape = x+','+y;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// this.entities['2,2'] = 'o'
|
||||
|
||||
// Setup the FOV and portion of the map to send to Client
|
||||
this.fovcalc = function(player_x, player_y, mapData, fovData, entities) {
|
||||
/* input callback */
|
||||
var lightPasses = function(x, y) {
|
||||
var key = x+','+y;
|
||||
if (key in mapData) {
|
||||
// console.log(key, mapData[key], mapData[key] == 0);
|
||||
return (mapData[key] === 0);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
var fov = new ROT.FOV.PreciseShadowcasting(lightPasses);
|
||||
/* output callback */
|
||||
fov.compute(player_x, player_y, 7, function(x, y, r, visibility) {
|
||||
// console.log(x, y, r, visibility);
|
||||
var key = x+','+y;
|
||||
var ch = (r ? "" : "@");
|
||||
if (r == 0) {ch = '@'}
|
||||
else if (mapData[key] == 0) {ch = '.'}
|
||||
else if (mapData[key] == 1) {ch = '#'}
|
||||
var color = ROT.Color.interpolate([242, 255, 92], [30, 30, 60], (r/7)*visibility);
|
||||
fovData[key] = {glyph: ch, color: color};
|
||||
if (key == this.escape) {
|
||||
fovData[key] = {glyph: '<', color: ROT.Color.fromString("fff")}
|
||||
}
|
||||
// display.draw(x, y, ch, "#fff", color);
|
||||
console.log(key, this.escape, fovData[key]);
|
||||
});
|
||||
}
|
||||
this.fovcalc(this.player_x, this.player_y, this.mapData,
|
||||
this.fovData, this.entities);
|
||||
|
||||
this.fovToSend = function (player_x, player_y, fovData, sendData) {
|
||||
var keys = Object.keys(fovData);
|
||||
var shift_x = 20-player_x;
|
||||
var shift_y = 10-player_y;
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
var x = parseInt(keys[i].split(',')[0]);
|
||||
var y = parseInt(keys[i].split(',')[1]);
|
||||
// console.log(fovData[x+','+y]);
|
||||
sendData[(x+shift_x)+','+(y+shift_y)] = fovData[x+','+y];
|
||||
}
|
||||
// console.log(sendData);
|
||||
}
|
||||
this.fovToSend(this.player_x, this.player_y, this.fovData, this.sendData);
|
||||
//console.log(this.mapData);
|
||||
|
||||
this.tryMove = function (dir) {
|
||||
var delta_x = dir[0];
|
||||
var delta_y = dir[1];
|
||||
var new_x = this.player_x + delta_x;
|
||||
var new_y = this.player_y + delta_y;
|
||||
if (this.mapData[new_x+','+new_y] == 0) {
|
||||
this.fovData = {};
|
||||
this.sendData = {};
|
||||
this.player_x = new_x;
|
||||
this.player_y = new_y;
|
||||
this.fovcalc(this.player_x, this.player_y, this.mapData, this.fovData, this.entities);
|
||||
this.fovToSend(this.player_x, this.player_y, this.fovData, this.sendData);
|
||||
console.log(this.fovToSend);
|
||||
var sendMessage = "Player has moved %s".format(dir);
|
||||
}
|
||||
else {var sendMessage = "Can't move there!"}
|
||||
return [this.sendData, sendMessage];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// var playerLookup = function (players, id) {
|
||||
// for (var i = 0; i < players.length; i++) {
|
||||
// if (players[i].id == id) {
|
||||
// return players[i];
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
Executable
+5519
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
var Glyph = require('./glyph.js');
|
||||
|
||||
Tile = function(glyph) {
|
||||
this._glyph = glyph;
|
||||
};
|
||||
|
||||
Tile.prototype.getGlyph = function() {
|
||||
return this._glyph;
|
||||
};
|
||||
|
||||
Tile.nullTile = new Tile(new Glyph.Glyph());
|
||||
Tile.floorTile = new Tile(new Glyph.Glyph('.'));
|
||||
Tile.wallTile = new Tile(new Glyph.Glyph('#', 'goldenrod'));
|
||||
|
||||
|
||||
module.exports = {
|
||||
Tile,
|
||||
};
|
||||
Reference in New Issue
Block a user