Skip to content

Fix flying to work properly for stackGL version. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 65 additions & 66 deletions flight.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,93 @@

module.exports = function(game, opts) {
return new Fly(game, opts)
return new Fly(game, opts)
}

module.exports.pluginInfo = {
loadAfter: ['voxel-keys']
loadAfter: ['voxel-keys']
}

function Fly(game, opts) {
this.game = game
this.physical = opts.physical
if (!this.game) throw new Error('voxel-flight requires game parameter');
if (!this.game.isClient) return;
this.keys = game.plugins.get('voxel-keys');
if (!this.keys) throw new Error('voxel-flight requires voxel-keys plugin');
this.flySpeed = opts.flySpeed || 0.8
this.game = game
this.physical = opts.physical
if (!this.game) throw new Error('voxel-flight requires game parameter');
if (!this.game.isClient) return;
this.keys = game.plugins.get('voxel-keys');
if (!this.keys) throw new Error('voxel-flight requires voxel-keys plugin');
this.flySpeed = opts.flySpeed || 0.8

this.enable()
this.enable()
}

Fly.prototype.enable = function() {
var self = this
var counter = 0
var spaceUpAfterFirstDown = false
var first = Date.now()
var self = this
var counter = 0
var spaceUpAfterFirstDown = false
var first = Date.now()

if (!this.physical) this.physical = this.game.controls.target()
if (!this.physical) this.physical = this.game.controls.target()

this.keys.down.on('jump', this.onJumpDown = function() {
if (counter === 1) {
if (Date.now() - first > 300) {
spaceUpAfterFirstDown = false
return first = Date.now()
} else {
if (spaceUpAfterFirstDown) {
self.toggleFlying()
this.keys.down.on('jump', this.onJumpDown = function() {
if (counter === 1) {
if (Date.now() - first > 300) {
spaceUpAfterFirstDown = false
return first = Date.now()
} else {
if (spaceUpAfterFirstDown) {
self.toggleFlying()
}
}
spaceUpAfterFirstDown = false
return counter = 0
}
}
spaceUpAfterFirstDown = false
return counter = 0
}
if (counter === 0) {
first = Date.now()
counter += 1
}
});

this.keys.up.on('jump', this.onJumpUp = function() {
if (counter === 1) {
spaceUpAfterFirstDown = true
}
});
if (counter === 0) {
first = Date.now()
counter += 1
}
});

this.keys.up.on('jump', this.onJumpUp = function() {
if (counter === 1) {
spaceUpAfterFirstDown = true
}
});
}

Fly.prototype.disable = function() {
if (this.flying)
this.stopFlying()
if (this.flying)
this.stopFlying()

this.keys.down.removeListener('jump', this.onJumpDown);
this.keys.up.removeListener('jump', this.onJumpUp);
this.keys.down.removeListener('jump', this.onJumpDown);
this.keys.up.removeListener('jump', this.onJumpUp);
}

Fly.prototype.startFlying = function() {
var self = this
this.flying = true
var physical = this.physical
physical.removeForce(this.game.gravity)
physical.onGameTick = function(dt) {
if (physical.atRestY() === -1) return self.stopFlying()
physical.friction.x = self.flySpeed
physical.friction.z = self.flySpeed
var press = self.game.controls.state
physical.velocity.y = 0
if (press['jump']) physical.velocity.y += 0.01
if (press['crouch']) physical.velocity.y -= 0.01
}
this.game.on('tick', physical.onGameTick)
var self = this
this.flying = true
var physical = this.physical
physical.removeForce(game.gravity)
physical.onGameTick = function(dt) {
if (physical.atRestY() === -1) return self.stopFlying()
physical.friction.x = self.flySpeed
physical.friction.z = self.flySpeed
var press = game.controls.state
if (press['crouch']) return physical.velocity[1] = -0.01
if (press['jump']) return physical.velocity[1] = 0.01
physical.velocity[1] = 0
}
this.game.on('tick', physical.onGameTick)
}

Fly.prototype.stopFlying = function() {
this.flying = false
var physical = this.physical
physical.subjectTo(this.game.gravity)
this.game.removeListener('tick', physical.onGameTick)
this.flying = false
var physical = this.physical
physical.subjectTo(this.game.gravity)
this.game.removeListener('tick', physical.onGameTick)
}

Fly.prototype.toggleFlying = function() {
if (this.flying) {
this.stopFlying()
} else {
this.startFlying()
}
if (this.flying) {
this.stopFlying()
} else {
this.startFlying()
}
}