Created
May 9, 2019 15:10
-
-
Save kane-thornwyrd/e2469298a6906969cb0511a53687a6b3 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
extends Camera2D | |
const LOOK_AHEAD_FACTOR:float = 0.45 | |
export var tilemap_path:NodePath | |
onready var tilemap = self.get_node(tilemap_path) | |
onready var camera_previous_position = self.get_camera_position() | |
var facing:int = 0 | |
func _on_ready() -> void: | |
var tilemap_rect = tilemap.get_used_rect() | |
var tilemap_cell_size = tilemap.cell_size | |
self.limit_left = tilemap_rect.position.x * tilemap_cell_size.x * tilemap.scale.x | |
self.limit_right = tilemap_rect.end.x * tilemap_cell_size.x * tilemap.scale.x | |
self.limit_top = tilemap_rect.position.y * tilemap_cell_size.y * tilemap.scale.y | |
self.limit_bottom = tilemap_rect.end.y * tilemap_cell_size.y * tilemap.scale.y | |
# warning-ignore:unused_argument | |
func _process(delta: float) -> void: | |
_check_facing() | |
camera_previous_position = self.get_camera_position() | |
func _check_facing(): | |
var new_facing = sign(self.get_camera_position().x - camera_previous_position.x) | |
if new_facing != 0 and facing != new_facing: | |
facing = new_facing | |
var target_offset = self.get_viewport_rect().size.x * LOOK_AHEAD_FACTOR * facing | |
var left_screen_border = self.get_camera_position().x - self.get_viewport_rect().size.x/2 | |
var right_screen_border = self.get_camera_position().x + self.get_viewport_rect().size.x/2 | |
if (facing == 1 and left_screen_border >= self.limit_right) or\ | |
(facing == -1 and right_screen_border <= self.limit_left) : | |
position.x = target_offset | |
func _on_player_grounded_updated(is_grounded): | |
drag_margin_v_enabled = !is_grounded |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment