-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathgame.js
53 lines (45 loc) · 1.56 KB
/
game.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
/*
* Author: Jerome Renaux
* E-mail: jerome.renaux@gmail.com
*/
var Game = {};
Game.init = function(){
game.stage.disableVisibilityChange = true;
};
Game.preload = function() {
game.load.tilemap('map', 'assets/map/example_map.json', null, Phaser.Tilemap.TILED_JSON);
game.load.spritesheet('tileset', 'assets/map/tilesheet.png',32,32);
game.load.image('sprite','assets/sprites/sprite.png');
};
Game.create = function(){
Game.playerMap = {};
var testKey = game.input.keyboard.addKey(Phaser.Keyboard.ENTER);
testKey.onDown.add(Client.sendTest, this);
var map = game.add.tilemap('map');
map.addTilesetImage('tilesheet', 'tileset'); // tilesheet is the key of the tileset in map's JSON file
var layer;
for(var i = 0; i < map.layers.length; i++) {
layer = map.createLayer(i);
}
layer.inputEnabled = true; // Allows clicking on the map ; it's enough to do it on the last layer
layer.events.onInputUp.add(Game.getCoordinates, this);
Client.askNewPlayer();
};
Game.getCoordinates = function(layer,pointer){
Client.sendClick(pointer.worldX,pointer.worldY);
};
Game.addNewPlayer = function(id,x,y){
Game.playerMap[id] = game.add.sprite(x,y,'sprite');
};
Game.movePlayer = function(id,x,y){
var player = Game.playerMap[id];
var distance = Phaser.Math.distance(player.x,player.y,x,y);
var tween = game.add.tween(player);
var duration = distance*10;
tween.to({x:x,y:y}, duration);
tween.start();
};
Game.removePlayer = function(id){
Game.playerMap[id].destroy();
delete Game.playerMap[id];
};