flag refactoring

This commit is contained in:
anyreso 2024-05-09 01:17:10 -04:00
parent 69c8b65093
commit 6ba368c76e
21 changed files with 389 additions and 198 deletions

Binary file not shown.

View file

@ -12,34 +12,39 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
class_name Flag extends Node3D
enum FlagState { FLAG_STATE_ON_STAND, FLAG_STATE_DROPPED, FLAG_STATE_TAKEN }
@export var flag_state : FlagState = FlagState.FLAG_STATE_ON_STAND
class_name Flag extends RigidBody3D
signal grabbed(grabber : Player)
signal regrabbed(grabber : Player)
signal dropped(dropper : Player)
enum FlagState { ON_STAND, DROPPED, TAKEN }
@export var state : FlagState = FlagState.ON_STAND
@onready var area : Area3D = $GripArea3D
@onready var mesh : Node3D = $Mesh
var last_carrier : Player = null
@onready var _mesh : Node = $Smoothing/Mesh
func can_be_grabbed() -> bool:
return flag_state != FlagState.FLAG_STATE_TAKEN
func _ready() -> void:
area.body_entered.connect(_on_body_entered)
func grab(grabber : Player) -> void:
if flag_state != FlagState.FLAG_STATE_TAKEN:
_mesh.hide()
flag_state = FlagState.FLAG_STATE_TAKEN
func _on_grabbed(grabber : Player) -> void:
if state != FlagState.TAKEN:
hide()
state = FlagState.TAKEN
if (last_carrier == null) or (grabber != last_carrier):
grabbed.emit(grabber)
last_carrier = grabber
else:
regrabbed.emit(grabber)
func drop(dropper : Player) -> void:
if flag_state == FlagState.FLAG_STATE_TAKEN:
_mesh.show()
flag_state = FlagState.FLAG_STATE_DROPPED
dropped.emit(dropper)
func _on_dropped(_dropper : Player) -> void:
if state == FlagState.TAKEN:
show()
state = FlagState.DROPPED
func _on_body_entered(body: Node) -> void:
if body is Player:
assert(body.flag_carry_component)
body.flag_carry_component.grab(self)

View file

@ -1,17 +1,15 @@
[gd_scene load_steps=8 format=3 uid="uid://c88l3h0ph00c7"]
[ext_resource type="Script" path="res://entities/flag/flag.gd" id="1_y7d3d"]
[ext_resource type="Script" path="res://addons/smoothing/smoothing.gd" id="2_es4ce"]
[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"]
[ext_resource type="Shape3D" uid="uid://dfko4x6uq27q" path="res://entities/flag/resources/collider.tres" id="2_kx0v0"]
[ext_resource type="Shape3D" uid="uid://b5a6oybteqj6n" path="res://entities/flag/resources/grip_area.tres" id="3_uhsc8"]
[ext_resource type="PackedScene" uid="uid://2350a3ce4xs8" path="res://interfaces/waypoint/waypoint3d.tscn" id="4_y8nj0"]
[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_4ymrw"]
friction = 0.5
bounce = 0.5
[sub_resource type="BoxShape3D" id="BoxShape3D_fkf1k"]
size = Vector3(0.5, 2.33994, 0.5)
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_lpijf"]
properties/0/path = NodePath(".:position")
properties/0/spawn = true
@ -19,10 +17,10 @@ properties/0/replication_mode = 1
properties/1/path = NodePath(".:linear_velocity")
properties/1/spawn = true
properties/1/replication_mode = 1
properties/2/path = NodePath(".:flag_state")
properties/2/path = NodePath("Smoothing/Mesh:visible")
properties/2/spawn = true
properties/2/replication_mode = 2
properties/3/path = NodePath("Smoothing/Mesh:visible")
properties/3/path = NodePath("Flag:state")
properties/3/spawn = true
properties/3/replication_mode = 2
@ -40,20 +38,23 @@ continuous_cd = true
script = ExtResource("1_y7d3d")
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.175, 0)
shape = SubResource("BoxShape3D_fkf1k")
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0.00601196)
shape = ExtResource("2_kx0v0")
[node name="GripArea3D" type="Area3D" parent="."]
[node name="CollisionShape3D" type="CollisionShape3D" parent="GripArea3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
shape = ExtResource("3_uhsc8")
[node name="Mesh" parent="." instance=ExtResource("2_i78em")]
[node name="Waypoint" parent="." instance=ExtResource("4_y8nj0")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.25, 0)
foreground_color = Color(0, 0.75, 0.75, 1)
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
replication_config = SubResource("SceneReplicationConfig_lpijf")
[node name="Smoothing" type="Node3D" parent="."]
script = ExtResource("2_es4ce")
[node name="WaypointAttachment" type="Marker3D" parent="Smoothing"]
[node name="Waypoint" parent="Smoothing/WaypointAttachment" node_paths=PackedStringArray("attach_point", "flag") instance=ExtResource("3_tu6jg")]
attach_point = NodePath("..")
flag = NodePath("../../..")
[node name="Mesh" parent="Smoothing" instance=ExtResource("2_i78em")]
transform = Transform3D(0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0, 0.0744106, 0)
[connection signal="dropped" from="." to="." method="_on_dropped"]
[connection signal="grabbed" from="." to="." method="_on_grabbed"]

View file

@ -0,0 +1,4 @@
[gd_resource type="BoxShape3D" format=3 uid="uid://dfko4x6uq27q"]
[resource]
size = Vector3(0.5, 2, 0.164246)

View file

@ -0,0 +1,4 @@
[gd_resource type="BoxShape3D" format=3 uid="uid://b5a6oybteqj6n"]
[resource]
size = Vector3(0.5, 2, 0.5)

View file

@ -1,52 +0,0 @@
# This file is part of open-fpsz.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
extends Control
@export var attach_point : Node3D
@export var flag : Flag
@onready var _iff_container : Control = $IFFContainer
func _ready() -> void:
attach_point.transform = attach_point.transform.translated(
Vector3(0, flag.get_node("CollisionShape3D").shape.size.y, 0))
func _process(_delta : float) -> void:
# disable waypoint repositioning when running headless
if DisplayServer.get_name() == "headless":
set_process(false)
return
# repositon iff on viewport
var camera : Camera3D = get_viewport().get_camera_3d()
var viewport_rect : Rect2 = get_viewport_rect()
# if the player is NOT infront of the camera or camera does NOT have LOS to player
if camera.is_position_behind(attach_point.global_position):
_iff_container.hide() # hide the IFF and exit function
return
else:
_iff_container.show() # display the IFF
# get the screen location of the players head
var new_iff_position : Vector2 = camera.unproject_position(attach_point.global_position)
# check if the unprojected point lies inside the viewport
if !Rect2(Vector2(0, 0), viewport_rect.size).has_point(new_iff_position):
_iff_container.hide() # hide the IFF and exit function
return
# move the IFF up so the bottom of it is at the players head
new_iff_position.y -= _iff_container.size.y
# move the IFF left so it's centered horizontally above players head
new_iff_position.x -= _iff_container.size.x / 2
# set the position of the IFF
position = new_iff_position

View file

@ -1,27 +0,0 @@
[gd_scene load_steps=2 format=3 uid="uid://bcgkc5fhhyauv"]
[ext_resource type="Script" path="res://entities/flag/waypoint.gd" id="1_gmnl6"]
[node name="IFF" type="Control"]
layout_mode = 3
anchors_preset = 0
script = ExtResource("1_gmnl6")
[node name="IFFContainer" type="MarginContainer" parent="."]
layout_mode = 0
offset_right = 11.0
offset_bottom = 20.0
mouse_filter = 2
[node name="Chevron" type="Label" parent="IFFContainer"]
layout_direction = 2
layout_mode = 2
size_flags_vertical = 1
theme_override_colors/font_color = Color(0, 1, 1, 0.784314)
theme_override_colors/font_outline_color = Color(0, 0, 0, 1)
theme_override_constants/outline_size = 3
theme_override_constants/line_spacing = -20
theme_override_font_sizes/font_size = 14
text = "▼"
horizontal_alignment = 1
vertical_alignment = 2