-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.html
185 lines (130 loc) · 4.19 KB
/
index.html
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
<!--
1) AUTHENTICATE WITH METAMASK
2) ADD USER TO DB
3) ADD MORE DATA TO THE USER PROFILE
-->
<html>
<head>
<title>Metamask Demo</title>
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<script src="https://npmcdn.com/[email protected]/dist/moralis.js"></script>
<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
</head>
<body>
<button onclick="login()">Login with Metamask</button>
<br><br>
<script>
Moralis.initialize("APP ID"); // Application id from moralis.io
Moralis.serverURL = "SERVER URL"; //Server url from moralis.io
var state = {}
var sprites = []
var buttonsLocked = {}
var gameInitialized = false;
const PLAYER_SIZE = 50;
const MOVE_COOLOFF = 300;
var lastMove = 0;
async function login(){
console.log("login clicked");
var user = await Moralis.Web3.authenticate();
if(user){
console.log(user);
location.reload();
}
}
console.log(Moralis.User.current())
var config = {
type: Phaser.AUTO,
width: 1200,
height: 700,
scene: {
preload: preload,
create: create,
update: update
},
physics: {
default: 'arcade',
arcade: { debug: true }
}
};
var game = new Phaser.Game(config);
var context;
var users = [];
function preload ()
{
context = this;
}
async function create ()
{
if(!Moralis.User.current())
return;
this.wKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W);
this.aKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A);
this.sKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S);
this.dKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D);
let query = new Moralis.Query('Room1');
let subscription = await query.subscribe();
subscription.on('update', (object) => {
if(!users[object.id])
users[object.id] = this.add.rectangle(object.get("x"), object.get("y"), PLAYER_SIZE, PLAYER_SIZE, 0x6666ff);
else{
let user = users[object.id];
user.setPosition(object.get("x"),object.get("y"))
}
});
await Moralis.Cloud.run("move", {direction:null}); //just to register the player
let nearbyPlayers = await Moralis.Cloud.run("playersNearby");
nearbyPlayers.forEach(player => {
users[player.id] = this.add.rectangle(player.get("x"), player.get("y"), PLAYER_SIZE, PLAYER_SIZE, 0x6666ff);
});
gameInitialized = true;
}
async function update ()
{
if(!gameInitialized)
return;
cursors = this.input.keyboard.createCursorKeys();
const params = { cursors: cursors };
// dont move if cool off hasnt passed
if(new Date()-lastMove < MOVE_COOLOFF)
return
lastMove = new Date();
if (this.wKey.isDown) {
if(!buttonsLocked["up"]){
console.log('W is pressed');
buttonsLocked["up"]=true;
let moveResult = await Moralis.Cloud.run("move", {direction:"up"});
console.log(moveResult)
buttonsLocked["up"]=false;
}
}
else if(this.aKey.isDown){
if(!buttonsLocked["left"]){
console.log('A is pressed');
buttonsLocked["left"]=true;
let moveResult = await Moralis.Cloud.run("move", {direction:"left"});
console.log(moveResult)
buttonsLocked["left"]=false;
}
}
else if(this.sKey.isDown){
if(!buttonsLocked["down"]){
console.log('S is pressed');
buttonsLocked["down"]=true;
let moveResult = await Moralis.Cloud.run("move", {direction:"down"});
console.log(moveResult)
buttonsLocked["down"]=false;
}
}
else if(this.dKey.isDown){
if(!buttonsLocked["right"]){
console.log('D is pressed');
buttonsLocked["right"]=true;
let moveResult = await Moralis.Cloud.run("move", {direction:"right"});
console.log(moveResult)
buttonsLocked["right"]=false;
}
}
}
</script>
</body>
</html>