Skip to content

Commit

Permalink
feat: limited path tiles 🖼️
Browse files Browse the repository at this point in the history
  • Loading branch information
eerii committed Dec 10, 2023
1 parent da41c1c commit 4064360
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 37 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ a game for the bevy jam 4

- [ ] important tweaks (dom mañ)
- [ ] add sprites
- [ ] zoom out screen
- [ ] limited path tiles
- [x] zoom out screen
- [x] limited path tiles
- [ ] end screen (win/lose)

- [ ] new features (dom tar)
Expand Down
47 changes: 37 additions & 10 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ use bevy_ecs_tilemap::prelude::*;
use rand::Rng;

use crate::{
tilemap::{play_to_real_size, EndTile, LevelSize, PathTile, StartTile, TileChanged},
tilemap::{
play_to_real_size, EndTile, LevelSize, PathTile, StartTile, TilesAvailable, MAP_SIZE,
},
GameState,
};

//0, 1, 2, 3, 4, 5, 130, 160, 250, 300, 400, 500, 700, 900, 1200, 1500, 2000, 2500, 3500,
const START_SCORES: [u32; 22] = [
0, 5, 30, 50, 70, 100, 130, 160, 250, 300, 400, 500, 700, 900, 1200, 1500, 2000, 2500, 3500,
5000, 7000, 8500,
Expand All @@ -26,7 +29,11 @@ impl Plugin for CharonPlugin {
)
.add_systems(
Update,
spawn_start_end.run_if(resource_exists_and_changed::<GameScore>()),
(
zoom_camera,
spawn_start_end.run_if(resource_exists_and_changed::<GameScore>()),
)
.run_if(in_state(GameState::Play)),
)
.add_systems(OnExit(GameState::Play), pause_game);
}
Expand All @@ -45,15 +52,21 @@ pub struct GameScore {
// Components
// ··········

#[derive(Component)]
pub struct GameCam;
#[derive(Component, Default)]
pub struct GameCam {
target_zoom: f32,
}

// ·······
// Systems
// ·······

fn init_game(mut cmd: Commands) {
cmd.spawn((Camera2dBundle::default(), RenderLayers::layer(0), GameCam));
cmd.spawn((
Camera2dBundle::default(),
RenderLayers::layer(0),
GameCam::default(),
));
cmd.insert_resource(GameScore::default())
}

Expand All @@ -73,10 +86,11 @@ fn spawn_start_end(
mut cmd: Commands,
score: Res<GameScore>,
mut level_size: ResMut<LevelSize>,
mut tile_changed: ResMut<TileChanged>,
mut available: ResMut<TilesAvailable>,
tilemap: Query<&TileStorage>,
starts: Query<&TilePos, With<StartTile>>,
ends: Query<&TilePos, With<EndTile>>,
mut cam: Query<&mut GameCam>,
mut start_spawned: Local<usize>,
mut end_spawned: Local<usize>,
) {
Expand Down Expand Up @@ -110,10 +124,13 @@ fn spawn_start_end(
return;
};

// Grow level size every 2 starts
if is_start && (*start_spawned + 2) % 3 == 0 {
// Grow level size every 2 starts (only if we are not at the max size)
if is_start && (*start_spawned + 2) % 3 == 0 && level_size.0.x < MAP_SIZE.x {
level_size.0.x += 2;
level_size.0.y += 2;
if let Ok(mut cam) = cam.get_single_mut() {
cam.target_zoom += 0.13;
}
}
let (offset, size) = play_to_real_size(&level_size);

Expand All @@ -130,7 +147,7 @@ fn spawn_start_end(
if let Some(pos) = pos {
cmd.entity(storage.get(&pos).unwrap())
.insert((StartTile::default(), PathTile::default()));
tile_changed.0 += 1;
available.0 += 3;
}
}

Expand All @@ -146,12 +163,18 @@ fn spawn_start_end(
if let Some(pos) = pos {
cmd.entity(storage.get(&pos).unwrap())
.insert((EndTile, PathTile::default()));
tile_changed.0 += 1;
available.0 += 5;
}
}
}
}

fn zoom_camera(mut cam: Query<(&mut OrthographicProjection, &GameCam)>) {
if let Ok((mut proj, cam)) = cam.get_single_mut() {
proj.scale = lerp(proj.scale, 0.7 + cam.target_zoom, 0.01);
}
}

// ·····
// Extra
// ·····
Expand Down Expand Up @@ -209,3 +232,7 @@ fn get_spawn_pos(
fn tile_distance(a: &TilePos, b: &TilePos) -> u32 {
((a.x as i32 - b.x as i32).abs() + (a.y as i32 - b.y as i32).abs()) as u32
}

fn lerp(a: f32, b: f32, t: f32) -> f32 {
a + (b - a) * t
}
39 changes: 34 additions & 5 deletions src/hud.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::prelude::*;

use crate::{game::GameScore, load::GameAssets, ui::*, GameState};
use crate::{game::GameScore, load::GameAssets, tilemap::TilesAvailable, ui::*, GameState};

// ······
// Plugin
Expand All @@ -23,6 +23,9 @@ impl Plugin for HudPlugin {
#[derive(Component)]
struct ScoreText;

#[derive(Component)]
struct TilesText;

// ·······
// Systems
// ·······
Expand All @@ -34,7 +37,7 @@ fn init_hud(mut cmd: Commands, assets: Res<GameAssets>, mut node: Query<Entity,
node.with_children(|parent| {
parent.spawn((
TextBundle::from_section(
"0",
"<> 0",
TextStyle {
font: assets.font.clone(),
font_size: 24.0,
Expand All @@ -49,14 +52,40 @@ fn init_hud(mut cmd: Commands, assets: Res<GameAssets>, mut node: Query<Entity,
}),
ScoreText,
));

parent.spawn((
TextBundle::from_section(
"[] 0",
TextStyle {
font: assets.font.clone(),
font_size: 24.0,
color: Color::WHITE,
},
)
.with_style(Style {
position_type: PositionType::Absolute,
left: Val::Px(5.0),
top: Val::Px(5.0),
..default()
}),
TilesText,
));
});
}
}
}

fn update_hud(score: Res<GameScore>, mut text: Query<&mut Text, With<ScoreText>>) {
for mut text in text.iter_mut() {
text.sections[0].value = format!("{}", score.score);
fn update_hud(
score: Res<GameScore>,
mut score_text: Query<&mut Text, (With<ScoreText>, Without<TilesText>)>,
tiles: Res<TilesAvailable>,
mut tiles_text: Query<&mut Text, (With<TilesText>, Without<ScoreText>)>,
) {
for mut text in score_text.iter_mut() {
text.sections[0].value = format!("<> {}", score.score);
}
for mut text in tiles_text.iter_mut() {
text.sections[0].value = format!("[] {}", tiles.0);
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/spirits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const SPIRIT_SIZE: f32 = 32.;
const MAX_SPIRITS_IN_TILE: u32 = 3;

pub const INITIAL_SPAWN_TIME: f32 = 1.0;
const LOSE_COUNT: u32 = 40;
const LOSE_COUNT: f32 = 40.;

const FUN_A: f32 = 10.;

Expand Down Expand Up @@ -89,7 +89,7 @@ fn spawn_spirit(
for (start_pos, mut start_tile, mut start_path) in start.iter_mut() {
if start_tile.spawn_timer.tick(time.delta()).just_finished() {
if let Ok((grid_size, map_type, trans)) = tilemap.get_single() {
start_tile.lose_counter += 1;
start_tile.lose_counter += 1.;

// Don't spawn entities if the path is not complete
if !start_tile.completed_once {
Expand All @@ -115,7 +115,7 @@ fn spawn_spirit(
},
Spirit::new(*start_pos, pos),
));
start_tile.lose_counter = start_tile.lose_counter.saturating_sub(2);
start_tile.lose_counter = (start_tile.lose_counter - 1.5).max(0.);

// Reduce timer 0.01 seconds until it is 0.5
let duration = start_tile.spawn_timer.duration().as_millis();
Expand All @@ -138,7 +138,7 @@ fn check_lose_count(
tilemap: Query<(&TilemapGridSize, &TilemapType, &Transform)>,
) {
for (pos, mut start) in start.iter_mut() {
if start.lose_counter > 10 {
if start.lose_counter > 10. {
let lose_text = start.lose_text;

if lose_text.is_none() {
Expand Down Expand Up @@ -167,13 +167,13 @@ fn check_lose_count(
};

if let Ok(mut text) = text.get_mut(lose_text.unwrap()) {
let remainder = 16 - ((start.lose_counter - 10) / 2).min(15);
text.sections[0].value = if remainder <= 2 {
let remainder = 16. - ((start.lose_counter - 10.) / 2.).min(15.);
text.sections[0].value = if remainder <= 2. {
"!!!".to_string()
} else if remainder > 15 {
} else if remainder > 15. {
"".to_string()
} else {
remainder.to_string()
remainder.round().to_string()
};
}
}
Expand Down
29 changes: 17 additions & 12 deletions src/tilemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ use crate::{
config::Keybinds,
input::{Bind, MousePosition},
load::TilemapAssets,
GameState, spirits::INITIAL_SPAWN_TIME,
spirits::INITIAL_SPAWN_TIME,
GameState,
};

const MAP_SIZE: TilemapSize = TilemapSize { x: 20, y: 15 };
pub const MAP_SIZE: TilemapSize = TilemapSize { x: 20, y: 15 };
const TILE_SIZE: TilemapTileSize = TilemapTileSize { x: 64., y: 64. };
const GRID_SIZE: TilemapGridSize = TilemapGridSize { x: 72., y: 72. };

Expand All @@ -29,7 +30,7 @@ pub struct TilePlugin;

impl Plugin for TilePlugin {
fn build(&self, app: &mut App) {
app.insert_resource(TileChanged(0))
app.insert_resource(TilesAvailable(9))
.add_plugins(TilemapPlugin)
.add_systems(OnEnter(GameState::Play), init_tilemap.run_if(run_once()))
.add_systems(
Expand All @@ -40,7 +41,7 @@ impl Plugin for TilePlugin {
PostUpdate,
(
highlight_tile,
(autotile, pathfinding).run_if(resource_changed::<TileChanged>()),
(autotile, pathfinding).run_if(resource_changed::<TilesAvailable>()),
)
.run_if(in_state(GameState::Play)),
);
Expand All @@ -52,7 +53,7 @@ impl Plugin for TilePlugin {
// ·········

#[derive(Resource)]
pub struct TileChanged(pub u32);
pub struct TilesAvailable(pub u32);

#[derive(Resource)]
pub struct LevelSize(pub TilemapSize);
Expand All @@ -68,7 +69,7 @@ pub struct SelectedTile;
pub struct StartTile {
pub completed_once: bool,
pub spawn_timer: Timer,
pub lose_counter: u32,
pub lose_counter: f32,
pub lose_text: Option<Entity>,
}

Expand All @@ -77,7 +78,7 @@ impl Default for StartTile {
Self {
completed_once: false,
spawn_timer: Timer::from_seconds(INITIAL_SPAWN_TIME, TimerMode::Repeating),
lose_counter: 0,
lose_counter: 0.,
lose_text: None,
}
}
Expand Down Expand Up @@ -195,7 +196,7 @@ fn click_tile(
tilemap: Query<(&TilemapSize, &TileStorage)>,
input: Res<Input<Bind>>,
keybinds: Res<Persistent<Keybinds>>,
mut changed: ResMut<TileChanged>,
mut available: ResMut<TilesAvailable>,
mut prev: Local<Option<(bool, Option<TilePos>, Option<TilePos>)>>,
) {
let select = keybinds.interact.iter().any(|bind| {
Expand All @@ -222,13 +223,16 @@ fn click_tile(
// Erase path
if path.is_some() {
cmd.entity(entity).remove::<PathTile>();
changed.0 -= 1;
available.0 += 1;
return;
}

// Add paths
if available.0 == 0 {
return;
}
cmd.entity(entity).insert(PathTile::default());
changed.0 += 1;
available.0 -= 1;

// After first and second path
if two_ago.is_some() {
Expand All @@ -242,6 +246,7 @@ fn click_tile(
if prev_neighbours.iter().any(|p| p == pos) {
let entity = storage.get(one).unwrap();
cmd.entity(entity).remove::<PathTile>();
available.0 += 1;
one_ago.replace(*pos);
return;
}
Expand Down Expand Up @@ -308,9 +313,9 @@ fn pathfinding(
mut paths: Query<(&TilePos, &mut PathTile)>,
) {
// Clear all paths
/*for (_, mut path) in paths.iter_mut() {
for (_, mut path) in paths.iter_mut() {
path.distance.clear();
}*/
}

if let Ok((size, storage)) = tilemap.get_single() {
for end_pos in end.iter() {
Expand Down

0 comments on commit 4064360

Please sign in to comment.