Skip to content

Commit

Permalink
- fix wrongly configured tileset
Browse files Browse the repository at this point in the history
- fix players list
- disconnect players on game end
  • Loading branch information
Yarwin committed Oct 6, 2024
1 parent 0c53f79 commit 3c3293e
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/multiplayer-bomber/godot/src/entities/rock.tscn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[gd_scene load_steps=6 format=3 uid="uid://ciqjnvw2opgvk"]

[ext_resource type="Texture2D" uid="uid://cb1y8d1bbygtk" path="res://assets/brickfloor.png" id="1_sl8s3"]
[ext_resource type="Texture2D" uid="uid://doxassdyodvel" path="res://assets/brickfloor.png" id="1_sl8s3"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_l5afi"]
size = Vector2(48, 48)
Expand Down
2 changes: 1 addition & 1 deletion examples/multiplayer-bomber/godot/src/game/world.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[ext_resource type="TileSet" uid="uid://do2l6lpuotti8" path="res://src/tiles/tileset.tres" id="1"]
[ext_resource type="PackedScene" uid="uid://ciqjnvw2opgvk" path="res://src/entities/rock.tscn" id="2"]
[ext_resource type="FontFile" uid="uid://b1mhpw5ornbak" path="res://assets/montserrat.otf" id="4"]
[ext_resource type="FontFile" uid="uid://dcgp8pqs7f2jv" path="res://assets/montserrat.otf" id="4"]
[ext_resource type="PackedScene" uid="uid://enwoaqi0rnei" path="res://src/entities/bomb.tscn" id="4_f1bha"]

[node name="World" type="World"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[gd_scene load_steps=3 format=3 uid="uid://c5m3rogpaglk1"]

[ext_resource type="Texture2D" uid="uid://cb1y8d1bbygtk" path="res://assets/brickfloor.png" id="1"]
[ext_resource type="Texture2D" uid="uid://doxassdyodvel" path="res://assets/brickfloor.png" id="1"]

[sub_resource type="RectangleShape2D" id="1"]
size = Vector2(48, 48)
Expand Down
8 changes: 5 additions & 3 deletions examples/multiplayer-bomber/godot/src/tiles/tileset.tres
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
[gd_resource type="TileSet" load_steps=3 format=3 uid="uid://do2l6lpuotti8"]

[ext_resource type="Texture2D" uid="uid://cb1y8d1bbygtk" path="res://assets/brickfloor.png" id="1"]
[ext_resource type="Texture2D" uid="uid://doxassdyodvel" path="res://assets/brickfloor.png" id="1_la6op"]

[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_qhkfp"]
texture = ExtResource("1")
texture = ExtResource("1_la6op")
texture_region_size = Vector2i(48, 48)
0:0/0 = 0
0:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-24, -24, 24, -24, 24, 24, -24, 24)
0:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-24, -24, -24, 24, 24, 24, 24, -24)
1:0/0 = 0
2:0/0 = 0
3:0/0 = 0

[resource]
tile_size = Vector2i(48, 48)
Expand Down
16 changes: 15 additions & 1 deletion examples/multiplayer-bomber/rust/src/game_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ impl GameState {
.done();
self.multiplayer.set_multiplayer_peer(&peer);
self.peer = Some(peer);
self.players
.entry(self.multiplayer.get_unique_id())
.or_insert(self.player_name.clone());
}

#[func]
Expand All @@ -170,14 +173,16 @@ impl GameState {
peer.create_client(address, Self::DEFAULT_PORT);
self.multiplayer.set_multiplayer_peer(&peer);
self.peer = Some(peer);
self.players
.entry(self.multiplayer.get_unique_id())
.or_insert(self.player_name.clone());
}

#[func]
pub fn begin_game(&mut self) {
if !self.multiplayer.is_server() {
panic!("Only server can start a game!")
}
self.players.entry(1).or_insert(self.player_name.clone());
self.base_mut().rpc("load_world".into(), &[]);
let Some(world) = self.game_board.as_mut() else {
panic!("no game board!")
Expand Down Expand Up @@ -239,6 +244,9 @@ impl GameState {
}
self.base_mut().emit_signal("game_ended".into(), &[]);
self.players.clear();
if let Some(peer) = self.peer.as_mut() {
peer.close();
};
}

#[func]
Expand All @@ -247,6 +255,12 @@ impl GameState {
}
}

impl GameState {
pub fn get_players(&self) -> &HashMap<i32, GString> {
&self.players
}
}

impl GameSingleton for GameState {
const NAME: &'static str = "GameState";
}
Expand Down
21 changes: 16 additions & 5 deletions examples/multiplayer-bomber/rust/src/lobby.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::game_state::{GameSingleton, GameState};
use godot::classes::{
AcceptDialog, Button, Control, IControl, ItemList, Label, LineEdit, Os, Panel,
AcceptDialog, Button, Control, IControl, ItemList, Label, LineEdit, MultiplayerApi, Os, Panel,
};
use godot::prelude::*;

Expand All @@ -27,12 +27,16 @@ pub struct Lobby {
start_button: OnReady<Gd<Button>>,
#[init(node = "ErrorDialog")]
error_dialog: OnReady<Gd<AcceptDialog>>,
#[init(val = OnReady::manual())]
multiplayer: OnReady<Gd<MultiplayerApi>>,
base: Base<Control>,
}

#[godot_api]
impl IControl for Lobby {
fn ready(&mut self) {
self.multiplayer
.init(self.base().get_multiplayer().unwrap());
let on_connection_failed = self.base().callable("on_connection_failed");
GameState::singleton().connect("connection_failed".into(), on_connection_failed);
let on_connection_success = self.base().callable("on_connection_success");
Expand Down Expand Up @@ -117,14 +121,21 @@ impl Lobby {

#[func]
fn refresh_lobby(&mut self) {
let mut players = GameState::singleton().bind().get_player_list();
players.sort_unstable();
// add current player at the top of the players list
self.players_list.clear();
self.players_list.add_item(GString::from(
format! {"{} (You)", GameState::singleton().bind().player_name},
));
for player in players.iter_shared() {
self.players_list.add_item(player);

let game_state = GameState::singleton();
let binding = game_state.bind();
let other_players = binding
.get_players()
.iter()
.filter(|(player_id, _)| **player_id != self.multiplayer.get_unique_id());

for (_, player) in other_players {
self.players_list.add_item(player.clone());
}
let is_server = self.base().get_multiplayer().unwrap().is_server();
self.start_button.set_disabled(!is_server);
Expand Down
14 changes: 10 additions & 4 deletions examples/multiplayer-bomber/rust/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use godot::prelude::*;
pub struct World {
#[init(node = "Rocks")]
pub rocks: OnReady<Gd<Node2D>>,
#[init(node = "Players")]
pub players: OnReady<Gd<Node2D>>,
#[init(node = "Winner")]
pub winner_label: OnReady<Gd<Label>>,
#[init(node = "Score")]
pub score: OnReady<Gd<ScoreBoard>>,
#[init(node = "SpawnPoints")]
pub spawn_points: OnReady<Gd<Node2D>>,
#[init(node = "Players")]
pub players: OnReady<Gd<Node2D>>,
base: Base<Node2D>,
}

Expand All @@ -33,13 +33,19 @@ impl INode2D for World {
impl World {
#[func]
fn on_rock_being_removed(&mut self, _rock: Gd<Node>) {
if self.rocks.get_child_count() <= 60 {
if self.rocks.get_child_count() <= 65 {
self.finish_game();
}
}

#[func]
fn on_exit_game_pressed(&self) {
fn on_exit_game_pressed(&mut self) {
self.base_mut().rpc("end_game".into(), &[]);
}

#[rpc(any_peer, call_local)]
#[func(gd_self)]
fn end_game(_this: Gd<Self>) {
GameState::singleton().bind_mut().end_game();
}
}
Expand Down

0 comments on commit 3c3293e

Please sign in to comment.