97 lines
3.3 KiB
GDScript3
97 lines
3.3 KiB
GDScript3
extends Camera2D
|
|
|
|
|
|
# Declare member variables here. Examples:
|
|
# var a = 2
|
|
# var b = "text"
|
|
export var debug_component :bool = false
|
|
|
|
var game_size := Vector2(320,180)
|
|
onready var window_scale :float = (OS.window_size / game_size).x
|
|
|
|
onready var actual_cam_pos := global_position
|
|
|
|
export var acceleration := 2
|
|
export var deceleration := 800.0
|
|
export var max_speed := 60.0
|
|
export var snap_unit := 10
|
|
|
|
export var tracking_node_path :NodePath
|
|
var _tracking_node :KinematicBody2D
|
|
|
|
func _ready():
|
|
_tracking_node = get_node(tracking_node_path)
|
|
|
|
var velocity :float = 0
|
|
var tracking_pos := Vector2(0,0)
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta):
|
|
#var tracking_pos = PlayerInfo.player_position
|
|
# var tracking_pos = lerp(global_position, _tracking_node.global_position , 0.7)
|
|
#var tracking_pos = _tracking_node.global_position.round()
|
|
var tp = _tracking_node.global_position.floor()
|
|
|
|
var snapped_target_pos = Vector2(
|
|
round(tp.x / snap_unit) * snap_unit,
|
|
round(tp.y / snap_unit) * snap_unit
|
|
)
|
|
|
|
# if tracking_pos.distance_to(tp) >= 4: # tile width?
|
|
# tracking_pos = tp.snapped(Vector2(2,2))
|
|
tracking_pos = snapped_target_pos
|
|
|
|
# actual_cam_pos = lerp(actual_cam_pos, tracking_pos, delta * 5)
|
|
#actual_cam_pos = lerp(actual_cam_pos, tracking_pos, .2)
|
|
var distance_to := global_position.distance_to(tracking_pos)
|
|
|
|
if distance_to < snap_unit:
|
|
velocity = 1
|
|
else:
|
|
velocity += delta * distance_to
|
|
|
|
|
|
# Clamp to max speed
|
|
if velocity > max_speed:
|
|
velocity = max_speed
|
|
|
|
actual_cam_pos = actual_cam_pos.move_toward(tracking_pos, velocity)
|
|
#actual_cam_pos = global_position
|
|
#actual_cam_pos = tracking_pos.floor()
|
|
#position = actual_cam_pos
|
|
#var cam_subpixel_pos = actual_cam_pos.round() - actual_cam_pos
|
|
var cam_subpixel_pos :Vector2 = global_position - tracking_pos
|
|
#cam_subpixel_pos = cam_subpixel_pos.snapped(Vector2(0.2,0.2))
|
|
global_position = actual_cam_pos.floor()
|
|
|
|
#LevelInfo.viewport_container.material.set_shader_param("cam_offset", cam_subpixel_pos )
|
|
|
|
#global_position = actual_cam_pos.round()
|
|
|
|
if debug_component:
|
|
UiManager.debug_text = ("SP: " + str(cam_subpixel_pos.snapped(Vector2(0.01,0.01))) +
|
|
"\nCP: " + str (global_position.snapped(Vector2(0.1,0.1))) +
|
|
"\nTP: " + str(tracking_pos) +
|
|
"\nDT: " + str(distance_to)
|
|
)
|
|
|
|
|
|
## The original Algo
|
|
# # First we get the "real" position of the mouse cursor and then the offset to the player
|
|
# # To do that, we need to divide the mouse position inside the local viewport by the game window scale
|
|
# # Then substract half of the game size and add the player position
|
|
#
|
|
# var mouse_pos = _global.viewport.get_mouse_position() / window_scale - (game_size/2) + player.global_position
|
|
# # Using a lerp, the cameras position is moved towards the mouse position
|
|
# var cam_pos = lerp( player.global_position, mouse_pos, 0.7)
|
|
#
|
|
# # Use another lerp to make the movement smooth
|
|
# actual_cam_pos = lerp(actual_cam_pos, cam_pos, 5*delta)#
|
|
# # Calculate the "subpixel" position of the new camera position
|
|
# var cam_subpixel_pos = actual_cam_pos.round() - actual_cam_pos
|
|
#
|
|
# # Update the Main ViewportContainer's shader uniform
|
|
# _global.viewport_container.material.set_shader_param("cam_offset", cam_subpixel_pos )
|
|
#
|
|
# # Set the camera's position to the new position and round it.
|
|
# global_position = actual_cam_pos.round()
|