Skip to content

Commit

Permalink
remove flag and spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Nov 8, 2024
1 parent 26f71bd commit d43e57e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 100 deletions.
18 changes: 8 additions & 10 deletions assets/level.bw
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
⬜⬜⬜⬜⬜🟩🟩🟩🟩🟩⬜⬜⬜⬜⬜⬜⬜⬜⬜
⬜⬜⬜⬜⬜🟩⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
⬜⬜⬜⬜⬜🟩⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
⬜🙂🟩⬜⬜🟩⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
⬜🟩🟩⬜⬜🟩⬜⬜🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩⬜
⬜⬜⬜⬜⬜🟩⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜🟩⬜
⬜⬜⬜⬜⬜🟩⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜🟩⬜
⬜⬜⬜⬜⬜🟩⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜🟩⬜
⬜⬜⬜⬜⬜🟩⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜🏁🟩⬜
⬜⬜⬜⬜⬜🟩🟩🟩🟩🟩⬜⬜⬜⬜🟩🟩🟩🟩⬜
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
⬜⬜⬜⬜⬜⬜🙂⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
⬜⬜⬜⬜⬜🟩🟩🟩🟩🟩🟩🟩🟩🟩⬜⬜⬜⬜⬜
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜
72 changes: 14 additions & 58 deletions src/game/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::time::Duration;

use bevy::{prelude::*, time::common_conditions::on_timer};
use bevy::prelude::*;

use crate::{
level_loader::{Level, LoadedLevel, Tile},
Expand All @@ -13,11 +11,7 @@ const SCALE: f32 = 0.5;

pub fn game_plugin(app: &mut App) {
app.add_plugins(player::player_plugin)
.add_systems(OnEnter(GameState::Game), display_level)
.add_systems(
Update,
animate_level.run_if(on_timer(Duration::from_secs_f32(0.25))),
);
.add_systems(OnEnter(GameState::Game), display_level);
}

#[derive(Component)]
Expand All @@ -40,12 +34,6 @@ struct Velocity {
#[derive(Component)]
struct Ground;

#[derive(Component)]
struct Flag;

#[derive(Event)]
struct ReachedFlag;

fn ground_tile_index(line: &[Tile], i: usize) -> usize {
match (
i == 0 || !matches!(line.get(i - 1).unwrap_or(&Tile::Empty), Tile::Ground),
Expand Down Expand Up @@ -83,36 +71,6 @@ fn display_tile(
StateScoped(GameState::Game),
));
}
Tile::Spawn => {
commands.spawn((
Sprite::from_atlas_image(
assets.player_image.clone(),
TextureAtlas {
layout: assets.player_layout.clone(),
index: 0,
},
),
Transform::from_xyz(x, y + 256.0 / 4.0 * SCALE, 2.0).with_scale(Vec3::splat(SCALE)),
StateScoped(GameState::Game),
Player,
));
}
Tile::Flag => {
commands
.spawn((
Sprite::from_atlas_image(
assets.items_image.clone(),
TextureAtlas {
layout: assets.items_layout.clone(),
index: 6,
},
),
Transform::from_xyz(x, y, 1.0).with_scale(Vec3::splat(SCALE)),
StateScoped(GameState::Game),
Flag,
))
.observe(reached_flag);
}
Tile::Empty => {}
}
}
Expand All @@ -134,19 +92,17 @@ fn display_level(
display_tile(&mut commands, tile, i, x, y, line, &assets);
}
}
}

fn animate_level(mut flags: Query<&mut Sprite, With<Flag>>) {
for mut flag in &mut flags {
let atlas = flag.texture_atlas.as_mut().unwrap();
if atlas.index == 6 {
atlas.index = 12;
} else {
atlas.index = 6;
}
}
}

fn reached_flag(_trigger: Trigger<ReachedFlag>, mut next: ResMut<NextState<GameState>>) {
next.set(GameState::Menu);
commands.spawn((
Sprite::from_atlas_image(
assets.player_image.clone(),
TextureAtlas {
layout: assets.player_layout.clone(),
index: 0,
},
),
Transform::from_xyz(0.0, 200.0, 0.0).with_scale(Vec3::splat(0.5)),
StateScoped(GameState::Game),
Player,
));
}
19 changes: 1 addition & 18 deletions src/game/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy::{

use crate::GameState;

use super::{AgainstWall, Flag, Ground, IsOnGround, Player, ReachedFlag, Velocity};
use super::{AgainstWall, Ground, IsOnGround, Player, Velocity};

pub fn player_plugin(app: &mut App) {
app.add_systems(
Expand All @@ -17,7 +17,6 @@ pub fn player_plugin(app: &mut App) {
player_animation,
death_by_fall,
gravity.after(on_ground),
near_flag,
)
.run_if(in_state(GameState::Game)),
);
Expand Down Expand Up @@ -180,19 +179,3 @@ fn death_by_fall(
next.set(GameState::Menu);
}
}

fn near_flag(
mut commands: Commands,
player_transform: Query<&Transform, With<Player>>,
flags: Query<(Entity, &Transform), With<Flag>>,
) {
let player_transform = player_transform.single();
for (flag, flag_transform) in &flags {
let distance = player_transform
.translation
.distance(flag_transform.translation);
if distance < 50.0 {
commands.entity(flag).trigger(ReachedFlag);
}
}
}
5 changes: 1 addition & 4 deletions src/level_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ pub struct Level {
pub enum Tile {
Empty,
Ground,
Spawn,
Flag,
}

#[derive(Default)]
Expand Down Expand Up @@ -56,8 +54,7 @@ impl AssetLoader for LevelLoader {
match char {
'⬜' => line.push(Tile::Empty),
'🟩' => line.push(Tile::Ground),
'🙂' => line.push(Tile::Spawn),
'🏁' => line.push(Tile::Flag),
'🙂' => (),
'\n' => {
tiles.push(line);
line = vec![];
Expand Down
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,4 @@ struct GameAssets {
player_layout: Handle<TextureAtlasLayout>,
ground_image: Handle<Image>,
ground_layout: Handle<TextureAtlasLayout>,
items_image: Handle<Image>,
items_layout: Handle<TextureAtlasLayout>,
}
8 changes: 0 additions & 8 deletions src/splash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,5 @@ fn load_assets(
None,
None,
)),
items_image: asset_server.load("spritesheet_items.png"),
items_layout: texture_atlas_layouts.add(TextureAtlasLayout::from_grid(
UVec2::new(128, 128),
6,
4,
None,
None,
)),
});
}

0 comments on commit d43e57e

Please sign in to comment.