forked from stemkoski/A-Frame-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtual-arcade.html
216 lines (175 loc) · 5.61 KB
/
virtual-arcade.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<!DOCTYPE html>
<html>
<head>
<title>A-Frame - Virtual Arcade</title>
<script src="js/phaser.min.js"></script>
<script src="js/aframe-master.min.js"></script>
<script src="js/aframe-extras.min.js"></script>
</head>
<body>
<script>
AFRAME.registerComponent('joystick', {
schema:
{
triggerPressed: {type: 'boolean', default: false},
triggerDown: {type: 'boolean', default: false},
triggerReleased: {type: 'boolean', default: false},
trackpadAxis0: {type: 'float', default: 0},
trackpadAxis1: {type: 'float', default: 0},
trackpadAngle: {type: 'float', default: 0},
trackpadTouch: {type: 'boolean', default: false}, // not implemented in this version of aframe
trackpadButtonPressed: {type: 'boolean', default: false},
trackpadButtonDown: {type: 'boolean', default: false},
trackpadButtonReleased: {type: 'boolean', default: false},
},
init: function()
{
let self = this;
this.el.addEventListener("triggerdown", function(event)
{
self.data.triggerDown = true;
});
this.el.addEventListener("triggerup", function(event)
{
self.data.triggerDown = false;
});
this.el.addEventListener("trackpaddown", function(event)
{
self.data.trackpadButtonDown = true;
});
this.el.addEventListener("trackpadup", function(event)
{
self.data.trackpadButtonDown = false;
});
this.el.addEventListener("axismove", function(event)
{
self.data.trackpadAxis0 = event.detail.axis[0];
self.data.trackpadAxis1 = event.detail.axis[1];
self.data.trackpadAngle = Math.atan2( event.detail.axis[1], event.detail.axis[0] ) * 180 / Math.PI;
});
},
tick: function (time, timeDelta)
{
}
});
</script>
<a-scene antialias="true">
<a-assets>
<img id="grid" src="images/border.png" crossorigin="anonymous" />
<img id="sky" src="images/stars.jpg" crossorigin="anonymous" />
<a-asset-item id="arcade-cabinet" src="models/arcade-cabinet/arcade-cabinet.gltf"></a-asset-item>
<img id="marquee" src="images/starfish-collector/marquee.png" crossorigin="anonymous" />
<canvas id="mycanvas" crossorigin="anonymous"></canvas>
<div id="gameArea"></div>
</a-assets>
<a-entity wasd-controls="acceleration: 15;" position="0 0 0">
<a-entity camera position="0 1.5 1" look-controls></a-entity>
<a-entity id="leftHand" laser-controls="hand: left" raycaster="" line="color: #FFCCCC"></a-entity>
<a-entity id="rightHand" laser-controls="hand: right" raycaster="" line="color: #CCCCFF"></a-entity>
</a-entity>
<a-sky
rotation = "0 0 0"
color = "#FFFFFF"
material = "src: #sky">
</a-sky>
<a-plane
width="100" height="100"
position=" 0.00 0.00 0.00"
rotation="-90 0 0"
color="#888888"
material="src: #grid; repeat:100 100; transparent: true; opacity: 0.75"
shadow="cast: false; receive: true">
</a-plane>
<a-entity position="0 0 -1.5">
<a-plane
id = "canvas-display"
width = "0.9" height = "0.615"
position="0 1.395 -0.120"
material = "shader: flat; src: #mycanvas;">
</a-plane>
<a-entity id='cabinet' gltf-model="#arcade-cabinet" scale="0.5 0.5 0.5"></a-entity>
<a-plane
width = "0.990" height = "0.280"
position="0 2.080 0.240"
material="src: #marquee;">
</a-plane>
</a-entity>
<a-entity
id = "joystick"
oculus-go-controls
joystick>
</a-entity>
</a-scene>
<script>
var codeObject =
{
// declare variables here if needed
turtle: null,
starfish: null,
score: null,
scoreText: null,
// load assets here
preload: function ()
{
// asset ID/Key, path to asset
game.load.image("water", "images/starfish-collector/water.png");
game.load.image("turtle", "images/starfish-collector/turtle.png");
game.load.image("starfish", "images/starfish-collector/starfish.png");
},
create: function ()
{
game.add.sprite(0,0,"water");
turtle = game.add.sprite(400,300, "turtle");
turtle.anchor.setTo(0.5, 0.5);
game.physics.enable(turtle, Phaser.Physics.ARCADE);
starfish = game.add.sprite(600,500, "starfish");
starfish.anchor.setTo(0.5, 0.5);
game.physics.enable(starfish, Phaser.Physics.ARCADE);
score = 0;
var scoreTextStyle = {font: "64px Arial", fontWeight: "bold", fill:"#44F", stroke:"#000", strokeThickness: "8"};
scoreText = game.add.text(10,10, "Starfish Collected: " + score.toString(), scoreTextStyle);
cursors = game.input.keyboard.createCursorKeys();
// hook phaser into A-Frame by setting id of phaser-generated canvas
document.querySelector("#gameArea").childNodes[0].id = "game-canvas";
document.querySelector("#canvas-display").setAttribute("material", "src", "#game-canvas");
},
update: function()
{
let speed = 4;
let dx = 0;
let dy = 0;
// isDown : continuous
if ( cursors.left.isDown )
dx -= speed;
if ( cursors.right.isDown )
dx += speed;
if ( cursors.up.isDown )
dy -= speed;
if ( cursors.down.isDown )
dy += speed;
// hook into aframe joystick controls
let joystick = document.querySelector("#joystick").components["joystick"]
if ( joystick )
{
dx += speed * joystick.data.trackpadAxis0;
dy += speed * joystick.data.trackpadAxis1;
}
turtle.x += dx;
turtle.y += dy;
if (dx != 0 || dy != 0)
turtle.rotation = Math.atan2(dy, dx);
game.physics.arcade.overlap(turtle, starfish, this.starfishHandler);
},
starfishHandler : function(turtleObject, starfishObject)
{
starfishObject.x = 700 * Math.random() + 50;
starfishObject.y = 450 * Math.random() + 100;
score++;
scoreText.setText( "Starfish Collected: " + score.toString() );
}
}
// constructor adds an HTML canvas element to the specified DIV element
var game = new Phaser.Game(800, 600, Phaser.AUTO, "gameArea", codeObject);
</script>
</body>
</html>