Skip to content

Commit

Permalink
Merge pull request #12 from endlessm/multiplayer-camera
Browse files Browse the repository at this point in the history
Multiplayer camera
  • Loading branch information
starnight authored Nov 12, 2024
2 parents 5adf7a4 + 5c7a056 commit a1d098c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
4 changes: 4 additions & 0 deletions doc/MODS.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ Duplicate the `Player` node. Move it next to the existing player. Then
change the Player to "Two" in the duplicated one. Now the game is
multiplayer.

You will notice that the camera is following one of the player characters. So
try disabling the `Camera2D` node that's a child of the `Player` node. And then
enable the `MultiplayerCamera` node that's at the bottom of the Scene Dock.

### Player and flag appearance

Click on the `Player` node. In the inspector, observe where it says `Sprite
Expand Down
11 changes: 10 additions & 1 deletion main.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=13 format=3 uid="uid://dhcpt1kt8cs0g"]
[gd_scene load_steps=14 format=3 uid="uid://dhcpt1kt8cs0g"]

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

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

Expand Down Expand Up @@ -221,3 +222,11 @@ position = Vector2(4096, 960)

[node name="Flag" parent="." instance=ExtResource("12_dkbog")]
position = Vector2(4192, 320)

[node name="MultiplayerCamera" type="Camera2D" parent="."]
position = Vector2(0, 15)
enabled = false
limit_left = 0
limit_bottom = 1080
position_smoothing_enabled = true
script = ExtResource("13_0d2mj")
45 changes: 45 additions & 0 deletions scripts/multiplayer_camera.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class_name MultiplayerCamera
extends Camera2D

const FRAME_MARGIN: int = 128
const MIN_ZOOM: float = 1.0
const MAX_ZOOM: float = 0.4

var player_characters: Array[Player]

@onready var viewport_size: Vector2 = get_viewport_rect().size


func _ready():
if not enabled or not is_current():
set_process(false)
return

for pc in get_tree().get_nodes_in_group("players"):
player_characters.append(pc as Player)


func _physics_process(delta: float):
if not player_characters:
return

var new_position: Vector2 = Vector2.ZERO
for pc: Player in player_characters:
new_position += pc.position
new_position /= player_characters.size()
if position_smoothing_enabled:
position = lerp(position, new_position, position_smoothing_speed * delta)
else:
position = new_position

var frame = Rect2(position, Vector2.ONE)
for pc: Player in player_characters:
frame = frame.expand(pc.position)
frame = frame.grow_individual(FRAME_MARGIN, FRAME_MARGIN, FRAME_MARGIN, FRAME_MARGIN)

var new_zoom: float
if frame.size.x > frame.size.y * viewport_size.aspect():
new_zoom = clamp(viewport_size.x / frame.size.x, MAX_ZOOM, MIN_ZOOM)
else:
new_zoom = clamp(viewport_size.y / frame.size.y, MAX_ZOOM, MIN_ZOOM)
zoom = lerp(zoom, Vector2.ONE * new_zoom, 0.5)
15 changes: 14 additions & 1 deletion spaces/background.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,24 @@ fill_to = Vector2(0, 1)

[node name="Background" type="ParallaxBackground"]
follow_viewport_enabled = true
scroll_ignore_camera_zoom = true
script = ExtResource("1_1jh5j")

[node name="ParallaxLayer" type="ParallaxLayer" parent="."]
unique_name_in_owner = true
motion_scale = Vector2(-1, -1)
motion_scale = Vector2(0, 0)
motion_mirroring = Vector2(1600, 0)

[node name="ColorRect" type="ColorRect" parent="ParallaxLayer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_top = -2352.0
offset_right = 5129.0
offset_bottom = 98.0
grow_horizontal = 2
grow_vertical = 2
color = Color(0.980392, 0.980392, 0.980392, 1)

[node name="Sprite2D" type="Sprite2D" parent="ParallaxLayer"]
texture = SubResource("GradientTexture2D_ljotv")
Expand Down

0 comments on commit a1d098c

Please sign in to comment.