Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import serializeAsColorCurves = BABYLON.serializeAsColorCurves;
- import Vector2 = BABYLON.Vector2;
- class Player extends StateMachine {
- private static _instance: Player;
- private _posStart: BABYLON.Vector3;
- private _canJump: boolean = true;
- private UP_ARROW: number = 38;
- private RIGHT_ARROW: number = 39;
- private LEFT_ARROW: number = 37;
- private BACK_ARROW: number = 40;
- private _currentAbsolutPos:number;
- public character: BABYLON.AbstractMesh;
- private _jumpHeight:number = 1; //hauteur a atteindre
- private _timeToApex:number = 1.2; // temps pour atteindre la hauteur (*2 le temps total du saut)
- private _distanceToTarget:number = 1;
- private _gravity:number = (2*this._jumpHeight)/Math.pow(this._timeToApex,2);
- private _forwardVel:Vector3 = new Vector3(0,0,0);
- private _endPos:Vector3 = new Vector3(0,0,0);
- private _canJumpBack:boolean = false;
- constructor() {
- super();
- this.setStateRun();
- this.character = Game.getInstance().playerAsset as BABYLON.AbstractMesh;//BABYLON.MeshBuilder.CreateBox('player', {size:1}, Game.getInstance().scene);
- this.character.rotation.y = -Math.PI / 2;
- this.character.setEnabled(true);
- this.init();
- document.addEventListener('keydown', this.keyboardInput.bind(this));
- }
- //get class instance
- public static getInstance(): Player {
- if (!Player._instance) {
- Player._instance = new Player();
- }
- return Player._instance;
- }
- private init() {
- this._currentAbsolutPos = 0;
- this._posStart = new BABYLON.Vector3(
- this.character.position.x,
- this.character.position.y,
- this.character.position.z
- );
- }
- protected doActionIdle(): void {
- super.doActionIdle();
- }
- protected doActionRun(): void {
- super.doActionRun();
- let deltaTime = Game.getInstance().getDeltaTime();
- //Collisions
- for (var spawner of SpawnerManager.getInstance().spawnerList) {
- for (var vehicle of spawner.vehicleList) {
- if (this.character.intersectsMesh(vehicle.mesh, false)) {
- this.setStateDeath();
- }
- }
- }
- if (!this._canJump) {
- this.character.position.y += this._forwardVel.y * deltaTime;
- this.character.position.x += this._forwardVel.x * deltaTime ;
- this.character.position.z += this._forwardVel.z * deltaTime;
- this._forwardVel.y -= this._gravity * Game.getInstance().getDeltaTime();
- if (this.character.position.y <= 0){
- this.character.position = this._endPos;
- this._forwardVel = new Vector3(0,0,0);
- this._canJump = true;
- }
- }
- }
- protected doActionDeath(): void {
- super.doActionDeath();
- this.character.position.y = 0;
- if(this.character.scaling.z > 0.1){
- this.character.scaling.z -= 0.20 * Game.getInstance().getDeltaTime();
- } else {
- this.character.scaling.z = 0.1;
- }
- //TODO pop up restart
- }
- public setStateIdle(): void {
- super.setStateIdle();
- document.removeEventListener('keydown', this.keyboardInput.bind(this));
- }
- public setStateRun(): void {
- super.setStateRun();
- document.addEventListener('keydown', this.keyboardInput.bind(this));
- }
- private keyboardInput(event: KeyboardEvent) {
- let canRight:boolean = true;
- let canLeft:boolean = true;
- let canFront:boolean = true;
- let jumpVelocity:number = this._gravity * this._timeToApex ;
- let forwardVel:number = this._distanceToTarget/(this._timeToApex*2);
- if( event.keyCode === this.UP_ARROW ||
- event.keyCode === this.LEFT_ARROW||
- event.keyCode === this.RIGHT_ARROW){
- //check right and left bounds of the map
- canFront = this.testRayCast(new Vector3(this.character.position.x + 1,0.1,this.character.position.z));
- canRight = (this._currentAbsolutPos < GroundManager.getInstance().amplitude) &&
- this.testRayCast(new Vector3(this.character.position.x,0.1,this.character.position.z - 1));
- canLeft = (this._currentAbsolutPos > -GroundManager.getInstance().amplitude) &&
- this.testRayCast(new Vector3(this.character.position.x,0.1,this.character.position.z + 1));
- }
- if(this._canJump) {
- if (event.keyCode === this.UP_ARROW ) {
- this.character.rotation.y = -Math.PI / 2;
- if(canFront){
- SoundManager.getInstance().PlaySFX(Sound.DEF_JUMP);
- this._canJump = false;
- this._endPos = new Vector3(this.character.position.x + this._distanceToTarget,0,this.character.position.z);
- this._forwardVel.y = jumpVelocity;
- this._forwardVel.x = forwardVel;
- this._canJumpBack = true;
- Game.getInstance().addScore();
- }
- } else if (event.keyCode === this.LEFT_ARROW) {
- this.character.rotation.y = Math.PI;
- if(canLeft){
- SoundManager.getInstance().PlaySFX(Sound.DEF_JUMP);
- this._canJump = false;
- this._endPos = new Vector3(this.character.position.x ,0,this.character.position.z + this._distanceToTarget);
- this._forwardVel.y = jumpVelocity;
- this._forwardVel.z = forwardVel;
- }
- } else if (event.keyCode === this.RIGHT_ARROW ) {
- this.character.rotation.y = 0;
- if(canRight){
- SoundManager.getInstance().PlaySFX(Sound.DEF_JUMP);
- this._canJump = false;
- this._endPos = new Vector3(this.character.position.x ,0,this.character.position.z - this._distanceToTarget);
- this._forwardVel.y = jumpVelocity;
- this._forwardVel.z = -forwardVel;
- }
- } else if (event.keyCode === this.BACK_ARROW ){
- this.character.rotation.y = Math.PI / 2;
- if(this._canJumpBack){
- SoundManager.getInstance().PlaySFX(Sound.DEF_JUMP);
- this._canJump = false;
- this._endPos = new Vector3(this.character.position.x - this._distanceToTarget,0,this.character.position.z);
- this._forwardVel.y = jumpVelocity;
- this._forwardVel.x = -forwardVel;
- this._canJumpBack = false;
- Game.getInstance().decreaseScore();
- }
- }
- }
- }
- private testRayCast(pEnd: Vector3):boolean {
- let start = new Vector3(this.character.position.x,
- 0.1,
- this.character.position.z);
- var rayPick = BABYLON.Ray.CreateNewFromTo(start, pEnd);
- var meshFound = Game.getInstance().scene.pickWithRay(rayPick, (mesh:BABYLON.AbstractMesh) => {
- if (mesh.name === 'Penguin' || !mesh.isEnabled()) {
- return false;
- }
- return true;
- }, true);
- if (meshFound.pickedMesh != null)
- {
- SoundManager.getInstance().PlaySFX(Sound.DEF_HITBUSH);
- return meshFound.pickedMesh.name.search('Tree') === -1;
- }else {
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement