Skip to content

Commit

Permalink
Hero Gallery improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
makscee committed Mar 15, 2024
1 parent b9c2516 commit 5d8806d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 13 deletions.
21 changes: 21 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@
"RUST_BACKTRACE": "1"
}
},
{
"type": "cargo",
"command": "run",
"problemMatcher": [
"$rustc"
],
"label": "Gallery",
"args": [
"--features",
"bevy/dynamic_linking",
"--",
"--mode",
"gallery"
],
"group": {
"kind": "build"
},
"env": {
"RUST_BACKTRACE": "1"
}
},
{
"type": "cargo",
"command": "run",
Expand Down
2 changes: 1 addition & 1 deletion assets/ron/houses/dragons.house.ron
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![enable(implicit_some)]
(
name: "Dragons",
color: ("#558B2F"),
color: ("#DC4405"),
statuses: [
(
name: "Growth",
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ pub enum RunMode {
Continue,
Sync,
Editor,
Gallery,
}

fn main() {
let args = Args::try_parse().unwrap_or_default();
let next_state = match args.mode {
RunMode::Regular => GameState::MainMenu,
RunMode::Custom => GameState::CustomBattle,
RunMode::Gallery => GameState::HeroGallery,
RunMode::Last => GameState::LastBattle,
RunMode::Editor => GameState::HeroEditor,
RunMode::Continue => GameState::Shop,
Expand All @@ -52,6 +54,7 @@ fn main() {
match args.mode {
RunMode::Regular
| RunMode::Custom
| RunMode::Gallery
| RunMode::Last
| RunMode::Continue
| RunMode::Editor => {
Expand Down Expand Up @@ -140,7 +143,7 @@ fn main() {
RunMode::Regular | RunMode::Continue | RunMode::Last | RunMode::Sync => {
app.add_systems(OnExit(GameState::Loading), LoginPlugin::setup);
}
RunMode::Test | RunMode::Custom | RunMode::Editor => {}
RunMode::Test | RunMode::Custom | RunMode::Gallery | RunMode::Editor => {}
}

app.run();
Expand Down
78 changes: 67 additions & 11 deletions src/plugins/hero_gallery.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,83 @@
use bevy_egui::egui::DragValue;

use super::*;

pub struct HeroGallery;

impl Plugin for HeroGallery {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(GameState::HeroGallery), Self::on_enter)
.add_systems(OnExit(GameState::HeroGallery), Self::on_leave);
app.add_systems(OnEnter(GameState::HeroGallery), Self::reload)
.add_systems(OnExit(GameState::HeroGallery), Self::on_leave)
.add_systems(Update, Self::ui.run_if(in_state(GameState::HeroGallery)));
}
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct HeroGalleryData {
per_row: usize,
show_limit: usize,
offset: Vec2,
cards: bool,
}

impl Default for HeroGalleryData {
fn default() -> Self {
Self {
per_row: 10,
offset: vec2(3.0, -3.0),
show_limit: default(),
cards: default(),
}
}
}

impl HeroGallery {
fn on_enter(world: &mut World) {
fn reload(world: &mut World) {
let data = PersistentData::load(world).hero_gallery_data;
let team = PackedTeam::spawn(Faction::Left, world);
let heroes = Pools::get(world).heroes.clone();
let per_slot = vec2(3.0, 0.0);
let start_pos = -((heroes.len() - 1) as f32) * 0.5 * per_slot + vec2(0.0, -3.0);
heroes.into_iter().enumerate().for_each(|(slot, (_, u))| {
let heroes = Pools::get(world)
.heroes
.values()
.cloned()
.sorted_by_key(|v| v.houses.clone())
.collect_vec();
let columns = data.per_row.max(1).min(heroes.len()) as f32 - 1.0;
let rows = (heroes.len() as f32 / data.per_row as f32).ceil() - 1.0;
let start_pos = vec2(-columns * 0.5 * data.offset.x, -rows * 0.5 * data.offset.y);

let mut row = 0;
let mut col = 0;
for u in heroes {
let u = u.unpack(team, None, world);
VarState::get_mut(u, world).init(
VarName::Position,
VarValue::Vec2(start_pos + per_slot * slot as f32),
);
let pos = start_pos + data.offset * vec2(col as f32, row as f32);
VarState::get_mut(u, world).init(VarName::Position, VarValue::Vec2(pos));
col += 1;
if data.per_row > 0 && data.per_row <= col {
col = 0;
row += 1;
}
}
}

fn ui(world: &mut World) {
let ctx = &if let Some(context) = egui_context(world) {
context
} else {
return;
};
let mut pd = PersistentData::load(world);
let mut data = pd.hero_gallery_data.clone();
TopBottomPanel::bottom("gallery controls").show(ctx, |ui| {
ui.horizontal(|ui| {
ui.label("per row:");
DragValue::new(&mut data.per_row).ui(ui);
});
});
if !data.eq(&pd.hero_gallery_data) {
pd.hero_gallery_data = data;
pd.save(world).unwrap();
Self::reload(world);
}
}

fn on_leave(world: &mut World) {
Expand Down
1 change: 1 addition & 0 deletions src/resourses/persistent_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::*;
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct PersistentData {
pub hero_editor_data: HeroEditorData,
pub hero_gallery_data: HeroGalleryData,
pub last_battle: (PackedTeam, PackedTeam),
}

Expand Down

0 comments on commit 5d8806d

Please sign in to comment.