Skip to content

Commit

Permalink
Fix new record logic and add new sprite win animation
Browse files Browse the repository at this point in the history
  • Loading branch information
septum committed Jul 25, 2022
1 parent 0ba313e commit 9dda954
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 45 deletions.
Binary file modified assets/images/player/spritesheet.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: 1 addition & 1 deletion src/core/level/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn spawn_entity(
texture,
Vec2::new(64.0, 64.0),
4,
6,
7,
Vec2::new(4.0, 4.0),
);
let texture_atlas_handle = texture_atlases.add(texture_atlas);
Expand Down
2 changes: 1 addition & 1 deletion src/core/save_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn set_record(save_file: &mut SaveFile, tag: &LevelTag, record: (usize, f32)

pub fn set_if_new_record(save_file: &mut SaveFile, tag: &LevelTag, moves: usize, time: f32) {
let record = get_record(save_file, tag);
if record.0 == 0 || record.0 > moves || record.1 > time {
if record.0 == 0 || record.0 > moves || record.0 >= moves && record.1 > time {
set_record(save_file, tag, (moves, time));
}
}
Expand Down
11 changes: 4 additions & 7 deletions src/resources/level/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,10 @@ impl Level {
}

#[must_use]
pub fn new_record_moves(&self) -> bool {
self.record.0 == 0 || self.moves < self.record.0
}

#[must_use]
pub fn new_record_time(&self) -> bool {
self.record.1 == 0.0 || self.stopwatch.elapsed().as_secs_f32() < self.record.1
pub fn new_record(&self) -> bool {
self.record.0 == 0
|| self.moves < self.record.0
|| self.moves <= self.record.0 && self.stopwatch.elapsed().as_secs_f32() < self.record.1
}

#[must_use]
Expand Down
27 changes: 17 additions & 10 deletions src/scenes/level/ui.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use bevy::prelude::*;

use crate::{
Expand All @@ -17,7 +19,12 @@ fn spawn_ui_camera(commands: &mut Commands) {
pub fn spawn_ui(commands: &mut Commands, level: &Level, fonts: &Fonts) {
let font = &fonts.upheavtt;
let record_new_level = if level.is_record_set() {
format!("Record: {}", level.record.0)
let duration = Duration::from_secs_f32(level.record.1);
let milliseconds = duration.subsec_millis();
let seconds = duration.as_secs() % 60;
let minutes = (duration.as_secs() / 60) % 60;
let time = format!("{:02}:{:02}:{:03}", minutes, seconds, milliseconds);
format!("{} moves in {}", level.record.0, time)
} else {
"New Level!".to_string()
};
Expand All @@ -41,8 +48,8 @@ pub fn spawn_ui(commands: &mut Commands, level: &Level, fonts: &Fonts) {
let mut undo_housing = Housing::percent(100.0, 50.0);

let mut level_name = EmbossedText::medium(format!("Level {}", level.get_name()), font);
let mut stopwatch = DynamicText::small("", font);
let mut record_new_level = EmbossedText::small(record_new_level, font);
let stopwatch = DynamicText::small("Time: ", font);
let moves = DynamicText::medium("Moves: ", font);
let undos_left = DynamicText::medium("Undos: ", font);
let undo = EmbossedText::small("(B) - Undo Movement", font);
Expand All @@ -51,7 +58,6 @@ pub fn spawn_ui(commands: &mut Commands, level: &Level, fonts: &Fonts) {

level_name.foreground_color(Colors::LIGHT).size(54.0);
record_new_level.foreground_color(Colors::SECONDARY);
stopwatch.color(Colors::SECONDARY);

overlay.justify_content(JustifyContent::SpaceBetween);
top.flex_direction(FlexDirection::Row);
Expand All @@ -63,13 +69,14 @@ pub fn spawn_ui(commands: &mut Commands, level: &Level, fonts: &Fonts) {
.align_items(AlignItems::FlexStart)
.position_type(PositionType::Absolute)
.top(-6.0);
stopwatch_housing
record_housing
.align_items(AlignItems::FlexStart)
.position_type(PositionType::Absolute)
.top(44.0);
moves_housing.align_items(AlignItems::FlexEnd);
record_housing
.align_items(AlignItems::FlexEnd)
stopwatch_housing
.width(Val::Px(152.0))
.align_items(AlignItems::FlexStart)
.position_type(PositionType::Absolute)
.top(44.0);
undo_housing.align_items(AlignItems::FlexEnd);
Expand All @@ -92,16 +99,16 @@ pub fn spawn_ui(commands: &mut Commands, level: &Level, fonts: &Fonts) {
level_housing.spawn(parent, |parent| {
level_name.spawn(parent);
});
stopwatch_housing.spawn(parent, |parent| {
stopwatch.spawn(parent, TextMarker::stopwatch());
record_housing.spawn(parent, |parent| {
record_new_level.spawn(parent);
});
});
top_right.spawn(parent, |parent| {
moves_housing.spawn(parent, |parent| {
moves.spawn(parent, TextMarker::moves());
});
record_housing.spawn(parent, |parent| {
record_new_level.spawn(parent);
stopwatch_housing.spawn(parent, |parent| {
stopwatch.spawn(parent, TextMarker::stopwatch());
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/scenes/selection/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn spawn_stock_buttons(parent: &mut ChildBuilder, save_file: &SaveFile, font: &H
let minutes = (duration.as_secs() / 60) % 60;
let time = format!("{:02}:{:02}:{:03}", minutes, seconds, milliseconds);
(
format!("Record: {}\nTime: {}", record.0, time),
format!("Record: {} moves\nin {}", record.0, time),
Colors::LIGHT,
)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/scenes/title/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn setup(
images.player.spritesheet.clone(),
Vec2::new(64.0, 64.0),
4,
6,
7,
Vec2::new(4.0, 4.0),
);
let texture_atlas_handle = texture_atlases.add(texture_atlas);
Expand Down
6 changes: 3 additions & 3 deletions src/scenes/win/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Plugin for WinPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(CharacterAnimation {
timer: Timer::from_seconds(0.125, true),
row: 0,
row: 6,
index: 0,
})
.add_system_set(
Expand Down Expand Up @@ -78,7 +78,7 @@ fn setup(
images.player.spritesheet.clone(),
Vec2::new(64.0, 64.0),
4,
6,
7,
Vec2::new(4.0, 4.0),
);
let texture_atlas_handle = texture_atlases.add(texture_atlas);
Expand All @@ -90,7 +90,7 @@ fn setup(
..TextureAtlasSprite::default()
},
texture_atlas: texture_atlas_handle,
transform: Transform::from_translation(Vec3::new(164.0, 16.0, 1.0)),
transform: Transform::from_translation(Vec3::new(160.0, 16.0, 1.0)),
..SpriteSheetBundle::default()
})
.insert(CharacterMarker);
Expand Down
28 changes: 7 additions & 21 deletions src/scenes/win/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,16 @@ fn spawn_ui_camera(commands: &mut Commands) {
pub fn spawn_ui(commands: &mut Commands, fonts: &Fonts, level: &Level) {
let font = &fonts.upheavtt;

let record_or_empty = if level.new_record_moves() {
format!("NEW RECORD: {}", level.moves)
} else {
" ".to_string()
};

let time_or_empty = if level.new_record_time() {
let record_or_empty = if level.new_record() {
let duration = level.stopwatch_elapsed();
let milliseconds = duration.subsec_millis();
let seconds = duration.as_secs() % 60;
let minutes = (duration.as_secs() / 60) % 60;
format!(
"NEW TIME: {:02}:{:02}:{:03}",
minutes, seconds, milliseconds
)
let time = format!("{:02}:{:02}:{:03}", minutes, seconds, milliseconds);

format!("NEW RECORD:\n{} moves in {}", level.moves, time)
} else {
" ".to_string()
"".to_string()
};

let overlay = Overlay::new();
Expand All @@ -42,31 +35,24 @@ pub fn spawn_ui(commands: &mut Commands, fonts: &Fonts, level: &Level) {
let title_housing = Housing::new(Val::Percent(100.0), Val::Px(98.0));
let press_button_housing = Housing::new(Val::Percent(100.0), Val::Px(32.0));

let mut time_or_empty = SimpleText::medium(time_or_empty, font);
let mut record_or_empty = SimpleText::medium(record_or_empty, font);
let mut title = EmbossedText::big("You Win! ", font);
let press_button = EmbossedText::small("Press (any button) to continue", font);

title.size(90.0);

time_or_empty.color(Colors::SECONDARY);
record_or_empty.color(Colors::SECONDARY);

floating
.position_type(PositionType::Absolute)
.top(248.0)
.top(240.0)
.justify_content(JustifyContent::FlexEnd);

overlay.spawn(
commands,
|parent| {
floating.spawn(parent, |parent| {
if level.new_record_time() {
time_or_empty.spawn(parent);
}
if level.new_record_moves() {
record_or_empty.spawn(parent);
}
record_or_empty.spawn(parent);
});
center.spawn(parent, |parent| {
title_housing.spawn(parent, |parent| {
Expand Down

0 comments on commit 9dda954

Please sign in to comment.