From f60fe309df255cfaea2b64ddaa64c16deb3bcc9f Mon Sep 17 00:00:00 2001 From: munvoseli Date: Sun, 18 Jun 2023 19:36:43 -0400 Subject: start of tele --- models/chunk.js | 13 +++++- models/crack.js | 23 +++++++--- public/images/sprites.png | Bin 1629 -> 2547 bytes public/javascript/game.js | 5 ++- tiles.txt | 46 ++++++++++++++++++++ utils/game.js | 17 ++++++++ utils/tele.js | 108 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 205 insertions(+), 7 deletions(-) create mode 100644 tiles.txt create mode 100644 utils/tele.js diff --git a/models/chunk.js b/models/chunk.js index 0ed0ee7..49059b1 100644 --- a/models/chunk.js +++ b/models/chunk.js @@ -68,6 +68,7 @@ var entityList = require("models/entity").entityList; var classUtils = require("utils/class"); var chunkUtils = require("utils/chunk"); var gameUtils = require("utils/game"); +let doorUtils = require("utils/tele"); var tempTerrainOffsetSet1 = [ new Pos(0, -1), @@ -267,7 +268,17 @@ Chunk.prototype.generateAllTiles = function() { this.addRestZone(); } if (Math.random() < 1 / 36) { - this.addTeleporter(); + this.addTeleporter(); + } + for (let i = 0; i < 30; i++) { + var tempPos = new Pos( + Math.floor(Math.random() * chunkSize), + Math.floor(Math.random() * chunkSize) + ); + tempPos.add(this.pos); + this.setTile(tempPos, 0x9b); + let id = doorUtils.registerDoor(tempPos); + console.log("Put door #" + id + " at " + tempPos.toString()); } chunkUtils.persistAllChunks(); } diff --git a/models/crack.js b/models/crack.js index 3f352f2..15ee833 100644 --- a/models/crack.js +++ b/models/crack.js @@ -18,6 +18,7 @@ module.exports = { var accountUtils = require("utils/account"); var gameUtils = require("utils/game"); var chunkUtils = require("utils/chunk"); +let doorUtils = require("utils/tele"); var tempResource = require("models/chunk"); var BLOCK_START_TILE = tempResource.BLOCK_START_TILE; @@ -37,16 +38,28 @@ Crack.prototype.tick = function() { Crack.prototype.giveTileToPlayer = function() { var tempTile = chunkUtils.getTile(this.pos); - var isAcceptableBlock = (tempTile >= BLOCK_START_TILE && tempTile < BLOCK_START_TILE + BLOCK_TILE_AMOUNT) || - (tempTile >= TELEPORTER_START_TILE && tempTile < TELEPORTER_START_TILE + TELEPORTER_TILE_AMOUNT); - if (!isAcceptableBlock) - return; + var isAcceptableBlock = + ( tempTile >= BLOCK_START_TILE && + tempTile < BLOCK_START_TILE + BLOCK_TILE_AMOUNT) || + ( tempTile >= TELEPORTER_START_TILE && + tempTile < TELEPORTER_START_TILE + TELEPORTER_TILE_AMOUNT) || + tempTile == 0x9b; + if (!isAcceptableBlock) return; var tempPlayer = gameUtils.getPlayerByUsername(this.username); if (tempPlayer === null) { return; } chunkUtils.setTile(this.pos, EMPTY_TILE); - tempPlayer.inventory.incrementTileCount(tempTile); + if (tempTile == 0x9b) { // teleporter + const doorid = doorUtils.findDoorIdWithPos(this.pos); + if (doorid === null) console.error("crack.js: door doesn't exist here"); + doorUtils.moveDoorToPlayer( + doorid, + tempPlayer.inventory, + tempPlayer.username); + } else { + tempPlayer.inventory.incrementTileCount(tempTile); + } } Crack.prototype.getClientInfo = function() { diff --git a/public/images/sprites.png b/public/images/sprites.png index e552f4d..2d760f5 100644 Binary files a/public/images/sprites.png and b/public/images/sprites.png differ diff --git a/public/javascript/game.js b/public/javascript/game.js index 57b3eb5..518f08e 100644 --- a/public/javascript/game.js +++ b/public/javascript/game.js @@ -746,7 +746,7 @@ Player.prototype.placeOrRemoveTile = function(direction) { var tempPos = this.getPosInWalkDirection(direction); var tempTile = getTileBufferValue(tempPos); if ((tempTile >= blockStartTile && tempTile < blockStartTile + blockTileAmount) - || (tempTile >= teleporterStartTile && tempTile < teleporterStartTile + teleporterTileAmount)) { + || (tempTile >= teleporterStartTile && tempTile < teleporterStartTile + teleporterTileAmount + 1)) { this.removeTile(direction); } if ((tempTile >= flourTile && tempTile <= breadTile) @@ -1116,6 +1116,9 @@ function drawTileOnContext(context, pos, size, which) { if (which >= teleporterStartTile && which < teleporterStartTile + teleporterTileAmount) { drawSpriteOnContext(context, pos, size, which - teleporterStartTile + 18); } + if (which == 0x9b) { + drawSpriteOnContext(context, pos, size, which - teleporterStartTile + 18); + } if (which >= symbolStartTile && which < symbolStartTile + symbolTileAmount) { drawSpriteOnContext(context, pos, size, which - (symbolStartTile - 1) + 80); } diff --git a/tiles.txt b/tiles.txt new file mode 100644 index 0000000..cdefc37 --- /dev/null +++ b/tiles.txt @@ -0,0 +1,46 @@ +ranges are inclusive + +0x00 +null tile +not supposed to exist +sometimes exists accidentally + +0x21 - 0x7e +symbol tiles +onstep: +onact: collect + +0x80 +empty tile +onstep: turn into trail + +0x81 - 0x88 +solid block tiles +onstep: NO +onact: add crack + +0x89 - 0x90 +trail tiles +onstep: turn into trail +onact: + +0x91 - 0x94 +flour, water, baking powder, bread +onstep: turn into trail+collect +onact: collect + +0x95 - 0x96 +oven, hospital +onstep: NO +onact: +onadjacent: bake, heal + +0x97 - 0x9a +tele north, east, south, west (standard bq direction order) +onstep: teleport 2000 blocks directionally +onact: add crack + +0x9b +linked teleporter (accompanied by entity) +onstep: teleport +onact: add crack diff --git a/utils/game.js b/utils/game.js index 4f91131..22bfa13 100644 --- a/utils/game.js +++ b/utils/game.js @@ -35,6 +35,7 @@ var Enemy = require("models/enemy").Enemy; var classUtils = require("utils/class.js"); var accountUtils = require("utils/account.js"); var chunkUtils = require("utils/chunk.js"); +let doorUtils = require("utils/tele"); GameUtils.prototype.getPlayerByUsername = function(username) { var index = 0; @@ -205,6 +206,9 @@ GameUtils.prototype.performUpdate = function(username, commandList, done) { if (tempCommand.commandName == "placeTile") { performPlaceTileCommand(tempCommand, tempPlayer, tempCommandList); } + if (tempCommand.commandName == "placeDoor") { + performPlaceDoorCommand(tempCommand, tempPlayer, tempCommandList); + } if (tempCommand.commandName == "collectTile") { performCollectTileCommand(tempCommand, tempPlayer, tempCommandList); } @@ -480,6 +484,18 @@ function performPlaceTileCommand(command, player, commandList) { } } +function performPlaceDoorCommand(command, player, commandList) { + if (!isDirection(command.direction)) + return; + const d = command.doorId; + const pos = player.getPosInWalkDirection(command.direction); + const curt = chunkUtils.getTile(pos); + if (!player.canPlaceOnTile(curt)) return; + if (!doorUtils.removeDoorFromInventory(player.inventory, d)) return; + doorUtils.placeDoor(d, pos); + addSetInventoryCommand(player.inventory, commandList); +} + function performCollectTileCommand(command, player, commandList) { if (isDirection(command.direction)) player.collectTile(command.direction); @@ -534,6 +550,7 @@ GameUtils.prototype.persistEverything = function(done) { } this.isPersistingEverything = true; chunkUtils.persistAllChunks(); + doorUtils.persistAllDoors(); var self = this; var index = 0; function persistNextEntity() { diff --git a/utils/tele.js b/utils/tele.js new file mode 100644 index 0000000..6794af2 --- /dev/null +++ b/utils/tele.js @@ -0,0 +1,108 @@ +// this file deals with linked teleporters, +// not directional teleporters + + + +/* +the corresponding tile is 0x9b + + +generate +[x] make new entry in global door registry +[x] set tile 0x9b +0x9b crackend +[x] put item in inventory +place +[x] set tile 0x9b +[x] remove from inventory +inventory UI +[ ] +0x9b onstep +[ ] +go through first time +[ ] get new coord +[ ] generate new chunk +[ ] generate new tele +*/ + +const doorPath = "doors.txt"; + + + +let fs = require("fs"); +let doors; { + try { + let data = fs.readFileSync(doorPath, 'utf8', function(e, data) { + if (e) console.log('ladsjflajksfj'); + console.log(data); + }); + doors = JSON.parse(data.toString()); + } catch (e) { + doors = []; + } +} +let dirty = false; + +let doorUtils = {}; +module.exports = doorUtils; + +let Pos, createPosFromJson; { + let t = require("models/pos"); + Pos = t.Pos; + createPosFromJson = t.createPosFromJson; +} +let chunkUtils = require("utils/chunk.js"); + +doorUtils.moveDoorToPlayer = function(doorid, inventory, username) { + let door = doors[doorid]; + door.postype = "player"; + door.pos = username; + let m = inventory.tileCountMap[0x9b]; + if (!m) inventory.tileCountMap[0x9b] = [doorid]; + else m.push(doorid); +}; + +doorUtils.findDoorIdWithPos = function(pos) { + for (let i = 0; i < doors.length; i++) { + if (doors[i].postype != "coordinate") continue; + if (pos.equals(doors[i].pos)) { + return i; + } + } + return null; +}; + +doorUtils.removeDoorFromInventory = function(inv, doorid) { + let m = inv.tileCountMap[0x9b]; + for (let i = 0; i < m.length; i++) { + if (m[i] === doorid) { + m.splice(i, 1); + return true; + } + } + return false; +}; + +doorUtils.placeDoor = function(doorid, pos) { + chunkUtils.setTile(pos, 0x9b); + doors[doorid].postype = "coordinate"; + doors[doorid].pos = pos; +}; + +doorUtils.registerDoor = function(pos) { + let doorId = doors.length; + doors.push({ + id: doorId, + postype: "coordinate", + pos: pos, + destId: null + }); + return doorId; +}; + + + +doorUtils.persistAllDoors = function() { + fs.writeFileSync(doorPath, JSON.stringify(doors)); + dirty = false; +}; -- cgit v1.2.3