Compare commits

..

No commits in common. "f732472f7cc8feab104a61706fec319bf079a9d1" and "949585dc5cff5c7c8d49c4f254084f009cb91646" have entirely different histories.

8 changed files with 44 additions and 300 deletions

View File

@ -5,17 +5,12 @@
[sub_resource type="LabelSettings" id="LabelSettings_1cgct"] [sub_resource type="LabelSettings" id="LabelSettings_1cgct"]
font_size = 32 font_size = 32
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_jbm04"] [node name="lobby" type="LobbyPanel" unique_id=915192272 node_paths=PackedStringArray("players_list", "name_field", "ready_button", "map_selection")]
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") players_list = NodePath("v_box_container/players_list")
name_field = NodePath("v_box_container/h_box_container/text_edit") name_field = NodePath("v_box_container/h_box_container/text_edit")
player_listing_prefab = ExtResource("1_o1atq") player_listing_prefab = ExtResource("1_o1atq")
ready_button = NodePath("ready_button") ready_button = NodePath("ready_button")
map_selection = NodePath("v_box_container2/option_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 anchors_preset = 15
anchor_right = 1.0 anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
@ -100,65 +95,3 @@ grow_horizontal = 0
[node name="option_button" type="OptionButton" parent="v_box_container2" unique_id=426342653] [node name="option_button" type="OptionButton" parent="v_box_container2" unique_id=426342653]
layout_mode = 2 layout_mode = 2
metadata/_edit_lock_ = true 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,6 +22,12 @@ use crate::{
pub const GAME_MANAGER_GLOBAL: &str = "GameManagerGlobal"; pub const GAME_MANAGER_GLOBAL: &str = "GameManagerGlobal";
pub struct Team {
name: String,
color: String,
id: u8,
}
#[derive(GodotClass)] #[derive(GodotClass)]
#[class(base=Node, init)] #[class(base=Node, init)]
pub struct GameManager { pub struct GameManager {
@ -83,57 +89,17 @@ impl GameManager {
} }
pub struct GameOptions { pub struct GameOptions {
pub values: HashMap<GameOption, GameOptionValue>, /// Wether anyone is allowed to change anyone's team, or just the host and
/// the player itself.
pub allow_any_team: bool,
} }
impl Default for GameOptions { impl Default for GameOptions {
fn default() -> Self { fn default() -> Self {
let mut opts = HashMap::new(); Self {
opts.insert(GameOption::AllowAnyMap, GameOptionValue::Boolean(false)); allow_any_team: 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)] #[derive(GodotClass)]
@ -248,8 +214,6 @@ impl Game {
pub fn on_player_team_changed(id: u16, team: u8); pub fn on_player_team_changed(id: u16, team: u8);
#[signal] #[signal]
pub fn on_map_changed(id: u8); pub fn on_map_changed(id: u8);
#[signal]
pub fn options_changed();
pub fn singleton() -> Option<Gd<Game>> { pub fn singleton() -> Option<Gd<Game>> {
GameManager::singleton() GameManager::singleton()
@ -257,18 +221,6 @@ impl Game {
.map(|g| g.cast::<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) { pub fn change_map_selection(&mut self, map_idx: u8, emit: bool) {
self.selected_map_idx = map_idx; self.selected_map_idx = map_idx;
if let Some(map) = self.maps.get(map_idx as usize) { if let Some(map) = self.maps.get(map_idx as usize) {
@ -358,7 +310,7 @@ impl Game {
self.self_id = Some(id); self.self_id = Some(id);
} }
pub fn new_player(&mut self, addr: SocketAddr, name: Option<String>, id: Option<u16>) -> u16 { pub fn new_player(&mut self, addr: SocketAddr, name: String, id: Option<u16>) -> u16 {
let id = if let Some(id) = id { let id = if let Some(id) = id {
id id
} else { } else {
@ -370,7 +322,7 @@ impl Game {
connection_addr: addr, connection_addr: addr,
data: NetPlayer { data: NetPlayer {
id, id,
name: name.unwrap_or(format!("Player {}", id)), name,
ping: 0, ping: 0,
team: 0, team: 0,
}, },

View File

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

View File

@ -3,7 +3,7 @@ use std::net::SocketAddr;
use teanet::PeerMessage; use teanet::PeerMessage;
use crate::{ use crate::{
game_manager::{Game, GameManager, GameOption}, game_manager::{Game, GameManager},
net::network_manager::{NetworkManager, Package, PeerKind}, net::network_manager::{NetworkManager, Package, PeerKind},
ui::cli::{CliColor, CommandLinePanel}, ui::cli::{CliColor, CommandLinePanel},
}; };
@ -73,7 +73,7 @@ impl NetworkManager {
if player.id != game.bind().self_id.unwrap_or(u16::MAX) { if player.id != game.bind().self_id.unwrap_or(u16::MAX) {
game.bind_mut().new_player( game.bind_mut().new_player(
SocketAddr::from(([0, 0, 0, 0], 0)), SocketAddr::from(([0, 0, 0, 0], 0)),
Some(player.name), player.name,
Some(player.id), Some(player.id),
); );
} }
@ -166,11 +166,6 @@ impl NetworkManager {
game.bind_mut().handle_player_team_change(player_id, team); 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);
}
}
_ => {} _ => {}
}, },
}, },
@ -183,7 +178,8 @@ impl NetworkManager {
if let Some(game) = &mut Game::singleton() { if let Some(game) = &mut Game::singleton() {
let self_id = let self_id =
game.bind_mut().new_player(connection.address, None, None); game.bind_mut()
.new_player(connection.address, String::new(), None);
peer.send_reliable( peer.send_reliable(
&connection.address, &connection.address,
Package::Players( Package::Players(
@ -323,21 +319,12 @@ impl NetworkManager {
.find_player_by_addr(&conn.address) .find_player_by_addr(&conn.address)
.map(|p| p.id()); .map(|p| p.id());
if let Some(self_id) = self_id { if let Some(self_id) = self_id {
if game.bind().opts.is_true(GameOption::AllowAnyTeam) if game.bind().opts.allow_any_team || self_id == player_id {
|| self_id == player_id
{
game.bind_mut().try_set_player_team(player_id, team, true); 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,7 +58,6 @@ pub trait IPlayer {
pub trait PlayerMovement { pub trait PlayerMovement {
fn player_update(&mut self, delta: f64); fn player_update(&mut self, delta: f64);
fn try_jump(&mut self) -> bool;
fn drop_ball_common(&mut self, with_velocity: bool) -> bool; fn drop_ball_common(&mut self, with_velocity: bool) -> bool;
fn get_look_dir(&self) -> Vector3; fn get_look_dir(&self) -> Vector3;
} }
@ -86,15 +85,6 @@ 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 { fn drop_ball_common(&mut self, with_velocity: bool) -> bool {
if let Some(weapon) = self.get_weapon() if let Some(weapon) = self.get_weapon()
&& let Ok(_) = weapon.try_cast::<BallWeapon>() && let Ok(_) = weapon.try_cast::<BallWeapon>()
@ -229,7 +219,7 @@ impl IPlayer for LocalPlayer {
} }
fn jump(&mut self) { fn jump(&mut self) {
if self.try_jump() { self.set_y_speed(5.);
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer { if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer {
match peer { match peer {
PeerKind::Client(peer, addr) => peer.send_reliable(&addr, Jump(0)), PeerKind::Client(peer, addr) => peer.send_reliable(&addr, Jump(0)),
@ -239,7 +229,6 @@ impl IPlayer for LocalPlayer {
} }
} }
} }
}
fn get_look_up(&self) -> f32 { fn get_look_up(&self) -> f32 {
self.look_up self.look_up
@ -409,7 +398,7 @@ impl ICharacterBody3D for LocalPlayer {
let input = Input::singleton(); let input = Input::singleton();
if self.base().is_on_floor() && input.is_action_just_pressed("jump") { if self.base().is_on_floor() && input.is_action_just_pressed("jump") {
self.try_jump(); self.jump();
} }
Vector3::RIGHT * input.is_action_pressed("right") as u32 as f32 Vector3::RIGHT * input.is_action_pressed("right") as u32 as f32
@ -608,7 +597,7 @@ impl IPlayer for RemotePlayer {
} }
fn jump(&mut self) { fn jump(&mut self) {
self.try_jump(); self.set_y_speed(5.);
} }
fn get_look_up(&self) -> f32 { fn get_look_up(&self) -> f32 {

View File

@ -1,15 +1,10 @@
use std::collections::HashMap;
use godot::{ use godot::{
classes::{ classes::{Button, IPanel, OptionButton, Panel, TextEdit, VBoxContainer},
Button, CheckBox, Control, GridContainer, IPanel, Label, OptionButton, Panel, TextEdit,
VBoxContainer,
},
prelude::*, prelude::*,
}; };
use crate::{ use crate::{
game_manager::{Game, GameManager, GameOption, GameOptionValue, GameOptions}, game_manager::{Game, GameManager},
net::network_manager::{NetworkManager, Package, PeerKind}, net::network_manager::{NetworkManager, Package, PeerKind},
ui::player_listing::PlayerListing, ui::player_listing::PlayerListing,
}; };
@ -27,12 +22,6 @@ pub struct LobbyPanel {
ready_button: Option<Gd<Button>>, ready_button: Option<Gd<Button>>,
#[export] #[export]
map_selection: Option<Gd<OptionButton>>, 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, is_ready: bool,
@ -75,29 +64,18 @@ impl IPanel for LobbyPanel {
} }
if let Some(game) = Game::singleton() { 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 { if let Some(map_selection) = &mut self.map_selection {
for map in game.bind().maps.iter_shared() { for map in game.bind().maps.iter_shared() {
map_selection.add_item(&map.bind().name); map_selection.add_item(&map.bind().name);
} }
map_selection.select(0);
}
let is_host = if let Some(peer) = &NetworkManager::singleton().bind().peer { let is_host = if let Some(peer) = &NetworkManager::singleton().bind().peer {
peer.is_host() peer.is_host()
} else { } else {
false false
}; };
self.on_game_opts_update(&game.bind().opts, is_host); map_selection.set_disabled(!is_host);
map_selection.select(0);
}
} }
self.update_map_selection(0); self.update_map_selection(0);
@ -135,85 +113,10 @@ impl IPanel for LobbyPanel {
} }
} }
self.update_ready_button(); 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 { 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) { fn update_ready_button(&mut self) {
if let Some(game) = Game::singleton() if let Some(game) = Game::singleton()
&& let Some(peer) = &NetworkManager::singleton().bind().peer && let Some(peer) = &NetworkManager::singleton().bind().peer
@ -291,11 +194,10 @@ impl LobbyPanel {
pub fn update_map_selection(&mut self, idx: u8) { pub fn update_map_selection(&mut self, idx: u8) {
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer { if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer {
match peer { match peer {
PeerKind::Client(peer, addr) => { PeerKind::Client(..) => {
if let Some(map_selection) = &mut self.map_selection { if let Some(map_selection) = &mut self.map_selection {
map_selection.select(idx as i32); map_selection.select(idx as i32);
self.run_deferred(|s| s.update_players(false)); self.run_deferred(|s| s.update_players(false));
peer.send_reliable(addr, Package::SelectMap(idx));
} }
} }
PeerKind::Server(peer, _) => { PeerKind::Server(peer, _) => {

View File

@ -4,7 +4,7 @@ use godot::{
}; };
use crate::{ use crate::{
game_manager::{Game, GameOption, GameOptions}, game_manager::{Game, GameOptions},
net::network_manager::NetworkManager, net::network_manager::NetworkManager,
team_resource::TeamResource, team_resource::TeamResource,
}; };
@ -52,14 +52,6 @@ impl IGridContainer for PlayerListing {
} }
if let Some(game) = &Game::singleton() { 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); self.update_game_options(&game.bind().opts, game.bind().self_id);
} }
} }
@ -70,9 +62,7 @@ impl PlayerListing {
let is_host = NetworkManager::singleton().bind().is_host(); let is_host = NetworkManager::singleton().bind().is_host();
if let Some(dropdown) = &mut self.team_dropdown { if let Some(dropdown) = &mut self.team_dropdown {
dropdown.set_disabled( dropdown.set_disabled(!is_host && !opts.allow_any_team && self.player_id != self_id);
!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 serde::{Deserialize, Serialize};
use crate::{ use crate::{
game_manager::{Game, GameOption}, game_manager::Game,
net::util::cast_ray, net::util::cast_ray,
player::{DamageSource, IPlayer}, player::{DamageSource, IPlayer},
}; };
@ -69,17 +69,9 @@ impl Weapon for Raygun {
if deal_damage && let Some(target) = target { if deal_damage && let Some(target) = target {
if let Ok(mut player) = target.collider.try_dynify::<dyn IPlayer>() { if let Ok(mut player) = target.collider.try_dynify::<dyn IPlayer>() {
self.run_deferred(move |_| { player
if let Some(game) = Game::singleton() .dyn_bind_mut()
&& game.bind().opts.is_true(GameOption::FriendlyFire) .take_damage(DamageSource::Player(player_id), 100, true);
{
player.dyn_bind_mut().take_damage(
DamageSource::Player(player_id),
100,
true,
);
}
});
} }
} }
} }