Refactor Multiplayer code

This commit is contained in:
Squinty 2024-04-26 22:24:34 +00:00
parent a1314dfea4
commit 4d577d751d
14 changed files with 127 additions and 59 deletions

View file

@ -0,0 +1,36 @@
# 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/>.
class_name DeathmatchScoringComponent extends Node
@export var ON_KILL_SCORE : int = 10
# this is the node that contains all players, typically the one populated via MultiplayerSpawner
@export var _players : Node
@export var _scoreboard : Scoreboard
# only subscribe once per player, per match
func subscribe_player(player : Player) -> void:
player.died.connect(_on_player_died)
func unsubscribe_player(player : Player) -> void:
player.died.disconnect(_on_player_died)
func _on_player_died(player : Player, killer_id : int) -> void:
if player.player_id != killer_id:
var node_name : String = str(killer_id)
if _players.has_node(node_name):
var killer : Player = _players.get_node(node_name)
_scoreboard.increment_kill_count(killer)
_scoreboard.add_score_to_player(killer, 10)

View file

@ -0,0 +1,53 @@
# 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/>.
class_name RabbitScoringComponent extends Node
@export var ON_GRAB_SCORE : int = 10
@export var ON_HOLD_SCORE : int = 10
@export var HOLD_SCORING_TIMER : float = 10.0 # seconds
@export var _scoreboard : Scoreboard
var _flag_carrier_scoring_timer : Timer = Timer.new()
var _team_chasers : Team
var _team_rabbit : Team
func _ready() -> void:
_flag_carrier_scoring_timer.wait_time = HOLD_SCORING_TIMER
add_child(_flag_carrier_scoring_timer)
_team_chasers = Team.new(0)
_team_rabbit = Team.new(1)
# setup only once per match, per flag (tested only with one flag so far)
func setup(flag : Flag) -> void:
_flag_carrier_scoring_timer.timeout.connect(_on_flag_carrier_scoring_timer_timeout.bind(flag))
flag.grabbed.connect(_on_flag_grabbed)
flag.regrabbed.connect(_on_flag_regrabbed)
flag.dropped.connect(_on_flag_dropped)
func _on_flag_grabbed(grabber : Player) -> void:
grabber.team_id = _team_rabbit.team_id
_scoreboard.add_score_to_player(grabber, ON_GRAB_SCORE)
_flag_carrier_scoring_timer.start()
func _on_flag_regrabbed(grabber : Player) -> void:
grabber.team_id = _team_rabbit.team_id
func _on_flag_dropped(dropper : Player) -> void:
dropper.team_id = _team_chasers.team_id
_flag_carrier_scoring_timer.stop()
func _on_flag_carrier_scoring_timer_timeout(flag : Flag) -> void:
_scoreboard.add_score_to_player(flag.last_carrier, ON_HOLD_SCORE)