summaryrefslogtreecommitdiff
path: root/models/crack.js
blob: d8944bd84524eb5404dbdb6098b195956b0aa540 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

var Pos = require("models/pos").Pos;
var Entity = require("models/entity").Entity;
var classUtils = require("utils/class");

function Crack(pos, username) {
    Entity.call(this, pos);
    this.username = username;
    var tempDate = new Date();
    this.expirationTime = tempDate.getTime() + 250;
}
classUtils.setParentClass(Crack, Entity);

module.exports = {
    Crack: Crack
}

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;
var BLOCK_TILE_AMOUNT = tempResource.BLOCK_TILE_AMOUNT;
var EMPTY_TILE = tempResource.EMPTY_TILE;
var TELEPORTER_START_TILE = tempResource.TELEPORTER_START_TILE;
var TELEPORTER_TILE_AMOUNT = tempResource.TELEPORTER_TILE_AMOUNT;

Crack.prototype.tick = function() {
    Entity.prototype.tick.call(this);
    var tempDate = new Date();
    if (tempDate.getTime() >= this.expirationTime) {
        this.giveTileToPlayer();
        this.remove();
    }
}

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) ||
           tempTile == 0x9b;
    if (!isAcceptableBlock) return;
    var tempPlayer = gameUtils.getPlayerByUsername(this.username);
    if (tempPlayer === null) {
        return;
    }
    chunkUtils.setTile(this.pos, EMPTY_TILE);
    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);
        tempPlayer.inventory.hasChanged = true;
    } else {
        tempPlayer.inventory.incrementTileCount(tempTile);
    }
}

Crack.prototype.getClientInfo = function() {
    return {
        className: "Crack",
        id: this.id,
        pos: this.pos.toJson()
    }
}