void Init(HWND hWnd) { // Set our global window handle to our main window g_hWnd = hWnd; // Create our double buffering our window g_Buffer.CreateDoubleBuffering(hWnd); // This is where we create the image of our character HBITMAP hPlayerImage = g_Buffer.LoadABitmap((LPSTR)kPlayerImage); // Initialize our player with it's image and position g_Player.Init(hPlayerImage, kPlayerStartX, kPlayerStartY); // Init, load and draw the first map file g_Map.Load(kStartMap); g_Map.SetDrawFlag(true); // Here we load then play the starting background music if(!g_Music.Init("techno.mod")) exit(0); g_Music.PlaySong(); // we need to seed our random number generator (rand()) for moving npcs. srand(GetTickCount()); // Set the backbuffer to black first (This clears the backbuffer) g_Buffer.ClearScreen(BLACK_BRUSH); }
void HandleGameInput() { // Store the position of the player before we possibly move POINT lastPosition = g_Player.GetLastPosition(); if(g_Input.IsKeyDown(VK_ESCAPE)) // If escape was pressed g_Menu.BringUpMenu(); // Bring up the main menu else if(g_Input.IsKeyDown(VK_UP)) // If up arrow key was pressed g_Player.Move(kUp); // Move the character up else if(g_Input.IsKeyDown(VK_DOWN)) // If down arrow key was pressed g_Player.Move(kDown); // Move the character down else if(g_Input.IsKeyDown(VK_LEFT)) // If left arrow key was pressed g_Player.Move(kLeft); // Move the character left else if(g_Input.IsKeyDown(VK_RIGHT)) // If right arrow key was pressed g_Player.Move(kRight); // Move the character right //////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** //////////////////// // If we hit space let's toggle on and off the stats at the botton of the screen else if(g_Input.IsKeyDown(VK_SPACE)) g_Map.ToggleStatsFlag(); //////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** //////////////////// // Store the new current position of the player POINT curPosition = g_Player.GetPosition(); // Check if the player moved by testing the current position against the last position if(lastPosition.x != curPosition.x || lastPosition.y != curPosition.y) { // We want to draw the screen again if the character moved g_Map.SetDrawFlag(true); //////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** //////////////////// // Since we need to pick up items, we need to check for them just like exits, etc. g_Map.CheckForItem(curPosition.x, curPosition.y); //////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** //////////////////// // If we run into an NPC, we need to know so we can display dialog g_Map.CheckForNpc(curPosition.x, curPosition.y); // Set the previous position of the player g_Player.SetLastPosition(curPosition); // Here we check to see if the player moved into an exit. If so, exit! g_Map.CheckForExit(curPosition.x, curPosition.y); } }
void Init() { // This is where we create the image of our character, a brown @ symbol CHAR_INFO playerImage = {'@', FOREGROUND_RED | FOREGROUND_GREEN}; // Initialize our player with it's image and position g_Player.Init(playerImage, kPlayerStartX, kPlayerStartY); // Init the map and load the first map. Then set the flag that tells // the map's Draw() function to draw that frame. We don't want to draw // every frame, but only when something happens. g_Map.SetDefault(); g_Map.Load(kStartMap); g_Map.SetDrawFlag(true); }
void HandleGameInput() { // This is not a function in our CInput class. We wanted the CInput class // to be generic and not actually handle our game code, but just tell us what // the user did. We can then make different function to handle the input // for a given situation. We do this for character movement, save\load prompts, // menus, etc... // Let's get the key that the user pressed int keyCode = g_Input.GetKeyCode(); // If we just hit a key from the keyboard, handle the input accordingly. if(g_Input.IsKeyboardEvent() && g_Input.IsKeyDown()) { if(keyCode == VK_ESCAPE) // If escape was pressed exit(0); // Quit the game else if(keyCode == VK_UP) // If up arrow key was pressed g_Player.Move(kUp); // Move the character up else if(keyCode == VK_DOWN) // If down arrow key was pressed g_Player.Move(kDown); // Move the character down else if(keyCode == VK_LEFT) // If left arrow key was pressed g_Player.Move(kLeft); // Move the character left else if(keyCode == VK_RIGHT) // If right arrow key was pressed g_Player.Move(kRight); // Move the character right // If the key pressed was for character movement, let's check for special tiles if(keyCode == VK_RIGHT || keyCode == VK_LEFT || keyCode == VK_UP || keyCode == VK_DOWN) { // Here we check if the character stepped on an exit, if so .... exit. g_Map.CheckForExit(g_Player.GetPosition().X, g_Player.GetPosition().Y); // We want to draw the screen again if the character moved g_Map.SetDrawFlag(true); } } }
void HandleGameInput() { // Store the position of the player before we possibly move POINT lastPosition = g_Player.GetLastPosition(); if(g_Input.IsKeyDown(VK_ESCAPE)) // If escape was pressed g_Menu.BringUpMenu(); // Bring up the main menu else if(g_Input.IsKeyDown(VK_UP)) // If up arrow key was pressed g_Player.Move(kUp); // Move the character up else if(g_Input.IsKeyDown(VK_DOWN)) // If down arrow key was pressed g_Player.Move(kDown); // Move the character down else if(g_Input.IsKeyDown(VK_LEFT)) // If left arrow key was pressed g_Player.Move(kLeft); // Move the character left else if(g_Input.IsKeyDown(VK_RIGHT)) // If right arrow key was pressed g_Player.Move(kRight); // Move the character right else if(g_Input.IsKeyDown(VK_SPACE)) // If space bar was pressed g_Map.ToggleStatsFlag(); // Turn on and off the stats menu // Store the new current position of the player POINT curPosition = g_Player.GetPosition(); // Check if the player moved by testing the current position against the last position if(lastPosition.x != curPosition.x || lastPosition.y != curPosition.y) { //////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** //////////////////// // This function updates our party to take the last position of the // party member in front of them. If we have 3 players in our party // there will be a domino effect each time the main player moves. The // second player will take the last step of the first player, and the // third player will take the last step of the second player. They follow // you every step of the way... What a loyal party :) g_Player.MoveParty(); //////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** //////////////////// // We want to draw the screen again if the character moved g_Map.SetDrawFlag(true); // Since we need to pick up items, we need to check for them just like exits, etc. g_Map.CheckForItem(curPosition.x, curPosition.y); // If we run into an NPC, we need to know so we can display dialog g_Map.CheckForNpc(curPosition.x, curPosition.y); //////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** //////////////////// // Now that we have monsters moving around we need to check to see if we // ran into them so we know when to attack. Notice that this takes no // parameters. This is because we do a loop inside that goes through all // of our party members and checks if they ran into the monster. g_Map.CheckForMonster(); //////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** //////////////////// // Set the previous position of the player g_Player.SetLastPosition(curPosition); // Here we check to see if the player moved into an exit. If so, exit! g_Map.CheckForExit(curPosition.x, curPosition.y); } }