summaryrefslogtreecommitdiff
path: root/models/player.js
blob: 659b708568f0fad24c047de4ab8f665c3b46448b (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395

var classUtils = require("utils/class");

var tempResource = require("models/pos");
var Pos = tempResource.Pos;
var createPosFromJson = tempResource.createPosFromJson;

var tempResource = require("models/entity");
var Entity = tempResource.Entity;
var entityList = tempResource.entityList;
var entityWalkOffsetList = tempResource.entityWalkOffsetList;

var tempResource = require("models/chatMessage");
var announceMessageInChat = tempResource.announceMessageInChat;
var getNextChatMessageId = tempResource.getNextChatMessageId;



function Player(account) {
    if ("respawnPos" in account) {
        this.respawnPos = createPosFromJson(account.respawnPos);
    } else {
        this.respawnPos = gameUtils.getNewPlayerRespawnPos();
    }
    this.respawnPosHasChanged = false;
    var tempPos;
    if ("pos" in account) {
        tempPos = createPosFromJson(account.pos);
    } else {
        tempPos = this.respawnPos.copy();
    }
    Entity.call(this, tempPos);
    this.username = account.username;
    this.avatar = account.avatar;
    this.avatarHasChanged = false;
    var tempDate = new Date();
    this.lastActivityTime = tempDate.getTime();
    this.lastChatMessageId = getNextChatMessageId() - 10;
    this.inventory = new Inventory(account);
    this.walkBudget = maximumWalkBudget;
    if ("health" in account) {
        this.health = account.health;
    } else {
        this.health = maximumPlayerHealth;
    }
    if ("guidelinePos" in account) {
        var tempData = account.guidelinePos;
        if (tempData === null) {
            this.guidelinePos = tempData;
        } else {
            this.guidelinePos = createPosFromJson(tempData);
        }
    } else {
        this.guidelinePos = null;
    }
    this.invincibilityDelay = 0;
    var tempEnemyList = gameUtils.getEntitiesByClassNearPos(Enemy, this.pos, 10);
    var index = 0;
    while (index < tempEnemyList.length) {
        var tempEnemy = tempEnemyList[index];
        if (gameUtils.isInDevelopmentMode) {
            console.log("Removing enemy at " + tempEnemy.pos.toString() + " near spawning player.");
        }
        tempEnemy.remove();
        index += 1;
    }
    announceMessageInChat(this.username + " has joined the game.");
}
classUtils.setParentClass(Player, Entity);

module.exports = {
    Player: Player
}

var Enemy = require("models/enemy").Enemy;
var Crack = require("models/crack").Crack;
var Inventory = require("models/inventory").Inventory;
var accountUtils = require("utils/account");
var gameUtils = require("utils/game");
var chunkUtils = require("utils/chunk");

var tempResource = require("models/chunk");
var EMPTY_TILE = tempResource.EMPTY_TILE;
var BLOCK_START_TILE = tempResource.BLOCK_START_TILE;
var BLOCK_TILE_AMOUNT = tempResource.BLOCK_TILE_AMOUNT;
var TRAIL_START_TILE = tempResource.TRAIL_START_TILE;
var TRAIL_TILE_AMOUNT = tempResource.TRAIL_TILE_AMOUNT;
var FLOUR_TILE = tempResource.FLOUR_TILE;
var WATER_TILE = tempResource.WATER_TILE;
var POWDER_TILE = tempResource.POWDER_TILE;
var BREAD_TILE = tempResource.BREAD_TILE;
var OVEN_TILE = tempResource.OVEN_TILE;
var HOSPITAL_TILE = tempResource.HOSPITAL_TILE;
var SYMBOL_START_TILE = tempResource.SYMBOL_START_TILE;
var SYMBOL_TILE_AMOUNT = tempResource.SYMBOL_TILE_AMOUNT;
var TELEPORTER_START_TILE = tempResource.TELEPORTER_START_TILE;
var TELEPORTER_TILE_AMOUNT = tempResource.TELEPORTER_TILE_AMOUNT;

var breadIngredientSet = [FLOUR_TILE, WATER_TILE, POWDER_TILE];

var maximumWalkBudget = 2 * gameUtils.framesPerSecond;
var maximumPlayerHealth = 5;

Player.prototype.setRespawnPos = function(pos) {
    this.respawnPos = pos.copy();
    this.respawnPosHasChanged = true;
}

Player.prototype.setAvatar = function(avatar) {
    this.avatar = avatar;
    this.avatarHasChanged = true;
}

Player.prototype.decrementNextItemCount = function(tileList) {
    var index = 0;
    while (index < tileList.length) {
        var tempTile = tileList[index];
        var tempResult = this.inventory.decrementTileCount(tempTile);
        if (tempResult) {
            return tempTile;
        }
        index += 1;
    }
    return null;
}

Player.prototype.dropItems = function(tileList) {
    var tempPos = new Pos(0, 0);
    var tempRadius = 0;
    while (true) {
        var tempHasExhaustedItems = false;
        var tempOffset = new Pos(-tempRadius, -tempRadius);
        while (tempOffset.y <= tempRadius) {
            var tempPos = this.pos.copy();
            tempPos.add(tempOffset);
            var tempTile = chunkUtils.getTile(tempPos);
            if (this.canPlaceOnTile(tempTile)) {
                var tempTile = this.decrementNextItemCount(tileList);
                if (tempTile === null) {
                    tempHasExhaustedItems = true;
                    break;
                }
                chunkUtils.setTile(tempPos, tempTile);
            }
            tempOffset.x += 1;
            if (tempOffset.x > tempRadius) {
                tempOffset.x = -tempRadius;
                tempOffset.y += 1;
            }
        }
        tempRadius += 1;
        if (tempHasExhaustedItems) {
            break;
        }
    }
}

Player.prototype.die = function() {
    this.dropItems([FLOUR_TILE, WATER_TILE, POWDER_TILE]);
    this.pos = this.respawnPos.copy();
    this.health = maximumPlayerHealth;
    announceMessageInChat(this.username + " has died.");
}

Player.prototype.isInvincible = function() {
    return (this.invincibilityDelay > 0);
}

Player.prototype.receiveDamage = function(amount) {
    if (this.isInvincible()) {
        return;
    }
    this.health -= amount;
    if (this.health <= 0) {
        this.die();
    }
    this.invincibilityDelay = 6 * gameUtils.framesPerSecond;
}

Player.prototype.tick = function() {
    Entity.prototype.tick.call(this);
    if (this.walkBudget < maximumWalkBudget) {
        this.walkBudget += 1;
    }
    if (this.invincibilityDelay > 0) {
        this.invincibilityDelay -= 1;
    }
    var tempCount = gameUtils.getEntityCountByClassNearPos(Enemy, this.pos, 0);
    if (tempCount >= 1) {
        this.receiveDamage(1);
    }
    var tempDate = new Date();
    var tempTime = tempDate.getTime();
    if (tempTime > this.lastActivityTime + 10 * 1000) {
        announceMessageInChat(this.username + " has left the game.");
        this.remove();
        return;
    }
}

Player.prototype.remove = function() {
    Entity.prototype.remove.call(this);
    this.persist(function() {});
}

Player.prototype.persist = function(done) {
    var self = this;
    accountUtils.acquireLock(function() {
        accountUtils.findAccountByUsername(self.username, function(error, index, account) {
            if (error) {
                accountUtils.releaseLock();
                console.log(error);
                return;
            }
            account.inventory = self.inventory.toJson();
            account.respawnPos = self.respawnPos.toJson();
            account.pos = self.pos.toJson();
            account.health = self.health;
            if (self.guidelinePos === null) {
                account.guidelinePos = self.guidelinePos;
            } else {
                account.guidelinePos = self.guidelinePos.toJson();
            }
            accountUtils.setAccount(index, account, function(error) {
                accountUtils.releaseLock();
                if (error) {
                    console.log(error);
                    return;
                }
                done();
            });
        });
    });
}

Player.prototype.getClientInfo = function() {
    return {
        className: "Player",
        id: this.id,
        pos: this.pos.toJson(),
        username: this.username,
        avatar: this.avatar,
        breadCount: this.inventory.getTileCount(BREAD_TILE),
        isInvincible: this.isInvincible()
    }
}

Player.prototype.bakeBread = function() {
    while (true) {
        var tempCanBakeBread = true;
        var index = 0;
        while (index < breadIngredientSet.length) {
            var tempTile = breadIngredientSet[index];
            if (this.inventory.getTileCount(tempTile) <= 0) {
                tempCanBakeBread = false;
                break;
            }
            index += 1;
        }
        if (!tempCanBakeBread) {
            break;
        }
        var index = 0;
        while (index < breadIngredientSet.length) {
            var tempTile = breadIngredientSet[index];
            this.inventory.decrementTileCount(tempTile);
            index += 1;
        }
        this.inventory.incrementTileCount(BREAD_TILE);
    }
}

Player.prototype.interactWithAdjacentTile = function(direction) {
    var tempPos = this.getPosInWalkDirection(direction);
    var tempTile = chunkUtils.getTile(tempPos);
    if (tempTile == OVEN_TILE) {
        this.bakeBread();
        this.setRespawnPos(this.pos.copy());
    }
    if (tempTile == HOSPITAL_TILE) {
        this.health = maximumPlayerHealth;
        this.setRespawnPos(this.pos.copy());
    }
}

Player.prototype.interactWithAdjacentTiles = function() {
    var tempDirection = 0;
    while (tempDirection < entityWalkOffsetList.length) {
        this.interactWithAdjacentTile(tempDirection);
        tempDirection += 1;
    }
}

Player.prototype.walk = function(direction) {
    var tempCost = (1 / 16) * gameUtils.framesPerSecond;
    if (this.walkBudget < tempCost) {
        return;
    }
    this.walkBudget -= tempCost;
    var tempCrack = gameUtils.getCrackByUsername(this.username);
    if (tempCrack !== null) {
        return;
    }
    var tempPos = this.getPosInWalkDirection(direction);
    var tempTile = chunkUtils.getTile(tempPos);
    if (!this.canWalkThroughTile(tempTile)) {
        return;
    }
    this.pos.set(tempPos);
    if ((tempTile >= TRAIL_START_TILE && tempTile < TRAIL_START_TILE + TRAIL_TILE_AMOUNT)
            || tempTile == EMPTY_TILE) {
        chunkUtils.setTile(tempPos, TRAIL_START_TILE + this.avatar);
    }
    if (tempTile >= FLOUR_TILE && tempTile <= BREAD_TILE) {
        chunkUtils.setTile(tempPos, TRAIL_START_TILE + this.avatar);
        this.inventory.incrementTileCount(tempTile);
    }
    if (tempTile >= TELEPORTER_START_TILE && tempTile < TELEPORTER_START_TILE + TELEPORTER_TILE_AMOUNT)
    {
	var tempVec = entityWalkOffsetList[tempTile - TELEPORTER_START_TILE].copy();
	tempVec.scale(2048);
	this.pos.add(tempVec);
    }
    this.interactWithAdjacentTiles();
}

Player.prototype.removeTile = function(direction) {
    if (gameUtils.getCrackByUsername(this.username) !== null) {
        return;
    }
    var tempPos = this.getPosInWalkDirection(direction);
    new Crack(tempPos, this.username);
}

Player.prototype.canPlaceOnTile = function(tile) {
    return ((tile >= TRAIL_START_TILE && tile < TRAIL_START_TILE + TRAIL_TILE_AMOUNT)
       || tile == EMPTY_TILE);
}

Player.prototype.placeTile = function(direction, tile) {
    var tempPos = this.getPosInWalkDirection(direction);
    var tempTile = chunkUtils.getTile(tempPos);
    if (!this.canPlaceOnTile(tempTile)) {
        return false;
    }
    var tempResult = this.inventory.decrementTileCount(tile);
    if (!tempResult) {
        return false;
    }
    chunkUtils.setTile(tempPos, tile);
    return true;
}

Player.prototype.placeSymbolTile = function(tile) {
    if (tile < SYMBOL_START_TILE || tile >= SYMBOL_START_TILE + SYMBOL_TILE_AMOUNT) {
        return false;
    }
    var tempTile = chunkUtils.getTile(this.pos);
    if (!this.canPlaceOnTile(tempTile)) {
        return false;
    }
    chunkUtils.setTile(this.pos, tile);
    return true;
}

Player.prototype.collectTile = function(direction) {
    var tempPos = this.getPosInWalkDirection(direction);
    var tempTile = chunkUtils.getTile(tempPos);
    var tempShouldAddToInventory = false;
    var tempShouldRemove = false;
    if (tempTile >= SYMBOL_START_TILE && tempTile < SYMBOL_START_TILE + SYMBOL_TILE_AMOUNT) {
        tempShouldRemove = true;
    }
    if (tempTile >= FLOUR_TILE && tempTile <= BREAD_TILE) {
        tempShouldAddToInventory = true;
        tempShouldRemove = true;
    }
    if (tempShouldAddToInventory) {
        this.inventory.incrementTileCount(tempTile);
    }
    if (tempShouldRemove) {
        chunkUtils.setTile(tempPos, EMPTY_TILE);
    }
}

Player.prototype.eatBread = function() {
    if (this.health >= maximumPlayerHealth) {
        return;
    }
    var tempResult = this.inventory.decrementTileCount(BREAD_TILE);
    if (!tempResult) {
        return;
    }
    this.health += 1;
}