|
| 1 | +/// <reference path="../Game.ts" /> |
| 2 | +/// <reference path="../geom/Quad.ts" /> |
| 3 | + |
| 4 | +/** |
| 5 | +* Phaser - ScrollRegion |
| 6 | +* |
| 7 | +* Creates a scrolling region within a ScrollZone. |
| 8 | +* It is scrolled via the scrollSpeed.x/y properties. |
| 9 | +*/ |
| 10 | + |
| 11 | +module Phaser { |
| 12 | + |
| 13 | + export class ScrollRegion{ |
| 14 | + |
| 15 | + constructor(x: number, y: number, width: number, height: number, speedX:number, speedY:number) { |
| 16 | + |
| 17 | + // Our seamless scrolling quads |
| 18 | + this._A = new Quad(0, 0, width, height); |
| 19 | + this._B = new Quad(); |
| 20 | + this._C = new Quad(); |
| 21 | + this._D = new Quad(); |
| 22 | + |
| 23 | + this._scroll = new MicroPoint(); |
| 24 | + this._offset = new MicroPoint(x, y); |
| 25 | + |
| 26 | + this.scrollSpeed = new MicroPoint(speedX, speedY); |
| 27 | + this.bounds = new Quad(0, 0, width, height); |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + private _A: Quad; |
| 32 | + private _B: Quad; |
| 33 | + private _C: Quad; |
| 34 | + private _D: Quad; |
| 35 | + |
| 36 | + private _scroll: MicroPoint; |
| 37 | + private _offset: MicroPoint; |
| 38 | + |
| 39 | + private _anchorWidth: number = 0; |
| 40 | + private _anchorHeight: number = 0; |
| 41 | + private _inverseWidth: number = 0; |
| 42 | + private _inverseHeight: number = 0; |
| 43 | + |
| 44 | + public bounds: Quad; |
| 45 | + public visible: bool = true; |
| 46 | + public scrollSpeed: MicroPoint; |
| 47 | + |
| 48 | + public update(delta: number) { |
| 49 | + |
| 50 | + this._scroll.x = Math.round(this._scroll.x + (this.scrollSpeed.x)); |
| 51 | + this._scroll.y = Math.round(this._scroll.y + (this.scrollSpeed.y)); |
| 52 | + |
| 53 | + if (this._scroll.x > this._offset.x + this.bounds.width) |
| 54 | + { |
| 55 | + this._scroll.x = this._offset.x; |
| 56 | + } |
| 57 | + |
| 58 | + if (this._scroll.x < this._offset.x) |
| 59 | + { |
| 60 | + this._scroll.x = this._offset.x + this.bounds.width; |
| 61 | + } |
| 62 | + |
| 63 | + if (this._scroll.y > this._offset.y + this.bounds.height) |
| 64 | + { |
| 65 | + this._scroll.y = this._offset.y; |
| 66 | + } |
| 67 | + |
| 68 | + if (this._scroll.y < this._offset.y) |
| 69 | + { |
| 70 | + this._scroll.y = this._offset.y + this.bounds.height; |
| 71 | + } |
| 72 | + |
| 73 | + // Anchor Dimensions |
| 74 | + this._anchorWidth = this.bounds.width - this._scroll.x; |
| 75 | + this._anchorHeight = this.bounds.height - this._scroll.y; |
| 76 | + |
| 77 | + if (this._anchorWidth > this.bounds.width) |
| 78 | + { |
| 79 | + this._anchorWidth = this.bounds.width; |
| 80 | + } |
| 81 | + |
| 82 | + if (this._anchorHeight > this.bounds.height) |
| 83 | + { |
| 84 | + this._anchorHeight = this.bounds.height; |
| 85 | + } |
| 86 | + |
| 87 | + this._inverseWidth = this.bounds.width - this._anchorWidth; |
| 88 | + this._inverseHeight = this.bounds.height - this._anchorHeight; |
| 89 | + |
| 90 | + // Quad A |
| 91 | + this._A.setTo(this._scroll.x, this._scroll.y, this._anchorWidth, this._anchorHeight); |
| 92 | + |
| 93 | + // Quad B |
| 94 | + this._B.y = this._scroll.y; |
| 95 | + this._B.width = this._inverseWidth; |
| 96 | + this._B.height = this._anchorHeight; |
| 97 | + |
| 98 | + // Quad C |
| 99 | + this._C.x = this._scroll.x; |
| 100 | + this._C.width = this._anchorWidth; |
| 101 | + this._C.height = this._inverseHeight; |
| 102 | + |
| 103 | + // Quad D |
| 104 | + this._D.width = this._inverseWidth; |
| 105 | + this._D.height = this._inverseHeight; |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + public render(context:CanvasRenderingContext2D, texture, dx: number, dy: number, dw: number, dh: number) { |
| 110 | + |
| 111 | + if (this.visible == false) |
| 112 | + { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + this.crop(context, texture, this._A.x, this._A.y, this._A.width, this._A.height, dx, dy, dw, dh, 0, 0); |
| 117 | + this.crop(context, texture, this._B.x, this._B.y, this._B.width, this._B.height, dx, dy, dw, dh, this._A.width, 0); |
| 118 | + this.crop(context, texture, this._C.x, this._C.y, this._C.width, this._C.height, dx, dy, dw, dh, 0, this._A.height); |
| 119 | + this.crop(context, texture, this._D.x, this._D.y, this._D.width, this._D.height, dx, dy, dw, dh, this._C.width, this._A.height); |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + private crop(context, texture, srcX, srcY, srcW, srcH, destX, destY, destW, destH, offsetX, offsetY) { |
| 124 | + |
| 125 | + offsetX += destX; |
| 126 | + offsetY += destY; |
| 127 | + |
| 128 | + if (srcW > (destX + destW) - offsetX) |
| 129 | + { |
| 130 | + srcW = (destX + destW) - offsetX; |
| 131 | + } |
| 132 | + |
| 133 | + if (srcH > (destY + destH) - offsetY) |
| 134 | + { |
| 135 | + srcH = (destY + destH) - offsetY; |
| 136 | + } |
| 137 | + |
| 138 | + if (srcW > 0 && srcH > 0) |
| 139 | + { |
| 140 | + context.drawImage(texture, srcX, srcY, srcW, srcH, offsetX, offsetY, srcW, srcH); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments