diff --git a/doc/MODS.md b/doc/MODS.md index 2230695..f55bc5d 100644 --- a/doc/MODS.md +++ b/doc/MODS.md @@ -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 diff --git a/main.tscn b/main.tscn index c09889f..e9d3ed6 100644 --- a/main.tscn +++ b/main.tscn @@ -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"] @@ -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"] @@ -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") diff --git a/scripts/multiplayer_camera.gd b/scripts/multiplayer_camera.gd new file mode 100644 index 0000000..872fd1e --- /dev/null +++ b/scripts/multiplayer_camera.gd @@ -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) diff --git a/spaces/background.tscn b/spaces/background.tscn index 233ec83..bcfed84 100644 --- a/spaces/background.tscn +++ b/spaces/background.tscn @@ -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")