Skip to content

Commit a1d098c

Browse files
authored
Merge pull request #12 from endlessm/multiplayer-camera
Multiplayer camera
2 parents 5adf7a4 + 5c7a056 commit a1d098c

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

doc/MODS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ Duplicate the `Player` node. Move it next to the existing player. Then
144144
change the Player to "Two" in the duplicated one. Now the game is
145145
multiplayer.
146146

147+
You will notice that the camera is following one of the player characters. So
148+
try disabling the `Camera2D` node that's a child of the `Player` node. And then
149+
enable the `MultiplayerCamera` node that's at the bottom of the Scene Dock.
150+
147151
### Player and flag appearance
148152

149153
Click on the `Player` node. In the inspector, observe where it says `Sprite

main.tscn

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[gd_scene load_steps=13 format=3 uid="uid://dhcpt1kt8cs0g"]
1+
[gd_scene load_steps=14 format=3 uid="uid://dhcpt1kt8cs0g"]
22

33
[ext_resource type="PackedScene" uid="uid://8st4scqt06l8" path="res://components/player/player.tscn" id="2_7yl00"]
44
[ext_resource type="PackedScene" uid="uid://jnrusvm3gric" path="res://spaces/background.tscn" id="2_tb5a2"]
@@ -12,6 +12,7 @@
1212
[ext_resource type="PackedScene" uid="uid://dk0xon0k7ga23" path="res://components/enemy/enemy.tscn" id="9_l6smt"]
1313
[ext_resource type="SpriteFrames" uid="uid://bo581k1esb50n" path="res://components/player/spriteframes-red.tres" id="9_qmofe"]
1414
[ext_resource type="PackedScene" uid="uid://beuisy5yrw0bq" path="res://components/flag/flag.tscn" id="12_dkbog"]
15+
[ext_resource type="Script" path="res://scripts/multiplayer_camera.gd" id="13_0d2mj"]
1516

1617
[node name="Main" type="Node2D"]
1718

@@ -221,3 +222,11 @@ position = Vector2(4096, 960)
221222

222223
[node name="Flag" parent="." instance=ExtResource("12_dkbog")]
223224
position = Vector2(4192, 320)
225+
226+
[node name="MultiplayerCamera" type="Camera2D" parent="."]
227+
position = Vector2(0, 15)
228+
enabled = false
229+
limit_left = 0
230+
limit_bottom = 1080
231+
position_smoothing_enabled = true
232+
script = ExtResource("13_0d2mj")

scripts/multiplayer_camera.gd

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class_name MultiplayerCamera
2+
extends Camera2D
3+
4+
const FRAME_MARGIN: int = 128
5+
const MIN_ZOOM: float = 1.0
6+
const MAX_ZOOM: float = 0.4
7+
8+
var player_characters: Array[Player]
9+
10+
@onready var viewport_size: Vector2 = get_viewport_rect().size
11+
12+
13+
func _ready():
14+
if not enabled or not is_current():
15+
set_process(false)
16+
return
17+
18+
for pc in get_tree().get_nodes_in_group("players"):
19+
player_characters.append(pc as Player)
20+
21+
22+
func _physics_process(delta: float):
23+
if not player_characters:
24+
return
25+
26+
var new_position: Vector2 = Vector2.ZERO
27+
for pc: Player in player_characters:
28+
new_position += pc.position
29+
new_position /= player_characters.size()
30+
if position_smoothing_enabled:
31+
position = lerp(position, new_position, position_smoothing_speed * delta)
32+
else:
33+
position = new_position
34+
35+
var frame = Rect2(position, Vector2.ONE)
36+
for pc: Player in player_characters:
37+
frame = frame.expand(pc.position)
38+
frame = frame.grow_individual(FRAME_MARGIN, FRAME_MARGIN, FRAME_MARGIN, FRAME_MARGIN)
39+
40+
var new_zoom: float
41+
if frame.size.x > frame.size.y * viewport_size.aspect():
42+
new_zoom = clamp(viewport_size.x / frame.size.x, MAX_ZOOM, MIN_ZOOM)
43+
else:
44+
new_zoom = clamp(viewport_size.y / frame.size.y, MAX_ZOOM, MIN_ZOOM)
45+
zoom = lerp(zoom, Vector2.ONE * new_zoom, 0.5)

spaces/background.tscn

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,24 @@ fill_to = Vector2(0, 1)
1616

1717
[node name="Background" type="ParallaxBackground"]
1818
follow_viewport_enabled = true
19+
scroll_ignore_camera_zoom = true
1920
script = ExtResource("1_1jh5j")
2021

2122
[node name="ParallaxLayer" type="ParallaxLayer" parent="."]
2223
unique_name_in_owner = true
23-
motion_scale = Vector2(-1, -1)
24+
motion_scale = Vector2(0, 0)
25+
motion_mirroring = Vector2(1600, 0)
26+
27+
[node name="ColorRect" type="ColorRect" parent="ParallaxLayer"]
28+
anchors_preset = 15
29+
anchor_right = 1.0
30+
anchor_bottom = 1.0
31+
offset_top = -2352.0
32+
offset_right = 5129.0
33+
offset_bottom = 98.0
34+
grow_horizontal = 2
35+
grow_vertical = 2
36+
color = Color(0.980392, 0.980392, 0.980392, 1)
2437

2538
[node name="Sprite2D" type="Sprite2D" parent="ParallaxLayer"]
2639
texture = SubResource("GradientTexture2D_ljotv")

0 commit comments

Comments
 (0)