Commit b4f2e18e authored by rockyl's avatar rockyl

纹理缓存

parent 06309eb6
...@@ -2,13 +2,12 @@ ...@@ -2,13 +2,12 @@
* Created by rockyl on 2018/8/16. * Created by rockyl on 2018/8/16.
*/ */
import { props } from "../props"; import {props} from "../props";
import { playSound, createSvga } from "./utils"; import {playSound, createSvga} from "./utils";
import ObjectPool = engine.ObjectPool; import ObjectPool = engine.ObjectPool;
import { getTextureByName } from "./utils"; import {getTextureByName} from "./utils";
import { Goods } from "./Goods"; import {Goods} from "./Goods";
import { PoolName } from "./object-pool-init"; import {PoolName} from "./object-pool-init";
export default class GameView extends engine.Container { export default class GameView extends engine.Container {
...@@ -28,24 +27,24 @@ export default class GameView extends engine.Container { ...@@ -28,24 +27,24 @@ export default class GameView extends engine.Container {
private timeSpace: number; private timeSpace: number;
private prizes; private prizes;
private bgImg:engine.Image; private bgImg: engine.Image;
private bgImgUrl:string; private bgImgUrl: string;
private itemInfo:any; private itemInfo: any;
private prizeMaxH:number; private prizeMaxH: number;
private orgY:number; private orgY: number;
private prizeId:string;
private container0:engine.Container; private prizeId: string;
private container1:engine.Container;
private container2:engine.Container;
private mainContainer:engine.Container; private container0: engine.Container;
private viewContainer:engine.Container; private container1: engine.Container;
private container2: engine.Container;
private configInfo:any; private mainContainer: engine.Container;
private viewContainer: engine.Container;
private configInfo: any;
private textureCache = {};
constructor() { constructor() {
super(); super();
...@@ -62,7 +61,7 @@ export default class GameView extends engine.Container { ...@@ -62,7 +61,7 @@ export default class GameView extends engine.Container {
// cOriginSize:{x:22, y:19},//遮罩左边定点位置 // cOriginSize:{x:22, y:19},//遮罩左边定点位置
// cSpaceSize:{x:23, y:0},//每列奖品的间隙 // cSpaceSize:{x:23, y:0},//每列奖品的间隙
// } // }
// this.bgImgUrl = '//yun.duiba.com.cn/aurora/assets/877058dcced99e42ddd2ccf19c2bce189fa45d5c.png'; // this.bgImgUrl = '//yun.duiba.com.cn/aurora/assets/877058dcced99e42ddd2ccf19c2bce189fa45d5c.png';
...@@ -109,7 +108,7 @@ export default class GameView extends engine.Container { ...@@ -109,7 +108,7 @@ export default class GameView extends engine.Container {
this.countdown = 150; this.countdown = 150;
this.timeSpace = 500; this.timeSpace = 500;
console.log("props.prizes=",props.prizes); console.log("props.prizes=", props.prizes);
} }
private waterSvga private waterSvga
...@@ -121,7 +120,7 @@ export default class GameView extends engine.Container { ...@@ -121,7 +120,7 @@ export default class GameView extends engine.Container {
if (this._hasSetup) { if (this._hasSetup) {
return; return;
} }
this._hasSetup = true; this._hasSetup = true;
this.itemInfo = {}; this.itemInfo = {};
...@@ -172,17 +171,17 @@ export default class GameView extends engine.Container { ...@@ -172,17 +171,17 @@ export default class GameView extends engine.Container {
// this.container2.width = 140; // this.container2.width = 140;
// this.container2.height = 221; // this.container2.height = 221;
if(this.prizes.length > 3){ if (this.prizes.length > 3) {
this.createItem(2); this.createItem(2);
this.prizeMaxH = this.prizes.length * (props.imgSizeW + props.spaceSizeY) + props.spaceSizeY; this.prizeMaxH = this.prizes.length * (props.imgSizeW + props.spaceSizeY) + props.spaceSizeY;
}else{ } else {
this.createItem(1); this.createItem(1);
this.prizeMaxH = 4 * (props.imgSizeW + props.spaceSizeY) + props.spaceSizeY; this.prizeMaxH = 4 * (props.imgSizeW + props.spaceSizeY) + props.spaceSizeY;
} }
this.orgY = -(this.prizeMaxH - props.maskSizeH) + props.imgSizeH * 0.5; this.orgY = -(this.prizeMaxH - props.maskSizeH) + props.imgSizeH * 0.5;
for(let i = 0; i < 3; i++){ for (let i = 0; i < 3; i++) {
this['container' + i].x = i * (props.cSpaceSizeX * 2 + props.imgSizeW); this['container' + i].x = i * (props.cSpaceSizeX * 2 + props.imgSizeW);
// this['container' + i].y = this.orgY; // this['container' + i].y = this.orgY;
this['container' + i].y = this.orgY; this['container' + i].y = this.orgY;
...@@ -195,20 +194,20 @@ export default class GameView extends engine.Container { ...@@ -195,20 +194,20 @@ export default class GameView extends engine.Container {
/** /**
* type 1.小于4个奖品 2.4个及以上奖品 * type 1.小于4个奖品 2.4个及以上奖品
*/ */
private createItem(type:number):void{ private createItem(type: number): void {
let len = 4; let len = 4;
if(type == 1){ if (type == 1) {
len = 4; len = 4;
}else if(type == 2){ } else if (type == 2) {
len = this.prizes.length; len = this.prizes.length;
} }
for(let j = 0; j < 3; j++){ for (let j = 0; j < 3; j++) {
this.itemInfo["items" + j] = []; this.itemInfo["items" + j] = [];
for(let i = 0; i < len; i++){ for (let i = 0; i < len; i++) {
let url = this.prizes[i % this.prizes.length].icon; let url = this.prizes[i % this.prizes.length].icon;
let item = new engine.Sprite(); let item = new engine.Sprite();
item.texture = engine.Texture.fromImage(url); item.texture = this.getTexture(url);
this.itemInfo["items" + j][i] = item; this.itemInfo["items" + j][i] = item;
this["container" + j].addChild(item); this["container" + j].addChild(item);
...@@ -218,7 +217,7 @@ export default class GameView extends engine.Container { ...@@ -218,7 +217,7 @@ export default class GameView extends engine.Container {
item.y = props.spaceSizeY + i * (props.imgSizeH + props.spaceSizeY); item.y = props.spaceSizeY + i * (props.imgSizeH + props.spaceSizeY);
// console.log('i=', i) // console.log('i=', i)
} }
} }
} }
...@@ -228,7 +227,7 @@ export default class GameView extends engine.Container { ...@@ -228,7 +227,7 @@ export default class GameView extends engine.Container {
* 重置场景 * 重置场景
*/ */
reset() { reset() {
for(let i = 0; i < 3; i++){ for (let i = 0; i < 3; i++) {
this["container" + i].y = this.orgY; this["container" + i].y = this.orgY;
} }
} }
...@@ -238,55 +237,55 @@ export default class GameView extends engine.Container { ...@@ -238,55 +237,55 @@ export default class GameView extends engine.Container {
*/ */
start() { start() {
this.prizeId = props.prizeId; this.prizeId = props.prizeId;
console.log("this.prizeId=",this.prizeId); console.log("this.prizeId=", this.prizeId);
for(let i = 0; i < 3; i++){ for (let i = 0; i < 3; i++) {
engine.Tween.removeTweens(this["container" + i]); engine.Tween.removeTweens(this["container" + i]);
setTimeout(() => { setTimeout(() => {
engine.Tween.get(this["container" + i],{loop:true}).to({y:0}, this.countdown).call(()=>{ engine.Tween.get(this["container" + i], {loop: true}).to({y: 0}, this.countdown).call(() => {
this["container" + i].y = this.orgY; this["container" + i].y = this.orgY;
}); });
}, this.timeSpace * i); }, this.timeSpace * i);
} }
setTimeout(() => { setTimeout(() => {
for(let i = 0; i < 3; i++){ for (let i = 0; i < 3; i++) {
// this["container" + i].y = 0;//this.orgY; // this["container" + i].y = 0;//this.orgY;
setTimeout(() => { setTimeout(() => {
this["container" + i].y = this.orgY; this["container" + i].y = this.orgY;
engine.Tween.removeTweens(this["container" + i]); engine.Tween.removeTweens(this["container" + i]);
engine.Tween.get(this["container" + i]).to({y:-(props.imgSizeH * 0.5 + props.spaceSizeY)}, 1500); engine.Tween.get(this["container" + i]).to({y: -(props.imgSizeH * 0.5 + props.spaceSizeY)}, 1500);
}, this.timeSpace * i); }, this.timeSpace * i);
} }
this.setPrizePos(); this.setPrizePos();
setTimeout(() => { setTimeout(() => {
engine.globalEvent.dispatchEvent('rock-gameover'); engine.globalEvent.dispatchEvent('rock-gameover');
}, this.timeSpace * 2 + 2000); }, this.timeSpace * 2 + 2000);
}, 2500); }, 2500);
} }
private setPrizePos():void{ private setPrizePos(): void {
let direct = Math.random() > 0.5 ? 1 : -1; let direct = Math.random() > 0.5 ? 1 : -1;
for(let j = 0; j < 3; j++){ for (let j = 0; j < 3; j++) {
let len = this.itemInfo["items" + j].length; let len = this.itemInfo["items" + j].length;
direct *= -1; direct *= -1;
for(let i = 0; i < len; i++){ for (let i = 0; i < len; i++) {
let plen = this.prizes.length; let plen = this.prizes.length;
if(direct > 0){ if (direct > 0) {
this.itemInfo["items" + j][i].texture = engine.Texture.fromImage(this.prizes[(plen - 1) - (i % plen)].icon); this.itemInfo["items" + j][i].texture = this.getTexture(this.prizes[(plen - 1) - (i % plen)].icon);
// this.itemInfo["items" + j][i].source = this.prizes[(plen - 1) - (i % plen)].icon; // this.itemInfo["items" + j][i].source = this.prizes[(plen - 1) - (i % plen)].icon;
}else{ } else {
this.itemInfo["items" + j][i].texture = engine.Texture.fromImage(this.prizes[(i % plen)].icon); this.itemInfo["items" + j][i].texture = this.getTexture(this.prizes[(i % plen)].icon);
// this.itemInfo["items" + j][i].source = this.prizes[(i % plen)].icon; // this.itemInfo["items" + j][i].source = this.prizes[(i % plen)].icon;
} }
} }
} }
for(let i = 0; i < 3; i++){ for (let i = 0; i < 3; i++) {
// this.prizeId = "sss_3"; // this.prizeId = "sss_3";
// this.itemInfo["items" + i][1].texture = engine.Texture.fromImage(this.getPrizeUrl(this.prizeId)); // this.itemInfo["items" + i][1].texture = this.getTexture(this.getPrizeUrl(this.prizeId));
let p = this.itemInfo["items" + i][1].parent; let p = this.itemInfo["items" + i][1].parent;
p.removeChild(this.itemInfo["items" + i][1]); p.removeChild(this.itemInfo["items" + i][1]);
...@@ -296,7 +295,7 @@ export default class GameView extends engine.Container { ...@@ -296,7 +295,7 @@ export default class GameView extends engine.Container {
let ih = this.itemInfo["items" + i][1].height; let ih = this.itemInfo["items" + i][1].height;
let nItem = new engine.Sprite(); let nItem = new engine.Sprite();
nItem.texture = engine.Texture.fromImage(this.getPrizeUrl(this.prizeId)); nItem.texture = this.getTexture(this.getPrizeUrl(this.prizeId));
this.itemInfo["items" + i][1] = nItem; this.itemInfo["items" + i][1] = nItem;
nItem.x = ix; nItem.x = ix;
nItem.y = iy; nItem.y = iy;
...@@ -305,16 +304,15 @@ export default class GameView extends engine.Container { ...@@ -305,16 +304,15 @@ export default class GameView extends engine.Container {
p.addChild(nItem); p.addChild(nItem);
// this.itemInfo["items" + i][1].source = this.getPrizeUrl(this.prizeId); // this.itemInfo["items" + i][1].source = this.getPrizeUrl(this.prizeId);
} }
} }
private getPrizeUrl(pid:string):string{ private getPrizeUrl(pid: string): string {
let purl = ""; let purl = "";
for(let i = 0; i < 3; i++){ for (let i = 0; i < 3; i++) {
if(this.prizes[i].prizeId == pid){ if (this.prizes[i].prizeId == pid) {
return this.prizes[i].icon; return this.prizes[i].icon;
} }
} }
...@@ -326,7 +324,7 @@ export default class GameView extends engine.Container { ...@@ -326,7 +324,7 @@ export default class GameView extends engine.Container {
* 暂停 * 暂停
*/ */
pause() { pause() {
} }
/** /**
...@@ -350,4 +348,15 @@ export default class GameView extends engine.Container { ...@@ -350,4 +348,15 @@ export default class GameView extends engine.Container {
this.pause() this.pause()
} }
/**
* 获取纹理(缓存)
* @param url
*/
getTexture(url) {
let texture = this.textureCache[url];
if (!texture) {
texture = this.textureCache[url] = engine.Texture.fromImage(url);
}
return texture;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment