Skip to content

Commit

Permalink
add finite ammo
Browse files Browse the repository at this point in the history
  • Loading branch information
caengen committed Jul 22, 2023
1 parent ef6a3a9 commit b17d324
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
Binary file added assets/textures/icons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/game/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,5 @@ pub struct Foreground;
pub struct TextColor;
#[derive(Component)]
pub struct DropBombTimer(pub Timer);
#[derive(Component)]
pub struct MissileReserve(pub usize);
3 changes: 2 additions & 1 deletion src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use self::{
effects::{flick_system, timed_removal_system},
prelude::stage_colors,
systems::{
animate_sprite_indices, animate_sprite_steps, change_colors, drop_bombs,
ammo_ui, animate_sprite_indices, animate_sprite_steps, change_colors, drop_bombs,
explosion_event_listener_system, explosion_system, flame_engulf_system, game_keys,
game_over_ui, gizmo_missile_trails, health_ui, missile_arrival_event_listner, move_cursor,
move_missile, move_ufo, reset_game_listener, rotate_player, score_ui, setup_player,
Expand Down Expand Up @@ -39,6 +39,7 @@ impl Plugin for GamePlugin {
animate_sprite_steps,
animate_sprite_indices,
score_ui,
ammo_ui,
)
.run_if(in_state(GameState::InGame).or_else(in_state(GameState::GameOver))),
// run these systems if we are in the InGame state
Expand Down
37 changes: 33 additions & 4 deletions src/game/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use super::{
AnimationIndices, AnimationStep, AnimeRemoveOnFinish, Cannon, CannonBase, ChainedMeta,
Cursor, DropBombTimer, Enemy, EnemySpawn, Engulfable, Explodable, Explosion,
ExplosionEvent, ExplosionMode, FlameRadius, Foreground, Health, IdCounter, Missile,
MissileArrivalEvent, Player, Score, Scoring, SpawnPoint, SplitTimer, Stepper, TargetLock,
Ufo, DROP_BOMB_CHANCE, MAX_SPLIT, PLAYER_MISSILE_SPEED, SPLIT_CHANCE,
MissileArrivalEvent, MissileReserve, Player, Score, Scoring, SpawnPoint, SplitTimer,
Stepper, TargetLock, Ufo, DROP_BOMB_CHANCE, MAX_SPLIT, PLAYER_MISSILE_SPEED, SPLIT_CHANCE,
},
effects::{Flick, TimedRemoval},
prelude::{color_from_vec, Stage, StageHandle},
Expand All @@ -28,15 +28,15 @@ pub fn game_keys(
mut id_counter: ResMut<IdCounter>,
mut commands: Commands,
images: Res<ImageAssets>,
player: Query<(Entity, With<Player>)>,
mut player: Query<((Entity, &mut MissileReserve), With<Player>)>,
mut cannon_base: Query<
(Entity, &mut Transform, Has<AnimationIndices>),
(With<CannonBase>, Without<Cursor>),
>,
time: Res<Time>,
) {
if buttons.just_pressed(MouseButton::Left) {
let player_entity = player.single().0;
let (player_entity, mut missile_reserve) = player.single_mut().0;
commands.entity(player_entity).insert((
AnimationIndices {
first: 0,
Expand All @@ -46,6 +46,12 @@ pub fn game_keys(
AnimeRemoveOnFinish,
));

if missile_reserve.0 == 0 {
return;
} else {
missile_reserve.0 -= 1;
}

let target_transform = cursor_pos.single();
let id = id_counter.next();
commands.spawn((
Expand Down Expand Up @@ -615,6 +621,7 @@ pub fn setup_player(mut commands: Commands, images: Res<ImageAssets>) {
Player,
Cannon,
Health { max: 3, current: 3 },
MissileReserve(30),
Foreground,
))
.id();
Expand All @@ -636,6 +643,28 @@ pub fn setup_player(mut commands: Commands, images: Res<ImageAssets>) {
/* UI
* Systems that are called every frame to update the egui UI
*/
pub fn ammo_ui(
mut contexts: EguiContexts,
images: Res<ImageAssets>,
missile_ammo: Query<&MissileReserve, With<Player>>,
) {
let ammo = missile_ammo.single();
let ammo_id = contexts.add_image(images.missile.clone_weak());

egui::Area::new("Ammo")
.anchor(Align2::LEFT_TOP, egui::emath::vec2(10., 5.))
.show(contexts.ctx_mut(), |ui: &mut egui::Ui| {
ui.with_layout(egui::Layout::left_to_right(egui::Align::TOP), |ui| {
ui.image(ammo_id, egui::emath::vec2(16., 16.));
ui.label(
RichText::new(format!("{:0>2}", ammo.0))
.font(FontId::proportional(24.))
.color(Color32::WHITE),
);
});
});
}

pub fn score_ui(mut contexts: EguiContexts, score: Res<Score>) {
egui::Area::new("Score")
.anchor(Align2::CENTER_TOP, egui::emath::vec2(10., 5.))
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub struct ImageAssets {
#[asset(texture_atlas(tile_size_x = 495.0, tile_size_y = 32.0, columns = 1, rows = 1))]
#[asset(path = "textures/ground.png")]
pub ground: Handle<TextureAtlas>,
#[asset(path = "textures/icons.png")]
pub missile: Handle<Image>,
}

#[derive(States, Hash, Clone, PartialEq, Eq, Debug, Default)]
Expand Down

0 comments on commit b17d324

Please sign in to comment.