#include #include "LevelManager.h" #include "player.h" #include /*! \brief Handles the key presses the player enters appropriately. \param HANDLE hOut - The window we are currently operating on \return An int acting as a boolean value to stop the loop in driver.c when 'x' is pressed \sa plyr_movement(HANDLE hOut) */ int plyr_movement(HANDLE hOut) { /* struct holding what was done on the keyboard */ INPUT_RECORD InRec; /* struct holding where input will be read from */ HANDLE hIn; /* a record of what has been read*/ DWORD NumRead; /* sets the input to the keyboard */ hIn = GetStdHandle(STD_INPUT_HANDLE); /* reads input from the keyboard, and stores it in InRec */ ReadConsoleInput(hIn,&InRec,1,&NumRead); /* if something happens on the keyboard */ if(InRec.EventType == KEY_EVENT){ /* see if it is any of these specific keys and do the appropriate action */ if (InRec.Event.KeyEvent.uChar.AsciiChar == 'x') { return FALSE; } if(InRec.Event.KeyEvent.bKeyDown){ if ((InRec.Event.KeyEvent.uChar.AsciiChar == 'w')) { plyrMvUp(); } if ((InRec.Event.KeyEvent.uChar.AsciiChar == 's')) { plyrMvDown(); } if ((InRec.Event.KeyEvent.uChar.AsciiChar == 'a')) { plyrMvLeft(); } if ((InRec.Event.KeyEvent.uChar.AsciiChar == 'd')) { plyrMvRight(); } } } return TRUE; } /*! \brief Takes the virtual map and translates it onto the screen directly on the screen buffer \param HANDLE hOut - The window we are currently operating on \param LEVEL *lvl - The current level we are on, to get the total level size \param COORD *viewCoord - The X and Y of the top left corner of the viewable area of the map \return TRUE if the thing didnt fail \sa int translate(HANDLE hOut, LEVEL *lvl, COORD *viewCoord) */ int translate(HANDLE hOut, LEVEL *lvl, COORD *viewCoord) { /* simple counters */ int i = 0; int j = 0; /* windows BS thingy that allows me to get the viewing rectangle */ CONSOLE_SCREEN_BUFFER_INFO info; /* holds the size of the array of chars(viewport size) */ COORD arraysize; /* holds the top left coordinate of where to start drawing from in the array (always 0,0) */ COORD screensize; /* the rectangle which holds the dimensions of the viewing rectangle */ SMALL_RECT viewRect; /* An array of chars (with color formatting) that make up what is printed on screen*/ CHAR_INFO cha[24][80]; /* set the arraysize to he size of the screen */ arraysize.X = 80; arraysize.Y = 24; /* sets the top left to 0,0 */ screensize.X = 0; screensize.Y = 0; /* basically this gets the size of the viewing rectangle */ GetConsoleScreenBufferInfo(hOut, &info); viewRect = info.srWindow; /* checks to see if the player is outside of the middle of the screen and that if it moves itself to keep the player inside the center that it won't be tryint to draw outside of the lvl, because that causes runtime errors */ if (player.X - viewCoord->X > 60) { if(viewCoord->X+81 < lvl->width) { viewCoord->X = viewCoord->X + 1; } } if (player.X - viewCoord->X < 20) { if(viewCoord->X-1 > 0) { viewCoord->X = viewCoord->X - 1; } } if (player.Y - viewCoord->Y > 18) { if(viewCoord->Y+24 < lvl->height) { viewCoord->Y = viewCoord->Y + 1; } } if (player.Y - viewCoord->Y < 5) { if(viewCoord->Y > 0) { viewCoord->Y = viewCoord->Y - 1; } } /* the two nested loops go from the top left of the screen to the bottom right of the screen*/ for(; i < 24; i++) { for(; j < 80; j++) { /* checks to see what is in the current space we are looking at and sets that area to the appropriate char and appropriate colors */ if(getLevelPosition(WALL_LAYER,j+viewCoord->X,i+viewCoord->Y)->character == 'X') { cha[i][j].Char.AsciiChar = 'X'; cha[i][j].Attributes = (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); } else if(getLevelPosition(PLAYER_LAYER,j+viewCoord->X,i+viewCoord->Y)->character == 'P') { cha[i][j].Char.AsciiChar = 'P'; cha[i][j].Attributes = (BACKGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); } else { cha[i][j].Char.AsciiChar = ' '; cha[i][j].Attributes = (BACKGROUND_GREEN); } } j = 0; } /* writes the array we just made to the screen all at once SUPER FAST STYLE!!!!!! */ WriteConsoleOutput(hOut, &cha, arraysize, screensize, &viewRect); return TRUE; }