From 874367f321117f0b8f932cf2d330a9f54c6829d4 Mon Sep 17 00:00:00 2001 From: structix Date: Wed, 24 May 2017 15:10:51 +0200 Subject: [PATCH] Added hitfeed; ClearAnimation 0 offset fix --- hangman.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++-------- hangman.h | 14 ++++++++++--- 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/hangman.c b/hangman.c index cde0413..881d522 100644 --- a/hangman.c +++ b/hangman.c @@ -34,13 +34,16 @@ int main(int argc, char **argv) { initscr(); atexit(quitProgram); game_state *gs; + hitfeed *hf; gs = malloc(sizeof(game_state)); + hf = malloc(sizeof(hitfeed)); //zeroes the whole struct initCoordinates(gs); gs->allowedMoves = DEFAULTTRIES; gs->moves = 0; gs->guesses = 0; gs->trollEnabled = 0; + hf->besthit = 0; keypad(stdscr, FALSE); nonl(); //No new line. Prevents the new line when hitting enter @@ -86,21 +89,23 @@ int main(int argc, char **argv) { initAlphabet(gs); drawAlphabet(gs, 0); drawFigure(gs, 0); - startGame(gs); + startGame(gs, hf); nl(); //enable newline again while (getch() != 10); clear(); free(gs); + free(hf); return 0; } -void startGame(game_state *gs) { +void startGame(game_state *gs, hitfeed *hf) { noecho(); /* Don't show the player input */ time(&gs->startTime); while (checkWin(gs)) { updateScreen(gs); - playerInput(gs); + printHitFeed(gs, hf); + playerInput(gs, hf); } } @@ -244,7 +249,7 @@ void drawGuessWord(game_state *gs) { refresh(); } -int playerInput(game_state *gs) { +int playerInput(game_state *gs, hitfeed *hf) { int inp; int i, found = 0; inp = getch(); @@ -260,7 +265,7 @@ int playerInput(game_state *gs) { if (fillCurrentWord(gs, inp)) { //gs->moves++; gs->guesses++; - trollHitScreen(gs, found); + trollHitScreen(gs, hf, found); } } else { /* no valid character found */ @@ -338,7 +343,7 @@ void printGameStats(game_state *gs) { } } else { for (z = 0; z <= gs->wordRows; z++) { - animateLineClear(gs, gs->centery + z, -1); //-1 to speed up the animation + animateLineClear(gs, gs->centery + z, 0); //-1 to speed up the animation } } @@ -394,7 +399,7 @@ void readRandomLine(char *file, char *result) { fclose(fp); } -void trollHitScreen(game_state *gs, int hits) { +void trollHitScreen(game_state *gs, hitfeed *hf, int hits) { if (gs->trollEnabled) { char *strings[] = {"Double Hit", "Triple Hit", "Multi Hit", \ "Ultra Hit", "Monster Hit", "Rampage", \ @@ -415,11 +420,50 @@ void trollHitScreen(game_state *gs, int hits) { flash(); /* flash the screen */ mvprintw(5, centerDiff(gs->centerx, strings[i]), strings[i]); refresh(); + /* Put the new streak to the hit feed */ + addHitToFeed(hf, strings[i], hits); animateLineClear(gs, 5, i); } } } +void addHitToFeed(hitfeed *hf, char *streak, int hit) { + /* Shift the array to put the new string to the first slot */ + int i; + for (i = HITFEEDSLOTS - 1; i > 0; i--) { + strcpy(hf->history[i], hf->history[i - 1]); + } + /* Set the new first item */ + strcpy(hf->history[0], streak); + if (hit > hf->besthit) { + strcpy(hf->beststreak, streak); + hf->besthit = hit; + } +} + +void printHitFeed(game_state *gs, hitfeed *hf) { + if (gs->trollEnabled) { + /* print the stats at the bootom right corner */ + int newMaxy = gs->maxy - HITFEEDSLOTS; + int newMaxx = gs->maxx - (gs->centerx / 2); + int i, z; + for (i = 0; i < HITFEEDSLOTS; i++) { + for (z = 0; z < gs->maxx - newMaxx; z++) { + mvprintw(newMaxy + i, newMaxx + z, " "); + } + } + for (i = 0; i < HITFEEDSLOTS; i++) { + mvprintw(newMaxy + i, newMaxx, hf->history[i]); + } + /* Print best score above history feed */ + char message[100]; + sprintf(message, "Best: %s (%i hits)", hf->beststreak, hf->besthit); + mvprintw(newMaxy - 1, newMaxx, message); + } +} + + + void drawFigure(game_state *gs, int drawNext) { int row = 6; int length = 8; @@ -442,7 +486,7 @@ void drawFigure(game_state *gs, int drawNext) { void animateLineClear(game_state *gs, int line, int offsetMultiplier) { /* sleep and vanish */ int j = 0, k = gs->maxx, usec; - usec = (ANIM_DURATION * 1000000 + (100000 * offsetMultiplier)) / gs->maxx; + usec = ((ANIM_DURATION * 1000000) + (100000 * offsetMultiplier)) / gs->maxx; for (j = 0; j <= gs->centerx; j++, k--) { mvprintw(line, j, "!"); diff --git a/hangman.h b/hangman.h index 7e2bd37..0c41f24 100644 --- a/hangman.h +++ b/hangman.h @@ -10,6 +10,7 @@ #define ALPHABET_NUM 36 #define ANIM_DURATION 1 #define LINEBREAK 16 //Linebreak offset +#define HITFEEDSLOTS 5 /* Data structures */ typedef struct { @@ -36,6 +37,11 @@ typedef struct word { struct word *next; } word; +typedef struct hitfeed { + char history[HITFEEDSLOTS][50]; //Prints the last 3 streaks + char beststreak[50]; //Prints the best streak + int besthit; //Prints the best hit number e.g. Godlike with 15 hits +} hitfeed; /* Function prototypes */ void quitProgram(void); @@ -45,17 +51,19 @@ void initGuessWord(game_state *gs, char *filename); void initAlphabet(game_state *gs); void drawAlphabet(game_state *gs, char usedchar); void drawGuessWord(game_state *gs); -int playerInput(game_state *gs); +int playerInput(game_state *gs, hitfeed *hf); int fillCurrentWord(game_state *gs, char validchar); int stackWrongCharacter(game_state *gs, char wrongchar); int checkWin(game_state *gs); void printGameStats(game_state *gs); void toLowerCase(char *str); void showStartScreen(game_state *gs); -void startGame(game_state *gs); +void startGame(game_state *gs, hitfeed *hf); void showHelp(game_state *gs); int centerDiff(int coordinate, char *str); void readRandomLine(char *file, char *result); -void trollHitScreen(game_state *gs, int hits); +void trollHitScreen(game_state *gs, hitfeed *hf, int hits); +void addHitToFeed(hitfeed *hf, char *streak, int hit); void drawFigure(game_state *gs, int drawNext); void animateLineClear(game_state *gs, int line, int offsetMultiplier); +void printHitFeed(game_state *gs, hitfeed *hf);