Browse Source

Move code from Game to Camera

It is a more relevant place, make the game lighter and code easier to
read.
DricomDragon 5 years ago
parent
commit
6156d5318f
3 changed files with 45 additions and 41 deletions
  1. 0 40
      Game.gd
  2. 3 1
      Game.tscn
  3. 42 0
      MainCamera.gd

+ 0 - 40
Game.gd

@@ -1,8 +1,6 @@
 extends Node2D
 
 onready var cam = $MainCamera
-export var dist_max = 900
-export var zoom_rate = 0.001
 
 var levelNames = ["res://Levels/Lab.tscn", "res://Levels/Square.tscn"]
 var levelScenes = []
@@ -17,9 +15,6 @@ func _ready():
 	for k in range(levelNames.size()):
 		levelScenes.append(null)
 
-func _process(_d):
-	move_camera()
-
 func _unhandled_input(event):
 	if event.is_action("ui_accept") and event.is_pressed():
 		if get_tree().has_group("living"):
@@ -67,38 +62,3 @@ func start():
 
 	# TODO Timer
 	get_tree().call_group("players", "_on_game_start")
-
-func move_camera():
-	if !get_tree().has_group("players"):
-		return
-
-	var living
-	if get_tree().has_group("living"):
-		living = get_tree().get_nodes_in_group("living")
-
-	if !living or living.size() == 0:
-		# Show every players
-		var players = get_tree().get_nodes_in_group("players")
-		center_on(players)
-	else:
-		# Show remaining players only
-		center_on(living)
-
-func center_on(players):
-	var position_accumulator = Vector2(0.0, 0.0)
-	for p in players:
-		position_accumulator += p.position
-
-	cam.position = position_accumulator / players.size()
-
-	var dist = 0
-	for i in range(players.size() - 1):
-		for j in range(i + 1, players.size()):
-			dist = max(dist, players[i].position.distance_to(players[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)

+ 3 - 1
Game.tscn

@@ -1,5 +1,6 @@
-[gd_scene load_steps=2 format=2]
+[gd_scene load_steps=3 format=2]
 
+[ext_resource path="res://MainCamera.gd" type="Script" id=1]
 [ext_resource path="res://Game.gd" type="Script" id=3]
 
 [node name="Game" type="Node2D"]
@@ -8,3 +9,4 @@ script = ExtResource( 3 )
 [node name="MainCamera" type="Camera2D" parent="."]
 current = true
 smoothing_enabled = true
+script = ExtResource( 1 )

+ 42 - 0
MainCamera.gd

@@ -0,0 +1,42 @@
+extends Camera2D
+
+export var dist_max = 900
+export var zoom_rate = 0.001
+
+func _process(_d):
+	move()
+
+func move():
+	if !get_tree().has_group("players"):
+		return
+
+	var living
+	if get_tree().has_group("living"):
+		living = get_tree().get_nodes_in_group("living")
+
+	if !living or living.size() == 0:
+		# Show every players
+		var players = get_tree().get_nodes_in_group("players")
+		center_on(players)
+	else:
+		# Show remaining players only
+		center_on(living)
+
+func center_on(players):
+	var position_accumulator = Vector2(0.0, 0.0)
+	for p in players:
+		position_accumulator += p.position
+
+	position = position_accumulator / players.size()
+
+	var dist = 0
+	for i in range(players.size() - 1):
+		for j in range(i + 1, players.size()):
+			dist = max(dist, players[i].position.distance_to(players[j].position))
+
+	# Extend camera zoom if liners are far from each other
+	var new_zoom = 1.0
+	if dist > dist_max:
+		new_zoom += (dist - dist_max) * zoom_rate
+
+	zoom = Vector2(new_zoom, new_zoom)