forked from greggman/hft-clean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.html
104 lines (89 loc) · 2.46 KB
/
controller.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no, minimal-ui">
<title>Clean Controls</title>
<style>
body {
margin: 0;
color: white;
font-size: xx-large;
font-weight: bold;
font-family: sans-serif;
}
#toucharea {
display: flex;
width: 100vw;
height: 100vh;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<div id="toucharea">
<div id="msg">touch to move</div>
</div>
</body>
<!--
The following script line includes happyfuntimes which defines HFT.
You could also use require.js or webpack etc...
-->
<script src="/node_modules/happyfuntimes/dist/hft.js"></script>
<script>
// Connect to the game
const client = new HFT.GameClient();
client.on('connect', handleConnect);
client.on('disconnect', handleDisconnect);
client.on('color', handleColor);
client.on('scored', handleScored);
let points = 0;
function handleConnect() {
// nothing to do here though maybe we don't want to
// send any touch events until we've connected for example.
}
function handleDisconnect() {
touchArea.textContent = "...disconnected...";
}
function handleColor(color) {
touchArea.style.backgroundColor = color;
}
function handleScored(data) {
points += data.points;
touchArea.textContent = "points: " + points;
}
const touchArea = document.querySelector('#toucharea');
touchArea.addEventListener('mousemove', handleMouseMove);
touchArea.addEventListener('touchmove', handleTouchMove);
touchArea.addEventListener('touchstart', (e) => {
e.preventDefault();
});
function handleTouchMove(event) {
// only handle the first touch
sendMoveCmd(getRelativePosition(event.touches[0], touchArea), touchArea);
}
function handleMouseMove(event) {
sendMoveCmd(getRelativePosition(event, touchArea), touchArea);
}
// This will generate a 'move' event in the corresponding
// NetPlayer object in the game.
function sendMoveCmd(position, target) {
// normalize the position from 0 to 1 because
// different players smartphones will have different size
// screens
client.sendCmd('move', {
x: position.x / target.clientWidth,
y: position.y / target.clientHeight,
});
};
function getRelativePosition(event, target) {
target = target || event.target;
var rect = target.getBoundingClientRect();
return {
x: event.clientX - rect.left,
y: event.clientY - rect.top,
}
}
</script>
</html>