mirror of
https://github.com/jwinterm/rotsock.git
synced 2026-10-17 05:41:29 +02:00
Update with new stuff
This commit is contained in:
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
<head>
|
<head>
|
||||||
<title>Socket.io Test</title>
|
<title>Socket.io Test</title>
|
||||||
<link rel="stylesheet" type="text/css" href="css/style.css">
|
<link rel="stylesheet" type="text/css" href="css/style.css">
|
||||||
<script src="node_modules/socket.io/socket.io.js"></script>
|
<script src="node_modules/socket.io/node_modules/socket.io-client/socket.io.js"></script>
|
||||||
<script src="clientjs/game.js"></script>
|
<script src="clientjs/game.js"></script>
|
||||||
<script src="clientjs/rot.js"></script>
|
<script src="clientjs/rot.js"></script>
|
||||||
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
|
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
|
||||||
|
|||||||
@@ -37,9 +37,9 @@ function findWithAttr(array, attr, value) {
|
|||||||
io.on('connection', function(socket){
|
io.on('connection', function(socket){
|
||||||
var id = socket.id;
|
var id = socket.id;
|
||||||
var ip = socket.request.connection.remoteAddress;
|
var ip = socket.request.connection.remoteAddress;
|
||||||
Game.Players.push(Player.createPlayer('jw', 'addy', id, ip));
|
Game.Players.push(Player.Player('jw', 'addy', id, ip, map));
|
||||||
console.log("Client connected with id %s and ip %s".format(id, ip));
|
console.log("Client connected with id %s and ip %s".format(id, ip));
|
||||||
//Players.push(new Player('jw', 'addy', id, ip));
|
map.consoleDisplay();
|
||||||
|
|
||||||
socket.on('info', function(data){
|
socket.on('info', function(data){
|
||||||
console.log(data);
|
console.log(data);
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
// Create our Mixins namespace
|
||||||
|
Mixins = {};
|
||||||
|
|
||||||
|
// Define our Moveable mixin
|
||||||
|
Mixins.Moveable = {
|
||||||
|
name: 'Moveable',
|
||||||
|
tryMove: function(x, y, map) {
|
||||||
|
var tile = map.getTile(x, y);
|
||||||
|
// Check if we can walk on the tile
|
||||||
|
// and if so simply walk onto it
|
||||||
|
if (tile.isWalkable()) {
|
||||||
|
// Update the entity's position
|
||||||
|
this._x = x;
|
||||||
|
this._y = y;
|
||||||
|
return true;
|
||||||
|
// Check if the tile is diggable, and
|
||||||
|
// if so try to dig it
|
||||||
|
} else if (tile.isDiggable()) {
|
||||||
|
map.dig(x, y);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Main player's actor mixin
|
||||||
|
Mixins.PlayerActor = {
|
||||||
|
name: 'PlayerActor',
|
||||||
|
groupName: 'Actor',
|
||||||
|
act: function() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Player template
|
||||||
|
PlayerTemplate = {
|
||||||
|
character: '@',
|
||||||
|
foreground: 'white',
|
||||||
|
background: 'black',
|
||||||
|
mixins: [Mixins.Moveable, Mixins.PlayerActor],
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
Mixins,
|
||||||
|
PlayerTemplate,
|
||||||
|
};
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* 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');
|
||||||
|
|
||||||
|
|
||||||
|
Entity = function(properties) {
|
||||||
|
properties = properties || {};
|
||||||
|
// Call the glyph's construtor with our set of properties
|
||||||
|
Glyph.Glyph.call(this, properties);
|
||||||
|
// Instantiate any properties from the passed object
|
||||||
|
this._name = properties['name'] || '';
|
||||||
|
this._x = properties['x'] || 0;
|
||||||
|
this._y = properties['y'] || 0;
|
||||||
|
this._map = null;
|
||||||
|
// Create an object which will keep track what mixins we have
|
||||||
|
// attached to this entity based on the name property
|
||||||
|
this._attachedMixins = {};
|
||||||
|
// Setup the object's mixins
|
||||||
|
var mixins = properties['mixins'] || [];
|
||||||
|
for (var i = 0; i < mixins.length; i++) {
|
||||||
|
// Copy over all properties from each mixin as long
|
||||||
|
// as it's not the name or the init property. We
|
||||||
|
// also make sure not to override a property that
|
||||||
|
// already exists on the entity.
|
||||||
|
for (var key in mixins[i]) {
|
||||||
|
if (key != 'init' && key != 'name' && !this.hasOwnProperty(key)) {
|
||||||
|
this[key] = mixins[i][key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Add the name of this mixin to our attached mixins
|
||||||
|
this._attachedMixins[mixins[i].name] = true;
|
||||||
|
// Finally call the init function if there is one
|
||||||
|
if (mixins[i].init) {
|
||||||
|
mixins[i].init.call(this, properties);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Make entities inherit all the functionality from glyphs
|
||||||
|
Entity.extend(Glyph.Glyph);
|
||||||
|
|
||||||
|
Entity.prototype.setName = function(name) {
|
||||||
|
this._name = name;
|
||||||
|
}
|
||||||
|
Entity.prototype.setX = function(x) {
|
||||||
|
this._x = x;
|
||||||
|
}
|
||||||
|
Entity.prototype.setY = function(y) {
|
||||||
|
this._y = y;
|
||||||
|
}
|
||||||
|
Entity.prototype.getName = function() {
|
||||||
|
return this._name;
|
||||||
|
}
|
||||||
|
Entity.prototype.getX = function() {
|
||||||
|
return this._x;
|
||||||
|
}
|
||||||
|
Entity.prototype.getY = function() {
|
||||||
|
return this._y;
|
||||||
|
}
|
||||||
|
Entity.prototype.setMap = function(map) {
|
||||||
|
this._map = map;
|
||||||
|
}
|
||||||
|
Entity.prototype.getMap = function() {
|
||||||
|
return this._map;
|
||||||
|
}
|
||||||
|
Entity.prototype.hasMixin = function(obj) {
|
||||||
|
// Allow passing the mixin itself or the name / group name as a string
|
||||||
|
if (typeof obj === 'object') {
|
||||||
|
return this._attachedMixins[obj.name];
|
||||||
|
} else {
|
||||||
|
return this._attachedMixins[obj] || this._attachedMixinGroups[obj];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
Entity,
|
||||||
|
};
|
||||||
+5
-4
@@ -4,11 +4,12 @@
|
|||||||
* and open the template in the editor.
|
* and open the template in the editor.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Glyph = function(chr, foreground, background) {
|
Glyph = function(properties) {
|
||||||
// Instantiate properties to default if they weren't passed
|
// Instantiate properties to default if they weren't passed
|
||||||
this._char = chr || ' ';
|
properties = properties || {};
|
||||||
this._foreground = foreground || 'white';
|
this._char = properties['character'] || ' ';
|
||||||
this._background = background || 'black';
|
this._foreground = properties['foreground'] || 'white';
|
||||||
|
this._background = properties['background'] || 'black';
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create standard getters for glyphs
|
// Create standard getters for glyphs
|
||||||
|
|||||||
+67
-5
@@ -15,6 +15,7 @@ function Map(tiles) {
|
|||||||
// the tiles array
|
// the tiles array
|
||||||
this._width = tiles.length;
|
this._width = tiles.length;
|
||||||
this._height = tiles[0].length;
|
this._height = tiles[0].length;
|
||||||
|
this._entities = [];
|
||||||
};
|
};
|
||||||
|
|
||||||
// Standard getters
|
// Standard getters
|
||||||
@@ -24,7 +25,6 @@ Map.prototype.getWidth = function() {
|
|||||||
Map.prototype.getHeight = function() {
|
Map.prototype.getHeight = function() {
|
||||||
return this._height;
|
return this._height;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Gets the tile for a given coordinate set
|
// Gets the tile for a given coordinate set
|
||||||
Map.prototype.getTile = function(x, y) {
|
Map.prototype.getTile = function(x, y) {
|
||||||
// Make sure we are inside the bounds. If we aren't, return
|
// Make sure we are inside the bounds. If we aren't, return
|
||||||
@@ -36,15 +36,77 @@ Map.prototype.getTile = function(x, y) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Map.prototype.getEntities = function() {
|
||||||
|
return this._entities;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map.prototype.getEntityAt = function(x, y){
|
||||||
|
// Iterate through all entities searching for one with
|
||||||
|
// matching position
|
||||||
|
for (var i = 0; i < this._entities.length; i++) {
|
||||||
|
if (this._entities[i].getX() == x && this._entities[i].getY() == y) {
|
||||||
|
return this._entities[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map.prototype.addEntity = function(entity) {
|
||||||
|
// Make sure the entity's position is within bounds
|
||||||
|
if (entity.getX() < 0 || entity.getX() >= this._width ||
|
||||||
|
entity.getY() < 0 || entity.getY() >= this._height) {
|
||||||
|
throw new Error('Adding entity out of bounds.');
|
||||||
|
}
|
||||||
|
// Update the entity's map
|
||||||
|
entity.setMap(this);
|
||||||
|
// Add the entity to the list of entities
|
||||||
|
this._entities.push(entity);
|
||||||
|
// Check if this entity is an actor, and if so add
|
||||||
|
// them to the scheduler
|
||||||
|
//if (entity.hasMixin('Actor')) {
|
||||||
|
// this._scheduler.add(entity, true);
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
|
||||||
|
Map.prototype.addEntityAtRandomPosition = function(entity) {
|
||||||
|
var position = this.getRandomFloorPosition();
|
||||||
|
entity.setX(position.x);
|
||||||
|
entity.setY(position.y);
|
||||||
|
this.addEntity(entity);
|
||||||
|
}
|
||||||
|
|
||||||
Map.prototype.consoleDisplay = function() {
|
Map.prototype.consoleDisplay = function() {
|
||||||
for (var x = 0; x < this.getWidth(); x++) {
|
for (var x = 0; x < this.getWidth(); x++) {
|
||||||
|
var printString = '';
|
||||||
for (var y = 0; y < this.getHeight(); y++) {
|
for (var y = 0; y < this.getHeight(); y++) {
|
||||||
console.log(this.getTile(x, y));
|
if (this.getEntityAt(x, y)) {
|
||||||
|
printString += this.getEntityAt(x, y).getChar();
|
||||||
|
} else {
|
||||||
|
printString += this.getTile(x, y).getChar();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
console.log('\n');
|
console.log(printString);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Map.prototype.dig = function(x, y) {
|
||||||
|
// If the tile is diggable, update it to a floor
|
||||||
|
if (this.getTile(x, y).isDiggable()) {
|
||||||
|
this._tiles[x][y] = Tile.Tile.floorTile;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Map.prototype.getRandomFloorPosition = function() {
|
||||||
|
// Randomly generate a tile which is a floor
|
||||||
|
var x, y;
|
||||||
|
do {
|
||||||
|
x = Math.floor(Math.random() * this._width);
|
||||||
|
y = Math.floor(Math.random() * this._width);
|
||||||
|
} while(this.getTile(x, y) != Tile.Tile.floorTile ||
|
||||||
|
this.getEntityAt(x, y));
|
||||||
|
return {x: x, y: y};
|
||||||
|
}
|
||||||
|
|
||||||
function createArena(w, h) {
|
function createArena(w, h) {
|
||||||
var tilearray = []
|
var tilearray = []
|
||||||
for (var x = 0; x < w; x++) {
|
for (var x = 0; x < w; x++) {
|
||||||
@@ -53,10 +115,10 @@ function createArena(w, h) {
|
|||||||
tilearray[x].push(Tile.Tile.nullTile);
|
tilearray[x].push(Tile.Tile.nullTile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(tilearray);
|
//console.log(tilearray);
|
||||||
var generator = new ROT.Map.Arena(w, h);
|
var generator = new ROT.Map.Arena(w, h);
|
||||||
generator.create(function(x, y, v) {
|
generator.create(function(x, y, v) {
|
||||||
if (v === 1) {
|
if (v === 0) {
|
||||||
tilearray[x][y] = Tile.Tile.floorTile;
|
tilearray[x][y] = Tile.Tile.floorTile;
|
||||||
} else {
|
} else {
|
||||||
tilearray[x][y] = Tile.Tile.wallTile;
|
tilearray[x][y] = Tile.Tile.wallTile;
|
||||||
|
|||||||
+4
-2
@@ -1,8 +1,9 @@
|
|||||||
// Player.js
|
// Player.js
|
||||||
// ============
|
// ============
|
||||||
|
var Entity = require('./entity.js');
|
||||||
|
var Entities = require('./entities.js')
|
||||||
|
|
||||||
|
function Player(nick, addy, id, ip, map) {
|
||||||
function Player(nick, addy, id, ip) {
|
|
||||||
// Setup the bookkeeping stuff
|
// Setup the bookkeeping stuff
|
||||||
this._nick = nick || "Anon";
|
this._nick = nick || "Anon";
|
||||||
this._addy = addy || "MLaMWLvFJMai3czhrK6peenVT2S2hFgmUi";
|
this._addy = addy || "MLaMWLvFJMai3czhrK6peenVT2S2hFgmUi";
|
||||||
@@ -11,6 +12,7 @@ function Player(nick, addy, id, ip) {
|
|||||||
this._jointime = Date.now();
|
this._jointime = Date.now();
|
||||||
this._x = 0;
|
this._x = 0;
|
||||||
this._y = 0;
|
this._y = 0;
|
||||||
|
map.addEntityAtRandomPosition(new Entity.Entity(Entities.PlayerTemplate));
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+31
-9
@@ -5,18 +5,40 @@
|
|||||||
*/
|
*/
|
||||||
var Glyph = require('./glyph.js');
|
var Glyph = require('./glyph.js');
|
||||||
|
|
||||||
Tile = function(glyph) {
|
Tile = function(properties) {
|
||||||
this._glyph = glyph;
|
properties = properties || {};
|
||||||
|
// Call the Glyph constructor with our properties
|
||||||
|
Glyph.Glyph.call(this, properties);
|
||||||
|
// Set up the properties. We use false by default.
|
||||||
|
this._walkable = properties['walkable'] || false;
|
||||||
|
this._diggable = properties['diggable'] || false;
|
||||||
|
this._blocksLight = (properties['blocksLight'] !== undefined) ?
|
||||||
|
properties['blocksLight'] : true;
|
||||||
};
|
};
|
||||||
|
// Make tiles inherit all the functionality from glyphs
|
||||||
|
Tile.extend(Glyph.Glyph);
|
||||||
|
|
||||||
Tile.prototype.getGlyph = function() {
|
// Standard getters
|
||||||
return this._glyph;
|
Tile.prototype.isWalkable = function() {
|
||||||
};
|
return this._walkable;
|
||||||
|
}
|
||||||
Tile.nullTile = new Tile(new Glyph.Glyph());
|
Tile.prototype.isDiggable = function() {
|
||||||
Tile.floorTile = new Tile(new Glyph.Glyph('.'));
|
return this._diggable;
|
||||||
Tile.wallTile = new Tile(new Glyph.Glyph('#', 'goldenrod'));
|
}
|
||||||
|
Tile.prototype.isBlocksLight = function() {
|
||||||
|
return this._blocksLight;
|
||||||
|
}
|
||||||
|
|
||||||
|
Tile.nullTile = new Tile({})
|
||||||
|
Tile.floorTile = new Tile({
|
||||||
|
character: '.',
|
||||||
|
walkable: true
|
||||||
|
});
|
||||||
|
Tile.wallTile = new Tile({
|
||||||
|
character: '#',
|
||||||
|
foreground: 'goldenrod',
|
||||||
|
diggable: true
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
Tile,
|
Tile,
|
||||||
|
|||||||
Reference in New Issue
Block a user