From 19c15b3138146473f040c5b07772e66972a402b5 Mon Sep 17 00:00:00 2001 From: jwinterm Date: Mon, 16 Apr 2018 09:42:35 -0700 Subject: [PATCH] Update with new stuff --- index.html | 2 +- server.js | 4 +-- serverjs/entities.js | 51 ++++++++++++++++++++++++++++ serverjs/entity.js | 79 ++++++++++++++++++++++++++++++++++++++++++++ serverjs/glyph.js | 9 ++--- serverjs/map.js | 72 +++++++++++++++++++++++++++++++++++++--- serverjs/player.js | 6 ++-- serverjs/tile.js | 42 +++++++++++++++++------ 8 files changed, 241 insertions(+), 24 deletions(-) create mode 100644 serverjs/entities.js create mode 100644 serverjs/entity.js diff --git a/index.html b/index.html index d7f8e2d..218eec1 100755 --- a/index.html +++ b/index.html @@ -3,7 +3,7 @@ Socket.io Test - + diff --git a/server.js b/server.js index 45ef6a4..e24af5a 100755 --- a/server.js +++ b/server.js @@ -37,9 +37,9 @@ function findWithAttr(array, attr, value) { io.on('connection', function(socket){ var id = socket.id; 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)); - //Players.push(new Player('jw', 'addy', id, ip)); + map.consoleDisplay(); socket.on('info', function(data){ console.log(data); diff --git a/serverjs/entities.js b/serverjs/entities.js new file mode 100644 index 0000000..079e02d --- /dev/null +++ b/serverjs/entities.js @@ -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, +}; \ No newline at end of file diff --git a/serverjs/entity.js b/serverjs/entity.js new file mode 100644 index 0000000..d43fa90 --- /dev/null +++ b/serverjs/entity.js @@ -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, +}; \ No newline at end of file diff --git a/serverjs/glyph.js b/serverjs/glyph.js index ad0ea9b..d235b02 100644 --- a/serverjs/glyph.js +++ b/serverjs/glyph.js @@ -4,11 +4,12 @@ * and open the template in the editor. */ -Glyph = function(chr, foreground, background) { +Glyph = function(properties) { // Instantiate properties to default if they weren't passed - this._char = chr || ' '; - this._foreground = foreground || 'white'; - this._background = background || 'black'; + properties = properties || {}; + this._char = properties['character'] || ' '; + this._foreground = properties['foreground'] || 'white'; + this._background = properties['background'] || 'black'; }; // Create standard getters for glyphs diff --git a/serverjs/map.js b/serverjs/map.js index 90a106e..d4197b8 100644 --- a/serverjs/map.js +++ b/serverjs/map.js @@ -15,6 +15,7 @@ function Map(tiles) { // the tiles array this._width = tiles.length; this._height = tiles[0].length; + this._entities = []; }; // Standard getters @@ -24,7 +25,6 @@ Map.prototype.getWidth = function() { 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 @@ -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() { for (var x = 0; x < this.getWidth(); x++) { + var printString = ''; 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) { var tilearray = [] for (var x = 0; x < w; x++) { @@ -53,10 +115,10 @@ function createArena(w, h) { tilearray[x].push(Tile.Tile.nullTile); } } - console.log(tilearray); + //console.log(tilearray); var generator = new ROT.Map.Arena(w, h); generator.create(function(x, y, v) { - if (v === 1) { + if (v === 0) { tilearray[x][y] = Tile.Tile.floorTile; } else { tilearray[x][y] = Tile.Tile.wallTile; diff --git a/serverjs/player.js b/serverjs/player.js index ee78f0e..cc9b04f 100644 --- a/serverjs/player.js +++ b/serverjs/player.js @@ -1,8 +1,9 @@ // Player.js // ============ - +var Entity = require('./entity.js'); +var Entities = require('./entities.js') -function Player(nick, addy, id, ip) { +function Player(nick, addy, id, ip, map) { // Setup the bookkeeping stuff this._nick = nick || "Anon"; this._addy = addy || "MLaMWLvFJMai3czhrK6peenVT2S2hFgmUi"; @@ -11,6 +12,7 @@ function Player(nick, addy, id, ip) { this._jointime = Date.now(); this._x = 0; this._y = 0; + map.addEntityAtRandomPosition(new Entity.Entity(Entities.PlayerTemplate)); return this; }; diff --git a/serverjs/tile.js b/serverjs/tile.js index d99843b..d744983 100644 --- a/serverjs/tile.js +++ b/serverjs/tile.js @@ -5,19 +5,41 @@ */ var Glyph = require('./glyph.js'); -Tile = function(glyph) { - this._glyph = glyph; +Tile = function(properties) { + 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() { - 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')); +// Standard getters +Tile.prototype.isWalkable = function() { + return this._walkable; +} +Tile.prototype.isDiggable = function() { + return this._diggable; +} +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 = { Tile, -}; +}; \ No newline at end of file