Limit Camera by TileMap

This commit is contained in:
2023-06-04 15:23:15 -04:00
parent 06574ac10e
commit af6dd97cb3
2 changed files with 41 additions and 3 deletions

27
game_arena.gd Normal file
View File

@@ -0,0 +1,27 @@
extends Node2D
var camera_follows :Node2D= null
# Called when the node enters the scene tree for the first time.
func _ready():
camera_follows = $Player
# Limit Camera bounds to the TileMap
var map_rect :Rect2i= $TileMap.get_used_rect()
var map_tile_size = $TileMap.tile_set.tile_size
var map_scale_x :float= $TileMap.scale.x * map_tile_size.x
var map_scale_y :float= $TileMap.scale.y * map_tile_size.y
var left_top = map_rect.position
var right_bottom = map_rect.end
$Camera2D.limit_left = left_top[0] * map_scale_x
$Camera2D.limit_top = left_top[1] * map_scale_y
$Camera2D.limit_right = right_bottom[0] * map_scale_x
$Camera2D.limit_bottom = right_bottom[1] * map_scale_y
func camera_follow():
if camera_follows == null:
return
$Camera2D.position = camera_follows.position
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
camera_follow()

File diff suppressed because one or more lines are too long