RingOfRaces/Tilemap_CameraView.gd

78 lines
2.0 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()
Global.current_camera = self
$dev_statistics.visible = Global.dev_stats
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
2020-09-24 19:34:34 +00:00
#function that calculates the borders/bounds of the map
2020-09-21 21:13:11 +00:00
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):
if(Global.dev_stats):
$dev_statistics/fps_stats.text = "FPS: " + str(Performance.get_monitor(Performance.TIME_FPS))
2020-09-21 18:45:28 +00:00
CameraToPlayer()
2020-09-21 19:25:56 +00:00
if once:
once = false
2020-09-24 19:34:34 +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):
2020-09-23 08:50:07 +00:00
pass
else:
position.x = x
2020-09-21 21:13:11 +00:00
if y < int(screen_size.y / 2):
2020-09-23 08:50:07 +00:00
pass
else:
position.y = y
2020-09-21 18:45:28 +00:00
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):
var tween = get_node("/root/Map1/Tween")
lockedPlayerCamera = true
2020-09-21 19:25:56 +00:00
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
func CameraToPlayer():
2020-09-21 19:25:56 +00:00
if lockedPlayerCamera == false:
MoveCamera(player.position.x, player.position.y)
2020-09-24 19:34:34 +00:00
func Update():
CameraToPlayer()