Skip to content

Commit

Permalink
Merge pull request #17 from Xenira/dev
Browse files Browse the repository at this point in the history
Networking and Shop improvements
  • Loading branch information
Xenira authored Jun 22, 2023
2 parents 5265584 + 99f3b7b commit eb2da53
Show file tree
Hide file tree
Showing 25 changed files with 503 additions and 138 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified client/assets/textures/ui/character_base.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions client/assets/textures/ui/character_base.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pedestal, stone, runes, glowing, from above, hexagon
Negative prompt: characters
Steps: 70, Sampler: DPM++ SDE Karras, CFG scale: 7, Seed: 2081295314, Size: 512x512, Model hash: b76cc78ad9, Model: dreamshaper_6BakedVae, Version: v1.2.1
63 changes: 57 additions & 6 deletions client/src/modules/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ use protocol::{characters::get_characters, protocol::CharacterInstance};
use crate::{
components::{
animation::{
AnimationIndices, AnimationRepeatType, AnimationState, AnimationTimer,
Animation, AnimationIndices, AnimationRepeatType, AnimationState, AnimationTimer,
AnimationTransition, AnimationTransitionType,
},
dragndrop::{DragEvent, Dragged},
hover::HoverEvent,
tooltip::SetTooltipEvent,
},
prefabs::animation,
states::startup::{CharacterAssets, UiAssets},
states::{
game_shop::CharacterCount,
startup::{CharacterAssets, UiAssets},
},
util::text::break_text,
Cleanup,
};
Expand Down Expand Up @@ -46,9 +49,9 @@ fn on_spawn(
mut commands: Commands,
character_assets: Res<CharacterAssets>,
ui_assets: Res<UiAssets>,
q_added: Query<(&Character, Entity), Added<Character>>,
q_added: Query<(&Character, Option<&CharacterCount>, Entity), Added<Character>>,
) {
for (character, entity) in q_added.iter() {
for (character, cnt, entity) in q_added.iter() {
commands
.entity(entity)
.insert(Cleanup)
Expand All @@ -59,6 +62,7 @@ fn on_spawn(
&character_assets,
&ui_assets,
false,
cnt,
);
});
}
Expand All @@ -70,6 +74,7 @@ fn spawn_character_portrait(
character_assets: &CharacterAssets,
ui_assets: &UiAssets,
full_info: bool,
count: Option<&CharacterCount>,
) {
let character_animation = animation::simple(0, 0)
.with_state(
Expand Down Expand Up @@ -107,6 +112,51 @@ fn spawn_character_portrait(
AnimationTimer(Timer::from_seconds(0.05, TimerMode::Repeating)),
))
.with_children(|parent| {
if character.upgraded {
parent.spawn((
SpriteSheetBundle {
texture_atlas: character_assets.upgrded.clone(),
sprite: TextureAtlasSprite::new(0),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0))
.with_scale(Vec3::splat(1.5)),

..Default::default()
},
animation::simple(0, 29),
AnimationTimer(Timer::from_seconds(0.05, TimerMode::Repeating)),
));
} else if let Some(count) = count {
if count.0 == 1 {
parent.spawn((
SpriteSheetBundle {
texture_atlas: character_assets.duplicate.clone(),
sprite: TextureAtlasSprite::new(0),
transform: Transform::from_translation(Vec3::new(
0.0, 0.0, 1.0,
))
.with_scale(Vec3::splat(1.5)),
..Default::default()
},
animation::simple(0, 29),
AnimationTimer(Timer::from_seconds(0.05, TimerMode::Repeating)),
));
} else if count.0 > 1 {
parent.spawn((
SpriteSheetBundle {
texture_atlas: character_assets.upgradable.clone(),
sprite: TextureAtlasSprite::new(0),
transform: Transform::from_translation(Vec3::new(
0.0, 0.0, 1.0,
))
.with_scale(Vec3::splat(1.5)),
..Default::default()
},
animation::simple(0, 29),
AnimationTimer(Timer::from_seconds(0.05, TimerMode::Repeating)),
));
}
}

parent
.spawn(SpatialBundle {
transform: Transform::from_scale(Vec3::splat(6.0))
Expand Down Expand Up @@ -263,12 +313,12 @@ fn on_character_hover(
mut commands: Commands,
mut ev_hover: EventReader<HoverEvent>,
mut ev_tooltip: EventWriter<SetTooltipEvent>,
q_character: Query<&Character, Without<Dragged>>,
q_character: Query<(&Character, Option<&CharacterCount>), Without<Dragged>>,
character_assets: Res<CharacterAssets>,
ui_assets: Res<UiAssets>,
) {
for HoverEvent(entity, is_hovered) in ev_hover.iter() {
if let Ok(character) = q_character.get(*entity).map(|c| &c.0) {
if let Ok((character, count)) = q_character.get(*entity).map(|(c, cnt)| (&c.0, cnt)) {
if *is_hovered {
let tooltip = commands
.spawn((
Expand All @@ -286,6 +336,7 @@ fn on_character_hover(
&character_assets,
&ui_assets,
true,
count,
);
})
.id();
Expand Down
12 changes: 11 additions & 1 deletion client/src/networking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ pub mod polling;
pub mod util;

pub mod networking_plugin {
use bevy::prelude::{debug, App, Plugin, Resource};
use bevy::{
prelude::{debug, App, Plugin, Resource},
time::Timer,
};
use reqwest::Url;

use crate::networking::polling::RateLimitTimer;

use super::{
networking_events::NetworkingEvent,
networking_ressource::{NetworkingRessource, ServerUrl},
Expand Down Expand Up @@ -36,6 +41,10 @@ pub mod networking_plugin {
.enable_all()
.build()
.unwrap();

let mut rate_limit_timer = Timer::from_seconds(1.0, bevy::time::TimerMode::Once);
rate_limit_timer.pause();

app.add_event::<NetworkingEvent>()
.insert_resource(ServerUrl(
Url::parse(self.0.as_str())
Expand All @@ -44,6 +53,7 @@ pub mod networking_plugin {
.unwrap(),
))
.insert_resource(Runtime(runtime))
.insert_resource(RateLimitTimer(rate_limit_timer))
.init_resource::<NetworkingRessource>()
.add_event::<PollingStatus>()
.add_system(request_dispatcher)
Expand Down
25 changes: 24 additions & 1 deletion client/src/networking/polling.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::networking::util::get_task;
use async_channel::Receiver;
use bevy::prelude::*;
use protocol::protocol::Protocol;
use reqwest::Method;
use serde::__private::de;

use super::{
networking_events::NetworkingEvent, networking_plugin::Runtime,
Expand All @@ -11,6 +13,9 @@ use super::{
#[derive(Component, Debug)]
pub struct PollingReceiver(Receiver<NetworkingEvent>);

#[derive(Resource, Debug)]
pub struct RateLimitTimer(pub Timer);

pub(crate) enum PollingStatus {
Start,
Stop,
Expand All @@ -19,7 +24,7 @@ pub(crate) enum PollingStatus {
pub(crate) fn on_polling_status_change(
mut commands: Commands,
mut ev_polling_status: EventReader<PollingStatus>,
res: ResMut<NetworkingRessource>,
res: Res<NetworkingRessource>,
query_poller: Query<Entity, With<PollingReceiver>>,
runtime: Res<Runtime>,
) {
Expand Down Expand Up @@ -47,10 +52,28 @@ pub(crate) fn polling_poller(
transform_tasks: Query<(Entity, &PollingReceiver)>,
res: Res<NetworkingRessource>,
runtime: Res<Runtime>,
mut res_rate_limit_timer: ResMut<RateLimitTimer>,
time: Res<Time>,
) {
res_rate_limit_timer.0.tick(time.delta());

if res_rate_limit_timer.0.finished() {
res_rate_limit_timer.0.reset();
res_rate_limit_timer.0.pause();
}
if !res_rate_limit_timer.0.paused() {
return;
}

for (entity, receiver) in transform_tasks.iter() {
if let Ok(event) = receiver.0.try_recv() {
debug!("Sending networking event {:?}", event);
if let Protocol::NetworkingError(e) = &event.0 {
debug!("Networking error, pausing polling: {:?}", e);
res_rate_limit_timer.0.unpause();
res_rate_limit_timer.0.reset();
}

commands.entity(entity).insert(PollingReceiver(get_task(
&runtime,
&res.polling_client,
Expand Down
61 changes: 33 additions & 28 deletions client/src/states/game_shop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ pub(crate) struct GameShopPlugin;

impl Plugin for GameShopPlugin {
fn build(&self, app: &mut App) {
app.add_event::<ShopChangedEvent>()
app.init_resource::<PlayerBoard>()
.add_event::<ShopChangedEvent>()
.add_event::<BoardChangedEvent>()
.add_event::<GameUsersChangedEvent>()
.add_system(setup.in_schedule(OnEnter(STATE)))
Expand Down Expand Up @@ -87,6 +88,12 @@ pub struct BoardChangedEvent(pub Vec<Option<CharacterInstance>>);
#[derive(Debug)]
pub struct GameUsersChangedEvent(pub Vec<GameOpponentInfo>);

#[derive(Resource, Debug, Default)]
pub struct PlayerBoard(pub Vec<Option<CharacterInstance>>);

#[derive(Component, Debug)]
pub struct CharacterCount(pub u8);

fn setup(
mut commands: Commands,
mut networking: ResMut<NetworkingRessource>,
Expand All @@ -110,11 +117,6 @@ fn setup(

// spawn character pedestals
let pedestal = asset_server.load("textures/ui/character_base.png");
let pedestal_atlas = TextureAtlas::from_grid(pedestal, Vec2::new(64.0, 64.0), 2, 1, None, None);
let pedestal_atlas_handle = texture_atlases.add(pedestal_atlas);

let mut pedestal_animation = animation::simple(0, 0);
animation::add_hover_state(&mut pedestal_animation, 0, 1);

commands
.spawn((
Expand All @@ -129,35 +131,29 @@ fn setup(
// front row
for i in 0..4 {
parent.spawn((
SpriteSheetBundle {
texture_atlas: pedestal_atlas_handle.clone(),
sprite: TextureAtlasSprite::new(0),
transform: Transform::from_scale(Vec3::splat(2.0))
SpriteBundle {
texture: pedestal.clone(),
transform: Transform::from_scale(Vec3::splat(0.25))
.with_translation(Vec3::new(68.0 * 2.0 * i as f32, 0.0, 1.0)),
..Default::default()
},
Hoverable("hover".to_string(), "leave".to_string()),
BoundingBox(Vec3::new(64.0, 64.0, 0.0), Quat::from_rotation_z(0.0)),
pedestal_animation.clone(),
AnimationTimer(Timer::from_seconds(0.05, TimerMode::Repeating)),
BoundingBox(Vec3::new(512.0, 512.0, 0.0), Quat::from_rotation_z(0.0)),
DropTagret,
Pedestal(i),
));
}
// back row
for i in 0..3 {
parent.spawn((
SpriteSheetBundle {
texture_atlas: pedestal_atlas_handle.clone(),
sprite: TextureAtlasSprite::new(0),
transform: Transform::from_scale(Vec3::splat(2.0))
SpriteBundle {
texture: pedestal.clone(),
transform: Transform::from_scale(Vec3::splat(0.25))
.with_translation(Vec3::new(68.0 + 68.0 * 2.0 * i as f32, -136.0, 1.0)),
..Default::default()
},
Hoverable("hover".to_string(), "leave".to_string()),
BoundingBox(Vec3::new(64.0, 64.0, 0.0), Quat::from_rotation_z(0.0)),
pedestal_animation.clone(),
AnimationTimer(Timer::from_seconds(0.05, TimerMode::Repeating)),
BoundingBox(Vec3::new(512.0, 512.0, 0.0), Quat::from_rotation_z(0.0)),
DropTagret,
Pedestal(4 + i),
));
Expand All @@ -166,18 +162,15 @@ fn setup(
// Bench
for i in 0..5 {
parent.spawn((
SpriteSheetBundle {
texture_atlas: pedestal_atlas_handle.clone(),
sprite: TextureAtlasSprite::new(0),
transform: Transform::from_scale(Vec3::splat(2.0)).with_translation(
SpriteBundle {
texture: pedestal.clone(),
transform: Transform::from_scale(Vec3::splat(0.25)).with_translation(
Vec3::new(-34.0 + 68.0 * 2.0 * i as f32, -136.0 * 2.0, 1.0),
),
..Default::default()
},
Hoverable("hover".to_string(), "leave".to_string()),
BoundingBox(Vec3::new(64.0, 64.0, 0.0), Quat::from_rotation_z(0.0)),
pedestal_animation.clone(),
AnimationTimer(Timer::from_seconds(0.05, TimerMode::Repeating)),
BoundingBox(Vec3::new(512.0, 512.0, 0.0), Quat::from_rotation_z(0.0)),
DropTagret,
Pedestal(7 + i),
));
Expand Down Expand Up @@ -426,6 +419,7 @@ fn generate_shop(
ui_assets: Res<UiAssets>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
asset_server: Res<AssetServer>,
res_board: Res<PlayerBoard>,
) {
for ev in ev_shop_change.iter() {
let shop_frame = asset_server.load("textures/ui/user_frame.png");
Expand All @@ -443,6 +437,13 @@ fn generate_shop(
commands.entity(shop).with_children(|parent| {
for (i, character) in ev.0.iter().enumerate() {
if let Some(character) = character {
let count = res_board
.0
.iter()
.filter_map(|c| c.as_ref())
.filter(|c| !c.upgraded && c.character_id == character.character_id)
.count() as u8;

parent
.spawn((
SpriteSheetBundle {
Expand All @@ -463,6 +464,7 @@ fn generate_shop(
},
Dragable,
Character(character.clone()),
CharacterCount(count),
))
.with_children(|parent| {
parent
Expand Down Expand Up @@ -504,8 +506,11 @@ fn generate_board(
q_pedestal: Query<(Entity, &Pedestal)>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
asset_server: Res<AssetServer>,
mut res_board: ResMut<PlayerBoard>,
) {
for ev in ev_board_change.iter() {
res_board.0 = ev.0.clone();

let shop_frame = asset_server.load("textures/ui/user_frame.png");
let shop_frame_atlas =
TextureAtlas::from_grid(shop_frame, Vec2::new(64.0, 64.0), 2, 1, None, None);
Expand Down Expand Up @@ -538,7 +543,7 @@ fn generate_board(
SpriteSheetBundle {
texture_atlas: shop_frame_atlas_handle.clone(),
sprite: TextureAtlasSprite::new(0),
transform: Transform::from_translation(Vec3::ZERO),
transform: Transform::from_scale(Vec3::splat(8.0)),
..Default::default()
},
Hoverable("hover".to_string(), "leave".to_string()),
Expand Down
Loading

0 comments on commit eb2da53

Please sign in to comment.