CleanCut / invaders

Open source terminal arcade game with audio - based off of the classic "Space Invaders"
206 stars 107 forks source link

Use `char` instead of `&str`, keep score at top left of frame #2

Closed xoryouyou closed 3 years ago

xoryouyou commented 3 years ago

I've added a simple score module in score.rs which implements the Drawable trait to print the current score in the top left corner of the Frame.

The score is simply calculated by subtracting Invaders.army.len() from the newly introduced Invaders.total_count which represents the total count of spawned invaders in the army.

So each hit invader results in one point added on the score.

Note

To make rendering easier I've refactored Frames data structure into a 2d char array in the form of [[char ; NUM_ROWS]; NUM_COLS].

This allows for the same access frame[x][y] but only takes char instead of &str.

It was done to enable the dynamic use of format! in Score.update which returns a String. That String is not easily convertable to &str especially given the different lifetimes.

This also gives the option to add more useful strings like "time played" or "level" onto the screen via a more complex module later on.

Would like to hear some feedback since it's my first contribution in rust :)

CleanCut commented 3 years ago

Just following up. Are you interesting in finishing this up so it can be merged?

xoryouyou commented 3 years ago

Same situation here as you've had it back in april. Somehow the notifications for this repo got lost.

Anyways I've commited the changes as requested and commented above about them. :smile:

xoryouyou commented 3 years ago

I've had to rework the Invader.kill_invader_at and Player.detect_hits functions to return u16.

Its not that clean since we now have to store the functions result like

before:

if(player.detect_hits(...))
{
  do_stuff();
}

now:

let hit_result = player.detect_hits(...);
if(hit_result > 0)
{
  do_stuff();
}

But that is the only way it works and we can later on move from a simple u16 which when positive to a more meaningful struct like HitResult which could feature detailed information and enable logic based on the hit invader like combos,effect,etc.

But should do for now :smile: