Advertisement
jmp909

Typescript Phaser Stars

Sep 29th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /// <reference path="F:/typings/phaser/phaser.d.ts" />
  3. module BanzaiThumbWars {
  4.  
  5. export class StarsDemo extends Phaser.State {
  6.    
  7.     private renderTexture: Phaser.RenderTexture;
  8.     private starSprite: Phaser.Sprite;
  9.     private NUM_STARS:number = 13; 
  10.    
  11.     private starData = []; // this will be used to store angle and position (index) per star
  12.     private starGroup:Phaser.Group; // group to place our sprites in ready for rendering to texture
  13.    
  14.     private w: number = 800;
  15.     private h: number = 600;
  16.    
  17.     private frameCount = 0; // keep track of frames during initial launch
  18.    
  19.     private bgRT: Phaser.RenderTexture; // texture to render star group to
  20.     private bgSprite: Phaser.Sprite; // sprite to view texture
  21.  
  22.     private ignoreFrameCount=false; // once all the stars are launched we don't need to stagger launches anymore
  23.  
  24.     private starSprites:Phaser.Sprite[] = []; // our first stars on the path
  25.     private extraSprites:Phaser.Sprite[] = []; // our second stars on the path
  26.  
  27.     private frontGroup:Phaser.Group; // UI etc (not used currently)
  28.     private backGroup:Phaser.Group; // our background animation texture/group
  29.  
  30.     // we scale our sprites to 1.7 (170 frames)..
  31.     // we want to launch a second star half way through
  32.     private nextStarIndex = 85;
  33.    
  34.     constructor () {
  35.         super();
  36.     }
  37.    
  38.     create()  {
  39.    
  40.         // this is the group of sprites we'll be rendering to a texture
  41.         this.starGroup = this.game.add.group(this.game, "stargroup", true);
  42.         this.starGroup.height = 800;
  43.         this.starGroup.width = 600;
  44.         this.starGroup.x=400;
  45.        
  46.         // place it off screen
  47.         // is there any way to render the sprites ready for the texture without displaying it?
  48.         this.starGroup.y=-800;
  49.        
  50.         // our render texture      
  51.         this.bgRT = this.game.add.renderTexture(800, 600, 'bgRT');
  52.  
  53.         // this is what the user will see the Render Texture with
  54.         this.bgSprite = this.game.add.sprite(0, 0, this.bgRT);
  55.         this.bgSprite.anchor.set(0.5,0.5)
  56.         this.bgSprite.x=400
  57.         this.bgSprite.y=300
  58.        
  59.         // position to anchor the base of our star sprites
  60.         var startY: number = 600;
  61.          
  62.        
  63.         for(var i = 0; i <= this.NUM_STARS-1; i++) {       
  64.                
  65.             // offset from the middle sprite
  66.             var a = (i-(this.NUM_STARS - 1)/2);
  67.            
  68.             // time to delay the animation start, so they are staggered
  69.             var t = Math.abs(a*12.8*70);
  70.    
  71.             // index is used to track position of the sprite in the animation
  72.             this.starData.push({ a: a, t:t, angle: a*12.8, index:0, index2:null});
  73.            
  74.             // create the first sprite on the path
  75.             var starSprite = this.starGroup.create(0, startY, "star");
  76.            
  77.             // create the second sprite on the path
  78.             var extraSprite = this.starGroup.create(0, startY, "star");
  79.            
  80.             // anchor our sprite tos the bottom
  81.             starSprite.anchor.set(0.5,1);
  82.             extraSprite.anchor.set(0.5,1);
  83.            
  84.             // fan our stars out
  85.             starSprite.angle = a * 12.8;
  86.             extraSprite.angle = a *12.8;
  87.            
  88.             // start small
  89.             starSprite.scale.set(0.1);
  90.             extraSprite.scale.set(0.1);
  91.            
  92.             // add the sprite to an array
  93.             this.starSprites.push(starSprite);
  94.             this.extraSprites.push(extraSprite);
  95.  
  96.         }
  97.  
  98.        
  99.         // create a group to put our animated background in
  100.         this.backGroup = this.game.add.group();
  101.         this.backGroup.add(this.bgSprite);
  102.         this.backGroup.add(this.starGroup);
  103.    
  104.         // used for UI later   
  105.         this.frontGroup = this.game.add.group();
  106.  
  107.     }
  108.  
  109.     update () {
  110.        
  111.             for(var i=0; i < this.NUM_STARS; i++) {
  112.            
  113.             // for the opening few frame of the animation we want to delay our scale "tweens"
  114.             // to create our staggered effect
  115.             if(this.frameCount > this.starData[i].t/30 || this.ignoreFrameCount) {
  116.                
  117.                 var starSprite = this.starSprites[i];
  118.                 starSprite.angle = this.starData[i].angle;
  119.                 var scale = this.starData[i].index / 100;
  120.                 starSprite.scale.setTo(scale);
  121.                
  122.                
  123.                 // index = animation position of starSprite[i]
  124.                 // index2 = animation position of extraSprite[i]
  125.                
  126.                 /*
  127.                 ---------------------------------------------------------------------------------------------
  128.                 ESSENTIALY:
  129.                
  130.                 the starSprite animates the whole way; once launched the extraSprite animates the bottom half
  131.                 once they're in sync we can swap as needed
  132.                 ----------------------------------------------------------------------------------------------
  133.                 */
  134.                
  135.                 // if there's no extraSprite on screen we scale starSprite all the way, otherwise we only need to scale the top half of the animation,
  136.                 // since the extra sprite covers the bottom half
  137.                 if(this.starData[i].index >= this.nextStarIndex || this.starData[i].index2!=null) {
  138.                    
  139.                     // if there's no extraSprite, reset starSprite to the beginning of the animation path
  140.                     if(this.starData[i].index2==null) {this.starData[i].index2=0}
  141.                    
  142.                     // if extraSprite animation position will be in the bottom half,
  143.                     // then scale it up
  144.                     if(this.starData[i].index2 < this.nextStarIndex) {
  145.                         var scale2 = this.starData[i].index2 / 100;
  146.                         var extraSprite = this.extraSprites[i];
  147.                         extraSprite.scale.setTo(scale2);
  148.                        
  149.                     }
  150.                     else {
  151.                         // we don't need the extraSprite now,
  152.                         // the starSprite can take over the top half of the animation
  153.                         this.starData[i].index2=null;
  154.                         this.starData[i].index=this.nextStarIndex;                 
  155.                     }
  156.                 }
  157.  
  158.                 // increment the animation frame index counter
  159.                 this.starData[i].index = this.starData[i].index+1;
  160.                
  161.                 // we've reached 1.7 scale
  162.                 if(this.starData[i].index == 170) {
  163.                    
  164.                     // if there's an extraSprite in the animation
  165.                     if(this.starData[i].index2 !=null)
  166.                     {
  167.                         // let the starSrprite do the top half of the animation
  168.                         this.starData[i].index=this.nextStarIndex;
  169.                     }
  170.                     else
  171.                     {
  172.                         // reset starSprite back to beginning of animation
  173.                         this.starData[i].index=0;
  174.                     }
  175.                 }
  176.                
  177.                 // if there's an extraSprite in the animation
  178.                 if(this.starData[i].index2!=null) {
  179.                    
  180.                     // increase it's animation frame index
  181.                     this.starData[i].index2 = this.starData[i].index2+1;
  182.                    
  183.                     // if it reaches half way, reset it to 0
  184.                     if(this.starData[i].index2 == 85) {
  185.                         this.starData[i].index2=0;
  186.                     }
  187.                 }
  188.             }
  189.            
  190.         }
  191.        
  192.        
  193.         // render all of our stars to a texture
  194.         // this is because 2 sets of stars on the path dont fill the whole screen at any one time
  195.         // so we need to keep the residual graphics
  196.         this.bgRT.renderXY(this.starGroup,400,0, false);
  197.        
  198.        
  199.         // after 2 full loops we don't need to stagger the launches anymore
  200.         // so we can ignore the frame count from now on
  201.         if(!this.ignoreFrameCount) {
  202.            
  203.             this.frameCount = this.frameCount+1;
  204.             if(this.frameCount > 340 ) {
  205.                 this.ignoreFrameCount=true;
  206.             }
  207.         }
  208.     }
  209.  
  210. }
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement