1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- extends Node2D
- onready var cam = $MainCamera
- onready var level = $Grid
- var dist_max = 900
- var zoom_rate = 0.001
- var playerScene = preload("res://Player.tscn")
- func _ready():
- var player
- # Player 1
- player = playerScene.instance()
- player.grid = level
- player.turn_left_action = "player_1_turn_left"
- player.turn_right_action = "player_1_turn_right"
- player.position = Vector2(4 * 64 + 32, 8 * 64 + 32)
- add_child(player)
- # Player 2
- player = playerScene.instance()
- player.grid = level
- player.turn_left_action = "player_2_turn_left"
- player.turn_right_action = "player_2_turn_right"
- player.position = Vector2(6 * 64 + 32, 8 * 64 + 32)
- add_child(player)
- func _process(d):
- center_camera()
- func center_camera():
- var position_accumulator = Vector2(0.0, 0.0)
- var players = get_tree().get_nodes_in_group("players")
- var living = get_tree().get_nodes_in_group("living")
- if living.size() == 0:
- # Keep camera in the last position
- return
- for p in living:
- position_accumulator += p.position
- cam.position = position_accumulator / living.size()
- var dist = 0
- for i in range(living.size() - 1):
- for j in range(i + 1, living.size()):
- dist = max(dist, living[i].position.distance_to(living[j].position))
- # Extend camera zoom if liners are far from each other
- var zoom = 1.0
- if dist > dist_max:
- zoom += (dist - dist_max) * zoom_rate
- cam.zoom = Vector2(zoom, zoom)
|