From ae75c64236c63c835fcae1942a448d0e88a7d2d4 Mon Sep 17 00:00:00 2001 From: Squinty Date: Wed, 24 Apr 2024 07:45:04 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20flag=20grabbing/carrying=20sc?= =?UTF-8?q?oring=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/terrain_3d/editor/components/ui.gd | 40 +++++++++++------------ components/flag_carry_component.gd | 3 +- entities/flag/flag.gd | 14 +++++++- entities/flag/flag.tscn | 2 +- entities/player/player.tscn | 3 +- maps/genesis/genesis.tscn | 2 +- modes/multiplayer.gd | 27 +++++++++++++-- modes/multiplayer.tscn | 4 +-- 8 files changed, 65 insertions(+), 30 deletions(-) diff --git a/addons/terrain_3d/editor/components/ui.gd b/addons/terrain_3d/editor/components/ui.gd index 0987c2a..41b8a7b 100644 --- a/addons/terrain_3d/editor/components/ui.gd +++ b/addons/terrain_3d/editor/components/ui.gd @@ -47,7 +47,7 @@ func _enter_tree() -> void: toolbar = Toolbar.new() toolbar.hide() toolbar.connect("tool_changed", _on_tool_changed) - + toolbar_settings = ToolSettings.new() toolbar_settings.connect("setting_changed", _on_setting_changed) toolbar_settings.connect("picking", _on_picking) @@ -89,7 +89,7 @@ func set_visible(p_visible: bool) -> void: visible = p_visible toolbar.set_visible(p_visible and plugin.terrain) terrain_tools.set_visible(p_visible) - + if p_visible and plugin.terrain: p_visible = plugin.editor.get_tool() != Terrain3DEditor.REGION toolbar_settings.set_visible(p_visible and plugin.terrain) @@ -98,19 +98,19 @@ func set_visible(p_visible: bool) -> void: func _on_tool_changed(p_tool: Terrain3DEditor.Tool, p_operation: Terrain3DEditor.Operation) -> void: clear_picking() - + if not visible or not plugin.terrain: return if plugin.editor: plugin.editor.set_tool(p_tool) plugin.editor.set_operation(p_operation) - + if p_tool != Terrain3DEditor.REGION: # Select which settings to hide. Options: # size, opactiy, height, slope, color, roughness, (height|color|roughness) picker var to_hide: PackedStringArray = [] - + if p_tool == Terrain3DEditor.HEIGHT: to_hide.push_back("color") to_hide.push_back("color picker") @@ -158,7 +158,7 @@ func _on_tool_changed(p_tool: Terrain3DEditor.Tool, p_operation: Terrain3DEditor to_hide.push_back("color picker") to_hide.push_back("slope") to_hide.push_back("enable") - + elif p_tool in [ Terrain3DEditor.AUTOSHADER, Terrain3DEditor.HOLES, Terrain3DEditor.NAVIGATION ]: to_hide.push_back("height") to_hide.push_back("height picker") @@ -173,16 +173,16 @@ func _on_tool_changed(p_tool: Terrain3DEditor.Tool, p_operation: Terrain3DEditor toolbar_settings.hide_settings(to_hide) - toolbar_settings.set_visible(p_tool != Terrain3DEditor.REGION) - + toolbar_settings.set_visible(p_tool != Terrain3DEditor.REGION) + operation_builder = null if p_operation == Terrain3DEditor.GRADIENT: operation_builder = GradientOperationBuilder.new() operation_builder.tool_settings = toolbar_settings - + _on_setting_changed() plugin.update_region_grid() - + func _on_setting_changed() -> void: @@ -206,7 +206,7 @@ func _on_setting_changed() -> void: var brush_imgs: Array = toolbar_settings.get_setting("brush") brush_data["image"] = brush_imgs[0] brush_data["texture"] = brush_imgs[1] - + update_decal() plugin.editor.set_brush_data(brush_data) @@ -226,7 +226,7 @@ func update_decal() -> void: else: # Wait for cursor to recenter after right-click before revealing # See https://github.com/godotengine/godot/issues/70098 - await get_tree().create_timer(.05).timeout + await get_tree().create_timer(.05).timeout decal.visible = true decal.size = Vector3.ONE * brush_data["size"] @@ -250,7 +250,7 @@ func update_decal() -> void: decal.modulate = COLOR_PICK_ROUGH decal.modulate.a = 1.0 else: - decal.texture_albedo = brush_data["texture"] + decal.texture_albedo = brush_data["texture"] match plugin.editor.get_tool(): Terrain3DEditor.HEIGHT: match plugin.editor.get_operation(): @@ -303,10 +303,10 @@ func update_decal() -> void: decal.albedo_mix = 1.0 decal.cull_mask = 1 << ( plugin.terrain.get_mouse_layer() - 1 ) decal_timer.start() - + for gradient_decal in gradient_decals: gradient_decal.visible = false - + if plugin.editor.get_operation() == Terrain3DEditor.GRADIENT: var index := 0 for point in brush_data["gradient_points"]: @@ -320,7 +320,7 @@ func update_decal() -> void: func _get_gradient_decal(index: int) -> Decal: if gradient_decals.size() > index: return gradient_decals[index] - + var gradient_decal := Decal.new() gradient_decal = Decal.new() gradient_decal.texture_albedo = picker_texture @@ -329,7 +329,7 @@ func _get_gradient_decal(index: int) -> Decal: gradient_decal.size.y = 1000. gradient_decal.cull_mask = decal.cull_mask add_child(gradient_decal) - + gradient_decals.push_back(gradient_decal) return gradient_decal @@ -351,10 +351,10 @@ func clear_picking() -> void: func is_picking() -> bool: if picking != Terrain3DEditor.TOOL_MAX: return true - + if operation_builder and operation_builder.is_picking(): return true - + return false @@ -373,7 +373,7 @@ func pick(p_global_position: Vector3) -> void: return picking_callback.call(picking, color, p_global_position) picking = Terrain3DEditor.TOOL_MAX - + elif operation_builder and operation_builder.is_picking(): operation_builder.pick(p_global_position, plugin.terrain) diff --git a/components/flag_carry_component.gd b/components/flag_carry_component.gd index af4e18c..54eaeaa 100644 --- a/components/flag_carry_component.gd +++ b/components/flag_carry_component.gd @@ -21,6 +21,7 @@ class_name FlagCarryComponent extends Node @export var max_throw_speed : float = 10.0 @export var attachment : Node3D @export var sensor : Area3D +@export var carrier : Player var _carried_flag : Flag @@ -34,7 +35,7 @@ func _process(_delta : float) -> void: func _grab(flag : Flag) -> void: if not _is_carrying(): - flag.grab() + flag.grab(carrier) _carried_flag = flag func _release(inherited_velocity : Vector3, throw_speed : float) -> void: diff --git a/entities/flag/flag.gd b/entities/flag/flag.gd index cbda81c..631b7f3 100644 --- a/entities/flag/flag.gd +++ b/entities/flag/flag.gd @@ -4,14 +4,26 @@ enum FlagState { FLAG_STATE_ON_STAND, FLAG_STATE_DROPPED, FLAG_STATE_TAKEN } @export var flag_state : FlagState = FlagState.FLAG_STATE_ON_STAND +signal grabbed(grabber : Player) +signal regrabbed +signal dropped + +var last_carrier : Player = null + func can_be_grabbed() -> bool: return flag_state != FlagState.FLAG_STATE_TAKEN -func grab() -> void: +func grab(grabber : Player) -> void: if flag_state != FlagState.FLAG_STATE_TAKEN: flag_state = FlagState.FLAG_STATE_TAKEN + if (last_carrier == null) or (grabber != last_carrier): + grabbed.emit(grabber) + last_carrier = grabber + else: + regrabbed.emit() func drop() -> void: if flag_state == FlagState.FLAG_STATE_TAKEN: flag_state = FlagState.FLAG_STATE_DROPPED + dropped.emit() diff --git a/entities/flag/flag.tscn b/entities/flag/flag.tscn index cfb5dde..cf67430 100644 --- a/entities/flag/flag.tscn +++ b/entities/flag/flag.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=7 format=3 uid="uid://c88l3h0ph00c7"] [ext_resource type="Script" path="res://entities/flag/flag.gd" id="1_y7d3d"] -[ext_resource type="PackedScene" path="res://entities/flag/assets/flag.glb" id="2_i78em"] +[ext_resource type="PackedScene" uid="uid://d3l7fvbdg6m5g" path="res://entities/flag/assets/flag.glb" id="2_i78em"] [ext_resource type="PackedScene" uid="uid://bcgkc5fhhyauv" path="res://entities/flag/waypoint.tscn" id="3_tu6jg"] [sub_resource type="PhysicsMaterial" id="PhysicsMaterial_4ymrw"] diff --git a/entities/player/player.tscn b/entities/player/player.tscn index f821659..e90e0fb 100644 --- a/entities/player/player.tscn +++ b/entities/player/player.tscn @@ -130,9 +130,10 @@ shape = ExtResource("2_vjqny") [node name="CollisionShape3D" type="CollisionShape3D" parent="HealthComponent"] shape = ExtResource("2_vjqny") -[node name="FlagCarryComponent" parent="." node_paths=PackedStringArray("attachment", "sensor") instance=ExtResource("7_e7s1a")] +[node name="FlagCarryComponent" parent="." node_paths=PackedStringArray("attachment", "sensor", "carrier") instance=ExtResource("7_e7s1a")] attachment = NodePath("../Smoothing/SpringArm3D/FlagCarryAttachment") sensor = NodePath("../Sensor") +carrier = NodePath("..") [node name="AnimationPlayer" type="AnimationPlayer" parent="."] libraries = { diff --git a/maps/genesis/genesis.tscn b/maps/genesis/genesis.tscn index f2a8065..1bbcd32 100644 --- a/maps/genesis/genesis.tscn +++ b/maps/genesis/genesis.tscn @@ -54,7 +54,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.42967, 0) height = 5.0 [node name="FlagStand" type="Marker3D" parent="Terrain3D/FlagPillar/CSGCylinder3D"] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.42315, 0) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.53822, 0) [node name="Sunlight" type="DirectionalLight3D" parent="."] transform = Transform3D(-0.866024, -0.433016, 0.25, 0, 0.499998, 0.866027, -0.500003, 0.75, -0.43301, 0, 100, 0) diff --git a/modes/multiplayer.gd b/modes/multiplayer.gd index 9a91d49..095fa68 100644 --- a/modes/multiplayer.gd +++ b/modes/multiplayer.gd @@ -28,10 +28,17 @@ class_name Multiplayer extends Node @onready var scoreboard_ui : Node = $ScoreboardUI var _map_manager : Map +var _flag : Flag +var _flag_carrier_scoring_timer : Timer = Timer.new() signal connected_to_server signal connection_failed +func _ready() -> void: + _flag_carrier_scoring_timer.wait_time = 10.0 + _flag_carrier_scoring_timer.timeout.connect(_on_flag_carrier_scoring_timer_timeout) + add_child(_flag_carrier_scoring_timer) + func start_server(port : int, nickname : String) -> void: var peer : ENetMultiplayerPeer = ENetMultiplayerPeer.new() peer.create_server(port, MAX_CLIENTS) @@ -101,9 +108,11 @@ func _load_map(scene : PackedScene, nickname : String) -> void: map.add_child(map_scene) func _add_flag() -> void: - var flag : Flag = FLAG.instantiate() - flag.global_position = _map_manager.get_flagstand().global_position - objectives.add_child(flag) + _flag = FLAG.instantiate() + _flag.grabbed.connect(_on_flag_grabbed) + _flag.dropped.connect(_on_flag_dropped) + _flag.global_position = _map_manager.get_flagstand().global_position + objectives.add_child(_flag) func _unhandled_input(event : InputEvent) -> void: if event.is_action_pressed("scoreboard"): @@ -123,6 +132,18 @@ func _join_match(nickname : String) -> void: if is_multiplayer_authority(): add_player(multiplayer.get_remote_sender_id(), nickname) +func _on_flag_grabbed(grabber : Player) -> void: + scoreboard.add_score_to_player(grabber, 10) + scoreboard.broadcast_player_score_update(grabber) + _flag_carrier_scoring_timer.start() + +func _on_flag_dropped() -> void: + _flag_carrier_scoring_timer.stop() + +func _on_flag_carrier_scoring_timer_timeout() -> void: + scoreboard.add_score_to_player(_flag.last_carrier, 10) + scoreboard.broadcast_player_score_update(_flag.last_carrier) + func _exit_tree() -> void: if is_multiplayer_authority(): multiplayer.peer_disconnected.disconnect(remove_player) diff --git a/modes/multiplayer.tscn b/modes/multiplayer.tscn index 7f504b0..cda1fae 100644 --- a/modes/multiplayer.tscn +++ b/modes/multiplayer.tscn @@ -1,13 +1,13 @@ [gd_scene load_steps=6 format=3 uid="uid://bvwxfgygm2xb8"] -[ext_resource type="Script" path="res://modes/multiplayer.gd" id="1_4t1ad"] [ext_resource type="PackedScene" uid="uid://chbno00ugl6te" path="res://maps/genesis/genesis.tscn" id="1_nulvv"] +[ext_resource type="Script" path="res://modes/multiplayer.gd" id="1_r1kd6"] [ext_resource type="PackedScene" uid="uid://cbhx1xme0sb7k" path="res://entities/player/player.tscn" id="2_og1vb"] [ext_resource type="PackedScene" uid="uid://c88l3h0ph00c7" path="res://entities/flag/flag.tscn" id="3_h0rie"] [ext_resource type="Script" path="res://modes/scoreboard.gd" id="4_n0mhp"] [node name="Multiplayer" type="Node"] -script = ExtResource("1_4t1ad") +script = ExtResource("1_r1kd6") MAP = ExtResource("1_nulvv") PLAYER = ExtResource("2_og1vb") FLAG = ExtResource("3_h0rie")