Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PlayerBehavior extends Sup.Behavior {
- speed = 0.1;
- jumpSpeed = 0.25;
- direction = 1;
- update() {
- // Use ArcadePhysics2D plugin to check collision
- Sup.ArcadePhysics2D.collides( this.actor.arcadeBody2D, Sup.ArcadePhysics2D.getAllBodies() );
- var animationName = "Idle";
- var animationLooping = true;
- var falling = true;
- var velocity = this.actor.arcadeBody2D.getVelocity();
- // Movement
- if ( Sup.Input.isKeyDown( "LEFT" ) ) {
- velocity.x = -this.speed;
- animationName = "Run";
- this.direction = -1;
- }
- else if ( Sup.Input.isKeyDown( "RIGHT" ) ) {
- velocity.x = this.speed;
- animationName = "Run";
- this.direction = 1;
- }
- else {
- velocity.x = 0;
- }
- // Jump
- if ( Sup.Input.isKeyDown("UP") && this.actor.arcadeBody2D.getTouches().bottom ) {
- velocity.y = this.jumpSpeed;
- }
- // Shoot
- else if ( Sup.Input.isKeyDown("SPACE") && this.actor.spriteRenderer.getAnimation() !== "Shoot" ) {
- animationName = "Shoot";
- animationLooping = false;
- var bulletActor = Sup.appendScene( Sup.get( "Bullet/Prefab", Sup.Scene) )[0]; // Append the scene to create the bullet
- bulletActor.getBehavior( BulletBehavior ).direction = this.direction; // Create the behavior to make it move
- bulletActor.setPosition( this.actor.getPosition() ); // Adjust its position into the player
- bulletActor.move( new Sup.Math.Vector3( 0, -0.4, 0 ) );
- new Sup.Audio.SoundInstance( Sup.get( "Bullet/Sound", Sup.Sound )).play();
- }
- // Update scale and velocity
- this.actor.setLocalScale( new Sup.Math.Vector3(this.direction, 1, 1 ) );
- this.actor.arcadeBody2D.setVelocity( velocity );
- // Apply animation - Make sure the "Shoot" animation is entirely played before switching to another one
- if ( this.actor.spriteRenderer.getAnimation() !== "Shoot" || ! this.actor.spriteRenderer.isAnimationPlaying() ) {
- this.actor.spriteRenderer.setAnimation( animationName, animationLooping );
- }
- // Respawn
- if ( this.actor.getPosition().y < -5 ) { Sup.loadScene( Sup.get( "Scene", Sup.Scene ) ); }
- }
- }
- Sup.registerBehavior( PlayerBehavior );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement