RingOfRaces/Tilemap_CameraView.gd

73 lines
1.8 KiB
GDScript3
Raw Normal View History

2020-09-21 18:45:28 +00:00
extends Camera2D
onready var player = get_node("/root/Map1/Player")
2020-09-21 21:13:11 +00:00
onready var background_map = get_node("/root/Map1/background")
onready var screen_size = self.get_viewport_rect().size
2020-09-21 18:45:28 +00:00
func _ready():
2020-09-21 21:13:11 +00:00
calculate_bounds()
print(screen_size)
2020-09-21 18:45:28 +00:00
2020-09-21 19:25:56 +00:00
var once = true
var lockedPlayerCamera = false
2020-09-21 21:13:11 +00:00
var min_x = 0
var min_y = 0
var max_x = 0
var max_y = 0
var max_x_pixel = 0
var max_y_pixel = 0
func calculate_bounds():
var used_cells = background_map.get_used_cells()
for pos in used_cells:
if pos.x < min_x:
min_x = int(pos.x)
elif pos.x > max_x:
max_x = int(pos.x)
if pos.y < min_y:
min_y = int(pos.y)
elif pos.y > max_y:
max_y = int(pos.y)
print(min_x,"-",max_x, " AND " ,min_y , "-" , max_y)
max_x_pixel = (max_x * 32)
max_y_pixel = (max_y * 32)
2020-09-21 18:45:28 +00:00
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
CameraToPlayer()
2020-09-21 19:25:56 +00:00
if once:
once = false
2020-09-21 21:13:11 +00:00
AnimateMoveCamera(player.position, Vector2(player.position.x - 100,player.position.y - 10), "position", 2)
2020-09-21 18:45:28 +00:00
pass
2020-09-21 21:13:11 +00:00
func get_global_pos():
return Vector2(position.x, position.y)
2020-09-21 18:45:28 +00:00
#Move camera to position
func MoveCamera(x, y):
2020-09-21 21:13:11 +00:00
if x < int(screen_size.x / 2):
print("passing")
return
if y < int(screen_size.y / 2):
print("passing")
return
2020-09-21 18:45:28 +00:00
position.x = x
position.y = y
2020-09-21 19:25:56 +00:00
func _on_Tween_tween_completed(object, key):
print(object, key)
lockedPlayerCamera = false
2020-09-21 18:45:28 +00:00
2020-09-21 19:25:56 +00:00
func AnimateMoveCamera(source, destination, key, time):
lockedPlayerCamera = true
var tween = get_node("/root/Map1/Tween")
tween.interpolate_property(get_node("/root/Map1/Camera2D"), key, source, destination, time, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
tween.start()
2020-09-21 18:45:28 +00:00
#Move camera close to player
func CameraToPlayer():
2020-09-21 19:25:56 +00:00
if lockedPlayerCamera == false:
MoveCamera(player.position.x, player.position.y)