Created
August 14, 2016 13:22
-
-
Save starry-abyss/aacfb5a972046c31986c55333d83b288 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
import flixel.FlxG; | |
import flixel.system.scaleModes.BaseScaleMode; | |
class MedvedScaleMode extends BaseScaleMode | |
{ | |
/*private var fixedWidth:Bool = false; | |
private var fixedHeight:Bool = false;*/ | |
var tileSize: Int; | |
public var zoom: Int = 1; | |
var zoomX: Float = 1; | |
var zoomY: Float = 1; | |
var referenceTileCountInHeight: Float = 8.5; | |
var referenceTileCountInBottom: Float = 2.5; | |
var tileCountInHeight: Float = 0; | |
public function new(tileSize: Int /*fixedWidth:Bool = false, fixedHeight:Bool = false*/) | |
{ | |
super(); | |
/*this.fixedWidth = fixedWidth; | |
this.fixedHeight = fixedHeight;*/ | |
this.tileSize = tileSize; | |
gameSize.set(FlxG.width * FlxG.initialZoom, FlxG.height * FlxG.initialZoom); | |
} | |
override public function onMeasure(Width:Int, Height:Int):Void | |
{ | |
/*FlxG.width = fixedWidth ? FlxG.initialWidth : Math.ceil(Width / FlxG.initialZoom); | |
FlxG.height = fixedHeight ? FlxG.initialHeight : Math.ceil(Height / FlxG.initialZoom);*/ | |
tileCountInHeight = 0; | |
for (testZoom in 1...(Math.ceil(Height / FlxG.initialHeight) + 1)) | |
{ | |
var testTileCountInHeight: Float = Height / (testZoom * tileSize); | |
if (Math.abs(testTileCountInHeight - referenceTileCountInHeight) < Math.abs(tileCountInHeight - referenceTileCountInHeight)) | |
{ | |
tileCountInHeight = testTileCountInHeight; | |
zoom = testZoom; | |
} | |
} | |
//FlxG.initialZoom = 2; | |
//zoom = 2; | |
//FlxG.width = FlxG.initialWidth; | |
//FlxG.height = FlxG.initialHeight; | |
FlxG.width = Math.ceil(Width / zoom); | |
FlxG.height = Math.ceil(Height / zoom); | |
zoomX = Width / FlxG.width; | |
zoomY = Height / FlxG.height; | |
updateGameSize(Width, Height); | |
updateDeviceSize(Width, Height); | |
updateScaleOffset(); | |
updateGamePosition(); | |
} | |
override private function updateGameSize(Width:Int, Height:Int):Void | |
{ | |
gameSize.x = FlxG.width * zoom; | |
gameSize.y = FlxG.height * zoom; | |
if (FlxG.camera != null) | |
{ | |
var oldWidth:Float = FlxG.camera.width; | |
var oldHeight:Float = FlxG.camera.height; | |
FlxG.camera.setSize(FlxG.width, FlxG.height); | |
FlxG.camera.scroll.x += 0.5 * (oldWidth - FlxG.width); | |
FlxG.camera.scroll.y += 0.5 * (oldHeight - FlxG.height); | |
//FlxG.camera.zoom = zoom; | |
updateCameraTarget(); | |
} | |
} | |
public function updateCameraTarget(): Void | |
{ | |
if ((FlxG.camera != null) && (FlxG.camera.target != null)) | |
{ | |
FlxG.camera.targetOffset.set(0, FlxG.camera.target.height / 2 + tileSize * (referenceTileCountInBottom - tileCountInHeight / 2)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment