Advertisement
dragnoz

Sprite behaviour

May 21st, 2015
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. class PlayerBehavior extends Sup.Behavior {
  2. speed = 0.1;
  3. jumpSpeed = 0.25;
  4. direction = 1;
  5.  
  6. update() {
  7. // Use ArcadePhysics2D plugin to check collision
  8. Sup.ArcadePhysics2D.collides( this.actor.arcadeBody2D, Sup.ArcadePhysics2D.getAllBodies() );
  9.  
  10. var animationName = "Idle";
  11. var animationLooping = true;
  12. var falling = true;
  13.  
  14. var velocity = this.actor.arcadeBody2D.getVelocity();
  15.  
  16. // Movement
  17. if ( Sup.Input.isKeyDown( "LEFT" ) ) {
  18. velocity.x = -this.speed;
  19. animationName = "Run";
  20. this.direction = -1;
  21. }
  22. else if ( Sup.Input.isKeyDown( "RIGHT" ) ) {
  23. velocity.x = this.speed;
  24. animationName = "Run";
  25. this.direction = 1;
  26. }
  27. else {
  28. velocity.x = 0;
  29. }
  30.  
  31. // Jump
  32. if ( Sup.Input.isKeyDown("UP") && this.actor.arcadeBody2D.getTouches().bottom ) {
  33. velocity.y = this.jumpSpeed;
  34. }
  35.  
  36. // Shoot
  37. else if ( Sup.Input.isKeyDown("SPACE") && this.actor.spriteRenderer.getAnimation() !== "Shoot" ) {
  38. animationName = "Shoot";
  39. animationLooping = false;
  40.  
  41. var bulletActor = Sup.appendScene( Sup.get( "Bullet/Prefab", Sup.Scene) )[0]; // Append the scene to create the bullet
  42. bulletActor.getBehavior( BulletBehavior ).direction = this.direction; // Create the behavior to make it move
  43. bulletActor.setPosition( this.actor.getPosition() ); // Adjust its position into the player
  44. bulletActor.move( new Sup.Math.Vector3( 0, -0.4, 0 ) );
  45.  
  46. new Sup.Audio.SoundInstance( Sup.get( "Bullet/Sound", Sup.Sound )).play();
  47. }
  48.  
  49. // Update scale and velocity
  50. this.actor.setLocalScale( new Sup.Math.Vector3(this.direction, 1, 1 ) );
  51. this.actor.arcadeBody2D.setVelocity( velocity );
  52.  
  53. // Apply animation - Make sure the "Shoot" animation is entirely played before switching to another one
  54. if ( this.actor.spriteRenderer.getAnimation() !== "Shoot" || ! this.actor.spriteRenderer.isAnimationPlaying() ) {
  55. this.actor.spriteRenderer.setAnimation( animationName, animationLooping );
  56. }
  57.  
  58. // Respawn
  59. if ( this.actor.getPosition().y < -5 ) { Sup.loadScene( Sup.get( "Scene", Sup.Scene ) ); }
  60. }
  61. }
  62. Sup.registerBehavior( PlayerBehavior );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement