-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathplayer.js
97 lines (81 loc) · 1.9 KB
/
player.js
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
"use strict";
var util = require("util");
var events = require("events");
var Snake = require('./snake');
var Vector = require('./vector');
function Player(socket, name, color) {
events.EventEmitter.call(this);
this.socket = socket;
this.color = color;
this.name = name;
this.connected = true;
//this.resendAllEntities();
var $this = this;
socket.on('playercontrol', function(target) {
if($this.snake) {
target = Vector.ify(target);
if(target)
$this.snake.target = target;
}
});
socket.on('chat', function(msg) {
$this.chat(msg);
});
socket.on('disconnect', function() {
$this.disconnect();
});
}
util.inherits(Player, events.EventEmitter);
Object.defineProperty(Player.prototype, 'coloredName', {
get: function() {
return this.name.colored(this.color);
}
})
Player.prototype.disconnect = function() {
if(this.connected) {
this.emit('quit');
this.connected = false;
this.name = null;
if(this.snake) this.snake.destroy();
this.snake = null;
}
}
Player.prototype.kill = function(reason) {
if(this.connected && this.snake) {
this.snake.destroy();
this.snake = null;
this.emit('death', reason);
}
}
Player.prototype.chat = function(msg) {
msg = (""+msg).trim();
if(this.connected && msg.length < 1024 && msg.length != 0) {
this.emit('chat', msg);
}
}
Player.prototype.spawnSnake = function(world) {
if(this.connected) {
if(this.snake) this.snake.destroy();
this.emit('spawn');
var $this = this;
var snake = new Snake(
10,
this.color,
world.randomPosition(),
world
);
snake.owner = this;
snake.target = snake.head.position.clone();
snake
.on('death', function(killer) {
$this.snake = null;
$this.emit('death', 'enemy', killer.owner)
})
.on('eat.tail', function(ball) {
if(ball.ownerSnake && ball.ownerSnake.owner)
$this.emit('attack', ball.ownerSnake.owner);
});
this.snake = snake;
}
}
module.exports = Player;