Advertisement
Guest User

Phaser Reels

a guest
Nov 13th, 2019
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export class MainScene extends Phaser.Scene
  2. {
  3.  
  4.     private reelXPositions = [51, 415, 779, 1143, 1507];
  5.     private reelYpositon = 197;
  6.  
  7.      public staticSymbols: string[] = ["symbol0", "symbol1", "symbol2", "symbol3", "symbol4", "symbol5", "symbol6", "symbol7", "symbol8", "symbol9"];
  8.      private symbolAnims: Phaser.GameObjects.Video[];
  9.      private symbolSize = new Phaser.Math.Vector2(726, 1102);
  10.  
  11.     private reels: Reel[];
  12.  
  13.     preload(): void
  14.     {
  15.    
  16.          // load static symbols
  17.             for (let i = 0; i < 10; i++)
  18.          {
  19.                 this.load.image(this.staticSymbols[i], skindata.staticsymbols[i]);
  20.     }
  21.  
  22.   }
  23.  
  24. create(): void
  25.   {
  26.     this.input.on("pointerup", this.onPointer, this);
  27.  
  28.     this.reels = new Array();
  29.     for (let i = 0; i < 5; i++)
  30.     {
  31.       this.reels.push(new Reel(this, this.reelXPositions[i], this.reelYpositon, i, this.symbolSize.y));
  32.     }
  33. }
  34.  
  35.  
  36. private onPointer(pointer: any)
  37.   {
  38.     console.log("pointer up");
  39.  
  40.     for (let i = 0; i < 1; i++)
  41.     {
  42.       if (this.reels[i].currentState == ReelState.Stopped)
  43.       {
  44.         this.reels[i].currentState = ReelState.SpinningUp;
  45.       }
  46.       else if (this.reels[i].currentState == ReelState.SpinningUp)
  47.       {
  48.         this.reels[i].currentState = ReelState.SpinningDown;
  49.       }
  50.      
  51.     }
  52.  
  53.   }
  54.  
  55. public update(_time: number, delta: number)
  56.   {
  57.     // console.log("update");
  58.  
  59.     for (let i = 0; i < 5; i++)
  60.     {
  61.       this.reels[i].update(_time, delta);
  62.     }
  63.  
  64.   }
  65.  
  66.  
  67. }
  68.  
  69.  
  70.  
  71.  
  72. export class Reel extends Phaser.GameObjects.Container
  73. {
  74.  
  75.     private _mainScene: MainScene = null;
  76.  
  77.  
  78.     private symbol1: Phaser.GameObjects.Image;
  79.     private symbol2: Phaser.GameObjects.Image;
  80.  
  81.    
  82.  
  83.  
  84. constructor(scene: MainScene, x: number, y: number, id: number, symbolheight: number)
  85.     {
  86.         super(scene, 0, 0);
  87.         this._mainScene = scene;
  88.         this.reelIndex = id;
  89.         this.symbolHeight = symbolheight;
  90.         this.reelXposition = x;
  91.         this.reelYposition = y;
  92.  
  93.         this.symbol1Index = scene.GetRandomActiveSymbol();
  94.         this.symbol2Index = scene.GetRandomActiveSymbol();
  95.  
  96.         this.symbol1 = scene.add.image(x, y, scene.staticSymbols[this.symbol1Index]).setOrigin(0, 0).setScale(this.basesymbolscale);
  97.         this.symbol2 = scene.add.image(x, y - (symbolheight * this.basesymbolscale), scene.staticSymbols[this.symbol2Index]).setOrigin(0, 0).setScale(this.basesymbolscale);
  98.  
  99.         // this.targetSymbol = Phaser.Math.RND.integerInRange(0, 4);
  100.         this.targetSymbol = 0;
  101.     }
  102.  
  103. public update(_time: number, delta: number)
  104.     {
  105.         // if(this.reelIndex == 0)
  106.             // console.log("reel update " + this.reelIndex);
  107.         // console.log("time: " + _time);
  108.         // console.log("delta: " + delta);
  109.         let deltaseconds = delta / 1000;
  110.         // console.log("deltaseconds: " + deltaseconds);
  111.         let correctsymbol = false;
  112.  
  113.         switch (this.currentState)
  114.         {
  115.             case ReelState.SpinningUp:
  116.                     if (this.velocity.y < this.maxvelocity.y)
  117.                     {
  118.                         this.velocity.x += this.accelFactor.x * deltaseconds;
  119.                         this.velocity.y += (this.accelFactor.y * deltaseconds);
  120.                     }
  121.                     else
  122.                     {
  123.                         this.velocity.y = this.maxvelocity.y;
  124.                     }
  125.                 break;
  126.             case ReelState.Spinning:
  127.                 // not much to do here
  128.                 break;
  129.             case ReelState.SpinningDown:
  130.                 this.velocity.x -= this.decelFactor.x * deltaseconds;
  131.                 this.velocity.y -= this.decelFactor.y * deltaseconds;
  132.                 if (this.velocity.y < this.stoppingSpeed.y)
  133.                 {
  134.                     this.velocity.y = this.stoppingSpeed.y;
  135.                     // state = 3;
  136.                     this.currentState = ReelState.Stopping;
  137.                 }
  138.                 break;
  139.             case ReelState.Stopping:
  140.                 if (this.symbol2Index == this.targetSymbol)
  141.                 {
  142.                     correctsymbol = true;
  143.                 }
  144.                 break;
  145.             case ReelState.Bouncing:
  146.                 break;
  147.             case ReelState.Stopped:
  148.                 // this.velocity.y = 0;
  149.                 break;
  150.         }
  151.  
  152.         this.symbol1.y += (this.velocity.y * deltaseconds);
  153.         this.symbol2.y += (this.velocity.y * deltaseconds);
  154.  
  155.         if (this.symbol1.y >= (this.reelYposition + (this.symbolHeight * this.basesymbolscale)))
  156.         {
  157.             if (this.currentState === ReelState.Bouncing)
  158.             {
  159.                 if (this.symbol1.y >= (this.reelYposition + this.bounceDistance.y))
  160.                 {
  161.                     this.velocity.y *= -1;
  162.                     console.log("reel " + this.reelIndex + " bounce done, returning");
  163.                     this.currentState = ReelState.BounceReturning;
  164.                 }
  165.             }
  166.             else if (this.currentState === ReelState.BounceReturning)
  167.             {
  168.                 if (this.symbol1.y <= this.reelYposition)
  169.                 {
  170.                     this.symbol1.y = this.reelYposition;
  171.                     this.symbol2.y = this.symbol1.y - (this.symbolHeight * this.basesymbolscale);
  172.                     this.currentState = ReelState.Stopped;
  173.                     console.log("reel " + this.reelIndex + " bounce returned, stopping");
  174.                 }
  175.             }
  176.             else
  177.             {
  178.                 this.symbol1Index = this.symbol2Index;
  179.                 this.symbol2Index = this._mainScene.GetRandomActiveSymbol();
  180.                 this.symbol1.y -= this.symbolHeight * this.basesymbolscale;
  181.                 this.symbol2.y = this.symbol1.y - (this.symbolHeight * this.basesymbolscale);
  182.                 this.symbol1.setTexture(this._mainScene.staticSymbols[this.symbol1Index]);
  183.                 this.symbol2.setTexture(this._mainScene.staticSymbols[this.symbol2Index]);
  184.  
  185.                 if (this.currentState === ReelState.Stopping)
  186.                 {
  187.                     if (correctsymbol)
  188.                     {
  189.                         console.log("reel " + this.reelIndex + " symbol correct, bouncing");
  190.                         this.currentState = ReelState.Bouncing;
  191.                     }
  192.                 }
  193.             }
  194.         }
  195.  
  196.        
  197.  
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement