Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor Cleanup #14

Merged
merged 11 commits into from
Dec 18, 2024
Merged
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
6 changes: 2 additions & 4 deletions src/action_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ pub static FONT_SIZE_COST: f32 = 12.0;
pub struct ActionPanel {
actions: Vec<ActionPanelItem>,
entities: Vec<Entity>,
/// Change this field's value to force an action panel update.
/// TODO: It should be possible now to manually trigger change detection instead.
pub update: u32,
}

struct ActionPanelItem {
Expand Down Expand Up @@ -288,7 +285,7 @@ fn update_action_panel(
Some(tower_slot) => {
match tower_query.get(tower_slot) {
Ok((_, _, stats)) => {
// TODO
// TODO allow more upgrades?
stats.level < 2
}
Err(_) => false,
Expand All @@ -305,6 +302,7 @@ fn update_action_panel(

let price = match item.action {
Action::BuildTower(tower_type) => match tower_type {
// All towers are currently the same price.
TowerKind::Basic | TowerKind::Support | TowerKind::Debuff => TOWER_PRICE,
},
Action::UpgradeTower => match selection.selected {
Expand Down
45 changes: 22 additions & 23 deletions src/bullet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,33 @@ impl Plugin for BulletPlugin {
}

#[derive(Component)]
struct Bullet {
#[require(Sprite)]
pub struct Bullet {
target: Entity,
damage: u32,
speed: f32,
status_effect: Option<StatusEffect>,
}

pub fn spawn(
mut position: Vec3,
target: Entity,
damage: u32,
speed: f32,
status_effect: Option<StatusEffect>,
commands: &mut Commands,
image: Handle<Image>,
) {
position.z = layer::BULLET;

commands.spawn((
Sprite { image, ..default() },
Transform::from_translation(position),
Bullet {
target,
damage,
speed,
status_effect,
},
));
impl Bullet {
pub fn bundle(
position: Vec2,
image: Handle<Image>,
target: Entity,
damage: u32,
speed: f32,
status_effect: Option<StatusEffect>,
) -> impl Bundle {
(
Sprite { image, ..default() },
Transform::from_translation(position.extend(layer::BULLET)),
Bullet {
target,
damage,
speed,
status_effect,
},
)
}
}

fn update(
Expand Down
3 changes: 2 additions & 1 deletion src/enemy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ pub fn death(
currency.current = currency.current.saturating_add(2);
currency.total_earned = currency.total_earned.saturating_add(2);

action_panel.update += 1;
// Force an action panel update
action_panel.set_changed();
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/game_over.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ fn spawn_game_over(
},
BackgroundColor(ui_color::OVERLAY.into()),
GlobalZIndex(1),
StateScoped(TaipoState::GameOver),
))
.with_children(|parent| {
parent
Expand Down
20 changes: 10 additions & 10 deletions src/healthbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,21 @@ fn update(
for (healthbar, hp, children) in health_query.iter() {
let frac = (hp.current as f32 / hp.max as f32).clamp(0.0, 1.0);

let invisible = (hp.current >= hp.max && !healthbar.show_full)
|| (hp.current == 0 && !healthbar.show_empty);
let invisible = (!healthbar.show_full && hp.current >= hp.max)
|| (!healthbar.show_empty && hp.current == 0);

for child in children {
// Update the bar itself

if let Ok((mut transform, mut sprite)) = bar_query.get_mut(*child) {
if invisible {
sprite.color = HEALTHBAR_INVISIBLE.into();
sprite.color = if invisible {
HEALTHBAR_INVISIBLE.into()
} else if frac < 0.25 {
sprite.color = HEALTHBAR_CRITICAL.into();
HEALTHBAR_CRITICAL.into()
} else if frac < 0.75 {
sprite.color = HEALTHBAR_INJURED.into();
HEALTHBAR_INJURED.into()
} else {
sprite.color = HEALTHBAR_HEALTHY.into();
HEALTHBAR_HEALTHY.into()
};

let current_width = frac * healthbar.size.x;
Expand All @@ -114,10 +114,10 @@ fn update(
// Update the bar background

if let Ok(mut sprite) = bg_query.get_mut(*child) {
if invisible {
sprite.color = HEALTHBAR_INVISIBLE.into();
sprite.color = if invisible {
HEALTHBAR_INVISIBLE.into()
} else {
sprite.color = HEALTHBAR_BACKGROUND.into();
HEALTHBAR_BACKGROUND.into()
}
}
}
Expand Down
31 changes: 12 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ enum TaipoState {
Playing,
GameOver,
}
#[derive(Resource, Default)]
pub struct GameState {
// Just so we can keep these in the correct order
tower_slots: Vec<Entity>,
}

#[derive(Resource)]
pub struct Currency {
current: u32,
Expand Down Expand Up @@ -229,14 +225,14 @@ fn typing_target_finished_event(
currency.total_earned = currency.total_earned.saturating_add(1);
} else if let Action::SelectTower(tower) = *action {
selection.selected = Some(tower);
action_panel.update += 1;
action_panel.set_changed();
} else if let Action::UnselectTower = *action {
selection.selected = None;
action_panel.update += 1;
action_panel.set_changed();
} else if let Action::SwitchLanguageMode = *action {
toggle_events.send(AsciiModeEvent::Toggle);
toggled_ascii_mode = true;
action_panel.update += 1;
action_panel.set_changed();
} else if let Action::ToggleMute = *action {
sound_settings.mute = !sound_settings.mute;
} else if let Action::UpgradeTower = *action {
Expand All @@ -255,7 +251,7 @@ fn typing_target_finished_event(
}
}

action_panel.update += 1;
action_panel.set_changed();
} else if let Action::BuildTower(tower_kind) = *action {
if currency.current < TOWER_PRICE {
continue;
Expand Down Expand Up @@ -303,7 +299,7 @@ fn typing_target_finished_event(
}
}

action_panel.update += 1;
action_panel.set_changed();
}

// Any action except for toggling ascii "help" mode should disable ascii mode.
Expand Down Expand Up @@ -461,7 +457,6 @@ fn update_tower_slot_labels(

fn spawn_map_objects(
mut commands: Commands,
mut game_state: ResMut<GameState>,
mut typing_targets: ResMut<TypingTargets>,
mut waves: ResMut<Waves>,
level_handles: Res<LevelHandles>,
Expand Down Expand Up @@ -546,9 +541,8 @@ fn spawn_map_objects(

commands.spawn((
Goal,
// TODO does this actually need a Sprite?
Sprite::default(),
transform,
Visibility::default(),
HitPoints::full(hp),
HealthBar {
size,
Expand Down Expand Up @@ -600,8 +594,6 @@ fn spawn_map_objects(
})
.id();

game_state.tower_slots.push(tower);

let target = typing_targets.pop_front();

commands
Expand Down Expand Up @@ -648,7 +640,7 @@ fn spawn_map_objects(

fn check_spawn(
mut next_state: ResMut<NextState<TaipoState>>,
mut actions: ResMut<ActionPanel>,
mut action_panel: ResMut<ActionPanel>,
typing_targets: Query<Entity, With<ActionPanelItemImage>>,
waves: Res<Waves>,
) {
Expand All @@ -671,7 +663,7 @@ fn check_spawn(
// every time, but we should probably be on the lookout for actions not getting
// initialized

actions.update += 1;
action_panel.set_changed();

next_state.set(TaipoState::Playing);
}
Expand Down Expand Up @@ -722,8 +714,7 @@ fn main() {
.add_plugins(GameOverPlugin)
.add_plugins(ActionPanelPlugin);

app.init_resource::<GameState>()
.init_resource::<Currency>()
app.init_resource::<Currency>()
.init_resource::<TowerSelection>()
.init_resource::<AudioSettings>();

Expand Down Expand Up @@ -755,5 +746,7 @@ fn main() {
.run_if(in_state(TaipoState::Playing)),
);

app.enable_state_scoped_entities::<TaipoState>();

app.run();
}
13 changes: 1 addition & 12 deletions src/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@ impl Plugin for MainMenuPlugin {
Update,
(main_menu, button_system).run_if(in_state(TaipoState::MainMenu)),
);

app.add_systems(OnExit(TaipoState::MainMenu), main_menu_cleanup);
}
}

#[derive(Component)]
pub struct MainMenuMarker;

fn main_menu_startup(
mut commands: Commands,
font_handles: Res<FontHandles>,
Expand Down Expand Up @@ -57,7 +52,7 @@ fn main_menu_startup(
..default()
},
BackgroundColor(ui_color::OVERLAY.into()),
MainMenuMarker,
StateScoped(TaipoState::MainMenu),
))
.with_children(|parent| {
parent
Expand Down Expand Up @@ -106,12 +101,6 @@ fn main_menu_startup(

fn main_menu() {}

fn main_menu_cleanup(mut commands: Commands, main_menu_query: Query<Entity, With<MainMenuMarker>>) {
for ent in main_menu_query.iter() {
commands.entity(ent).despawn_recursive();
}
}

fn button_system(
mut interaction_query: Query<
(&Interaction, &mut BackgroundColor, &WordListMenuItem),
Expand Down
20 changes: 7 additions & 13 deletions src/tower.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::prelude::*;

use crate::{
bullet, enemy::EnemyKind, layer, typing_target_finished_event, AfterUpdate, HitPoints,
bullet::Bullet, enemy::EnemyKind, layer, typing_target_finished_event, AfterUpdate, HitPoints,
StatusDownSprite, StatusEffect, StatusEffectKind, StatusEffects, StatusUpSprite, TaipoState,
TextureHandles, TowerSelection,
};
Expand Down Expand Up @@ -361,9 +361,6 @@ fn shoot_enemies(
// - lowest health

if let Some((enemy, _, _)) = in_range.next() {
let mut bullet_translation = transform.translation;
bullet_translation.y += 24.0; // XXX magic sprite offset

let texture = match tower_type {
TowerKind::Basic => texture_handles.bullet_shuriken.clone(),
TowerKind::Debuff => texture_handles.bullet_debuff.clone(),
Expand All @@ -382,15 +379,12 @@ fn shoot_enemies(
.damage
.saturating_add(status_effects.get_total_add_damage());

bullet::spawn(
bullet_translation,
enemy,
damage,
100.0,
status,
&mut commands,
texture,
);
// XXX magic sprite offset
let bullet_pos = transform.translation.truncate() + Vec2::new(0.0, 24.0);

commands.spawn(Bullet::bundle(
bullet_pos, texture, enemy, damage, 100.0, status,
));
}
}
}
Expand Down
Loading