Compare commits

...

6 Commits

8 changed files with 300 additions and 44 deletions

View File

@ -5,12 +5,17 @@
[sub_resource type="LabelSettings" id="LabelSettings_1cgct"]
font_size = 32
[node name="lobby" type="LobbyPanel" unique_id=915192272 node_paths=PackedStringArray("players_list", "name_field", "ready_button", "map_selection")]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_jbm04"]
bg_color = Color(0.30692267, 0.3069227, 0.30692264, 1)
[node name="lobby" type="LobbyPanel" unique_id=915192272 node_paths=PackedStringArray("players_list", "name_field", "ready_button", "map_selection", "options_panel", "options_grid")]
players_list = NodePath("v_box_container/players_list")
name_field = NodePath("v_box_container/h_box_container/text_edit")
player_listing_prefab = ExtResource("1_o1atq")
ready_button = NodePath("ready_button")
map_selection = NodePath("v_box_container2/option_button")
options_panel = NodePath("options_panel")
options_grid = NodePath("options_panel/h_box_container/options_grid")
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
@ -95,3 +100,65 @@ grow_horizontal = 0
[node name="option_button" type="OptionButton" parent="v_box_container2" unique_id=426342653]
layout_mode = 2
metadata/_edit_lock_ = true
[node name="options_panel" type="Panel" parent="." unique_id=1194589850]
visible = false
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -291.5
offset_top = -213.5
offset_right = 291.5
offset_bottom = 213.5
grow_horizontal = 2
grow_vertical = 2
theme_override_styles/panel = SubResource("StyleBoxFlat_jbm04")
[node name="options_close" type="Button" parent="options_panel" unique_id=2115857774]
layout_mode = 1
anchors_preset = 1
anchor_left = 1.0
anchor_right = 1.0
offset_left = -33.5
offset_bottom = 31.0
grow_horizontal = 0
text = "X"
[node name="h_box_container" type="VBoxContainer" parent="options_panel" unique_id=1163233032]
layout_mode = 0
offset_left = 0.5
offset_top = 3.5
offset_right = 115.5
offset_bottom = 43.5
[node name="label" type="Label" parent="options_panel/h_box_container" unique_id=1002581329]
layout_mode = 2
text = "Game Options"
[node name="v_box_container" type="HBoxContainer" parent="options_panel/h_box_container" unique_id=1445409921]
layout_mode = 2
[node name="options_grid" type="GridContainer" parent="options_panel/h_box_container" unique_id=504671462]
layout_mode = 2
columns = 2
[node name="options_button" type="Button" parent="." unique_id=2109111628]
layout_mode = 1
anchors_preset = 3
anchor_left = 1.0
anchor_top = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = -309.0
offset_top = -166.0
offset_right = -239.0
offset_bottom = -135.0
grow_horizontal = 0
grow_vertical = 0
text = "Options"
[connection signal="pressed" from="options_panel/options_close" to="." method="close_options"]
[connection signal="pressed" from="options_button" to="." method="open_options"]

View File

@ -22,12 +22,6 @@ use crate::{
pub const GAME_MANAGER_GLOBAL: &str = "GameManagerGlobal";
pub struct Team {
name: String,
color: String,
id: u8,
}
#[derive(GodotClass)]
#[class(base=Node, init)]
pub struct GameManager {
@ -89,19 +83,59 @@ impl GameManager {
}
pub struct GameOptions {
/// Wether anyone is allowed to change anyone's team, or just the host and
/// the player itself.
pub allow_any_team: bool,
pub values: HashMap<GameOption, GameOptionValue>,
}
impl Default for GameOptions {
fn default() -> Self {
Self {
allow_any_team: false,
let mut opts = HashMap::new();
opts.insert(GameOption::AllowAnyMap, GameOptionValue::Boolean(false));
opts.insert(GameOption::AllowAnyTeam, GameOptionValue::Boolean(false));
opts.insert(GameOption::FriendlyFire, GameOptionValue::Boolean(true));
Self { values: opts }
}
}
impl GameOptions {
pub fn is_true(&self, opt: GameOption) -> bool {
let opt = self
.values
.get(&opt)
.copied()
.unwrap_or(GameOptionValue::Boolean(false));
match opt {
GameOptionValue::Boolean(value) => value,
_ => false,
}
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Hash, Copy, Clone, Serialize, Deserialize)]
pub enum GameOption {
/// Allow anyone to select a map, not just the host
AllowAnyMap,
/// Allow anyone to select anyone's team, not just themselves and the host
AllowAnyTeam,
/// Allow anyone to shoot anyone, not just other team's members
FriendlyFire,
}
impl ToString for GameOption {
fn to_string(&self) -> String {
match self {
GameOption::AllowAnyMap => "Allow anyone to select the map",
GameOption::AllowAnyTeam => "Allow anyone to select any teams",
GameOption::FriendlyFire => "Friendly fire",
}
.to_owned()
}
}
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum GameOptionValue {
Boolean(bool),
}
#[derive(GodotClass)]
#[class(base=Node, init)]
pub struct Game {
@ -214,6 +248,8 @@ impl Game {
pub fn on_player_team_changed(id: u16, team: u8);
#[signal]
pub fn on_map_changed(id: u8);
#[signal]
pub fn options_changed();
pub fn singleton() -> Option<Gd<Game>> {
GameManager::singleton()
@ -221,6 +257,18 @@ impl Game {
.map(|g| g.cast::<Game>())
}
pub fn change_option(&mut self, opt: GameOption, value: GameOptionValue) {
self.opts.values.insert(opt, value);
self.run_deferred(move |s| {
s.signals().options_changed().emit();
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer {
if let PeerKind::Server(peer, _) = peer {
peer.broadcast_reliable(Package::GameOptionChanged(opt, value));
}
}
});
}
pub fn change_map_selection(&mut self, map_idx: u8, emit: bool) {
self.selected_map_idx = map_idx;
if let Some(map) = self.maps.get(map_idx as usize) {
@ -310,7 +358,7 @@ impl Game {
self.self_id = Some(id);
}
pub fn new_player(&mut self, addr: SocketAddr, name: String, id: Option<u16>) -> u16 {
pub fn new_player(&mut self, addr: SocketAddr, name: Option<String>, id: Option<u16>) -> u16 {
let id = if let Some(id) = id {
id
} else {
@ -322,7 +370,7 @@ impl Game {
connection_addr: addr,
data: NetPlayer {
id,
name,
name: name.unwrap_or(format!("Player {}", id)),
ping: 0,
team: 0,
},

View File

@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use teanet::{Peer, PeerConfig, PeerMessage};
use crate::{
game_manager::{Game, GameManager, NetPlayer},
game_manager::{Game, GameManager, GameOption, GameOptionValue, NetPlayer},
net::{net_stats::Stats, util::NetVector3},
player::{DamageSource, NetTransform},
ui::cli::{CliColor, CommandLinePanel},
@ -175,7 +175,7 @@ impl NetworkManager {
if let Some(game) = &mut Game::singleton() {
let id = game.bind_mut().new_player(
SocketAddr::from(([0, 0, 0, 0], 0)),
"Host".to_owned(),
Some("Host".to_owned()),
None,
);
game.bind_mut().update_player_ready(id, true);
@ -256,6 +256,7 @@ pub enum Package {
NewPlayer(NetPlayer),
PlayerLeft(NetPlayer),
SetTeam(u16, u8),
GameOptionChanged(GameOption, GameOptionValue),
// Lobby packages
SetReady(u16, bool),

View File

@ -3,7 +3,7 @@ use std::net::SocketAddr;
use teanet::PeerMessage;
use crate::{
game_manager::{Game, GameManager},
game_manager::{Game, GameManager, GameOption},
net::network_manager::{NetworkManager, Package, PeerKind},
ui::cli::{CliColor, CommandLinePanel},
};
@ -73,7 +73,7 @@ impl NetworkManager {
if player.id != game.bind().self_id.unwrap_or(u16::MAX) {
game.bind_mut().new_player(
SocketAddr::from(([0, 0, 0, 0], 0)),
player.name,
Some(player.name),
Some(player.id),
);
}
@ -166,6 +166,11 @@ impl NetworkManager {
game.bind_mut().handle_player_team_change(player_id, team);
}
}
Package::GameOptionChanged(opt, value) => {
if let Some(game) = &mut Game::singleton() {
game.bind_mut().change_option(opt, value);
}
}
_ => {}
},
},
@ -178,8 +183,7 @@ impl NetworkManager {
if let Some(game) = &mut Game::singleton() {
let self_id =
game.bind_mut()
.new_player(connection.address, String::new(), None);
game.bind_mut().new_player(connection.address, None, None);
peer.send_reliable(
&connection.address,
Package::Players(
@ -319,12 +323,21 @@ impl NetworkManager {
.find_player_by_addr(&conn.address)
.map(|p| p.id());
if let Some(self_id) = self_id {
if game.bind().opts.allow_any_team || self_id == player_id {
if game.bind().opts.is_true(GameOption::AllowAnyTeam)
|| self_id == player_id
{
game.bind_mut().try_set_player_team(player_id, team, true);
}
}
}
}
Package::SelectMap(map_idx) => {
if let Some(mut game) = Game::singleton() {
if game.bind().opts.is_true(GameOption::AllowAnyMap) {
game.bind_mut().change_map_selection(map_idx, true);
}
}
}
_ => {}
},
},

View File

@ -58,6 +58,7 @@ pub trait IPlayer {
pub trait PlayerMovement {
fn player_update(&mut self, delta: f64);
fn try_jump(&mut self) -> bool;
fn drop_ball_common(&mut self, with_velocity: bool) -> bool;
fn get_look_dir(&self) -> Vector3;
}
@ -85,6 +86,15 @@ impl<T: IPlayer + ICharacterBody3D + WithBaseField> PlayerMovement for T {
}
}
fn try_jump(&mut self) -> bool {
if self.base().is_on_floor() {
self.set_y_speed(5.);
true
} else {
false
}
}
fn drop_ball_common(&mut self, with_velocity: bool) -> bool {
if let Some(weapon) = self.get_weapon()
&& let Ok(_) = weapon.try_cast::<BallWeapon>()
@ -219,12 +229,13 @@ impl IPlayer for LocalPlayer {
}
fn jump(&mut self) {
self.set_y_speed(5.);
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer {
match peer {
PeerKind::Client(peer, addr) => peer.send_reliable(&addr, Jump(0)),
PeerKind::Server(peer, _) => {
peer.broadcast_reliable(Jump(self.player_id.unwrap_or(0)))
if self.try_jump() {
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer {
match peer {
PeerKind::Client(peer, addr) => peer.send_reliable(&addr, Jump(0)),
PeerKind::Server(peer, _) => {
peer.broadcast_reliable(Jump(self.player_id.unwrap_or(0)))
}
}
}
}
@ -398,7 +409,7 @@ impl ICharacterBody3D for LocalPlayer {
let input = Input::singleton();
if self.base().is_on_floor() && input.is_action_just_pressed("jump") {
self.jump();
self.try_jump();
}
Vector3::RIGHT * input.is_action_pressed("right") as u32 as f32
@ -597,7 +608,7 @@ impl IPlayer for RemotePlayer {
}
fn jump(&mut self) {
self.set_y_speed(5.);
self.try_jump();
}
fn get_look_up(&self) -> f32 {

View File

@ -1,10 +1,15 @@
use std::collections::HashMap;
use godot::{
classes::{Button, IPanel, OptionButton, Panel, TextEdit, VBoxContainer},
classes::{
Button, CheckBox, Control, GridContainer, IPanel, Label, OptionButton, Panel, TextEdit,
VBoxContainer,
},
prelude::*,
};
use crate::{
game_manager::{Game, GameManager},
game_manager::{Game, GameManager, GameOption, GameOptionValue, GameOptions},
net::network_manager::{NetworkManager, Package, PeerKind},
ui::player_listing::PlayerListing,
};
@ -22,6 +27,12 @@ pub struct LobbyPanel {
ready_button: Option<Gd<Button>>,
#[export]
map_selection: Option<Gd<OptionButton>>,
#[export]
options_panel: Option<Gd<Panel>>,
#[export]
options_grid: Option<Gd<GridContainer>>,
option_nodes: HashMap<GameOption, Gd<Control>>,
is_ready: bool,
@ -64,18 +75,29 @@ impl IPanel for LobbyPanel {
}
if let Some(game) = Game::singleton() {
game.signals().options_changed().connect_other(self, |s| {
s.run_deferred(|s| {
if let Some(game) = Game::singleton() {
s.on_game_opts_update(
&game.bind().opts,
NetworkManager::singleton().bind().is_host(),
);
}
});
});
if let Some(map_selection) = &mut self.map_selection {
for map in game.bind().maps.iter_shared() {
map_selection.add_item(&map.bind().name);
}
let is_host = if let Some(peer) = &NetworkManager::singleton().bind().peer {
peer.is_host()
} else {
false
};
map_selection.set_disabled(!is_host);
map_selection.select(0);
}
let is_host = if let Some(peer) = &NetworkManager::singleton().bind().peer {
peer.is_host()
} else {
false
};
self.on_game_opts_update(&game.bind().opts, is_host);
}
self.update_map_selection(0);
@ -113,10 +135,85 @@ impl IPanel for LobbyPanel {
}
}
self.update_ready_button();
let mut nodes = Vec::new();
if let Some(game) = Game::singleton() {
for (option, value) in &game.bind().opts.values {
let mut label = Label::new_alloc();
label.set_text(&format!("{}:", option.to_string()));
nodes.push(label.upcast());
let value_node = match value {
GameOptionValue::Boolean(value) => {
let mut checkbox = CheckBox::new_alloc();
checkbox.set_pressed(*value);
checkbox.set_disabled(!NetworkManager::singleton().bind().is_host());
let checkbox_clone = checkbox.clone();
let option = option.clone();
checkbox.signals().pressed().connect_other(self, move |s| {
s.option_changed(
option,
GameOptionValue::Boolean(checkbox_clone.is_pressed()),
);
});
self.option_nodes.insert(option, checkbox.clone().upcast());
checkbox.upcast::<Control>()
}
};
nodes.push(value_node);
}
}
if let Some(grid) = &mut self.options_grid {
for node in nodes {
grid.add_child(&node);
}
}
}
}
#[godot_api]
impl LobbyPanel {
#[func]
fn open_options(&mut self) {
if let Some(panel) = &mut self.options_panel {
panel.set_visible(true);
}
}
#[func]
fn close_options(&mut self) {
if let Some(panel) = &mut self.options_panel {
panel.set_visible(false);
}
}
fn option_changed(&mut self, key: GameOption, value: GameOptionValue) {
if let Some(game) = &mut Game::singleton() {
game.bind_mut().change_option(key, value);
}
}
fn on_game_opts_update(&mut self, opts: &GameOptions, is_host: bool) {
if let Some(selection) = &mut self.map_selection {
selection.set_disabled(!is_host && !opts.is_true(GameOption::AllowAnyMap));
}
for (key, value) in &opts.values {
if let Some(control) = self.option_nodes.get(key).cloned() {
match value {
GameOptionValue::Boolean(value) => {
if let Ok(mut checkbox) = control.try_cast::<CheckBox>() {
checkbox.set_pressed(*value);
}
}
}
}
}
}
fn update_ready_button(&mut self) {
if let Some(game) = Game::singleton()
&& let Some(peer) = &NetworkManager::singleton().bind().peer
@ -194,10 +291,11 @@ impl LobbyPanel {
pub fn update_map_selection(&mut self, idx: u8) {
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer {
match peer {
PeerKind::Client(..) => {
PeerKind::Client(peer, addr) => {
if let Some(map_selection) = &mut self.map_selection {
map_selection.select(idx as i32);
self.run_deferred(|s| s.update_players(false));
peer.send_reliable(addr, Package::SelectMap(idx));
}
}
PeerKind::Server(peer, _) => {

View File

@ -4,7 +4,7 @@ use godot::{
};
use crate::{
game_manager::{Game, GameOptions},
game_manager::{Game, GameOption, GameOptions},
net::network_manager::NetworkManager,
team_resource::TeamResource,
};
@ -52,6 +52,14 @@ impl IGridContainer for PlayerListing {
}
if let Some(game) = &Game::singleton() {
game.signals().options_changed().connect_other(self, |s| {
s.run_deferred(|s| {
if let Some(game) = Game::singleton() {
s.update_game_options(&game.bind().opts, game.bind().self_id);
}
});
});
self.update_game_options(&game.bind().opts, game.bind().self_id);
}
}
@ -62,7 +70,9 @@ impl PlayerListing {
let is_host = NetworkManager::singleton().bind().is_host();
if let Some(dropdown) = &mut self.team_dropdown {
dropdown.set_disabled(!is_host && !opts.allow_any_team && self.player_id != self_id);
dropdown.set_disabled(
!is_host && !opts.is_true(GameOption::AllowAnyTeam) && self.player_id != self_id,
);
}
}

View File

@ -2,7 +2,7 @@ use godot::{classes::AudioStreamPlayer3D, prelude::*};
use serde::{Deserialize, Serialize};
use crate::{
game_manager::Game,
game_manager::{Game, GameOption},
net::util::cast_ray,
player::{DamageSource, IPlayer},
};
@ -69,9 +69,17 @@ impl Weapon for Raygun {
if deal_damage && let Some(target) = target {
if let Ok(mut player) = target.collider.try_dynify::<dyn IPlayer>() {
player
.dyn_bind_mut()
.take_damage(DamageSource::Player(player_id), 100, true);
self.run_deferred(move |_| {
if let Some(game) = Game::singleton()
&& game.bind().opts.is_true(GameOption::FriendlyFire)
{
player.dyn_bind_mut().take_damage(
DamageSource::Player(player_id),
100,
true,
);
}
});
}
}
}