-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (73 loc) · 2.46 KB
/
index.js
File metadata and controls
87 lines (73 loc) · 2.46 KB
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
/**
* Gun
*
* A game where users can shoot a projectile from a gun.
*
* @license MIT
* @author FrancoBolevin
*/
module.exports = class GunPlugin extends BasePlugin {
/** Plugin info */
static get id() { return 'gunplugin' }
static get name() { return 'Gun shooting game' }
static get description() { return 'A game where users can shoot a projectile from a gun.' }
/** Called when the plugin is loaded */
async onLoad() {
this.instanceID = Math.random().toString(36).substr(2)
const gunModel = {
name: 'GunModel',
type: 'model',
url: this.paths.absolute('shotgun.glb'),
clientOnly: true,
parent: 'accessoryslot',
y: -0.5,
x: 0.25,
height: -0.4,
rotation_y: 3.14159
}
this.audio.preload(this.paths.absolute('shot.wav'))
this.hooks.addHandler('controls.key.down', this.fireGun)
await this.objects.create(gunModel)
}
/** Called when the plugin is unloaded */
onUnload() {
this.hooks.removeHandler('controls.key.down', this.fireGun)
}
/**
* Fires the gun.
* @param {KeyboardEvent} e Keyboard event.
*/
fireGun = async e => {
// Not the correct key
if (e.key != 'g' && e.key != 'G') {
return
}
this.audio.play(this.paths.absolute('shot.wav'))
const objectsHit = await this.world.raycast({ collision: true })
const hit = objectsHit[0]
// Did not hit any objects
if (!hit) {
return
}
const splat = Object.assign(hit.wallProps, {
name: 'Splat',
type: 'image',
url: this.paths.absolute('splat.png'),
clientOnly: true,
doublesided: true,
alpha_test: 0.5,
targetable: false
})
await this.objects.create(splat)
this.hooks.trigger('ydangle.gun.hit', { id: hit.id })
this.messages.send({ action: 'ydangle.gun.shot', splat, id: this.instanceID, position: await this.user.getPosition() }, true)
}
/** Called when a message has been received */
async onMessage(e) {
if (e.id != this.instanceID) {
const splat = e.splat
this.audio.play(this.paths.absolute('shot.wav'), { x: e.position.x, y: e.position.z, height: e.position.y, radius: 40 })
await this.objects.create(splat)
}
}
}