Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. import serializeAsColorCurves = BABYLON.serializeAsColorCurves;
  2. import Vector2 = BABYLON.Vector2;
  3. class Player extends StateMachine {
  4.  
  5. private static _instance: Player;
  6. private _posStart: BABYLON.Vector3;
  7. private _canJump: boolean = true;
  8.  
  9. private UP_ARROW: number = 38;
  10. private RIGHT_ARROW: number = 39;
  11. private LEFT_ARROW: number = 37;
  12. private BACK_ARROW: number = 40;
  13. private _currentAbsolutPos:number;
  14.  
  15. public character: BABYLON.AbstractMesh;
  16.  
  17. private _jumpHeight:number = 1; //hauteur a atteindre
  18. private _timeToApex:number = 1.2; // temps pour atteindre la hauteur (*2 le temps total du saut)
  19. private _distanceToTarget:number = 1;
  20. private _gravity:number = (2*this._jumpHeight)/Math.pow(this._timeToApex,2);
  21. private _forwardVel:Vector3 = new Vector3(0,0,0);
  22. private _endPos:Vector3 = new Vector3(0,0,0);
  23.  
  24. private _canJumpBack:boolean = false;
  25.  
  26.  
  27. constructor() {
  28. super();
  29. this.setStateRun();
  30. this.character = Game.getInstance().playerAsset as BABYLON.AbstractMesh;//BABYLON.MeshBuilder.CreateBox('player', {size:1}, Game.getInstance().scene);
  31. this.character.rotation.y = -Math.PI / 2;
  32.  
  33. this.character.setEnabled(true);
  34. this.init();
  35. document.addEventListener('keydown', this.keyboardInput.bind(this));
  36.  
  37. }
  38.  
  39. //get class instance
  40.  
  41. public static getInstance(): Player {
  42. if (!Player._instance) {
  43. Player._instance = new Player();
  44. }
  45. return Player._instance;
  46. }
  47.  
  48. private init() {
  49. this._currentAbsolutPos = 0;
  50. this._posStart = new BABYLON.Vector3(
  51. this.character.position.x,
  52. this.character.position.y,
  53. this.character.position.z
  54. );
  55. }
  56.  
  57. protected doActionIdle(): void {
  58. super.doActionIdle();
  59. }
  60.  
  61. protected doActionRun(): void {
  62. super.doActionRun();
  63. let deltaTime = Game.getInstance().getDeltaTime();
  64. //Collisions
  65. for (var spawner of SpawnerManager.getInstance().spawnerList) {
  66. for (var vehicle of spawner.vehicleList) {
  67.  
  68. if (this.character.intersectsMesh(vehicle.mesh, false)) {
  69. this.setStateDeath();
  70. }
  71. }
  72. }
  73. if (!this._canJump) {
  74. this.character.position.y += this._forwardVel.y * deltaTime;
  75. this.character.position.x += this._forwardVel.x * deltaTime ;
  76. this.character.position.z += this._forwardVel.z * deltaTime;
  77.  
  78. this._forwardVel.y -= this._gravity * Game.getInstance().getDeltaTime();
  79. if (this.character.position.y <= 0){
  80. this.character.position = this._endPos;
  81. this._forwardVel = new Vector3(0,0,0);
  82. this._canJump = true;
  83. }
  84. }
  85. }
  86.  
  87.  
  88. protected doActionDeath(): void {
  89. super.doActionDeath();
  90. this.character.position.y = 0;
  91.  
  92. if(this.character.scaling.z > 0.1){
  93. this.character.scaling.z -= 0.20 * Game.getInstance().getDeltaTime();
  94. } else {
  95. this.character.scaling.z = 0.1;
  96. }
  97. //TODO pop up restart
  98.  
  99. }
  100.  
  101. public setStateIdle(): void {
  102. super.setStateIdle();
  103. document.removeEventListener('keydown', this.keyboardInput.bind(this));
  104. }
  105.  
  106. public setStateRun(): void {
  107. super.setStateRun();
  108. document.addEventListener('keydown', this.keyboardInput.bind(this));
  109. }
  110.  
  111.  
  112.  
  113.  
  114. private keyboardInput(event: KeyboardEvent) {
  115. let canRight:boolean = true;
  116. let canLeft:boolean = true;
  117. let canFront:boolean = true;
  118. let jumpVelocity:number = this._gravity * this._timeToApex ;
  119. let forwardVel:number = this._distanceToTarget/(this._timeToApex*2);
  120.  
  121.  
  122. if( event.keyCode === this.UP_ARROW ||
  123. event.keyCode === this.LEFT_ARROW||
  124. event.keyCode === this.RIGHT_ARROW){
  125.  
  126. //check right and left bounds of the map
  127. canFront = this.testRayCast(new Vector3(this.character.position.x + 1,0.1,this.character.position.z));
  128. canRight = (this._currentAbsolutPos < GroundManager.getInstance().amplitude) &&
  129. this.testRayCast(new Vector3(this.character.position.x,0.1,this.character.position.z - 1));
  130. canLeft = (this._currentAbsolutPos > -GroundManager.getInstance().amplitude) &&
  131. this.testRayCast(new Vector3(this.character.position.x,0.1,this.character.position.z + 1));
  132. }
  133. if(this._canJump) {
  134. if (event.keyCode === this.UP_ARROW ) {
  135. this.character.rotation.y = -Math.PI / 2;
  136.  
  137. if(canFront){
  138. SoundManager.getInstance().PlaySFX(Sound.DEF_JUMP);
  139. this._canJump = false;
  140. this._endPos = new Vector3(this.character.position.x + this._distanceToTarget,0,this.character.position.z);
  141. this._forwardVel.y = jumpVelocity;
  142. this._forwardVel.x = forwardVel;
  143. this._canJumpBack = true;
  144. Game.getInstance().addScore();
  145. }
  146. } else if (event.keyCode === this.LEFT_ARROW) {
  147. this.character.rotation.y = Math.PI;
  148.  
  149. if(canLeft){
  150. SoundManager.getInstance().PlaySFX(Sound.DEF_JUMP);
  151. this._canJump = false;
  152. this._endPos = new Vector3(this.character.position.x ,0,this.character.position.z + this._distanceToTarget);
  153. this._forwardVel.y = jumpVelocity;
  154. this._forwardVel.z = forwardVel;
  155. }
  156. } else if (event.keyCode === this.RIGHT_ARROW ) {
  157. this.character.rotation.y = 0;
  158.  
  159. if(canRight){
  160. SoundManager.getInstance().PlaySFX(Sound.DEF_JUMP);
  161. this._canJump = false;
  162. this._endPos = new Vector3(this.character.position.x ,0,this.character.position.z - this._distanceToTarget);
  163. this._forwardVel.y = jumpVelocity;
  164. this._forwardVel.z = -forwardVel;
  165. }
  166. } else if (event.keyCode === this.BACK_ARROW ){
  167. this.character.rotation.y = Math.PI / 2;
  168.  
  169. if(this._canJumpBack){
  170. SoundManager.getInstance().PlaySFX(Sound.DEF_JUMP);
  171. this._canJump = false;
  172. this._endPos = new Vector3(this.character.position.x - this._distanceToTarget,0,this.character.position.z);
  173. this._forwardVel.y = jumpVelocity;
  174. this._forwardVel.x = -forwardVel;
  175. this._canJumpBack = false;
  176. Game.getInstance().decreaseScore();
  177. }
  178. }
  179. }
  180. }
  181.  
  182.  
  183. private testRayCast(pEnd: Vector3):boolean {
  184. let start = new Vector3(this.character.position.x,
  185. 0.1,
  186. this.character.position.z);
  187.  
  188. var rayPick = BABYLON.Ray.CreateNewFromTo(start, pEnd);
  189. var meshFound = Game.getInstance().scene.pickWithRay(rayPick, (mesh:BABYLON.AbstractMesh) => {
  190. if (mesh.name === 'Penguin' || !mesh.isEnabled()) {
  191. return false;
  192. }
  193. return true;
  194. }, true);
  195.  
  196. if (meshFound.pickedMesh != null)
  197. {
  198. SoundManager.getInstance().PlaySFX(Sound.DEF_HITBUSH);
  199. return meshFound.pickedMesh.name.search('Tree') === -1;
  200. }else {
  201. return true;
  202. }
  203.  
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement