cstoquer / pixelbox

A sandbox framework to fast-prototype tile-based games in HTML5 and JavaScript
MIT License
469 stars 36 forks source link

Use TileMap functions without loading the engine? #13

Closed GoodClover closed 4 years ago

GoodClover commented 4 years ago

Hi, I'm creating a simple server with node.js and socket.io where there can be multiple players and they walk around. The TileMaps were stored client-side and I want them to be server side and they are sent to the player.

I've downloaded pixelbox on the server with npm bit it appears to export nothing and doing var TileMap = require("./node_modules/pixelbox/TileMap") doesn't appear to work either. Is there any way to use the TileMaps without creating a separate 'game' that hosts the server?

My code is as follows:

var rawMaps = fs.readFileSync(__dirname + "/client/assets/maps.json", "utf8")
var mapsJSON = JSON.parse(rawMaps)
var maps = {}
for (map of mapsJSON.maps) {
    maps[map.name] = new TileMap(20, 18)
    maps[map.name].load(map)
}

also tried maps[map.name] = new TileMap().load(map) and maps[map.name] = TileMap.load(map) bit it appears to export nothing

Edit: formatting

cstoquer commented 4 years ago

This is a use case scenario I did not thought about.

I made small modifications to allow this. Please update pixelbox to 2.0.1 in your package.json and reinstall.

You should then be able to do the following:

var TileMap = require("pixelbox/TileMap")

var rawMaps = fs.readFileSync(__dirname + "/client/assets/maps.json", "utf8")
var mapsJSON = JSON.parse(rawMaps)

TileMap.loadBank(mapsJSON)
var map = TileMap.getMap('map')

Please tell me if that resolve your issue.

GoodClover commented 4 years ago

The server appears to work now, I just had to ensure that the "sheet" property was set to "" in maps.json or it would error out trying to find the tilesheet as it isn't in the default location. This dosen't matter for me as the server doesn't need this tilesheet anyways.

I assume this is my code not working now but on the client I get this:

Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)'
    at n.draw (index.js:1)
    at window.draw (index.js:1)
    at Object.i.update (index.js:1)
    at t (index.js:1)

I'm not quite sure what causes this, but I guess it may be the tilesheet not being loaded? The code is checking that the map isn't null before attempting to draw it. Will look into it more when I get time.

GoodClover commented 4 years ago

I have it working now have to add .copy() in the server and .paste() in the client as the TileMap looses its ability to work once sent to the client.