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
|
package;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.ui.FlxVirtualPad;
import flixel.util.FlxColor;
import io.colyseus.Client;
import io.colyseus.Room;
import models.State;
import openfl.display.BlendMode;
class PlayState extends FlxState
{
var client = new Client('ws://dungeon-server.glitch.me');
public static var virtualPad:FlxVirtualPad;
private var room:Room<State>;
private var players:Map<String, Player> = new Map();
override public function create()
{
super.create();
virtualPad = new FlxVirtualPad(FULL, NONE);
add(virtualPad);
this.client.joinOrCreate("my_room", [], State, function(err, room)
{
if (err != null)
{
trace("JOIN ERROR: " + err);
return;
}
this.room = room;
this.room.state.players.onAdd = function(player, key)
{
trace("Player" + player.x);
var _player = new Player(player.x, player.y, player.name, room.sessionId);
this.players[key] = _player;
add(_player);
if (room.sessionId == player.name)
{
FlxG.camera.follow(_player);
}
trace(room.id);
}
this.room.state.players.onChange = function(player, key)
{
trace("PLAYER CHANGED AT: ", key);
this.players[key].x = player.x;
this.players[key].y = player.y;
}
this.room.state.players.onRemove = function(player, key)
{
trace("PLAYER REMOVED AT: ", key);
remove(this.players[key]);
}
});
}
override public function update(elapsed:Float)
{
var up, down, left, right;
#if desktop
up = FlxG.keys.pressed.UP;
down = FlxG.keys.justPressed.DOWN;
left = FlxG.keys.justReleased.LEFT;
right = FlxG.keys.justReleased.RIGHT;
#end
#if mobile
up = virtualPad.buttonUp.pressed;
down = virtualPad.buttonDown.pressed;
left = virtualPad.buttonLeft.pressed;
right = virtualPad.buttonRight.pressed;
#end
if (up)
{
// The up arrow key is currently pressed
// This code is executed every frame, while the key is pressed\
this.room.send("move", {y: -1 * elapsed * 200});
}
if (down)
{
// The left arrow key has just been pressed
// This code is only executed once, on the frame immediately after the key has been pressed
this.room.send("move", {y: 1 * elapsed * 200});
}
if (left)
{
// The left arrow key has just been released
// This code is only executed once, on the frame immediately after the key has been released
this.room.send("move", {x: -1 * elapsed * 200});
}
if (right)
{
// The left arrow key has just been released
// This code is only executed once, on the frame immediately after the key has been released
this.room.send("move", {x: 1 * elapsed * 200});
}
super.update(elapsed);
}
}
|