Last active
March 9, 2020 04:18
-
-
Save yushimatenjin/a6b36d2e842ea0fc84cacce68d71dc3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint esversion: 6, asi: true, laxbreak: true*/ | |
const Animation = pc.createScript('animation'); | |
// 使用方法 | |
// 使用する前にタグとしてdoor-○○のタグを追加してください | |
const attributes = { | |
"delay": { "type": "number", default: 1 }, | |
"loop": { "type": "boolean", default: true }, | |
"yoyo": { "type": "boolean", default: true }, | |
"repeat": { "type": "number", default: 0 }, | |
"animation": { "type": "asset", assetType: "animation" } | |
} | |
for (let attr in attributes) { | |
Animation.attributes.add(attr, attributes[attr]) | |
} | |
Animation.prototype.initialize = function () { | |
const options = { | |
"delay": this.delay, | |
"loop": this.loop, | |
"yoyo": this.yoyo, | |
"repeat": this.repeat | |
} | |
this.app.on(`animation:activate`, () => { | |
this._play(this.animation, options) | |
}, this) | |
this.app.fire(`animation:activate`) | |
}; | |
Animation.prototype._play = async function (animation, options) { | |
const {name} = animation; | |
const {delay, loop, yoyo, repeat} = options | |
const duration = animation.resource.duration | |
this.entity.animation.play(name) | |
await this._delay(duration * 1000 + delay * 1000) | |
this.entity.animation.speed = -1 | |
if(yoyo){ | |
for (let i = duration; i > 0; i -= 0.01){ | |
this.entity.animation.currentTime = i; | |
await this._delay(10) | |
} | |
} | |
} | |
Animation.prototype._delay = function (ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var FirstPersonCamera = pc.createScript('firstPersonCamera'); | |
// initialize code called once per entity | |
FirstPersonCamera.prototype.initialize = function() { | |
// Camera euler angle rotation around x and y axes | |
this.ex = 0; | |
this.ey = 0; | |
// Disabling the context menu stops the browser displaying a menu when | |
// you right-click the page | |
this.app.mouse.disableContextMenu(); | |
this.app.mouse.on(pc.input.EVENT_MOUSEMOVE, this.onMouseMove, this); | |
this.app.mouse.on(pc.input.EVENT_MOUSEDOWN, this.onMouseDown, this); | |
}; | |
FirstPersonCamera.prototype.update = function(dt) { | |
this.entity.setEulerAngles(this.ex, this.ey, 0); | |
}; | |
FirstPersonCamera.prototype.onMouseMove = function (event) { | |
this.ex -= event.dy / 5; | |
this.ex = pc.math.clamp(this.ex, -90, 90); | |
this.ey -= event.dx / 5; | |
} | |
FirstPersonCamera.prototype.onMouseDown = function (event) { | |
if (!pc.input.Mouse.isPointerLocked()) { | |
this.app.mouse.enablePointerLock(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const delay = (ms) => { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint esversion: 6, asi: true, laxbreak: true*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint esversion: 6, asi: true, laxbreak: true*/ | |
var DynamicCharactorController = pc.createScript('dynamicCharactorController'); | |
var SPEED = 5; | |
var JUMP_IMPULSE = 400; | |
var origin = new pc.Vec3(); | |
var groundCheckRay = new pc.Vec3(0, -0.51, 0); | |
var rayEnd = new pc.Vec3(); | |
// initialize code called once per entity | |
DynamicCharactorController.prototype.initialize = function() { | |
this.speed = SPEED; | |
this.jumpImpulse = new pc.Vec3(0, JUMP_IMPULSE, 0); | |
this.onGround = true; | |
this.controller = new pc.input.Controller(window); | |
this.controller.registerKeys('run', [pc.input.KEY_SHIFT]); | |
}; | |
// update code called every frame | |
DynamicCharactorController.prototype.update = function(dt) { | |
this._checkGround(); | |
}; | |
// | |
DynamicCharactorController.prototype.move = function (direction) { | |
if (this.onGround) { | |
this.entity.rigidbody.activate(); | |
direction.scale(this.speed); | |
this.entity.rigidbody.linearVelocity = direction; | |
} | |
}; | |
DynamicCharactorController.prototype.jump = function () { | |
if (this.onGround) { | |
this.entity.rigidbody.activate(); | |
this.entity.rigidbody.applyImpulse(this.jumpImpulse, origin); | |
this.onGround = false; | |
} | |
} | |
DynamicCharactorController.prototype._checkGround = function () { | |
var self = this; | |
var pos = this.entity.getPosition(); | |
rayEnd.add2(pos, groundCheckRay); | |
self.onGround = false; | |
// Fire a ray straight down to just below the bottom of the rigid body, | |
// if it hits something then the character is standing on something. | |
this.app.context.systems.rigidbody.raycastFirst(pos, rayEnd, function (result) { | |
self.onGround = true; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Input = pc.createScript('input'); | |
Input.attributes.add("Camera", {"type": "entity"}) | |
Input.attributes.add("audio", {"type": "entity"}) | |
// initialize code called once per entity | |
Input.prototype.initialize = function() { | |
this.x = new pc.Vec3(); | |
this.z = new pc.Vec3(); | |
this.heading = new pc.Vec3(); | |
this.a = 0; | |
this.timer = 0; | |
// var element = document.body; | |
this.controller = new pc.input.Controller(window); | |
this.controller.registerKeys('forward', [pc.input.KEY_UP, pc.input.KEY_W]); | |
this.controller.registerKeys('back', [pc.input.KEY_DOWN, pc.input.KEY_S]); | |
this.controller.registerKeys('left', [pc.input.KEY_LEFT, pc.input.KEY_A, pc.input.KEY_Q]); | |
this.controller.registerKeys('right', [pc.input.KEY_RIGHT, pc.input.KEY_D]); | |
this.controller.registerKeys('jump', [pc.input.KEY_SPACE]); | |
this.character = this.entity; | |
this.characterController = 'dynamic_character_controller'; | |
this.app.keyboard.on(pc.input.EVENT_KEYDOWN, this.onKeyDown, this); | |
console.log( this.Camera) | |
}; | |
// update code called every frame | |
Input.prototype.update = function(dt) { | |
var input = false; | |
this.timer += dt; | |
// Calculate the camera's heading in the XZ plane | |
var transform = this.Camera.getWorldTransform(); | |
transform.getZ(this.z); | |
this.z.y = 0; | |
this.z.normalize(); | |
transform.getX(this.x); | |
this.x.y = 0; | |
this.x.normalize(); | |
this.heading.set(0, 0, 0); | |
// Strafe left/right | |
if (this.controller.isPressed('left')) { | |
this.heading.sub(this.x); | |
input = true; | |
this.a = 1; | |
} else if (this.controller.isPressed('right')) { | |
this.heading.add(this.x); | |
input = true; | |
this.a = 1; | |
} | |
// Move forwards/backwards | |
if (this.controller.isPressed('forward')) { | |
this.heading.sub(this.z); | |
input = true; | |
} else if (this.controller.isPressed('back')) { | |
this.heading.add(this.z); | |
input = true; | |
} | |
if (this.controller.wasPressed('left')) { | |
this.a = 1; | |
}else if (this.controller.wasPressed('right')) { | |
this.a = 1; | |
} | |
if (this.controller.wasPressed('forward')) { | |
this.a = 1; | |
}else if (this.controller.wasPressed('back')) { | |
this.a = 1; | |
} | |
if (input) { | |
this.heading.normalize(); | |
} | |
this.character.script.dynamicCharactorController.move(this.heading); | |
if (this.controller.wasPressed('jump')) { | |
this.character.script.dynamicCharactorController.jump(); | |
this.a = 0; | |
} | |
// if (this.a === 1 && this.timer >= 0.6) { | |
// this.audio.audiosource.play('tap'); | |
// this.a = 0; | |
// this.timer = 0; | |
// } | |
}; | |
Input.prototype.onKeyDown = function (event) { | |
event.event.preventDefault(); | |
} | |
// swap method called for script hot-reloading | |
// inherit your script state here | |
// Input.prototype.swap = function(old) { }; | |
// to learn more about script anatomy, please read: | |
// http://developer.playcanvas.com/en/user-manual/scripting/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint esversion: 6, asi: true, laxbreak: true*/ | |
const RotateAround = pc.createScript('rotateAround'); | |
const attributes = { | |
"target": { "type": "vec3" }, | |
"rotatespeed": { type: "vec3", default: [0, 0, 0] } | |
} | |
for (let attr in attributes) { | |
RotateAround.attributes.add(attr, attributes[attr]) | |
} | |
RotateAround.prototype.initialize = function () { | |
if (this.target) { | |
this.p = this.entity.getPosition(); | |
this.targetVec = this.target.getPosition(); | |
this.parent = new pc.Entity(); | |
this.app.root.addChild(this.parent); | |
if (this.parent) { | |
this.parent.setPosition(this.targetVec.x, this.targetVec.y, this.targetVec.z); | |
this.parent.setLocalScale(1, 1, 1); | |
this.parent.setLocalEulerAngles(0, 0, 0); | |
this.entity.reparent(this.parent); | |
this.entity.setPosition(this.p.x, this.p.y, this.p.z); | |
} | |
} | |
}; | |
RotateAround.prototype.update = function (dt) { | |
this.parent.rotateLocal(this.rotatespeed.x * dt, this.rotatespeed.y * dt, this.rotatespeed.z * dt); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint esversion: 6, asi: true, laxbreak: true*/ | |
const Rotate = pc.createScript('rotate'); | |
const attributes = { | |
"speed": {"type": "vec3"} | |
} | |
for(let attr in attributes){ | |
Rotate.attributes.add(attr, attributes[attr]) | |
} | |
Rotate.prototype.update = function(dt) { | |
this.entity.rotate(this.speed) | |
}; |
Author
yushimatenjin
commented
Mar 6, 2020
var AnimationController = pc.createScript('animationController');
// initialize code called once per entity
AnimationController.prototype.initialize = function() {
var animation = this.entity.animation;
this.lastState = {
playing: animation.playing,
anim: animation.currAnim
};
const duration = this.entity.animation.duration
this.close = true
animation.on('start', (value) => {
console.log('animation start: ', value);
});
animation.on('end', (value) => {
console.log('animation end: ', value);
setTimeout(() => {
this.entity.animation.speed = this.entity.animation.speed !== 0.5 ? 0.5 : -0.5
animation.play(value, duration);
}, 3000)
});
};
// update code called every frame
AnimationController.prototype.update = function(dt) {
var animation = this.entity.animation;
var currPlaying = animation.playing;
var lastPlaying = this.lastState.playing;
if (lastPlaying === currPlaying) return;
this.lastState.playing = animation.playing;
this.lastState.anim = animation.currAnim;
if (!lastPlaying && currPlaying) {
animation.fire('start', animation.currAnim);
} else if (lastPlaying && !currPlaying) {
animation.fire('end', animation.currAnim);
}
};
var myVar = setInterval(myTimer, 1000);
function myTimer() {
var d = new Date();
var t = d.toLocaleTimeString();
document.getElementById("demo").innerHTML = t;
}
function myStopFunction() {
clearInterval(myVar);
}
/*jshint esversion: 6, asi: true, laxbreak: true*/
var Door = pc.createScript('door');
Door.attributes.add("isOpenned", {type: "boolean"})
Door.attributes.add("value", {type: "number", default: 0, max: 1, min:0})
Door.attributes.add("player", {type: "entity"})
Door.attributes.add("delay", {type: "number", default: 2000})
const delay = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}
Door.prototype.initialize = function() {
const animation = Object.values(this.entity.animation.animations)
if(!animation && animation.length === 0) return
const currentAnimation = animation[0]
const data ={
value: this.value
}
const doorTagName = this.entity.tags.list().find(tag => tag.includes("door"))
const doors = this.app.root.findByTag(doorTagName)
this.app[doorTagName] = false;
const duration = currentAnimation.duration
const animationName = Object.values(this.entity.animation.animationsIndex)[0]
this.entity.animation.loop = false
this.app.on(`door:open:${doorTagName}`, async () => {
if(this.isOpenned) return
console.log(this.isOpenned)
this.entity.animation.speed = this.entity.animation.speed !== 0.5 ? 0.5 : -0.5
this.entity.animation.play(animationName)
this.isOpenned = true
})
this.app.on(`door:close:${doorTagName}`, () => {
this.entity.animation.speed = this.entity.animation.speed !== 0.5 ? 0.5 : -0.5
this.entity.animation.play(animationName, duration)
this.isOpenned = false
})
this.entity.collision.on("collisionstart", (e) =>{
const { other } = e
if(other.name === this.player.name && !this.isOpenned){
this.app.fire(`door:open:${doorTagName}`)
doors.forEach((entity) => {
entity.rigidbody.enabled = false
})
if(this.app[doorTagName]) return
this.app[doorTagName] = true
setTimeout(() => {
if(!this.entity.animation.playing){
this.app.fire(`door:close:${doorTagName}`)
doors.forEach((entity) => {
entity.rigidbody.enabled = true
})
this.app[doorTagName] = false
}
}, 2000)
}
})
};
/*jshint esversion: 6, asi: true, laxbreak: true*/
const Door = pc.createScript('door');
Door.attributes.add("player", { type: "entity" })
Door.prototype.initialize = function () {
const ANIMATION_NAME = "open"
const animation = this.entity.animation.getAnimation(ANIMATION_NAME)
const duration = animation.duration
this.isOpenned = false
this.app.on(`door:open`, () => {
this.entity.rigidbody.enabled = false
this.entity.animation.speed = 0.5
this.entity.animation.play(ANIMATION_NAME)
this.isOpenned = true
})
this.app.on(`door:close`, () => {
this.entity.rigidbody.enabled = true
this.entity.animation.speed = -0.5
this.entity.animation.play(ANIMATION_NAME, duration)
this.isOpenned = false
})
this.entity.collision.on("collisionstart", (e) => {
const { other } = e
if (other.name === this.player.name && !this.isOpenned) {
this.app.fire(`door:open`)
setTimeout(() => {
if (!this.entity.animation.playing) {
this.app.fire(`door:close`)
}
}, 3000)
}
})
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment