Skip to content

Commit

Permalink
merge: health and score
Browse files Browse the repository at this point in the history
  • Loading branch information
LiteHell committed Apr 30, 2024
2 parents f01acc8 + 78cb769 commit de5385c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
4 changes: 3 additions & 1 deletion game/src/game/display_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ fn render_game_result(canvas: &mut Canvas<Window>, result: &GameResult) {

let texts =
format!(
"{:<9} {:>4}\n{:<9} {:>4}\n{:<9} {:>4}\n{:<9} {:>4}\n{:<9} {:>4}\n{:<9} {:>4}\n{:<9} {:>4}",
"{:<9} {:>4}\n{:<9} {:>4}\n{:<9} {:>4}\n{:<9} {:>4}\n{:<9} {:>4}\n{:<9} {:>4}\n{:<9} {:>4}\n{:<9} {:>4}\n{}/{}",
"Overchaos", result.overchaos_count,
"Perfect", result.perfect_count,
"Great", result.great_count,
"Good", result.good_count,
"Bad", result.bad_count,
"Miss", result.miss_count,
"Combo", result.combo,
"Score", result.score,
result.health, result.max_health
);

for (idx, text) in texts.split("\n").enumerate() {
Expand Down
3 changes: 3 additions & 0 deletions game/src/game/game_player/game_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ pub(crate) struct GameResult {
pub bad_count: u64,
pub miss_count: u64,
pub combo: u64,
pub score: u64,
pub health: i64,
pub max_health: u64,
}
62 changes: 47 additions & 15 deletions game/src/game/game_player/timing_judge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ pub(crate) struct TimingJudge {
bad_count: u64,
miss_count: u64,
combo: u64,
score: u64,
health: i64,
max_health: u64,
}

pub(crate) struct JudgeResult {
pub accuracy: NoteAccuracy,
pub note_id: u64,
}

fn note_accuracy_from_time_difference(difference: i64) -> NoteAccuracy {
let difference_abs = difference.abs();
fn note_accuracy_from_time_difference(difference_abs: i64) -> NoteAccuracy {
if difference_abs <= OVERCHAOS_TIMING {
NoteAccuracy::Overchaos
} else if difference_abs <= PERFECT_TIMING {
Expand Down Expand Up @@ -101,15 +103,21 @@ impl TimingJudge {
.cmp(&b.note.timing_in_ms(b.bpm, b.delay))
});

let all_note_count = chart.left_face.len() + chart.right_face.len();
let max_health = all_note_count as u64 * 100;

return TimingJudge {
notes: notes,
bad_count: 0,
combo: 0,
good_count: 0,
great_count: 0,
miss_count: 0,
overchaos_count: 0,
perfect_count: 0,
great_count: 0,
good_count: 0,
bad_count: 0,
miss_count: 0,
combo: 0,
score: 0,
health: max_health as i64,
max_health: max_health,
};
}

Expand Down Expand Up @@ -179,8 +187,15 @@ impl TimingJudge {

// if it's processable note, calculate accuracy
if let Some(hit_timing) = i.hit_timing {
let note_accuracy =
note_accuracy_from_time_difference(hit_timing as i64 - precise_timing as i64);
let difference_abs = (hit_timing as i64 - precise_timing as i64).abs();

// calculte score by the accuracy
self.score += ((f64::abs(
BAD_TIMING as f64 - difference_abs.clamp(OVERCHAOS_TIMING, BAD_TIMING) as f64,
) / (BAD_TIMING - OVERCHAOS_TIMING) as f64)
* 1000.0) as u64;

let note_accuracy = note_accuracy_from_time_difference(difference_abs);

judged_notes.push(JudgeResult {
note_id: i.id,
Expand All @@ -195,34 +210,48 @@ impl TimingJudge {
self.notes
.remove(self.notes.iter().position(|x| x.id == i.note_id).unwrap());

let is_health_zero = self.health == 0;
// increase or set combo and count
match i.accuracy {
NoteAccuracy::Overchaos => {
self.combo += 1;
self.overchaos_count += 1;
self.health += 400;
}
NoteAccuracy::Perfect => {
self.combo += 1;
self.perfect_count += 1;
self.health += 200;
}
NoteAccuracy::Great => {
self.combo += 1;
self.great_count += 1;
self.health += 100;
}
NoteAccuracy::Good => {
self.combo += 1;
self.good_count += 1;
}
NoteAccuracy::Bad => {
self.combo += 1;
self.combo = 0;
self.bad_count += 1;
self.health -= 100;
}
NoteAccuracy::Miss => {
// miss breaks the combo
self.combo = 0;
self.miss_count += 1;
self.health -= 200;
}
}

// check if the health is zero -> already died
if is_health_zero {
self.health = 0;
} else {
// clamp the health between 0 and max_health
self.health = self.health.clamp(0, self.max_health as i64);
}
}

// return judgement result of the judged note
Expand All @@ -232,13 +261,16 @@ impl TimingJudge {
/// Creates game result
pub fn get_game_result(&self) -> GameResult {
return GameResult {
bad_count: self.bad_count,
combo: self.combo,
good_count: self.good_count,
great_count: self.great_count,
miss_count: self.miss_count,
overchaos_count: self.overchaos_count,
perfect_count: self.perfect_count,
great_count: self.great_count,
good_count: self.good_count,
bad_count: self.bad_count,
miss_count: self.miss_count,
combo: self.combo,
score: self.score,
health: self.health,
max_health: self.max_health,
};
}
}

0 comments on commit de5385c

Please sign in to comment.