Last active
August 23, 2018 20:55
-
-
Save WesleiRamos/57eea90b7644269e9e7a22e175aa545d to your computer and use it in GitHub Desktop.
Camera follow player using openfl
This file contains hidden or 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 utils; | |
| import motion.Actuate; | |
| import motion.easing.Quad; | |
| import openfl.geom.Rectangle; | |
| import openfl.display.Sprite; | |
| import openfl.Lib; | |
| class CameraFollow extends Sprite | |
| { | |
| // Medidas tela | |
| private var sw2 : Float; // Screen Width / 2 | |
| private var sh2 : Float; // Screen Height / 2 | |
| // Medidas do mundo | |
| private var medidaMaximaX : Int; | |
| private var medidaMaximaY : Int; | |
| private var maxX : Int; | |
| private var maxY : Int; | |
| private var minX : Int; | |
| private var minY : Int; | |
| // Cena | |
| private var cena : Sprite; | |
| // Objeto que será observado | |
| private var observe : Sprite; | |
| /** | |
| * Cria uma camera que segue um objeto | |
| * | |
| * @param scene Objeto cena | |
| * @param medMinX Medida minima no eixo X | |
| * @param medMinY Medida minima no eixo Y | |
| * @param medMaxX Medida máxima no eixo X | |
| * @param medMaxY Medida máxima no eixo Y | |
| */ | |
| public function new(scene: Sprite, medMinX: Int = 0, medMinY = 0, medMaxX : Int = 0, medMaxY : Int = 0) | |
| { | |
| super(); | |
| this.cena = scene; | |
| this.cena.scrollRect = new Rectangle(0, 0, Lib.current.stage.stageWidth, Lib.current.stage.stageHeight); | |
| // Salva X e Y pra redimensionar depois | |
| this.medidaMaximaX = medMaxX; | |
| this.medidaMaximaY = medMaxY; | |
| this.minX = medMinX; | |
| this.minY = medMinY; | |
| } | |
| /** | |
| * Determina o objeto que será observado | |
| * | |
| * @param obj Sprite | |
| */ | |
| public function observar(obj : Sprite) { | |
| // Objeto que será observado | |
| this.observe = obj; | |
| // Gera as medidas padrão | |
| this.onResize(); | |
| } | |
| /** | |
| * Atualiza as medidas da câmera | |
| */ | |
| public function onResize() : Void { | |
| // Metade da tela menos a matade da medida do obervado | |
| this.sw2 = (Lib.current.stage.stageWidth / 2); | |
| this.sh2 = (Lib.current.stage.stageHeight / 2); | |
| this.maxX = this.medidaMaximaX - Lib.current.stage.stageWidth; | |
| this.maxY = this.medidaMaximaY - Lib.current.stage.stageHeight; | |
| } | |
| public function atualizarCamera(deltaTime: Float) { | |
| deltaTime *= 0.6; | |
| var nx = this.observe.x - this.sw2; | |
| var ny = this.observe.y - this.sh2; | |
| /* | |
| * Se caso "nx" for menor que a medida minima, então "nx" deve ser igual | |
| * a medida minima | |
| * | |
| * Caso "nx" for maior que a medida máxima, então "nx" deve ser igual a | |
| * medida máxima - tamanho da tela | |
| * | |
| * mesmo se aplica ao eixo Y | |
| */ | |
| if (nx > this.maxX) nx = this.maxX; | |
| else if (nx < this.minX) nx = this.minX; | |
| if (ny > this.maxY) ny = this.maxY; | |
| else if (ny < this.minY) ny = this.minY; | |
| if (nx == this.cena.scrollRect.x || ny == this.cena.scrollRect.y) { | |
| trace("RETORNANDO..."); | |
| return; | |
| } | |
| // Move a camera de forma mais suave | |
| nx = this.cena.scrollRect.x + ((nx - this.cena.scrollRect.x) * deltaTime); | |
| ny = this.cena.scrollRect.y + ((ny - this.cena.scrollRect.y) * deltaTime); | |
| this.atualizaPosicaoCamera(nx, ny); | |
| } | |
| private function atualizaPosicaoCamera(x:Float, y:Float) { | |
| this.cena.scrollRect = new Rectangle(x, y, Lib.current.stage.stageWidth, Lib.current.stage.stageHeight); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment