Skip to content

Commit

Permalink
Updated GUI stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac8668 committed Jan 27, 2025
1 parent 9c48134 commit b727871
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 92 deletions.
127 changes: 64 additions & 63 deletions src/chunk_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,75 +43,76 @@ impl ChunkManager {
}
}

//Still needs to save file chunks to file after this function is called
pub fn move_manager(
&mut self,
commands: &mut Commands,
images: &mut ResMut<Assets<Image>>,
chunk_textures: &Entity,
image_entities: &Query<(&Parent, Entity, &Handle<Image>)>,
file_chunks: &mut HashMap<IVec2, Chunk>,
move_dir: MoveDir,
) {
let (for_load, vec_load, dir, y) = match move_dir {
MoveDir::X(dir) => {
self.pos.x += dir;
(LOAD_HEIGHT, LOAD_WIDTH, dir, false)
}
MoveDir::Y(dir) => {
self.pos.y += dir;
(LOAD_WIDTH, LOAD_HEIGHT, dir, true)
}
};

let mut images_vec = vec![];
let mut to_remove = vec![];
for i in 0..for_load {
{
//Save far chunks
let mut to_add = ivec2(if dir == -1 { vec_load } else { -1 }, i);
if y {
to_add = to_add.yx()
/* //Still needs to save file chunks to file after this function is called
pub fn move_manager(
&mut self,
commands: &mut Commands,
images: &mut ResMut<Assets<Image>>,
chunk_textures: &Entity,
image_entities: &Query<(&Parent, Entity, &Handle<Image>)>,
file_chunks: &mut HashMap<IVec2, Chunk>,
move_dir: MoveDir,
) {
let (for_load, vec_load, dir, y) = match move_dir {
MoveDir::X(dir) => {
self.pos.x += dir;
(LOAD_HEIGHT, LOAD_WIDTH, dir, false)
}
let pos = self.pos + to_add;
MoveDir::Y(dir) => {
self.pos.y += dir;
(LOAD_WIDTH, LOAD_HEIGHT, dir, true)
}
};
let mut images_vec = vec![];
let mut to_remove = vec![];
for i in 0..for_load {
{
//Save far chunks
let mut to_add = ivec2(if dir == -1 { vec_load } else { -1 }, i);
if y {
to_add = to_add.yx()
}
let pos = self.pos + to_add;
let changed_chunk = self.chunks.remove(&pos).unwrap();
to_remove.push(changed_chunk.texture.clone());
images.remove(changed_chunk.texture.clone());
let changed_chunk = self.chunks.remove(&pos).unwrap();
to_remove.push(changed_chunk.texture.clone());
images.remove(changed_chunk.texture.clone());
if let Some(chunk) = file_chunks.get_mut(&pos) {
*chunk = changed_chunk;
} else {
file_chunks.insert(pos, changed_chunk);
if let Some(chunk) = file_chunks.get_mut(&pos) {
*chunk = changed_chunk;
} else {
file_chunks.insert(pos, changed_chunk);
}
}
}
{
//Load new chunks
let mut to_add = ivec2(if dir == 1 { vec_load - 1 } else { 0 }, i);
if y {
to_add = to_add.yx()
}
let pos = self.pos + to_add;
{
//Load new chunks
let mut to_add = ivec2(if dir == 1 { vec_load - 1 } else { 0 }, i);
if y {
to_add = to_add.yx()
}
let pos = self.pos + to_add;
let chunk = if let Some(file_chunk) = file_chunks.get(&pos) {
file_chunk.clone()
} else {
Chunk::new(Handle::default(), pos)
};
let chunk = if let Some(file_chunk) = file_chunks.get(&pos) {
file_chunk.clone()
} else {
Chunk::new(Handle::default(), pos)
};
images_vec.push(add_chunk(commands, images, self, chunk, pos));
images_vec.push(add_chunk(commands, images, self, chunk, pos));
}
}
}
for (parent, ent, handle) in image_entities.iter() {
if parent.get() == *chunk_textures && to_remove.contains(handle) {
commands.get_entity(ent).unwrap().despawn();
for (parent, ent, handle) in image_entities.iter() {
if parent.get() == *chunk_textures && to_remove.contains(handle) {
commands.get_entity(ent).unwrap().despawn();
}
}
let mut chunk_textures = commands.get_entity(*chunk_textures).unwrap();
chunk_textures.insert_children(0, &images_vec);
}
let mut chunk_textures = commands.get_entity(*chunk_textures).unwrap();
chunk_textures.insert_children(0, &images_vec);
}
*/
}

impl Drop for ChunkManager {
Expand Down Expand Up @@ -539,7 +540,7 @@ pub fn add_chunk(
chunk_manager.chunks.insert(index, chunk);
entity
}

/*
pub fn update_manager_pos(
mut commands: Commands,
chunk_textures: Query<Entity, With<ChunksParent>>,
Expand Down Expand Up @@ -625,7 +626,7 @@ pub fn update_manager_pos(
Poll::Pending => {}
}
}

*/
#[derive(Resource, Default, Deref, DerefMut)]
pub struct ExtractedTextureUpdates(pub Vec<ExtractedTextureUpdate>);

Expand Down Expand Up @@ -722,7 +723,7 @@ impl Plugin for ChunkManagerPlugin {
.add_systems(
Update,
(
update_manager_pos,
//update_manager_pos,
add_colliders.after(update_has_collider),
remove_colliders.after(update_has_collider),
update_has_collider,
Expand All @@ -737,11 +738,11 @@ impl Plugin for ChunkManagerPlugin {
.init_resource::<ChunkManager>()
.init_resource::<DirtyRects>();

if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
if let Some(render_app) = app.get_sub_app_mut(RenderApp) {
render_app
.init_resource::<ExtractedTextureUpdates>()
.add_systems(ExtractSchedule, extract_chunk_texture_updates);
Image::register_system(
GpuImage::register_system(
render_app,
prepare_chunk_gpu_textures.in_set(RenderSet::PrepareAssets),
)
Expand Down
42 changes: 13 additions & 29 deletions src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ fn spawn_menu_buttons(mut commands: Commands, asset_server: Res<AssetServer>) {
let background = asset_server.load("images/menu_background.png");

commands.spawn((
SpriteBundle {
texture: background.clone(),
..Default::default()
},
Sprite::from(background.clone()),
Background(background),
MenuUI,
));

let button_style: Style = Style {
let button_style = Node {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(5.0)),
Expand All @@ -34,7 +31,7 @@ fn spawn_menu_buttons(mut commands: Commands, asset_server: Res<AssetServer>) {
..default()
};

let ui_style = Style {
let ui_style = Node {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
align_items: AlignItems::Center,
Expand All @@ -44,43 +41,30 @@ fn spawn_menu_buttons(mut commands: Commands, asset_server: Res<AssetServer>) {
..default()
};

let text_style = TextStyle {
let text_style = TextFont {
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
..Default::default()
};
let text_color = TextColor(Color::srgb(0.9, 0.9, 0.9));

commands
.spawn(NodeBundle {
style: ui_style,
..default()
})
.spawn(ui_style)
.insert(MenuUI)
.with_children(|parent| {
//Start
parent
.spawn(ButtonBundle {
style: button_style.clone(),
border_color: BorderColor(Color::BLACK),
background_color: NORMAL_BUTTON.into(),
..default()
})
.spawn((Button, button_style.clone()))
.insert(ButtonType::Start)
.with_children(|parent| {
parent.spawn(TextBundle::from_section("Start", text_style.clone()));
parent.spawn((Text::new("Start"), text_style.clone(), text_color));
});

//Quit
parent
.spawn(ButtonBundle {
style: button_style,
border_color: BorderColor(Color::BLACK),
background_color: NORMAL_BUTTON.into(),
..default()
})
.spawn((Button, button_style))
.insert(ButtonType::Quit)
.with_children(|parent| {
parent.spawn(TextBundle::from_section("Quit", text_style));
parent.spawn((Text::new("Quit"), text_style, text_color));
});
});
}
Expand All @@ -91,7 +75,7 @@ fn background_system(
window: Query<&Window>,
) {
let (mut transform, handle) = background.single_mut();
let Some(image) = images.get(handle.0.clone()) else {
let Some(image) = images.get(&handle.0) else {
return;
};
let window = window.single();
Expand Down Expand Up @@ -120,12 +104,12 @@ fn button_system(
match *interaction {
Interaction::Pressed => {
*color = PRESSED_BUTTON.into();
border_color.0 = Color::RED;
border_color.0 = Color::srgb(1., 0., 0.);

match *button_type {
ButtonType::Start => next_state.set(GameState::Game),
ButtonType::Quit => {
exit.send(AppExit);
exit.send(AppExit::Success);
}
}
}
Expand Down

0 comments on commit b727871

Please sign in to comment.