Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion game_client/src/gameboard/minions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bevy::prelude::{
use bevy::sprite::{SpriteSheetBundle, TextureAtlasSprite};
use game_shared::game::minion::{Health, MinionId};
use game_shared::game::tiles::TileIndex;
use log::debug;
use log::{debug, info};
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};

#[derive(Resource)]
Expand Down Expand Up @@ -60,6 +60,7 @@ fn receive_minions(

while let Ok((minion_id, tile_index, health)) = received_minions_channel.receiver.try_recv() {
debug!("Minion system update: {minion_id:?} {tile_index:?}");
info!("Minion update: {minion_id:?} {tile_index:?} {health:?}");

let position = Vec2::new(tile_index.x as f32, tile_index.y as f32);

Expand Down
6 changes: 5 additions & 1 deletion game_client/src/gameboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ use game_shared::game::policies::{
};
use game_shared::game::tiles::TileIndex;
use game_shared::network::serialization::SerializationFormat;
use game_shared::password::Password;
use log::{error, info};
use ordered_float::NotNan;
use std::ops::Deref;
use std::sync::Arc;
use std::time::Duration;
use url::Url;

Expand All @@ -44,6 +46,8 @@ const ARROW_MOVE_STEP_SIZE: f32 = 256.0;
pub struct GameConfig {
pub serializer: SerializationFormat,
pub server_url: Url,
pub username: Arc<str>,
pub password: Password,
}

#[derive(Resource, Default, Clone)]
Expand Down Expand Up @@ -208,7 +212,7 @@ fn setup_network(

info!("Authenticating");
client
.authenticate("player".into(), "secret".into())
.authenticate(config.username.clone(), config.password.clone())
.await
.expect("Authentication should work");
info!("Authenticated");
Expand Down
8 changes: 7 additions & 1 deletion game_client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod network;
mod ui;

use bevy::prelude::{default, App, PluginGroup, PreStartup};
use bevy::window::{Window, WindowPlugin};
use bevy::window::{PresentMode, Window, WindowPlugin};
use bevy::DefaultPlugins;
use bevy_egui::EguiPlugin;
use clap::{arg, Parser};
Expand All @@ -25,6 +25,10 @@ use ui::GameUiPlugin;
struct Args {
#[arg(long)]
serializer: Option<String>,
#[arg(long)]
username: Option<String>,
#[arg(long)]
password: Option<String>,
}

fn main() {
Expand Down Expand Up @@ -52,6 +56,8 @@ fn main() {
GameBoardPlugin::new(GameConfig {
serializer,
server_url: "ws://127.0.0.1:8080".try_into().unwrap(),
username: args.username.unwrap_or("player".into()).into(),
password: args.password.unwrap_or("secret".into()).into(),
}),
GameUiPlugin,
))
Expand Down
2 changes: 1 addition & 1 deletion game_server/src/game/async_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ struct ActivateTemporaryPolicyMessage {

impl ExecuteMessage<Result<(), ()>> for ActivateTemporaryPolicyMessage {
fn execute(self, world: &mut World) -> Result<(), ()> {
world.activate_temporary_policy(&self.player_id, &self.minion_id, &self.policy)
world.activate_temporary_policy(&self.player_id, self.minion_id, &self.policy)
}
}

Expand Down
64 changes: 64 additions & 0 deletions game_server/src/game/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use super::minions::minion_manager::MinionManager;
use super::minions::policy_evaluator::PolicyEvaluator;
use game_shared::game::minion::{Damage, MinionId};
use game_shared::game::tiles::TileIndex;
use std::ops::AddAssign;

pub(crate) enum GameEvent {
MinionSetPosition {
position: TileIndex,
target: MinionId,
},
MinionTakeDamage {
damage: Damage,
target: MinionId,
},
}

impl GameEvent {
pub fn execute<const CHUNK_SIZE: usize>(
self,
minion_manager: &mut MinionManager<CHUNK_SIZE>,
policy_evaluator: &mut PolicyEvaluator,
) {
match self {
GameEvent::MinionSetPosition { position, target } => {
minion_manager.set_position(&target, position, policy_evaluator);
},
GameEvent::MinionTakeDamage { damage, target } => {
minion_manager.take_damage(&target, damage, policy_evaluator);
},
}
}
}

pub(crate) struct GameEvents {
inner: Vec<GameEvent>,
}

impl GameEvents {
pub fn empty() -> Self {
Self { inner: Vec::new() }
}
}

impl IntoIterator for GameEvents {
type IntoIter = std::vec::IntoIter<Self::Item>;
type Item = GameEvent;

fn into_iter(self) -> Self::IntoIter {
self.inner.into_iter()
}
}

impl AddAssign for GameEvents {
fn add_assign(&mut self, rhs: Self) {
self.inner.extend(rhs.inner)
}
}

impl From<Vec<GameEvent>> for GameEvents {
fn from(inner: Vec<GameEvent>) -> Self {
Self { inner }
}
}
4 changes: 0 additions & 4 deletions game_server/src/game/game_board/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ impl<const CHUNK_SIZE: usize> GameBoard<CHUNK_SIZE> {
self.chunk_manager.chunk(chunk_index)
}

pub fn try_chunk(&self, chunk_index: &ChunkIndex) -> Option<&Chunk<CHUNK_SIZE>> {
self.chunk_manager.try_chunk(chunk_index)
}

pub fn terminate(&mut self) {
self.chunk_manager.terminate();
}
Expand Down
121 changes: 63 additions & 58 deletions game_server/src/game/game_board/path_finding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,64 +123,7 @@ pub(crate) fn find_shortest_path<const CHUNK_SIZE: usize>(

let cost_to_current = *cost_from_start.get(&current_index).unwrap();

let neighbors = [
(
TileIndex {
x: current_index.x + 1,
y: current_index.y,
},
1.0,
),
(
TileIndex {
x: current_index.x,
y: current_index.y + 1,
},
1.0,
),
(
TileIndex {
x: current_index.x - 1,
y: current_index.y,
},
1.0,
),
(
TileIndex {
x: current_index.x,
y: current_index.y - 1,
},
1.0,
),
(
TileIndex {
x: current_index.x + 1,
y: current_index.y + 1,
},
consts::SQRT_2,
),
(
TileIndex {
x: current_index.x + 1,
y: current_index.y - 1,
},
consts::SQRT_2,
),
(
TileIndex {
x: current_index.x - 1,
y: current_index.y + 1,
},
consts::SQRT_2,
),
(
TileIndex {
x: current_index.x - 1,
y: current_index.y - 1,
},
consts::SQRT_2,
),
];
let neighbors = get_neighbors(&current_index);

for (neighbor_index, cost_factor) in neighbors {
let tentative_cost = cost_from_start
Expand Down Expand Up @@ -220,6 +163,68 @@ pub(crate) fn find_shortest_path<const CHUNK_SIZE: usize>(
None
}

fn get_neighbors(position: &TileIndex) -> impl Iterator<Item = (TileIndex, Cost)> {
[
(
TileIndex {
x: position.x + 1,
y: position.y,
},
1.0,
),
(
TileIndex {
x: position.x,
y: position.y + 1,
},
1.0,
),
(
TileIndex {
x: position.x - 1,
y: position.y,
},
1.0,
),
(
TileIndex {
x: position.x,
y: position.y - 1,
},
1.0,
),
(
TileIndex {
x: position.x + 1,
y: position.y + 1,
},
consts::SQRT_2,
),
(
TileIndex {
x: position.x + 1,
y: position.y - 1,
},
consts::SQRT_2,
),
(
TileIndex {
x: position.x - 1,
y: position.y + 1,
},
consts::SQRT_2,
),
(
TileIndex {
x: position.x - 1,
y: position.y - 1,
},
consts::SQRT_2,
),
]
.into_iter()
}

#[inline]
fn to_priority(value: Cost) -> CostPriority {
Reverse(NotNan::new(value).unwrap())
Expand Down
7 changes: 6 additions & 1 deletion game_server/src/game/game_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ impl GameLoop {
rt.block_on(async move {
let player_database = Arc::new(PlayerDatabase::new(database.clone()));
let user_database = Arc::new(UserDatabase::new(database.clone()));
let _ = user_database.create_dummy_user().await;
user_database
.create_user("player".into(), "secret".into())
.await;
user_database
.create_user("second".into(), "second".into())
.await;

info!("Initializing world");
let mut world = World::new(&data_path).await.unwrap();
Expand Down
Loading