Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {CFG as config} from '../config.js'
  2.  
  3. export class CardClass extends Phaser.GameObjects.GameObject{
  4.  
  5.         constructor(scene, type, params, data) {
  6.                 super(scene, type)
  7.  
  8.                 let w = params.width
  9.                 let h = params.height
  10.  
  11.                 this.container = scene.add.container(w,h).setVisible(0)
  12.                 this.rt = scene.add.renderTexture(0,0,w,h)
  13.  
  14.                 this.isFaceUp   = params.isFaceUp
  15.  
  16.                 // make containers for front/back of card
  17.                 this.front              = this.makeFace(params)
  18.                 this.back               = this.makeFace(params, false)
  19.  
  20.                 this.front.setVisible(0)
  21.                 this.back.setVisible(0)
  22.  
  23.                 this.container.add([this.front, this.back])
  24.  
  25.                 // info button, flips the card. not yet implemented!
  26.                 // this.info_button = scene.add.button()
  27.                 // this.info_button.on('mouseDown', flip, this)
  28.  
  29.                 this.update()
  30.  
  31.                 scene.sys.displayList.add(this.front)
  32.                 scene.sys.updateList.add(this.back)
  33.         }
  34.  
  35.         this.update(){
  36.                 if (this.isFaceUp) {
  37.                         this.container.remove(this.back)
  38.                 } else {
  39.                         this.container.add(this.back)
  40.                 }
  41.                 this.rt.clear()
  42.                 this.rt.draw(container)
  43.         }
  44.  
  45.         flip(){
  46.                 this.isFaceUp ^= 1 // toggle
  47.                 // animation stuff here
  48.                 this.update()
  49.         }
  50.  
  51.         setFaceUp(){
  52.                 if (this.isFaceUp)
  53.                         return
  54.                 this.flip()
  55.         }
  56.  
  57.         setFaceDown(){
  58.                 if ( !(this.isFaceUp) )
  59.                         return
  60.                 this.flip()
  61.         }
  62.  
  63.         makeFace(params, data, isFront=true){
  64.                 // returns a rt
  65.                 let w = params.width
  66.                 let h = params.height
  67.                 let container = this.scene.add.container(w,h)
  68.                 container.setVisible(0)
  69.  
  70.                 let upper = this.scene.add.graphics(w,h)
  71.                 let lower = this.scene.add.graphics(w,h)
  72.  
  73.                 let face_params = params[ isFront ? 'front' : 'back']
  74.  
  75.                 upper.fillStyle(face_params.upper.color, 1)
  76.                 upper.fillRoundedRect(
  77.                         face_params.upper.origin.x * w,
  78.                         face_params.upper.origin.y * h,
  79.                         face_params.upper.dimensions.x * w,
  80.                         face_params.upper.dimensions.y * h,
  81.                         face_params.upper.corners
  82.                 )
  83.  
  84.                 lower.fillStyle(face_params.lower.color, 1)
  85.                 lower.fillRoundedRect(
  86.                         face_params.lower.origin.x * w,
  87.                         face_params.lower.origin.y * h,
  88.                         face_params.lower.dimensions.x * w,
  89.                         face_params.lower.dimensions.y * h,
  90.                         face_params.lower.corners
  91.                 )
  92.  
  93.                 container.add([upper, lower])
  94.  
  95.                 // more stuff to parse the data to place image/text on card face here...
  96.  
  97.                 let rt = this.scene.add.renderTexture(0,0, w, h)
  98.  
  99.  
  100.                 rt.draw(container)
  101.                 container.destroy()
  102.  
  103.                 return rt
  104.         }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement